WxNotificationFacadeRepository.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Repositories\Contracts\WxNotificationInterface;
  4. use App\Facades\WxDecryptFacade;
  5. use App\Facades\WxServiceFacade;
  6. use App\Models\User;
  7. use App\Models\WxServiceQueue;
  8. /**
  9. *
  10. * @author lilin
  11. *
  12. */
  13. class WxNotificationFacadeRepository extends BaseRepository implements WxNotificationInterface
  14. {
  15. public function getNotification(string $xml, $request)
  16. {
  17. WxServiceFacade::getAccessToken();
  18. self::setLog(self::TYPENAME.'开始解密', $this->startTime());
  19. $array = WxDecryptFacade::xmlToArray($xml);
  20. self::setLog(self::TYPENAME.'解析XML得到', $this->startTime, $array);
  21. $decryptData = WxDecryptFacade::decryptMsg($array, $request->msg_signature, $request->timestamp, $request->nonce);
  22. self::setLog(self::TYPENAME.'解密后得到', $this->startTime, $decryptData);
  23. if ($decryptData['MsgType'] == 'event'){
  24. $type = strtolower($decryptData['MsgType'] . '_' . $decryptData['Event']);
  25. }else{
  26. $type = $decryptData['MsgType'];
  27. }
  28. switch ($type) {
  29. case 'text':
  30. $this->_text($decryptData);
  31. break;
  32. case 'event_scan':
  33. case 'event_subscribe':
  34. WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'你好,欢迎关注智悦盐都!']);
  35. break;
  36. case 'event_unsubscribe':
  37. // WxServiceFacade::sendMessage($data['FromUserName'], 'text', ['content'=>'客观别走!']);
  38. break;
  39. case 'event_click':
  40. $this->_eventClick($decryptData);
  41. break;
  42. }
  43. }
  44. private function _text($decryptData){
  45. self::setLog(self::TYPENAME.'执行 _text', $this->startTime, $decryptData);
  46. $nowHour = date('H');
  47. if ($nowHour >= 9 && $nowHour <= 17){ //上班时间人工回复
  48. self::setLog(self::TYPENAME.'执行 _text 上班时间人工回复', $this->startTime, $decryptData);
  49. }else{ //下班时间直接回复
  50. self::setLog(self::TYPENAME.'执行 _text 下班时间直接回复', $this->startTime, $decryptData);
  51. WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'感谢您的留言,稍后会与您联系。']);
  52. }
  53. }
  54. private function _eventClick($decryptData)
  55. {
  56. self::setLog(self::TYPENAME.'执行 _eventClick', $this->startTime, $decryptData);
  57. switch ($decryptData['EventKey']){
  58. /**
  59. * 点击菜单“微信咨询”
  60. * 第一次点:您好!欢迎访问智悦盐都心理援助热线正在接入服务中心,请稍后!
  61. * 如果客服没有回过信息,第二次点:非常抱歉!当前咨询人数较多,请耐心等待!
  62. * 如果是上班时间,回复 咨询员[管理员]正在为您服务,如果您有任何问题可以直接留言!
  63. *
  64. */
  65. case 'ZYYD_CLICK_001':
  66. //判断是否是新的一次接入
  67. $user = User::where('wx_openid', $decryptData['FromUserName'])->first(['id']);
  68. if (!$user){
  69. $wxUserInfo = WxServiceFacade::getUserInfo($decryptData['FromUserName']);
  70. $wxUserInfo = $wxUserInfo->getData()->data;
  71. $data = [
  72. 'wx_openid' => $wxUserInfo->openid,
  73. 'wx_nickname' => $wxUserInfo->nickname,
  74. 'wx_sex' => $wxUserInfo->sex,
  75. 'wx_province' => $wxUserInfo->city,
  76. 'wx_city' => $wxUserInfo->province,
  77. 'wx_country' => $wxUserInfo->country,
  78. 'wx_headimgurl' => $wxUserInfo->headimgurl,
  79. ];
  80. $user = User::updateOrCreate(['wx_openid' => $wxUserInfo->openid], $data);
  81. }
  82. self::setLog(self::TYPENAME.'执行 _eventClick 得到user', $this->startTime, ['user'=>$user]);
  83. $wxServiceQueue = WxServiceQueue::where('uid', $user->id)->orderBy('id', 'desc')->first();
  84. self::setLog(self::TYPENAME.'执行 _eventClick 得到wxServiceQueue', $this->startTime, ['wxServiceQueue'=>$wxServiceQueue]);
  85. //如果没有数据,表示是第一次
  86. if (!$wxServiceQueue){
  87. $serviceQueueData = [
  88. 'uid' => $user->id,
  89. ];
  90. WxServiceQueue::create($serviceQueueData);
  91. return WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'您好!欢迎访问智悦盐都心理援助热线正在接入服务中心,请稍后!']);
  92. }
  93. $nowHour = date('H');
  94. if ($nowHour >= 9 && $nowHour <= 17){
  95. return WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'咨询员[管理员]正在为您服务,如果您有任何问题可以直接留言!']);
  96. }
  97. switch ($wxServiceQueue->status){
  98. case WxServiceQueue::STATUS_0:
  99. self::setLog(self::TYPENAME.'执行 _eventClick STATUS_0', $this->startTime);
  100. WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'非常抱歉!当前咨询人数较多,请耐心等待!']);
  101. break;
  102. case WxServiceQueue::STATUS_1:
  103. self::setLog(self::TYPENAME.'执行 _eventClick STATUS_1', $this->startTime);
  104. WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'咨询员[管理员]正在为您服务,如果您有任何问题可以直接留言!']);
  105. break;
  106. case WxServiceQueue::STATUS_2: //表示是第一次
  107. case WxServiceQueue::STATUS_3:
  108. case WxServiceQueue::STATUS_4:
  109. self::setLog(self::TYPENAME.'执行 _eventClick STATUS_2,3,4', $this->startTime);
  110. $serviceQueueData = [
  111. 'uid' => $user->id,
  112. ];
  113. WxServiceQueue::create($serviceQueueData);
  114. WxServiceFacade::sendMessage($decryptData['FromUserName'], 'text', ['content'=>'您好!欢迎访问智悦盐都心理援助热线正在接入服务中心,请稍后!']);
  115. break;
  116. }
  117. }
  118. }
  119. }