MessageSubscribe.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. WechatUser::saveUser($message->FromUserName);
  53. $event = isset($message->Event) ?
  54. $message->MsgType . (
  55. $message->Event == 'subscribe' && isset($message->EventKey) ? '_scan' : ''
  56. ) . '_' . $message->Event : $message->MsgType;
  57. WechatMessage::setMessage(json_encode($message), $message->FromUserName, strtolower($event));
  58. }
  59. /**
  60. * 用户取消关注公众号前置操作
  61. * @param $event
  62. */
  63. public function onWechatEventUnsubscribeBefore($event)
  64. {
  65. list($message) = $event;
  66. WechatUser::unSubscribe($message->FromUserName);
  67. }
  68. /**
  69. * 下发送短信事件
  70. * @param array $event
  71. */
  72. public function onShortMssageSend($event)
  73. {
  74. //$actions 可为数组
  75. list($order_id, $actions) = $event;
  76. try {
  77. if (is_array($actions)) {
  78. foreach ($actions as $action) {
  79. $actionName = 'onMssageSend' . $action;
  80. if (method_exists($this, $actionName)) $this->$actionName($order_id);
  81. }
  82. } else {
  83. $actionName = 'onMssageSend' . $actions;
  84. if (method_exists($this, $actionName)) $this->$actionName($order_id);
  85. }
  86. } catch (\Exception $e) {
  87. Log::error('短信下发事件发生系统错误,错误原因:' . $e->getMessage());
  88. }
  89. }
  90. /**
  91. * 支付成功短信提醒
  92. * @param string $order_id
  93. */
  94. public function onMssageSendPaySuccess($order_id)
  95. {
  96. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0])->find();
  97. if (!$storeInfo) return;
  98. $switch = sys_config('lower_order_switch') ? true : false;
  99. //模板变量
  100. $pay_price = $storeInfo->pay_price;
  101. ShortLetterRepositories::send($switch, $storeInfo->user_phone, compact('order_id', 'pay_price'), 'PAY_SUCCESS_CODE', '用户支付成功发送短信失败,订单号为:' . $order_id);
  102. }
  103. /**
  104. * 发货短信提醒
  105. * @param string $order_id
  106. */
  107. public function onMssageSendDeliver($order_id)
  108. {
  109. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 1])->find();
  110. if (!$storeInfo) return;
  111. $switch = sys_config('deliver_goods_switch') ? true : false;
  112. //模板变量
  113. $nickname = User::where('uid', $storeInfo->uid)->value('nickname');
  114. $store_name = StoreOrder::getProductTitle($storeInfo->cart_id);
  115. ShortLetterRepositories::send($switch, $storeInfo->user_phone, compact('order_id', 'store_name', 'nickname'), 'DELIVER_GOODS_CODE', '用户发货发送短信失败,订单号为:' . $order_id);
  116. }
  117. /**
  118. * 确认收货短信提醒
  119. * @param string $order_id
  120. */
  121. public function onMssageSendReceiving($order_id)
  122. {
  123. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 2])->find();
  124. if (!$storeInfo) return;
  125. $switch = sys_config('confirm_take_over_switch') ? true : false;
  126. //模板变量
  127. $store_name = StoreOrder::getProductTitle($storeInfo->cart_id);
  128. ShortLetterRepositories::send($switch, $storeInfo->user_phone, compact('store_name', 'order_id'), 'TAKE_DELIVERY_CODE', '用户确认收货发送短信失败,订单号为:' . $order_id);
  129. }
  130. /**
  131. * 用户下单管理员短信提醒
  132. * @param string $order_id
  133. */
  134. public function onMssageSendAdminPlaceAnOrder($order_id)
  135. {
  136. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 0, 'refund_status' => 0, 'status' => 0])->find();
  137. if (!$storeInfo) return;
  138. $switch = sys_config('admin_lower_order_switch') ? true : false;
  139. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  140. //模板变量
  141. $admin_name = $userInfo->nickname;
  142. $order_id = $storeInfo->order_id;
  143. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_PLACE_ORDER_CODE', '用户下单成功管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  144. });
  145. }
  146. /**
  147. * 支付成功管理员短信提醒
  148. * @param string $order_id
  149. */
  150. public function onMssageSendAdminPaySuccess($order_id)
  151. {
  152. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 0])->find();
  153. if (!$storeInfo) return;
  154. $switch = sys_config('admin_pay_success_switch') ? true : false;
  155. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  156. //模板变量
  157. $admin_name = $userInfo->nickname;
  158. $order_id = $storeInfo->order_id;
  159. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_PAY_SUCCESS_CODE', '用户支付成功管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  160. });
  161. }
  162. /**
  163. * 用户确认收货管理员短信提醒
  164. * @param string $order_id
  165. */
  166. public function onMssageSendAdminConfirmTakeOver($order_id)
  167. {
  168. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 0, 'status' => 2])->find();
  169. if (!$storeInfo) return;
  170. $switch = sys_config('admin_confirm_take_over_switch') ? true : false;
  171. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  172. //模板变量
  173. $admin_name = $userInfo->nickname;
  174. $order_id = $storeInfo->order_id;
  175. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_TAKE_DELIVERY_CODE', '用户确认收货成功管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  176. });
  177. }
  178. /**
  179. * 用户发起退款管理员短信提醒
  180. * @param string $order_id
  181. */
  182. public function onMssageSendAdminRefund($order_id)
  183. {
  184. $storeInfo = StoreOrder::where(['order_id' => $order_id, 'paid' => 1, 'refund_status' => 1])->find();
  185. if (!$storeInfo) return;
  186. $switch = sys_config('admin_refund_switch') ? true : false;
  187. $switch && CustomerRepository::getAdminNoticeAuth(function ($userInfo) use ($storeInfo) {
  188. //模板变量
  189. $admin_name = $userInfo->nickname;
  190. $order_id = $storeInfo->order_id;
  191. ShortLetterRepositories::send(true, $userInfo->phone, compact('admin_name', 'order_id'), 'ADMIN_RETURN_GOODS_CODE', '用户退款管理员发送短信通知失败,订单号为:' . $storeInfo->order_id);
  192. });
  193. }
  194. }