NoticeManageController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. $patient_ids = Order::where('organization_id', $user['org_id'])->pluck('patient_id')->toArray();
  25. $builder = Patient::whereIn('id', $patient_ids)->orderBy('id', 'desc');
  26. if (!empty($req['user'])) {
  27. $userIds = User::where('nickname', 'like', '%'.$req['user'].'%')->pluck('id')->toArray();
  28. $builder->whereIn('user_id', $userIds);
  29. }
  30. if (!empty($req['name'])) {
  31. $builder->where('name', 'like', '%'.$req['name'].'%');
  32. }
  33. if (!empty($req['guardian_name'])) {
  34. $builder->where('guardian_name', 'like', '%'.$req['guardian_name'].'%');
  35. }
  36. if (!empty($req['phone'])) {
  37. $builder->where('phone', $req['phone']);
  38. }
  39. if (!empty($req['product_type'])) {
  40. $user_ids = Order::where('product_type', $req['product_type'])->distinct('user_id')->pluck('user_id');
  41. $builder->whereIn('user_id', $user_ids);
  42. }
  43. $list = $builder->paginate();
  44. $data['req'] = $req;
  45. $data['list'] = $list;
  46. $data['ids'] = Cache::get('notice-'.$user['id']) ?? [];
  47. $content = new Content();
  48. Admin::disablePjax();
  49. return $content->title('群发通知')->view('cdms/notice_list', $data);
  50. }
  51. public function saveIds()
  52. {
  53. $req = request()->post();
  54. $user = Admin::user();
  55. $arr = Cache::get($req['type'].'-'.$user['id']) ?? [];
  56. foreach ($req['ids'] as $k => $v) {
  57. if (isset($arr[$v])) {
  58. unset($arr[$v]);
  59. }
  60. else {
  61. $arr[$v] = $v;
  62. }
  63. }
  64. Cache::set($req['type'].'-'.$user['id'], $arr, 300);
  65. return out();
  66. }
  67. public function sendNotice()
  68. {
  69. $req = request()->post();
  70. admin_validate($req, [
  71. 'content|内容' => 'required',
  72. 'type|服务类型' => 'required',
  73. 'stime|服务时间' => 'required',
  74. 'remark|备注' => 'required',
  75. ]);
  76. $user = Admin::user();
  77. $arr = Cache::get('notice-'.$user['id']) ?? [];
  78. $ids = array_values(array_unique($arr));
  79. if (empty($ids)) {
  80. return out(null, 10001, '请勾选要发送通知的用户');
  81. }
  82. Cache::delete('notice-'.$user['id']);
  83. foreach ($ids as $id) {
  84. $user_id = Patient::where('id', $id)->value('user_id');
  85. if(empty($user_id)) continue;
  86. $openid = User::where('id', $user_id)->value('openid');
  87. if(empty($openid)) continue;
  88. $content = $req['content'];
  89. $remark= $req['remark'];
  90. $stime= $req['stime'];
  91. $type= $req['type'];
  92. $service_arr = [4 => '疫苗接种服务', 5 => '儿保服务'];
  93. $service_name = $service_arr[$type];
  94. $template_arr = [
  95. 4 => 'xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo',
  96. 5 => 'CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc',
  97. ];
  98. $tempId = $template_arr[$type];
  99. $msg['content'] = $content;
  100. $msg['service_date'] = $stime;
  101. $msg['remark'] = $remark;
  102. $msg['service_name'] = $service_name;
  103. $this->send($openid, $tempId, $msg);
  104. }
  105. return out();
  106. }
  107. public function getCacheUsers()
  108. {
  109. $req = request()->post();
  110. $user = Admin::user();
  111. $ids = Cache::get($req['type'].'-'.$user['id']) ?? [];
  112. $nameText = '';
  113. if (!empty($ids)) {
  114. $nameArr = Patient::whereIn('id', $ids)->pluck('name')->toArray();
  115. $nameText = implode(';', $nameArr);
  116. }
  117. return out($nameText);
  118. }
  119. private function send($open_id, $tempId, $msg)
  120. {
  121. try {
  122. $template = [
  123. 'touser' => $open_id,
  124. 'mp_template_msg' => [
  125. 'appid' => env('OFFICIAL_WECHAT_APPID'),
  126. 'template_id' => $tempId,
  127. 'url' => '',
  128. 'miniprogram' => [
  129. 'appid' => env('WECHAT_APPID', 'wxd41dd232837996c4'),
  130. 'pagepath' => '',
  131. ],
  132. 'data' => [
  133. 'first' => [
  134. 'value' => $msg['content'],
  135. ],
  136. 'keyword1' => [
  137. 'value' => $msg['service_name'],
  138. ],
  139. 'keyword2' => [
  140. 'value' => $msg['service_date'],
  141. ],
  142. 'remark' => [
  143. 'value' => $msg['remark'],
  144. ],
  145. ],
  146. ],
  147. ];
  148. $app = Factory::miniProgram(config('config.wechat_small_program'));
  149. $ret = $app->uniform_message->send($template);
  150. if (isset($ret['errcode']) && $ret['errcode'] != 0) {
  151. trace(['后台发送消息通知失败,请求参数' => $template, '返回数据' => $ret], 'error');
  152. }
  153. } catch (Exception $e) {
  154. trace(['后台发送消息通知失败' => $e->getMessage(), '请求参数' => $template ?? '', '返回数据' => $ret ?? ''], 'error');
  155. }
  156. return true;
  157. }
  158. }