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.

244 lines
8.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Model\v3\Order;
  4. use App\Model\v3\OrderGoods;
  5. use App\Model\v3\OrderMain;
  6. use App\Model\v3\Store;
  7. use App\Model\v3\User;
  8. use App\Service\v3\Interfaces\MiniprogramServiceInterface;
  9. use EasyWeChat\Factory;
  10. use Hyperf\Guzzle\CoroutineHandler;
  11. class MiniprogramService implements MiniprogramServiceInterface
  12. {
  13. /**
  14. * @inheritDoc
  15. */
  16. public function sendTemMsgForOnlineOrder($globalOrderId)
  17. {
  18. // 查询订单信息
  19. $order = OrderMain::query()->where(['global_order_id' => $globalOrderId])->first()->toArray();
  20. $payTypes = ['1' => '微信支付', '2' => '余额支付', '3' => '积分支付', '4' => '货到付款'];
  21. $address_store = $order['address'] . ';' .$order['name']. ';'. substr_replace($order['tel'],'****',3,4);
  22. $address = $order['address'] . ';' .$order['name']. ';'. $order['tel'];
  23. // 查询子订单,用于发消息给商户
  24. $order_children = Order::query()
  25. ->with('orderMain')
  26. ->where(['order_main_id' => $globalOrderId])
  27. ->get();
  28. $goods_temp_all = [];
  29. foreach ($order_children as $key => &$item) {
  30. // 订单商品
  31. $order_goods = OrderGoods::query()
  32. ->where(['order_id' => $item->id])
  33. ->get()
  34. ->toArray();
  35. $goods_temp = [];
  36. foreach ($order_goods as $k => &$goods) {
  37. array_push($goods_temp, $goods['name']."*".$goods['number']."/".($goods['goods_unit']));
  38. array_push($goods_temp_all, $goods['name']."*".$goods['number']."/".($goods['goods_unit']));
  39. }
  40. // 商户/门店的openid
  41. $store = Store::query()->select(['id', 'name', 'user_id'])
  42. ->where(['id' => $item->store_id])
  43. ->first()->toArray();
  44. $store['openid'] = User::query()
  45. ->where(['id' => $store['user_id']])
  46. ->value('openid');
  47. // 模板数据
  48. $data_store = [
  49. 'first' => ['您有新的外卖订单!订单编号:'.$item->order_num, '#ff0000'],
  50. 'keyword' => [
  51. ["您的外卖订单详情:\r\n".implode(";\r\n",$goods_temp), '#ff0000'],
  52. $item->money,
  53. $payTypes[$order['pay_type']],
  54. $item->created_at_text??'',
  55. $address_store,
  56. ],
  57. 'remark' => [$item->note, '#4e6ef2']
  58. ];
  59. $ret_store = $this->sendTempMsg($store['openid'], '-M7DG_ACwJxqdAvyvJuAnPpx4xaLf3VkkN0fckno71c',$data_store);
  60. }
  61. // 模板数据发送消息给用户
  62. $data_user = [
  63. 'first' => '您好,下单成功!订单编号:'.$order['global_order_id'],
  64. 'keyword' => [
  65. implode(";\r\n", $goods_temp_all),
  66. $order['money'],
  67. $payTypes[$order['pay_type']],
  68. $order['created_at_text'],
  69. $address,
  70. ],
  71. 'remark' => '感谢您的光临,欢迎下次再来!'
  72. ];
  73. // 获取用户openid,发送给用户
  74. $user_openid = User::query()->where(['id' => $order['user_id']])->value('openid');
  75. $ret_user = $this->sendTempMsg($user_openid,'-M7DG_ACwJxqdAvyvJuAnPpx4xaLf3VkkN0fckno71c', $data_user);
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function sendTemMsgForOfflineOrder($globalOrderId)
  81. {
  82. // 查询子订单,用于发消息给商户
  83. $order_children = Order::query()
  84. ->where(['order_main_id' => $globalOrderId])
  85. ->get()
  86. ->toArray();
  87. foreach ($order_children as $key => &$item) {
  88. // 商户/门店的openid
  89. $store = Store::query()
  90. ->where(['id' => $item['store_id']])
  91. ->first()->toArray();
  92. $store['openid'] = User::query()
  93. ->where(['id' => $store['user_id']])
  94. ->value('openid');
  95. // 模板数据
  96. $data_store = [
  97. 'first' => '您有新订单收入!订单编号:'.$item['order_num'],
  98. 'keyword' => [
  99. $store['name']?:'',
  100. $item['created_at_text']??'',
  101. '暂无',
  102. $item['money']
  103. ],
  104. 'remark' => '感谢您的使用!'
  105. ];
  106. // $ret_store = $this->sendTempMsg($store['openid'], 'lxVbC6PVpKbiO44bYqLmacl-BaME70D47Q0jn2Link0',$data_store);
  107. $ret_store = $this->sendTempMsg(
  108. $store['openid'],
  109. 'b9SR_3j6GhJvVjuIoYlil4-m09plcjNnI5lls1wHfpo',
  110. $data_store,
  111. true,
  112. '/pages/orderDetail/orderDetail?global_order_id='.$globalOrderId,
  113. '1461',
  114. $item['money']
  115. );
  116. }
  117. }
  118. /**
  119. * @inheritDoc
  120. */
  121. public function sendTemMsgForAward($money, $note, $openid, $time)
  122. {
  123. // 模板数据发送消息给用户
  124. $data_user = [
  125. 'first' => '恭喜!您有一笔新的奖励收入!',
  126. 'keyword' => [
  127. $money,
  128. $note,
  129. $time
  130. ],
  131. 'remark' => '感谢您的使用!'
  132. ];
  133. // 获取用户openid,发送给用户
  134. $ret_user = $this->sendTempMsg($openid,'ypZ7xdHUjWrRG8P-MD42dhpp6kUlh4Unoh7eTSrLZEg', $data_user);
  135. }
  136. /**
  137. * @inheritDoc
  138. */
  139. public function sendTempMsg($openid, $template_id, $data, $isApplet = false, $redirect_url = '', $appletFormId = '', $appletEmKeyword = '', $applet_config = ['appid' => '', 'pagepath' => ''])
  140. {
  141. if (empty($openid) || empty($template_id) || empty($data)) {
  142. return ;
  143. }
  144. // 先拼个基础的
  145. $template = [
  146. 'touser' => $openid,
  147. 'mp_template_msg' => [
  148. 'appid' => config('wechat.official.app_id'),
  149. 'template_id' => $template_id,
  150. 'url' => $redirect_url,
  151. ]
  152. ];
  153. if ($isApplet) {
  154. $template['weapp_template_msg'] = [
  155. 'template_id' => $template_id,
  156. 'page' => $redirect_url,
  157. 'form_id' => $appletFormId,
  158. 'emphasis_keyword' => $appletEmKeyword,
  159. ];
  160. }
  161. // 看看有没有小程序跳转的要求
  162. $template['mp_template_msg']['miniprogram'] = $applet_config;
  163. // 重点来了,拼接关键数据data
  164. if (!is_array($data)) { # 数组都不是,请回去
  165. return false;
  166. }
  167. if ( !$isApplet ) {
  168. if (is_array($data['first'])) {
  169. $template['mp_template_msg']['data']['first']['value'] = $data['first'][0] ?? '';
  170. $template['mp_template_msg']['data']['first']['color'] = $data['first'][1] ?? '';
  171. } else {
  172. $template['mp_template_msg']['data']['first']['value'] = $data['first'];
  173. }
  174. if (isset($data['keyword'])&&is_array($data['keyword'])) {
  175. foreach ($data['keyword'] as $key => &$keyword) {
  176. $index = $key+1;
  177. if (is_array($keyword)) {
  178. $template['mp_template_msg']['data']['keyword'.$index]['value'] = $keyword[0] ?? '';
  179. $template['mp_template_msg']['data']['keyword'.$index]['color'] = $keyword[1] ?? '';
  180. } else {
  181. $template['mp_template_msg']['data']['keyword'.$index]['value'] = $keyword;
  182. }
  183. }
  184. }
  185. if (is_array($data['remark'])) {
  186. $template['mp_template_msg']['data']['remark']['value'] = $data['remark'][0] ?? '';
  187. $template['mp_template_msg']['data']['remark']['color'] = $data['remark'][1] ?? '';
  188. } else {
  189. $template['mp_template_msg']['data']['remark']['value'] = $data['remark'];
  190. }
  191. } else {
  192. if (isset($data['keyword'])&&is_array($data['keyword'])) {
  193. foreach ($data['keyword'] as $key => &$keyword) {
  194. $index = $key+1;
  195. if (is_array($keyword)) {
  196. $template['weapp_template_msg']['data']['keyword'.$index]['value'] = $keyword[0] ?? '';
  197. } else {
  198. $template['weapp_template_msg']['data']['keyword'.$index]['value'] = $keyword;
  199. }
  200. }
  201. }
  202. }
  203. $app = Factory::miniProgram(config('wechat.applet'));
  204. $app['guzzle_handler'] = CoroutineHandler::class;
  205. $app->uniform_message->send($template);
  206. }
  207. }