MessageWallController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers\Miniprogram;
  3. use App\Models\Common\TokenModel;
  4. use App\Models\User\UserModel;
  5. use App\Models\Messagewall\MessagewallModel;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. class MessageWallController extends Controller
  9. {
  10. /**
  11. * @param Request $request
  12. * @param int $master_uid
  13. * @return \Illuminate\Http\JsonResponse
  14. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  15. */
  16. public function show(Request $request, $master_uid = 0)
  17. {
  18. $uid = Auth::auth();
  19. $master_uid = $master_uid ?: $uid;
  20. $user = UserModel::select('uid', 'nickname', 'headimgurl', 'sex')->where('uid', $master_uid)->first();
  21. $user->self = ($uid == $master_uid) ? true : false;
  22. $build = MessagewallModel::where('deleted_at', 0)->where('master_uid', $master_uid);
  23. $messages = $build->orderBy('id', 'desc')->paginate($request->get('per_page', $request->get('limit', 20)));
  24. foreach ($messages as $message) {
  25. $message->user;
  26. }
  27. return response()->json([
  28. 'code' => 200,
  29. 'message' => 'OK',
  30. 'data' => [
  31. 'user' => $user,
  32. 'messages' => $messages
  33. ]
  34. ]);
  35. }
  36. /**
  37. * 创建留言
  38. * @param Request $request
  39. * @return \Illuminate\Http\JsonResponse
  40. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  41. */
  42. public function store(Request $request)
  43. {
  44. $uid = Auth::auth();
  45. $validator = \Validator::make($request->all(), [
  46. 'master_uid' => 'required|integer',
  47. 'content' => 'required|max:32',
  48. ], [
  49. 'master_uid.*' => '选择指定人',
  50. ]);
  51. if ($validator->fails()) {
  52. return response()->json([
  53. 'code' => 422,
  54. 'errors' => $validator->errors(),
  55. 'message' => $validator->errors()->first()
  56. ]);
  57. }
  58. $message = MessagewallModel::create($request->merge(['guest_uid' => $uid])->all());
  59. return response()->json([
  60. 'code' => 200,
  61. 'message' => 'OK',
  62. 'data' => $message
  63. ]);
  64. }
  65. /**
  66. * 删除
  67. * @param Request $request
  68. * @param $id
  69. * @return \Illuminate\Http\JsonResponse
  70. */
  71. public function destroy(Request $request, $id)
  72. {
  73. $uid = Auth::auth();
  74. $message = MessagewallModel::find($id);
  75. if (!$message) {
  76. return response()->json([
  77. 'code' => 404,
  78. 'message' => '留言不存在'
  79. ]);
  80. }
  81. if (!in_array($uid, [$message->master_uid, $message->guest_uid])) {
  82. return response()->json([
  83. 'code' => 403,
  84. 'message' => '没有权限'
  85. ]);
  86. }
  87. $message->deleted_at = time();
  88. $message->save();
  89. return response()->json([
  90. 'code' => 200,
  91. 'message' => '删除成功'
  92. ]);
  93. }
  94. public function qrcode(Request $request)
  95. {
  96. $uid = Auth::auth();
  97. $public_id = config('miniprogram.public_id');
  98. $access_token = TokenModel::getToken($public_id);
  99. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
  100. $data = [
  101. 'scene' => "uid={$uid}&launch_type=wall",
  102. "page" => "pages/starter/starter",
  103. 'width' => '430',
  104. 'auto_color' => false,
  105. 'line_color' => [
  106. 'r' => 0,
  107. 'g' => 0,
  108. 'b' => 0,
  109. ],
  110. 'is_hyaline' => false,
  111. ];
  112. $response = \Curl::to($url)->withData($data)->asJsonRequest()->post();
  113. return response($response, 200, ['Content-Type' => 'image/png']);
  114. }
  115. }