NoticeManageController.php 5.6 KB

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