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.

335 lines
12 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\Payment;
  4. use App\Model\v3\Order;
  5. use App\Model\v3\OrderGoods;
  6. use App\Model\v3\OrderMain;
  7. use App\Model\v3\Store;
  8. use App\Model\v3\SubscribeMessageTemplate;
  9. use App\Model\v3\User;
  10. use App\Service\v3\Interfaces\MiniprogramServiceInterface;
  11. use EasyWeChat\Factory;
  12. use Hyperf\Guzzle\CoroutineHandler;
  13. class MiniprogramService implements MiniprogramServiceInterface
  14. {
  15. /**
  16. * @inheritDoc
  17. */
  18. public function sendTemMsgForOnlineOrder($globalOrderId)
  19. {
  20. // 查询订单信息
  21. $order = OrderMain::query()->where(['global_order_id' => $globalOrderId])->first()->toArray();
  22. $payTypes = ['1' => '微信支付', '2' => '余额支付', '3' => '积分支付', '4' => '货到付款'];
  23. $address_store = $order['address'] . ';' .$order['name']. ';'. substr_replace($order['tel'],'****',3,4);
  24. $address = $order['address'] . ';' .$order['name']. ';'. $order['tel'];
  25. // 查询子订单,用于发消息给商户
  26. $order_children = Order::query()
  27. ->with('orderMain')
  28. ->where(['order_main_id' => $globalOrderId])
  29. ->get();
  30. $goods_temp_all = [];
  31. foreach ($order_children as $key => &$item) {
  32. // 订单商品
  33. $order_goods = OrderGoods::query()
  34. ->where(['order_id' => $item->id])
  35. ->get()
  36. ->toArray();
  37. $goods_temp = [];
  38. foreach ($order_goods as $k => &$goods) {
  39. array_push($goods_temp, $goods['name']."*".$goods['number']."/".($goods['goods_unit']));
  40. array_push($goods_temp_all, $goods['name']."*".$goods['number']."/".($goods['goods_unit']));
  41. }
  42. // 商户/门店的openid
  43. $store = Store::query()->select(['id', 'name', 'user_id'])
  44. ->where(['id' => $item->store_id])
  45. ->first()->toArray();
  46. $store['openid'] = User::query()
  47. ->where(['id' => $store['user_id']])
  48. ->value('openid');
  49. // 模板数据
  50. $data_store = [
  51. 'first' => ['您有新的外卖订单!订单编号:'.$item->order_num, '#ff0000'],
  52. 'keyword' => [
  53. ["您的外卖订单详情:\r\n".implode(";\r\n",$goods_temp), '#ff0000'],
  54. $item->money,
  55. $payTypes[$order['pay_type']],
  56. $item->created_at_text??'',
  57. $address_store,
  58. ],
  59. 'remark' => [$item->note, '#4e6ef2']
  60. ];
  61. $ret_store = $this->sendTempMsg($store['openid'], '-M7DG_ACwJxqdAvyvJuAnPpx4xaLf3VkkN0fckno71c',$data_store);
  62. // 小程序订阅消息发给商户
  63. // 支付金额:{{amount1.DATA}}\n订单编号:{{character_string2.DATA}}\n支付时间:{{date3.DATA}}\n商品详情:{{thing4.DATA}}\n备注:{{thing7.DATA}}\n
  64. $tmplId = 'UfCON4Dj_7aH_Q5k_HvDgzUb3gu67TokPITsJuvCMxc';
  65. $page = 'pages/shopOrders/shopOrders?status=paid&store_id='.$store['id'];
  66. $this->sendSubscribeMessage(
  67. $store['openid'],
  68. $tmplId,
  69. [
  70. 'amount1' => $item->money,
  71. 'character_string2' => $item->order_num,
  72. 'date3' => $item->created_at_text??'',
  73. 'thing4' => implode(";", $goods_temp_all),
  74. 'thing7' => $item->note,
  75. ],
  76. $page
  77. );
  78. }
  79. // 模板数据发送消息给用户
  80. $data_user = [
  81. 'first' => '您好,下单成功!订单编号:'.$order['global_order_id'],
  82. 'keyword' => [
  83. implode(";\r\n", $goods_temp_all),
  84. $order['money'],
  85. $payTypes[$order['pay_type']],
  86. $order['created_at_text'],
  87. $address,
  88. ],
  89. 'remark' => '感谢您的光临,欢迎下次再来!'
  90. ];
  91. // 获取用户openid,发送给用户
  92. $user_openid = User::query()->where(['id' => $order['user_id']])->value('openid');
  93. $ret_user = $this->sendTempMsg($user_openid,'-M7DG_ACwJxqdAvyvJuAnPpx4xaLf3VkkN0fckno71c', $data_user);
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function sendTemMsgForOfflineOrder($globalOrderId)
  99. {
  100. // 主订单信息
  101. $orderMain = OrderMain::query()->where(['global_order_id' => $globalOrderId])->first();
  102. // 查询子订单,用于发消息给商户
  103. $order_children = Order::query()
  104. ->where(['order_main_id' => $globalOrderId])
  105. ->get()
  106. ->toArray();
  107. foreach ($order_children as $key => &$item) {
  108. // 商户/门店的openid
  109. $store = Store::query()
  110. ->where(['id' => $item['store_id']])
  111. ->first()->toArray();
  112. $store['openid'] = User::query()
  113. ->where(['id' => $store['user_id']])
  114. ->value('openid');
  115. // 模板数据
  116. $data_store = [
  117. 'first' => '您有新订单收入!订单编号:'.$item['order_num'],
  118. 'keyword' => [
  119. $store['name']?:'',
  120. $item['created_at_text']??'',
  121. '暂无',
  122. $item['money']
  123. ],
  124. 'remark' => '感谢您的使用!'
  125. ];
  126. $ret_store = $this->sendTempMsg($store['openid'], 'lxVbC6PVpKbiO44bYqLmacl-BaME70D47Q0jn2Link0',$data_store);
  127. // 小程序订阅消息发给商户
  128. // 付款金额:{{amount1.DATA}}\n商户名称:{{thing2.DATA}}\n支付方式:{{thing3.DATA}}\n交易单号:{{character_string4.DATA}}\n交易时间:{{time5.DATA}}\n
  129. $tmplId = 'PCRNmKGb7t98xsz_GPux3rhXjsu68TPI3nXR7749dC0';
  130. $page = 'pages/faceOrderList/faceOrderList?store_id='.$store['id'];
  131. $this->sendSubscribeMessage(
  132. $store['openid'],
  133. $tmplId,
  134. [
  135. 'amount1' => $item['money'],
  136. 'thing2' => $store['name'],
  137. 'thing3' => Payment::getMessage($orderMain->pay_type),
  138. 'character_string4' => $item['order_num'],
  139. 'time5' => $item['created_at_text']??'',
  140. ],
  141. $page
  142. );
  143. }
  144. }
  145. /**
  146. * @inheritDoc
  147. */
  148. public function sendTemMsgForAward($money, $note, $openid, $time)
  149. {
  150. // 模板数据发送消息给用户
  151. $data_user = [
  152. 'first' => '恭喜!您有一笔新的奖励收入!',
  153. 'keyword' => [
  154. $money,
  155. $note,
  156. $time
  157. ],
  158. 'remark' => '感谢您的使用!'
  159. ];
  160. // 获取用户openid,发送给用户
  161. $ret_user = $this->sendTempMsg($openid,'ypZ7xdHUjWrRG8P-MD42dhpp6kUlh4Unoh7eTSrLZEg', $data_user);
  162. }
  163. /**
  164. * @inheritDoc
  165. */
  166. public function subscribeMsgForSingleRefund($orderId, $refundStoreAmount, $orderGoodsId = '')
  167. {
  168. $order = Order::query()->where(['id' => $orderId])->first();
  169. $store = Store::query()->where(['id' => $order->store_id])->first();
  170. $storeOpenid = User::query()->where(['id' => $store->user_id])->value('openid');
  171. $goodsInfo = [];
  172. if ($orderGoodsId) {
  173. $goodsInfo = OrderGoods::query()->where(['id' => $orderGoodsId])->pluck('name')->toArray();
  174. } else {
  175. $goodsInfo = OrderGoods::query()->where(['order_id' => $orderId])->pluck('name')->toArray();
  176. }
  177. // 小程序订阅消息发给商户
  178. // 订单编号:{{character_string7.DATA}}\n商品名称:{{thing10.DATA}}\n退款金额:{{amount3.DATA}}\n处理时间:{{date4.DATA}}\n退款说明:{{thing5.DATA}}\n
  179. $tmplId = 'OaWeg0q40NVW2WeO5BY1TFdWi4GsgIY1rFBwo2wDa0g';
  180. $page = 'pages/shopOrders/shopOrders?status=refund&store_id='.$store->id;
  181. $this->sendSubscribeMessage(
  182. $storeOpenid,
  183. $tmplId,
  184. [
  185. 'character_string7' => $order->order_num,
  186. 'thing10' => implode(';', $goodsInfo),
  187. 'amount3' => $refundStoreAmount,
  188. 'date4' => $order->updated_at_text ?? '',
  189. 'thing5' => $order->refund_note,
  190. ],
  191. $page
  192. );
  193. }
  194. /**
  195. * @inheritDoc
  196. */
  197. public function sendTempMsg($openid, $template_id, $data, $redirect_url = '', $applet_config = ['appid' => '', 'pagepath' => ''])
  198. {
  199. if (empty($openid) || empty($template_id) || empty($data)) {
  200. return ;
  201. }
  202. // 先拼个基础的
  203. $template = [
  204. 'touser' => $openid,
  205. 'mp_template_msg' => [
  206. 'appid' => config('wechat.official.app_id'),
  207. 'template_id' => $template_id,
  208. 'url' => $redirect_url,
  209. ]
  210. ];
  211. // 看看有没有小程序跳转的要求
  212. $template['mp_template_msg']['miniprogram'] = $applet_config;
  213. // 重点来了,拼接关键数据data
  214. if (!is_array($data)) { # 数组都不是,请回去
  215. return false;
  216. }
  217. if (is_array($data['first'])) {
  218. $template['mp_template_msg']['data']['first']['value'] = $data['first'][0] ?? '';
  219. $template['mp_template_msg']['data']['first']['color'] = $data['first'][1] ?? '';
  220. } else {
  221. $template['mp_template_msg']['data']['first']['value'] = $data['first'];
  222. }
  223. if (isset($data['keyword'])&&is_array($data['keyword'])) {
  224. foreach ($data['keyword'] as $key => &$keyword) {
  225. $index = $key+1;
  226. if (is_array($keyword)) {
  227. $template['mp_template_msg']['data']['keyword'.$index]['value'] = $keyword[0] ?? '';
  228. $template['mp_template_msg']['data']['keyword'.$index]['color'] = $keyword[1] ?? '';
  229. } else {
  230. $template['mp_template_msg']['data']['keyword'.$index]['value'] = $keyword;
  231. }
  232. }
  233. }
  234. if (is_array($data['remark'])) {
  235. $template['mp_template_msg']['data']['remark']['value'] = $data['remark'][0] ?? '';
  236. $template['mp_template_msg']['data']['remark']['color'] = $data['remark'][1] ?? '';
  237. } else {
  238. $template['mp_template_msg']['data']['remark']['value'] = $data['remark'];
  239. }
  240. $app = Factory::miniProgram(config('wechat.applet'));
  241. $app['guzzle_handler'] = CoroutineHandler::class;
  242. $app->uniform_message->send($template);
  243. }
  244. public function sendSubscribeMessage($openid, $template_id, $data, $page = '')
  245. {
  246. if (empty($openid) || empty($template_id) || empty($data)) {
  247. return ;
  248. }
  249. $app = Factory::miniProgram(config('wechat.applet'));
  250. $app['guzzle_handler'] = CoroutineHandler::class;
  251. $msgData = [
  252. 'touser' => $openid,
  253. 'template_id' => $template_id,
  254. 'page' => $page,
  255. 'miniprogram_state' => env('APP_ENV')=='prod' ? 'formal' : 'developer',
  256. 'data' => $data
  257. ];
  258. return $app->subscribe_message->send($msgData);
  259. }
  260. public function getSubscribeMessageTempList($forUserType)
  261. {
  262. if ($forUserType == '') {
  263. $app = Factory::miniProgram(config('wechat.applet'));
  264. $app['guzzle_handler'] = CoroutineHandler::class;
  265. $result = $app->subscribe_message->getTemplates()['data'];
  266. $res = [];
  267. foreach ($result as $key => &$tmpl) {
  268. $res[] = [
  269. 'template_id' => $tmpl['priTmplId'] ?? '',
  270. 'title' => $tmpl['title'] ?? '',
  271. ];
  272. }
  273. return $res;
  274. } else {
  275. $builder = SubscribeMessageTemplate::query()->select('pri_tmpl_id as template_id', 'title');
  276. if ($forUserType != 'all') {
  277. $builder = $builder->where(['for_user_type' => $forUserType]);
  278. }
  279. return $builder->get()->toArray();
  280. }
  281. }
  282. }