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

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