123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 2021/3/8
- * Time: 10:31 上午
- */
- namespace App\Community\Controllers;
- use App\Models\Order;
- use App\Models\Patient;
- use App\User;
- use Cache;
- use EasyWeChat\Factory;
- use Encore\Admin\Controllers\AdminController;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Facades\Admin;
- use Exception;
- class NoticeManageController extends AdminController
- {
- public function noticelist()
- {
- $req = request()->query();
- $user = Admin::user();
- $builder = Patient::orderBy('id', 'desc');
- if (!empty($req['user'])) {
- $userIds = User::where('nickname', 'like', '%'.$req['user'].'%')->pluck('id')->toArray();
- $builder->whereIn('user_id', $userIds);
- }
- if (!empty($req['name'])) {
- $builder->where('name', 'like', '%'.$req['name'].'%');
- }
- if (!empty($req['guardian_name'])) {
- $builder->where('guardian_name', 'like', '%'.$req['guardian_name'].'%');
- }
- if (!empty($req['phone'])) {
- $builder->where('phone', $req['phone']);
- }
- if (!empty($req['product_type'])) {
- $user_ids = Order::where('product_type', $req['product_type'])->distinct('user_id')->pluck('user_id');
- $builder->whereIn('user_id', $user_ids);
- }
- $list = $builder->paginate();
- $data['req'] = $req;
- $data['list'] = $list;
- $data['ids'] = Cache::get('notice-'.$user['id'])[$req['page']??1] ?? [];
- $content = new Content();
- Admin::disablePjax();
- return $content->title('群发通知')->view('cdms/notice_list', $data);
- }
- public function saveIds()
- {
- $req = request()->post();
- $user = Admin::user();
- $arr = Cache::get($req['type'].'-'.$user['id']) ?? [];
- $arr[$req['page']] = $req['ids']??[];
- Cache::set($req['type'].'-'.$user['id'], $arr, 300);
- return out();
- }
- public function sendNotice()
- {
- $req = request()->post();
- admin_validate($req, [
- 'content|内容' => 'required',
- 'type|服务类型' => 'required',
- 'stime|服务时间' => 'required',
- 'remark|备注' => 'required',
- ]);
- $user = Admin::user();
- $arr = Cache::get('notice-'.$user['id']) ?? [];
- $ids = [];
- foreach ($arr as $k => $v) {
- $ids = array_merge($ids, $v);
- }
- $ids = array_values(array_unique($ids));
- if (empty($ids)) {
- return out(null, 10001, '请勾选要发送通知的用户');
- }
- Cache::delete('notice-'.$user['id']);
- foreach ($ids as $id) {
- $user_id = Patient::where('id', $id)->value('user_id');
- if(empty($user_id)) continue;
- $openid = User::where('id', $user_id)->value('openid');
- if(empty($openid)) continue;
- $content = $req['content'];
- $remark= $req['remark'];
- $stime= $req['stime'];
- $type= $req['type'];
- $service_arr = [4 => '疫苗接种服务', 5 => '儿保服务'];
- $service_name = $service_arr[$type];
- $template_arr = [
- 4 => 'xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo',
- 5 => 'CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc',
- ];
- $tempId = $template_arr[$type];
- $msg['content'] = $content;
- $msg['service_date'] = $stime;
- $msg['remark'] = $remark;
- $msg['service_name'] = $service_name;
- $this->send($openid, $tempId, $msg);
- }
- return out();
- }
- private function send($open_id, $tempId, $msg)
- {
- try {
- $template = [
- 'touser' => $open_id,
- 'mp_template_msg' => [
- 'appid' => env('OFFICIAL_WECHAT_APPID'),
- 'template_id' => $tempId,
- 'url' => '',
- 'miniprogram' => [
- 'appid' => env('WECHAT_APPID', 'wxd41dd232837996c4'),
- 'pagepath' => '',
- ],
- 'data' => [
- 'first' => [
- 'value' => $msg['content'],
- ],
- 'keyword1' => [
- 'value' => $msg['service_name'],
- ],
- 'keyword2' => [
- 'value' => $msg['service_date'],
- ],
- 'remark' => [
- 'value' => $msg['remark'],
- ],
- ],
- ],
- ];
- $app = Factory::miniProgram(config('config.wechat_small_program'));
- $ret = $app->uniform_message->send($template);
- if (isset($ret['errcode']) && $ret['errcode'] != 0) {
- trace(['后台发送消息通知失败,请求参数' => $template, '返回数据' => $ret], 'error');
- }
- } catch (Exception $e) {
- trace(['后台发送消息通知失败' => $e->getMessage(), '请求参数' => $template ?? '', '返回数据' => $ret ?? ''], 'error');
- }
- return true;
- }
- }
|