FeedbackController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Http\Controllers\Miniprogram;
  3. use App\Models\LogoutModel;
  4. use App\Models\User\AuthKey;
  5. use App\Models\User\Openid;
  6. use App\Services\WeChat\AccessToken;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. use App\Models\User\UserModel;
  10. use App\Models\FeedbackModel;
  11. use PocketBE\MsyPush\Jobs\RegistEventJob;
  12. class FeedbackController extends Controller
  13. {
  14. //
  15. public function create(Request $request)
  16. {
  17. $this->validate($request, [
  18. 'title' => '',
  19. 'content' => 'required|max:200',
  20. 'images' => 'array',
  21. 'phone' => 'required',
  22. 'device' => '',
  23. 'be_uid' => '',
  24. ], [
  25. 'content.max' => '内容最长200字',
  26. ]);
  27. $uid = Auth::auth();
  28. $user = UserModel::find($uid);
  29. // 添加搜索关键字
  30. $search = [];
  31. array_push($search, $request->phone);
  32. array_push($search, $user->nickname);
  33. array_push($search, $user->uid);
  34. array_push($search, $user->phone);
  35. $attribute = $request->all();
  36. $attribute['uid'] = $uid;
  37. $attribute['search'] = $search;
  38. ## 注销
  39. if (strpos($request->title, '注销') !== false) {
  40. // 申请注销未注册
  41. if (LogoutModel::where(['uid' => $uid, 'reply_at' => 0])->first()) {
  42. return response([
  43. 'code' => 409,
  44. 'message' => '已经申请注销,等待1-3个工作日处理'
  45. ]);
  46. }
  47. $logout = LogoutModel::create($attribute);
  48. try {
  49. $this->toAdminLogout($logout);
  50. } catch (\Exception $e) {
  51. }
  52. } else {
  53. $feedback = FeedbackModel::create($attribute);
  54. try {
  55. $this->toAdminFeedback($feedback);
  56. } catch (\Exception $e) {
  57. }
  58. }
  59. return response([
  60. 'code' => 200,
  61. 'message' => 'Success',
  62. ]);
  63. }
  64. private function toAdminFeedback(FeedbackModel $feedback)
  65. {
  66. $public_id = "gh_43ba50c16fba";
  67. $appId = 'wxadd11625f069258a';
  68. $admins = Openid::where("public_id", $public_id)
  69. ->whereIn("uid", [10001, 10002, 10007, 11111, 94302, 103143, 62726, 10288718])
  70. ->pluck("openid");
  71. $url = "https://m.fenpeiduixiang.com/fpdx-feedback/index.html?#/reply?id={$feedback->id}";
  72. foreach ($admins as $openid) {
  73. $payload = [
  74. 'to_user' => [
  75. $appId => $openid,
  76. ]
  77. ];
  78. dispatch(new RegistEventJob(10505, $payload))->onQueue("{push}");
  79. $data = [
  80. 'first' => [
  81. 'value' => "【分配对象小程序】收到新的用户反馈!",
  82. 'color' => '#ff0000',
  83. ],
  84. 'keyword1' => [
  85. 'value' => $feedback->title ?: "空",
  86. 'color' => '#000000',
  87. ],
  88. 'keyword2' => [
  89. 'value' => date('Y年m月d日 H:i'),
  90. 'color' => '#000000',
  91. ],
  92. 'remark' => [
  93. 'value' => $feedback->content,
  94. 'color' => '#2ba083',
  95. ],
  96. ];
  97. $access_token = (new AccessToken())->get($public_id);
  98. $api_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
  99. \Curl::to($api_url)->withData([
  100. 'touser' => $openid,
  101. 'template_id' => 'DhPjGWXQZqKd4ic9hSTccOOSm3AGHiYjM1ppb7td8Wo',
  102. 'url' => $url,
  103. 'data' => $data
  104. ])->asJsonRequest()->post();
  105. }
  106. }
  107. private function toAdminLogout(LogoutModel $logout)
  108. {
  109. $public_id = "gh_43ba50c16fba";
  110. $appId = 'wxadd11625f069258a';
  111. $admins = Openid::where("public_id", $public_id)
  112. ->whereIn("uid", [10001, 10002, 10007, 11111, 94302, 103143, 62726, 10288718])
  113. ->pluck("openid");
  114. foreach ($admins as $openid) {
  115. $payload = [
  116. 'to_user' => [
  117. $appId => $openid,
  118. ]
  119. ];
  120. dispatch(new RegistEventJob(10506, $payload))->onQueue("{push}");
  121. $data = [
  122. 'first' => [
  123. 'value' => "【分配对象小程序】收到新的用户注销!",
  124. 'color' => '#ff0000',
  125. ],
  126. 'keyword1' => [
  127. 'value' => $logout->title ?: "空",
  128. 'color' => '#000000',
  129. ],
  130. 'keyword2' => [
  131. 'value' => date('Y年m月d日 H:i'),
  132. 'color' => '#000000',
  133. ],
  134. 'remark' => [
  135. 'value' => $logout->content,
  136. 'color' => '#2ba083',
  137. ],
  138. ];
  139. $access_token = (new AccessToken())->get($public_id);
  140. $api_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
  141. \Curl::to($api_url)->withData([
  142. 'touser' => $openid,
  143. 'template_id' => 'DhPjGWXQZqKd4ic9hSTccOOSm3AGHiYjM1ppb7td8Wo',
  144. 'data' => $data
  145. ])->asJsonRequest()->post();
  146. }
  147. }
  148. }