NoticeManageController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 2021/3/8
  6. * Time: 10:31 上午
  7. */
  8. namespace App\Community\Controllers;
  9. use App\Models\Order;
  10. use App\Models\Patient;
  11. use App\User;
  12. use Cache;
  13. use EasyWeChat\Factory;
  14. use Encore\Admin\Controllers\AdminController;
  15. use Encore\Admin\Layout\Content;
  16. use Encore\Admin\Facades\Admin;
  17. use Exception;
  18. class NoticeManageController extends AdminController
  19. {
  20. public function noticelist()
  21. {
  22. $req = request()->query();
  23. $user = Admin::user();
  24. $builder = Patient::orderBy('id', 'desc');
  25. if (!empty($req['user'])) {
  26. $userIds = User::where('nickname', 'like', '%'.$req['user'].'%')->pluck('id')->toArray();
  27. $builder->whereIn('user_id', $userIds);
  28. }
  29. if (!empty($req['name'])) {
  30. $builder->where('name', 'like', '%'.$req['name'].'%');
  31. }
  32. if (!empty($req['guardian_name'])) {
  33. $builder->where('guardian_name', 'like', '%'.$req['guardian_name'].'%');
  34. }
  35. if (!empty($req['phone'])) {
  36. $builder->where('phone', $req['phone']);
  37. }
  38. if (!empty($req['product_type'])) {
  39. $user_ids = Order::where('product_type', $req['product_type'])->distinct('user_id')->pluck('user_id');
  40. $builder->whereIn('user_id', $user_ids);
  41. }
  42. $list = $builder->paginate();
  43. $data['req'] = $req;
  44. $data['list'] = $list;
  45. $data['ids'] = Cache::get('notice-'.$user['id'])[$req['page']??1] ?? [];
  46. $content = new Content();
  47. Admin::disablePjax();
  48. return $content->title('群发通知')->view('cdms/notice_list', $data);
  49. }
  50. public function saveIds()
  51. {
  52. $req = request()->post();
  53. $user = Admin::user();
  54. $arr = Cache::get($req['type'].'-'.$user['id']) ?? [];
  55. $arr[$req['page']] = $req['ids']??[];
  56. Cache::set($req['type'].'-'.$user['id'], $arr, 300);
  57. return out();
  58. }
  59. public function sendNotice()
  60. {
  61. $req = request()->post();
  62. admin_validate($req, [
  63. 'content|内容' => 'required',
  64. 'type|服务类型' => 'required',
  65. 'stime|服务时间' => 'required',
  66. 'remark|备注' => 'required',
  67. ]);
  68. $user = Admin::user();
  69. $arr = Cache::get('notice-'.$user['id']) ?? [];
  70. $ids = [];
  71. foreach ($arr as $k => $v) {
  72. $ids = array_merge($ids, $v);
  73. }
  74. $ids = array_values(array_unique($ids));
  75. if (empty($ids)) {
  76. return out(null, 10001, '请勾选要发送通知的用户');
  77. }
  78. Cache::delete('notice-'.$user['id']);
  79. foreach ($ids as $id) {
  80. $user_id = Patient::where('id', $id)->value('user_id');
  81. if(empty($user_id)) continue;
  82. $openid = User::where('id', $user_id)->value('openid');
  83. if(empty($openid)) continue;
  84. $content = $req['content'];
  85. $remark= $req['remark'];
  86. $stime= $req['stime'];
  87. $type= $req['type'];
  88. $service_arr = [4 => '疫苗接种服务', 5 => '儿保服务'];
  89. $service_name = $service_arr[$type];
  90. $template_arr = [
  91. 4 => 'xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo',
  92. 5 => 'CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc',
  93. ];
  94. $tempId = $template_arr[$type];
  95. $msg['content'] = $content;
  96. $msg['service_date'] = $stime;
  97. $msg['remark'] = $remark;
  98. $msg['service_name'] = $service_name;
  99. $this->send($openid, $tempId, $msg);
  100. }
  101. return out();
  102. }
  103. private function send($open_id, $tempId, $msg)
  104. {
  105. try {
  106. $template = [
  107. 'touser' => $open_id,
  108. 'mp_template_msg' => [
  109. 'appid' => env('OFFICIAL_WECHAT_APPID'),
  110. 'template_id' => $tempId,
  111. 'url' => '',
  112. 'miniprogram' => [
  113. 'appid' => env('WECHAT_APPID', 'wxd41dd232837996c4'),
  114. 'pagepath' => '',
  115. ],
  116. 'data' => [
  117. 'first' => [
  118. 'value' => $msg['content'],
  119. ],
  120. 'keyword1' => [
  121. 'value' => $msg['service_name'],
  122. ],
  123. 'keyword2' => [
  124. 'value' => $msg['service_date'],
  125. ],
  126. 'remark' => [
  127. 'value' => $msg['remark'],
  128. ],
  129. ],
  130. ],
  131. ];
  132. $app = Factory::miniProgram(config('config.wechat_small_program'));
  133. $ret = $app->uniform_message->send($template);
  134. if (isset($ret['errcode']) && $ret['errcode'] != 0) {
  135. trace(['后台发送消息通知失败,请求参数' => $template, '返回数据' => $ret], 'error');
  136. }
  137. } catch (Exception $e) {
  138. trace(['后台发送消息通知失败' => $e->getMessage(), '请求参数' => $template ?? '', '返回数据' => $ret ?? ''], 'error');
  139. }
  140. return true;
  141. }
  142. }