NoticeRepositories.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xurongyao <763569752@qq.com>
  5. * Date: 2019/11/13 4:52 PM
  6. */
  7. namespace crmeb\repositories;
  8. use app\models\user\WechatUser;
  9. use app\models\wechat\WechatTemplate;
  10. use crmeb\services\printer\Printer;
  11. use crmeb\services\WechatService;
  12. use crmeb\services\WechatTemplateService;
  13. use crmeb\services\workerman\ChannelService;
  14. use app\models\routine\RoutineTemplate;
  15. use app\models\store\StoreOrderCartInfo;
  16. use app\models\user\User;
  17. use app\models\user\UserExtract;
  18. use app\models\store\StoreOrder;
  19. use app\models\store\StoreProduct;
  20. use app\models\store\StoreProductReply;
  21. use think\facade\Route;
  22. use think\facade\Log;
  23. /** 消息通知静态类
  24. * Class NoticeRepositories
  25. * @package crmeb\repositories
  26. */
  27. class NoticeRepositories
  28. {
  29. /** 支付成功通知
  30. * @param $order
  31. */
  32. public static function noticeOrderPaySuccess($order, $mer_id = '')
  33. {
  34. $wechatUser = WechatUser::where('uid', $order['uid'])->field(['openid', 'routine_openid'])->find();
  35. if ($wechatUser) {
  36. $openid = $wechatUser['openid'];
  37. $routineOpenid = $wechatUser['routine_openid'];
  38. try {
  39. if ($openid && in_array($order['is_channel'], [0, 2])) {//公众号发送模板消息
  40. $wechatTemplate = new WechatTemplate();
  41. $wechatTemplate->sendOrderPaySuccess($order);
  42. //订单支付成功后给客服发送模版消息
  43. WechatTemplateService::sendAdminNoticeTemplate([
  44. 'first' => "亲,您有一个新订单 \n订单号:{$order['order_id']}",
  45. 'keyword1' => '新订单',
  46. 'keyword2' => '已支付',
  47. 'keyword3' => date('Y/m/d H:i', time()),
  48. 'remark' => '请及时处理'
  49. ]);
  50. //订单支付成功后给客服发送客服消息
  51. CustomerRepository::sendOrderPaySuccessCustomerService($order, 1);
  52. } else if ($routineOpenid && in_array($order['is_channel'], [1, 2])) {//小程序发送模板消息
  53. RoutineTemplate::sendOrderSuccess($order['uid'], $order['pay_price'], $order['order_id']);
  54. //订单支付成功后给客服发送客服消息
  55. CustomerRepository::sendOrderPaySuccessCustomerService($order, 0);
  56. }
  57. } catch (\Exception $e) {
  58. }
  59. }
  60. //打印小票
  61. $switch = sys_config('pay_success_printing_switch', '', $mer_id) ? true : false;
  62. if ($switch) {
  63. try {
  64. $order['cart_id'] = is_string($order['cart_id']) ? json_decode($order['cart_id'], true) : $order['cart_id'];
  65. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $order['cart_id'])->field('cart_info')->select();
  66. $cartInfo = count($cartInfo) ? $cartInfo->toArray() : [];
  67. $product = [];
  68. foreach ($cartInfo as $item) {
  69. $value = is_string($item['cart_info']) ? json_decode($item['cart_info']) : $item['cart_info'];
  70. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  71. $value['productInfo']['store_name'] = StoreOrderCartInfo::getSubstrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  72. $product[] = $value;
  73. }
  74. (new Printer())->setPrinterContent([
  75. 'name' => sys_config('site_name', '', $mer_id),
  76. 'orderInfo' => is_object($order) ? $order->toArray() : $order,
  77. 'product' => $product
  78. ])->startPrinter();
  79. } catch (\Exception $e) {
  80. Log::error('小票打印出现错误,错误原因:' . $e->getMessage());
  81. }
  82. }
  83. //短信通知 下发用户支付成功 下发管理员支付通知
  84. event('ShortMssageSend', [$order['order_id'], ['PaySuccess', 'AdminPaySuccess']]);
  85. }
  86. /**
  87. * 待办消息提醒
  88. * @return array
  89. */
  90. public static function jnotice($mer_id = '')
  91. {
  92. $data['ordernum'] = StoreOrder::where('mer_id', $mer_id)->where('paid', 1)->where('status', 0)
  93. ->where('shipping_type', 1)->where('refund_status', 0)
  94. ->where('is_del', 0)->count();
  95. $store_stock = sys_config('store_stock');
  96. if ($store_stock < 0) $store_stock = 2;
  97. $data['inventory'] = StoreProduct::where('mer_id', $mer_id)->where('stock', '<=', $store_stock)->where('is_show', 1)->where('is_del', 0)->count();//库存
  98. $data['commentnum'] = StoreProductReply::alias('r')->where('p.mer_id', $mer_id)->join('store_product p', 'r.product_id=p.id')->where('r.is_reply', 0)->count();
  99. $data['reflectnum'] = UserExtract::where('mer_id', $mer_id)->where('status', 0)->count();//提现
  100. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  101. $data['newOrderId'] = StoreOrder::statusByWhere(1, StoreOrder::where('mer_id', $mer_id))->where('is_remind', 0)->column('order_id', 'id');
  102. if (count($data['newOrderId'])) StoreOrder::where('order_id', 'in', $data['newOrderId'])->update(['is_remind' => 1]);
  103. $value = [];
  104. if ($data['ordernum'] != 0) {
  105. $value[] = [
  106. 'title' => "您还有$data[ordernum]个待发货的订单",
  107. 'type' => 'bulb'
  108. ];
  109. }
  110. if ($data['inventory'] != 0) {
  111. $value[] = [
  112. 'title' => "您有$data[inventory]个商品库存预警",
  113. 'type' => 'information',
  114. ];
  115. }
  116. if ($data['commentnum'] != 0) {
  117. $value[] = [
  118. 'title' => "您有$data[commentnum]条评论待回复",
  119. 'type' => 'bulb'
  120. ];
  121. }
  122. if ($data['reflectnum'] != 0) {
  123. $value[] = [
  124. 'title' => "您有$data[reflectnum]个提现申请待审核",
  125. 'type' => 'bulb'
  126. ];
  127. }
  128. return static::noticeData($value);
  129. }
  130. /**
  131. * 消息返回格式
  132. * @param array $data
  133. * @return array
  134. */
  135. public static function noticeData(array $data): array
  136. {
  137. // 消息图标
  138. $iconColor = [
  139. // 邮件 消息
  140. 'mail' => [
  141. 'icon' => 'md-mail',
  142. 'color' => '#3391e5'
  143. ],
  144. // 普通 消息
  145. 'bulb' => [
  146. 'icon' => 'md-bulb',
  147. 'color' => '#87d068'
  148. ],
  149. // 警告 消息
  150. 'information' => [
  151. 'icon' => 'md-information',
  152. 'color' => '#fe5c57'
  153. ],
  154. // 关注 消息
  155. 'star' => [
  156. 'icon' => 'md-star',
  157. 'color' => '#ff9900'
  158. ],
  159. // 申请 消息
  160. 'people' => [
  161. 'icon' => 'md-people',
  162. 'color' => '#f06292'
  163. ],
  164. ];
  165. // 消息类型
  166. $type = array_keys($iconColor);
  167. // 默认数据格式
  168. $default = [
  169. 'icon' => 'md-bulb',
  170. 'iconColor' => '#87d068',
  171. 'title' => '',
  172. 'url' => '',
  173. 'type' => 'bulb',
  174. 'read' => 0,
  175. 'time' => 0
  176. ];
  177. $value = [];
  178. foreach ($data as $item) {
  179. $val = array_merge($default, $item);
  180. if (isset($item['type']) && in_array($item['type'], $type)) {
  181. $val['type'] = $item['type'];
  182. $val['iconColor'] = $iconColor[$item['type']]['color'] ?? '';
  183. $val['icon'] = $iconColor[$item['type']]['icon'] ?? '';
  184. }
  185. $value[] = $val;
  186. }
  187. return $value;
  188. }
  189. }