123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Repositories\Contracts\WxNotificationInterface;
- use App\Facades\WxDecryptFacade;
- use App\Facades\WxServiceFacade;
- use App\Models\User;
- use App\Models\WxServiceQueue;
- /**
- *
- * @author lilin
- *
- */
- class WxNotificationFacadeRepository extends BaseRepository implements WxNotificationInterface
- {
- public function getNotification(string $xml, $request)
- {
- WxServiceFacade::getAccessToken();
- self::setLog(self::TYPENAME.'开始解密', $this->startTime());
- $array = WxDecryptFacade::xmlToArray($xml);
- self::setLog(self::TYPENAME.'解析XML得到', $this->startTime, $array);
- $decryptData = WxDecryptFacade::decryptMsg($array, $request->msg_signature, $request->timestamp, $request->nonce);
- self::setLog(self::TYPENAME.'解密后得到', $this->startTime, $decryptData);
- if ($decryptData['MsgType'] == 'event'){
- $type = strtolower($decryptData['MsgType'] . '_' . $decryptData['Event']);
- }else{
- $type = $decryptData['MsgType'];
- }
- switch ($type) {
- case 'text':
- $this->_text($decryptData);
- break;
- case 'event_scan':
- case 'event_subscribe':
- WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'你好,欢迎关注智悦盐都!']);
- break;
- case 'event_unsubscribe':
- // WxServiceFacade::sendMessage($data['FromUserName'], 'text', ['content'=>'客观别走!']);
- break;
- case 'event_click':
- $this->_eventClick($decryptData);
- break;
- }
- }
- private function _text($decryptData){
- self::setLog(self::TYPENAME.'执行 _text', $this->startTime, $decryptData);
- $nowHour = date('H');
- if ($nowHour >= 9 && $nowHour <= 17){ //上班时间人工回复
- self::setLog(self::TYPENAME.'执行 _text 上班时间人工回复', $this->startTime, $decryptData);
- }else{ //下班时间直接回复
- self::setLog(self::TYPENAME.'执行 _text 下班时间直接回复', $this->startTime, $decryptData);
- WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'感谢您的留言,稍后会与您联系。']);
- }
- }
- private function _eventClick($decryptData)
- {
- self::setLog(self::TYPENAME.'执行 _eventClick', $this->startTime, $decryptData);
- switch ($decryptData['EventKey']){
- /**
- * 点击菜单“微信咨询”
- * 第一次点:您好!欢迎访问智悦盐都心理援助热线正在接入服务中心,请稍后!
- * 如果客服没有回过信息,第二次点:非常抱歉!当前咨询人数较多,请耐心等待!
- * 如果是上班时间,回复 咨询员[管理员]正在为您服务,如果您有任何问题可以直接留言!
- *
- */
- case 'ZYYD_CLICK_001':
- //判断是否是新的一次接入
- $user = User::where('wx_openid', $decryptData['FromUserName'])->first(['id']);
- if (!$user){
- $wxUserInfo = WxServiceFacade::getUserInfo($decryptData['FromUserName']);
- $wxUserInfo = $wxUserInfo->getData()->data;
- $data = [
- 'wx_openid' => $wxUserInfo->openid,
- 'wx_nickname' => $wxUserInfo->nickname,
- 'wx_sex' => $wxUserInfo->sex,
- 'wx_province' => $wxUserInfo->city,
- 'wx_city' => $wxUserInfo->province,
- 'wx_country' => $wxUserInfo->country,
- 'wx_headimgurl' => $wxUserInfo->headimgurl,
- ];
- $user = User::updateOrCreate(['wx_openid' => $wxUserInfo->openid], $data);
- }
- self::setLog(self::TYPENAME.'执行 _eventClick 得到user', $this->startTime, ['user'=>$user]);
- $wxServiceQueue = WxServiceQueue::where('uid', $user->id)->orderBy('id', 'desc')->first();
- self::setLog(self::TYPENAME.'执行 _eventClick 得到wxServiceQueue', $this->startTime, ['wxServiceQueue'=>$wxServiceQueue]);
- //如果没有数据,表示是第一次
- if (!$wxServiceQueue){
- $serviceQueueData = [
- 'uid' => $user->id,
- ];
- WxServiceQueue::create($serviceQueueData);
- return WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'您好!欢迎访问智悦盐都心理援助热线正在接入服务中心,请稍后!']);
- }
- $nowHour = date('H');
- if ($nowHour >= 9 && $nowHour <= 17){
- return WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'咨询员[管理员]正在为您服务,如果您有任何问题可以直接留言!']);
- }
- switch ($wxServiceQueue->status){
- case WxServiceQueue::STATUS_0:
- self::setLog(self::TYPENAME.'执行 _eventClick STATUS_0', $this->startTime);
- WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'非常抱歉!当前咨询人数较多,请耐心等待!']);
- break;
- case WxServiceQueue::STATUS_1:
- self::setLog(self::TYPENAME.'执行 _eventClick STATUS_1', $this->startTime);
- WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'咨询员[管理员]正在为您服务,如果您有任何问题可以直接留言!']);
- break;
- case WxServiceQueue::STATUS_2: //表示是第一次
- case WxServiceQueue::STATUS_3:
- case WxServiceQueue::STATUS_4:
- self::setLog(self::TYPENAME.'执行 _eventClick STATUS_2,3,4', $this->startTime);
- $serviceQueueData = [
- 'uid' => $user->id,
- ];
- WxServiceQueue::create($serviceQueueData);
- WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'您好!欢迎访问智悦盐都心理援助热线正在接入服务中心,请稍后!']);
- break;
- }
- }
- }
- }
|