FeedbackController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Miniprogram\Core;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\FeedbackModel;
  7. class FeedbackController extends Controller
  8. {
  9. /**
  10. * 获取举报列表
  11. * @param Request $request
  12. * @return array
  13. * @throws \ApiException
  14. */
  15. public function index(Request $request)
  16. {
  17. $limit = $request->get('limit', 10);
  18. $offset = ($request->input('page', 1) - 1) * $limit;
  19. $build = FeedbackModel::when($request->filled('search'), function ($query) use ($request) {
  20. return $query->where('search', 'like', "%" . $request->search . "%");
  21. })->when($request->filled('reply'), function ($query) use ($request) {
  22. if ($request->reply) {
  23. return $query->where('reply_at', '>', 0);
  24. } else {
  25. return $query->where('reply_at', '=', 0);
  26. }
  27. });
  28. $count = $build->count();
  29. $feedbacks = $build->orderBy(
  30. $request->input('sort_by', 'id'),
  31. $request->input('order', 'asc')
  32. )->offset($offset)
  33. ->limit($limit)
  34. ->get();
  35. foreach ($feedbacks as $key => $feedback) {
  36. $feedback->userinfo;
  37. }
  38. return response([
  39. 'code' => 200,
  40. 'message' => 'OK',
  41. 'data' => [
  42. 'list' => $feedbacks,
  43. 'count' => $count,
  44. 'limit' => $limit
  45. ]
  46. ]);
  47. }
  48. public function show($id)
  49. {
  50. $feedback = FeedbackModel::findOrFail($id);
  51. $feedback->userinfo;
  52. return response([
  53. 'code' => 200,
  54. 'message' => 'OK',
  55. 'data' => $feedback
  56. ]);
  57. }
  58. /**
  59. * 回复
  60. * @param Request $request
  61. * @return array
  62. */
  63. public function reply(Request $request)
  64. {
  65. $this->validate($request, [
  66. 'id' => 'required|integer',
  67. 'content' => 'required|max:200',
  68. ], [
  69. 'id.required' => '请选择一条反馈',
  70. 'content.required' => '请写回复内容',
  71. 'content.max' => '回复内容字数不得超出200字',
  72. ]);
  73. $feedback = FeedbackModel::findOrFail($request->id);
  74. if (!$feedback) {
  75. return ['code' => 404, 'message' => '不存在'];
  76. }
  77. if ($feedback->reply_at > 0) {
  78. return ['code' => 403, 'message' => '已回复'];
  79. }
  80. $feedback->reply_content = $request->input('content');
  81. $feedback->reply_at = time();
  82. $response = $this->notice($feedback);
  83. if ($response) {
  84. $feedback->save();
  85. return response([
  86. 'code' => 200,
  87. 'message' => 'OK'
  88. ]);
  89. } else {
  90. return response([
  91. 'code' => 422,
  92. 'message' => '发送失败',
  93. ]);
  94. }
  95. }
  96. public function notice($feedback)
  97. {
  98. $public_id = config('miniprogram.public_id');
  99. $template_id = "IFUqurIlP88yg1KJDSGI3zAA0oRSRLFaxgeZerTrbe4";
  100. $page = 'pages/starter/starter?launch_type=free&url=%2Fpages%2Fme%2Fme';
  101. $data = array(
  102. 'keyword1' => [
  103. 'value' => $feedback->content,
  104. ],
  105. 'keyword2' => [
  106. 'value' => $feedback->created_at->format('Y-m-d H:i'),
  107. ],
  108. 'keyword3' => [
  109. 'value' => "已回复",
  110. ],
  111. 'keyword4' => [
  112. 'value' => $feedback->reply_content,
  113. ],
  114. 'keyword5' => [
  115. 'value' => "感谢你为我们提供宝贵的建议",
  116. ],
  117. );
  118. $core = new Core();
  119. $result = $core->miniTemplate($feedback->uid, $template_id, $public_id, $page, $data);
  120. return $result;
  121. }
  122. /**
  123. * 忽略
  124. * @param $id
  125. * @return array|\Illuminate\Http\JsonResponse
  126. */
  127. public function ignore($id)
  128. {
  129. $feedback = FeedbackModel::find($id);
  130. if ($feedback->reply_at > 0) {
  131. return response([
  132. 'code' => 403,
  133. 'message' => '已处理'
  134. ]);
  135. }
  136. $feedback->reply_at = time();
  137. $feedback->save();
  138. return response([
  139. "code" => 200,
  140. "message" => "OK"
  141. ]);
  142. }
  143. }