MessageSubscribe.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace crmeb\subscribes;
  3. use app\admin\model\wechat\WechatMessage;
  4. use app\models\store\StoreOrder;
  5. use app\models\store\StoreProduct;
  6. use app\models\store\StoreProductReply;
  7. use app\models\store\StoreService;
  8. use app\models\user\User;
  9. use app\models\user\UserExtract;
  10. use app\models\user\WechatUser;
  11. use crmeb\repositories\CustomerRepository;
  12. use crmeb\repositories\ShortLetterRepositories;
  13. use crmeb\services\SystemConfigService;
  14. use crmeb\services\workerman\ChannelService;
  15. use think\facade\Log;
  16. /**
  17. * 用户消息事件
  18. * Class MessageSubscribe
  19. * @package crmeb\subscribes
  20. */
  21. class MessageSubscribe
  22. {
  23. public function handle()
  24. {
  25. }
  26. /** 后台订单下单,评论,支付成功,后台消息提醒
  27. * @param $event
  28. */
  29. public function onAdminNewPush($event)
  30. {
  31. try {
  32. $data['ordernum'] = StoreOrder::where('paid', 1)->where('status', 0)
  33. ->where('shipping_type', 1)->where('refund_status', 0)
  34. ->where('is_del', 0)->count();
  35. $store_stock = sys_config('store_stock');
  36. if ($store_stock < 0) $store_stock = 2;
  37. $data['inventory'] = StoreProduct::where('stock', '<=', $store_stock)->where('is_show', 1)->where('is_del', 0)->count();//库存
  38. $data['commentnum'] = StoreProductReply::where('is_reply', 0)->count();
  39. $data['reflectnum'] = UserExtract::where('status', 0)->count();//提现
  40. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  41. ChannelService::instance()->send('ADMIN_NEW_PUSH', $data);
  42. } catch (\Exception $e) {
  43. }
  44. }
  45. /**
  46. * 微信消息前置操作
  47. * @param $event
  48. */
  49. public function onWechatMessageBefore($event)
  50. {
  51. list($message) = $event;
  52. @file_put_contents('scan.txt', json_encode($event) . PHP_EOL, FILE_APPEND);
  53. WechatUser::saveUser($message->FromUserName);
  54. $event = isset($message->Event) ?
  55. $message->MsgType . (
  56. $message->Event == 'subscribe' && isset($message->EventKey) ? '_scan' : ''
  57. ) . '_' . $message->Event : $message->MsgType;
  58. WechatMessage::setMessage(json_encode($message), $message->FromUserName, strtolower($event));
  59. }
  60. /**
  61. * 用户取消关注公众号前置操作
  62. * @param $event
  63. */
  64. public function onWechatEventUnsubscribeBefore($event)
  65. {
  66. list($message) = $event;
  67. WechatUser::unSubscribe($message->FromUserName);
  68. }
  69. /**
  70. * 下发送短信事件
  71. * @param array $event
  72. */
  73. public function onShortMssageSend($event)
  74. {
  75. //$actions 可为数组
  76. list($order_id, $actions) = $event;
  77. try {
  78. if (is_array($actions)) {
  79. foreach ($actions as $action) {
  80. $actionName = 'onMssageSend' . $action;
  81. if (method_exists($this, $actionName)) $this->$actionName($order_id);
  82. }
  83. } else {
  84. $actionName = 'onMssageSend' . $actions;
  85. if (method_exists($this, $actionName)) $this->$actionName($order_id);
  86. }
  87. } catch (\Exception $e) {
  88. Log::error('短信下发事件发生系统错误,错误原因:' . $e->getMessage());
  89. }
  90. }
  91. /**
  92. * 支付成功短信提醒
  93. * @param string $order_id
  94. */
  95. public function onMssageSendPaySuccess($order_id)
  96. {
  97. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0])->find();
  98. if (!$storeInfo) return;
  99. $switch = sys_config('lower_order_switch') ? true : false;
  100. //模板变量
  101. $pay_price = $storeInfo->pay_price;
  102. ShortLetterRepositories::send($switch, $storeInfo->user_phone, compact('order_id', 'pay_price'), 'PAY_SUCCESS_CODE', '用户支付成功发送短信失败,订单号为:' . $order_id);
  103. }
  104. /**
  105. * 发货短信提醒
  106. * @param string $order_id
  107. */
  108. public function onMssageSendDeliver($order_id)
  109. {
  110. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 1])->find();
  111. if (!$storeInfo) return;
  112. $switch = sys_config('deliver_goods_switch') ? true : false;
  113. //模板变量
  114. $nickname = User::where('uid', $storeInfo->uid)->value('nickname');
  115. $store_name = StoreOrder::getProductTitle($storeInfo->cart_id);
  116. ShortLetterRepositories::send($switch, $storeInfo->user_phone, compact('order_id', 'store_name', 'nickname'), 'DELIVER_GOODS_CODE', '用户发货发送短信失败,订单号为:' . $order_id);
  117. }
  118. /**
  119. * 确认收货短信提醒
  120. * @param string $order_id
  121. */
  122. public function onMssageSendReceiving($order_id)
  123. {
  124. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 2])->find();
  125. if (!$storeInfo) return;
  126. $switch = sys_config('confirm_take_over_switch') ? true : false;
  127. //模板变量
  128. $store_name = StoreOrder::getProductTitle($storeInfo->cart_id);
  129. ShortLetterRepositories::send($switch, $storeInfo->user_phone, compact('store_name', 'order_id'), 'TAKE_DELIVERY_CODE', '用户确认收货发送短信失败,订单号为:' . $order_id);
  130. }
  131. /**
  132. * 用户下单管理员短信提醒
  133. * @param string $order_id
  134. */
  135. public function onMssageSendAdminPlaceAnOrder($order_id)
  136. {
  137. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 0, 'refund_status' => 0, 'status' => 0])->find();
  138. if (!$storeInfo) return;
  139. $switch = sys_config('admin_lower_order_switch') ? true : false;
  140. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  141. //模板变量
  142. $admin_name = $userInfo->nickname;
  143. $order_id = $storeInfo->order_id;
  144. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_PLACE_ORDER_CODE', '用户下单成功管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  145. });
  146. }
  147. /**
  148. * 支付成功管理员短信提醒
  149. * @param string $order_id
  150. */
  151. public function onMssageSendAdminPaySuccess($order_id)
  152. {
  153. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 0])->find();
  154. if (!$storeInfo) return;
  155. $switch = sys_config('admin_pay_success_switch') ? true : false;
  156. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  157. //模板变量
  158. $admin_name = $userInfo->nickname;
  159. $order_id = $storeInfo->order_id;
  160. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_PAY_SUCCESS_CODE', '用户支付成功管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  161. });
  162. }
  163. /**
  164. * 用户确认收货管理员短信提醒
  165. * @param string $order_id
  166. */
  167. public function onMssageSendAdminConfirmTakeOver($order_id)
  168. {
  169. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 2])->find();
  170. if (!$storeInfo) return;
  171. $switch = sys_config('admin_confirm_take_over_switch') ? true : false;
  172. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  173. //模板变量
  174. $admin_name = $userInfo->nickname;
  175. $order_id = $storeInfo->order_id;
  176. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_TAKE_DELIVERY_CODE', '用户确认收货成功管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  177. });
  178. }
  179. /**
  180. * 用户发起退款管理员短信提醒
  181. * @param string $order_id
  182. */
  183. public function onMssageSendAdminRefund($order_id)
  184. {
  185. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 1])->find();
  186. if (!$storeInfo) return;
  187. $switch = sys_config('admin_refund_switch') ? true : false;
  188. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  189. //模板变量
  190. $admin_name = $userInfo->nickname;
  191. $order_id = $storeInfo->order_id;
  192. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_RETURN_GOODS_CODE', '用户退款管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  193. });
  194. }
  195. }