You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Order;
  4. use App\Model\OrderGoods;
  5. use App\Model\OrderMain;
  6. use App\Model\Store;
  7. use App\Model\Users;
  8. use EasyWeChat\Factory;
  9. use Hyperf\Guzzle\CoroutineHandler;
  10. class MiniprogramService implements MiniprogramServiceInterface
  11. {
  12. public function sendTemMsgForOnlineOrder($order_main_id)
  13. {
  14. // 查询订单信息
  15. $order = OrderMain::find($order_main_id);
  16. $payTypes = ['1' => '微信支付', '2' => '余额支付', '3' => '积分支付', '4' => '货到付款'];
  17. $address_store = $order['address'] . ';' .$order['name']. ';'. substr_replace($order['tel'],'****',3,4);
  18. $address = $order['address'] . ';' .$order['name']. ';'. $order['tel'];
  19. // 查询子订单,用于发消息给商户
  20. $order_children = Order::query()->select(['id', 'order_num', 'store_id', 'money', 'time'])
  21. ->where(['order_main_id' => $order_main_id])
  22. ->get()
  23. ->toArray();
  24. $goods_temp_all = [];
  25. foreach ($order_children as $key => &$item) {
  26. $item = (array)$item;
  27. // 订单商品
  28. $order_goods = OrderGoods::query()->select(['name', 'number', 'spec', 'good_unit'])
  29. ->where(['order_id' => $item['id']])
  30. ->get()
  31. ->toArray();
  32. $goods_temp = [];
  33. foreach ($order_goods as $k => &$goods) {
  34. array_push($goods_temp, $goods['name']."*".$goods['number']."/".($goods['spec']?:$goods['good_unit']));
  35. array_push($goods_temp_all, $goods['name']."*".$goods['number']."/".($goods['spec']?:$goods['good_unit']));
  36. }
  37. // 商户/门店的openid
  38. $store = Store::query()->select(['id', 'name', 'user_id'])
  39. ->where(['id' => $item['store_id']])
  40. ->first()->toArray();
  41. $store['openid'] = Users::query()
  42. ->where(['id' => $store['user_id']])
  43. ->value('openid');
  44. // 模板数据
  45. $data_store = [
  46. 'first' => ['您有新的外卖订单!订单编号:'.$item['order_num'], '#ff0000'],
  47. 'keyword' => [
  48. ["您的外卖订单详情:\r\n".implode(";\r\n",$goods_temp), '#ff0000'],
  49. $item['money'],
  50. $payTypes[$order['pay_type']],
  51. $item['time']?:'',
  52. $address_store,
  53. ],
  54. 'remark' => [$order['note'], '#4e6ef2']
  55. ];
  56. $ret_store = $this->sendTempMsg($store['openid'], '-M7DG_ACwJxqdAvyvJuAnPpx4xaLf3VkkN0fckno71c',$data_store);
  57. }
  58. // 模板数据发送消息给用户
  59. $data_user = [
  60. 'first' => '您好,下单成功!订单编号:'.$order['order_num'],
  61. 'keyword' => [
  62. implode(";\r\n", $goods_temp_all),
  63. $order['money'],
  64. $payTypes[$order['pay_type']],
  65. date('Y-m-d H:i:s', $order['time_add']),
  66. $address,
  67. ],
  68. 'remark' => '感谢您的光临,欢迎下次再来!'
  69. ];
  70. // 获取用户openid,发送给用户
  71. $user_openid = Users::query()->where(['id' => $order['user_id']])->value('openid');
  72. $ret_user = $this->sendTempMsg($user_openid,'-M7DG_ACwJxqdAvyvJuAnPpx4xaLf3VkkN0fckno71c', $data_user);
  73. }
  74. public function sendTempMsg($openid, $template_id, $data, $redirect_url = '', $applet_config = ['appid' => '', 'pagepath' => ''])
  75. {
  76. // 先拼个基础的
  77. $template = [
  78. 'touser' => $openid,
  79. 'mp_template_msg' => [
  80. 'appid' => env('OFFICIAL_APP_ID'),
  81. 'template_id' => $template_id,
  82. 'url' => $redirect_url,
  83. ]
  84. ];
  85. // 看看有没有小程序跳转的要求
  86. $template['mp_template_msg']['miniprogram'] = $applet_config;
  87. // 重点来了,拼接关键数据data
  88. if (!is_array($data)) { # 数组都不是,请回去
  89. return false;
  90. }
  91. if (is_array($data['first'])) {
  92. $template['mp_template_msg']['data']['first']['value'] = $data['first'][0] ?? '';
  93. $template['mp_template_msg']['data']['first']['color'] = $data['first'][1] ?? '';
  94. } else {
  95. $template['mp_template_msg']['data']['first']['value'] = $data['first'];
  96. }
  97. if (isset($data['keyword'])&&is_array($data['keyword'])) {
  98. foreach ($data['keyword'] as $key => &$keyword) {
  99. $index = $key+1;
  100. if (is_array($keyword)) {
  101. $template['mp_template_msg']['data']['keyword'.$index]['value'] = $keyword[0] ?? '';
  102. $template['mp_template_msg']['data']['keyword'.$index]['color'] = $keyword[1] ?? '';
  103. } else {
  104. $template['mp_template_msg']['data']['keyword'.$index]['value'] = $keyword;
  105. }
  106. }
  107. }
  108. if (is_array($data['remark'])) {
  109. $template['mp_template_msg']['data']['remark']['value'] = $data['remark'][0] ?? '';
  110. $template['mp_template_msg']['data']['remark']['color'] = $data['remark'][1] ?? '';
  111. } else {
  112. $template['mp_template_msg']['data']['remark']['value'] = $data['remark'];
  113. }
  114. $app = Factory::miniProgram(config('wxtempmsg'));
  115. $app['guzzle_handler'] = CoroutineHandler::class;
  116. $app->uniform_message->send($template);
  117. }
  118. }