CustomerRepository.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreBargain;
  4. use app\models\store\StoreCart;
  5. use app\models\store\StoreCombination;
  6. use app\models\store\StoreProduct;
  7. use app\models\store\StoreSeckill;
  8. use app\models\store\StoreService;
  9. use app\models\user\User;
  10. use app\models\user\WechatUser;
  11. use think\facade\Log;
  12. use crmeb\services\WechatService;
  13. /**
  14. * 客服消息推送
  15. * Class CustomerService
  16. * @package crmeb\services
  17. */
  18. class CustomerRepository
  19. {
  20. /**
  21. * 订单支付成功后给客服发送客服消息
  22. * @param $order
  23. * @param int $type 1 公众号 0 小程序
  24. * @return string
  25. */
  26. public static function sendOrderPaySuccessCustomerService($order,$type = 0)
  27. {
  28. $serviceOrderNotice = StoreService::getStoreServiceOrderNotice();
  29. if(count($serviceOrderNotice)){
  30. foreach ($serviceOrderNotice as $key=>&$item){
  31. $userInfo = WechatUser::get($item);
  32. if($userInfo){
  33. $userInfo = $userInfo->toArray();
  34. if($userInfo['subscribe'] && $userInfo['openid']){
  35. $orderStatus = StoreService::orderServiceStatus($userInfo['uid']);
  36. if($orderStatus){
  37. // 统计管理开启 推送图文消息
  38. $head = '订单提醒 订单号:'.$order['order_id'];
  39. $url = sys_config('site_url') . '/pages/admin/orderDetail/index?id='.$order['order_id'];
  40. $description = '';
  41. $image = sys_config('site_logo');
  42. if(isset($order['seckill_id']) && $order['seckill_id'] > 0){
  43. $description .= '秒杀商品:'.StoreSeckill::getProductField($order['seckill_id'], 'title');
  44. $image = StoreSeckill::getProductField($order['seckill_id'], 'image');
  45. }else if(isset($order['combination_id']) && $order['combination_id'] > 0){
  46. $description .= '拼团商品:'.StoreCombination::getCombinationField($order['combination_id'], 'title');
  47. $image = StoreCombination::getCombinationField($order['combination_id'], 'image');
  48. }else if(isset($order['bargain_id']) && $order['bargain_id'] > 0){
  49. $description .= '砍价商品:'.StoreBargain::getBargainField($order['bargain_id'], 'title');
  50. $image = StoreBargain::getBargainField($order['bargain_id'], 'image');
  51. }else{
  52. $productIds = StoreCart::getCartIdsProduct((array)$order['cart_id']);
  53. $storeProduct = StoreProduct::getProductStoreNameOrImage($productIds);
  54. if(count($storeProduct)){
  55. foreach ($storeProduct as $value){
  56. $description .= $value['store_name'].' ';
  57. $image = $value['image'];
  58. }
  59. }
  60. }
  61. $message = WechatService::newsMessage($head, $description, $url, $image);
  62. try {
  63. WechatService::staffService()->message($message)->to($userInfo['openid'])->send();
  64. } catch (\Exception $e) {
  65. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  66. }
  67. }else{
  68. // 推送文字消息
  69. $head = "客服提醒:亲,您有一个新订单 \r\n订单单号:{$order['order_id']}\r\n支付金额:¥{$order['pay_price']}\r\n备注信息:{$order['mark']}\r\n订单来源:小程序";
  70. if($type) $head = "客服提醒:亲,您有一个新订单 \r\n订单单号:{$order['order_id']}\r\n支付金额:¥{$order['pay_price']}\r\n备注信息:{$order['mark']}\r\n订单来源:公众号";
  71. try {
  72. WechatService::staffService()->message($head)->to($userInfo['openid'])->send();
  73. } catch (\Exception $e) {
  74. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * 提取管理员权限
  84. * @param callable $callable 回调函数
  85. */
  86. public static function getAdminNoticeAuth(callable $callable)
  87. {
  88. $serviceOrderNotice = StoreService::getStoreServiceOrderNotice();
  89. if (count($serviceOrderNotice)) {
  90. foreach ($serviceOrderNotice as $uid) {
  91. $userInfo = User::where('uid', $uid)->find();
  92. if ($userInfo && is_callable($callable)) $callable($userInfo);
  93. }
  94. }
  95. }
  96. }