NoticeRepositories.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 crmeb\services\WechatService;
  10. use crmeb\services\WechatTemplateService;
  11. use crmeb\services\workerman\ChannelService;
  12. use app\models\routine\RoutineTemplate;
  13. use app\models\store\StoreOrderCartInfo;
  14. use app\models\user\User;
  15. use crmeb\services\YLYService;
  16. use think\facade\Log;
  17. use think\facade\Route;
  18. /** 消息通知静态类
  19. * Class NoticeRepositories
  20. * @package crmeb\repositories
  21. */
  22. class NoticeRepositories
  23. {
  24. /** 支付成功通知
  25. * @param $order
  26. * @param $formId
  27. */
  28. public static function noticeOrderPaySuccess($order)
  29. {
  30. $wechatUser = WechatUser::where('uid', $order['uid'])->field(['openid', 'routine_openid'])->find();
  31. if ($wechatUser) {
  32. $openid = $wechatUser['openid'];
  33. $routineOpenid = $wechatUser['routine_openid'];
  34. try {
  35. if ($openid && in_array($order['is_channel'], [0, 2])) {//公众号发送模板消息
  36. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_PAY_SUCCESS, [
  37. 'first' => '亲,您购买的商品已支付成功',
  38. 'keyword1' => $order['order_id'],
  39. 'keyword2' => $order['pay_price'],
  40. 'remark' => '点击查看订单详情'
  41. ], Route::buildUrl('order/detail/' . $order['order_id'])->suffix('')->domain(true)->build());
  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. Log::error('购买后发送提醒失败,错误原因:' . $e->getMessage());
  59. }
  60. }
  61. //打印小票
  62. $switch = sys_config('pay_success_printing_switch') ? true : false;
  63. if ($switch) {
  64. try {
  65. $order['cart_id'] = is_string($order['cart_id']) ? json_decode($order['cart_id'], true) : $order['cart_id'];
  66. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $order['cart_id'])->field('cart_info')->select();
  67. $cartInfo = count($cartInfo) ? $cartInfo->toArray() : [];
  68. $product = [];
  69. foreach ($cartInfo as $item) {
  70. $value = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  71. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  72. $value['productInfo']['store_name'] = StoreOrderCartInfo::getSubstrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  73. $product[] = $value;
  74. }
  75. YLYService::instance()->setContent(sys_config('site_name'), is_object($order) ? $order->toArray() : $order, $product)->orderPrinting();
  76. } catch (\Exception $e) {
  77. Log::error('小票打印出现错误,错误原因:' . $e->getMessage());
  78. }
  79. }
  80. //短信通知 下发用户支付成功 下发管理员支付通知
  81. event('ShortMssageSend', [$order['order_id'], ['PaySuccess', 'AdminPaySuccess']]);
  82. }
  83. }