WechatTemplateMessageService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\common\repositories\store\order\StoreGroupOrderRepository;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use app\common\repositories\store\order\StoreOrderStatusRepository;
  15. use app\common\repositories\store\order\StoreRefundOrderRepository;
  16. use app\common\repositories\store\product\ProductGroupBuyingRepository;
  17. use app\common\repositories\store\product\ProductGroupUserRepository;
  18. use app\common\repositories\store\product\ProductRepository;
  19. use app\common\repositories\store\product\ProductTakeRepository;
  20. use app\common\repositories\store\service\StoreServiceRepository;
  21. use app\common\repositories\system\merchant\MerchantRepository;
  22. use app\common\repositories\system\notice\SystemNoticeConfigRepository;
  23. use app\common\repositories\user\UserBillRepository;
  24. use app\common\repositories\user\UserExtractRepository;
  25. use app\common\repositories\user\UserRechargeRepository;
  26. use app\common\repositories\wechat\WechatUserRepository;
  27. use crmeb\listens\pay\UserRechargeSuccessListen;
  28. use crmeb\services\template\Template;
  29. use app\common\repositories\user\UserRepository;
  30. use think\facade\Log;
  31. use think\facade\Route;
  32. class WechatTemplateMessageService
  33. {
  34. /**
  35. * 微信模板消息
  36. * @param array $data
  37. * @param string|null $link
  38. * @param string|null $color
  39. * @return bool
  40. * @author Qinii
  41. * @day 2020-06-29
  42. */
  43. public function sendTemplate(array $data)
  44. {
  45. event('wechat.template.before',compact('data'));
  46. $res = $this->templateMessage($data);
  47. if(!$res || !is_array($res))
  48. return true;
  49. foreach($res as $item){
  50. if(is_array($item['uid'])){
  51. foreach ($item['uid'] as $value){
  52. $openid = $this->getUserOpenID($value['uid']);
  53. if (!$openid) continue;
  54. $this->send($openid,$item['tempCode'],$item['data'],'wechat',$item['link'],$item['color']);
  55. }
  56. } else {
  57. $openid = $this->getUserOpenID($item['uid']);
  58. if (!$openid) continue;
  59. $this->send($openid,$item['tempCode'],$item['data'],'wechat',$item['link'],$item['color']);
  60. }
  61. }
  62. event('wechat.template',compact('res'));
  63. }
  64. /**
  65. * 公众号模板整理
  66. * @param string $tempCode
  67. * @param $id
  68. * @return array|bool
  69. * @author Qinii
  70. * @day 2020-07-01
  71. */
  72. public function templateMessage($params)
  73. {
  74. $data = [];
  75. $id = $params['id'];
  76. $tempId = $params['tempId'];
  77. $params = $params['params'] ?? [];
  78. $bill_make = app()->make(UserBillRepository::class);
  79. $order_make = app()->make(StoreOrderRepository::class);
  80. $refund_make = app()->make(StoreRefundOrderRepository::class);
  81. $stie_url = rtrim(systemConfig('site_url'), '/');
  82. switch ($tempId)
  83. {
  84. //订单生成通知 2.1
  85. case 'ORDER_CREATE':
  86. /*
  87. 订单号:{{character_string1.DATA}}
  88. 商品名称:{{thing2.DATA}}
  89. 订单金额:{{amount3.DATA}}
  90. 联系方式:{{phone_number5.DATA}}
  91. 下单时间:{{time9.DATA}}
  92. */
  93. $res = $order_make->selectWhere(['group_order_id' => $id]);
  94. if(!$res) return false;
  95. foreach ($res as $item){
  96. if($item->activity_type == 20) return false;
  97. $order = $order_make->getWith($item['order_id'],'orderProduct');
  98. $data[] = [
  99. 'tempCode' => 'ORDER_CREATE',
  100. 'uid' => app()->make(StoreServiceRepository::class)->getNoticeServiceInfo($item->mer_id),
  101. 'data' => [
  102. 'character_string1' => $item->order_sn,
  103. 'thing2' => '「' . mb_substr($order['orderProduct'][0]['cart_info']['product']['store_name'],0,14) . '」等',
  104. 'amount3' => $item->pay_price,
  105. 'phone_number5' => $order['user_phone'],
  106. 'time9' => $item->create_time,
  107. ],
  108. 'link' => $stie_url. '/pages/admin/orderList/index?types=1&merId=' . $item->mer_id,
  109. 'color' => null
  110. ];
  111. }
  112. break;
  113. //支付成功 2.1
  114. case 'ORDER_PAY_SUCCESS':
  115. /*
  116. 订单号: {{character_string2.DATA}}
  117. 商品名称: {{thing3.DATA}}
  118. 支付金额: {{amount5.DATA}}
  119. 下单时间: {{time4.DATA}}
  120. */
  121. $group_order = app()->make(StoreGroupOrderRepository::class)->get($id);
  122. if(!$group_order) return false;
  123. $link = $stie_url . '/pages/users/order_list/index?status=1';
  124. if ($group_order->activity_type == 20) $link = $stie_url.'/pages/points_mall/exchange_record?delta=0';
  125. $data[] = [
  126. 'tempCode' => 'ORDER_PAY_SUCCESS',
  127. 'uid' => $group_order->uid,
  128. 'data' => [
  129. 'character_string2' => $group_order->group_order_sn,
  130. 'thing3' => mb_substr($group_order->orderList[0]->orderProduct[0]['cart_info']['product']['store_name'],0,15),
  131. 'amount5' => $group_order->pay_price,
  132. 'time4' => $group_order->pay_time,
  133. ],
  134. 'link' => $link,
  135. 'color' => null
  136. ];
  137. break;
  138. //管理员支付成功提醒 2.1
  139. case 'ADMIN_PAY_SUCCESS_CODE':
  140. /*
  141. 订单号: {{character_string2.DATA}}
  142. 商品名称: {{thing3.DATA}}
  143. 支付金额: {{amount5.DATA}}
  144. 下单时间: {{time4.DATA}}
  145. */
  146. $res = $order_make->selectWhere(['group_order_id' => $id]);
  147. if (!$res) return false;
  148. foreach ($res as $item) {
  149. $link = $stie_url . '/pages/admin/orderList/index?types=2&merId=' . $item->mer_id;
  150. if ($item->activity_type == 20) $link = '';
  151. $data[] = [
  152. 'tempCode' => 'ADMIN_PAY_SUCCESS_CODE',
  153. 'uid' => app()->make(StoreServiceRepository::class)->getNoticeServiceInfo($item->mer_id),
  154. 'data' => [
  155. 'character_string2' => $item->order_sn,
  156. 'thing3' => mb_substr($item->orderProduct[0]->cart_info['product']['store_name'],0,15),
  157. 'amount5' => $item->pay_price,
  158. 'time4' => $item->pay_time,
  159. ],
  160. 'link' => $link,
  161. 'color' => null
  162. ];
  163. }
  164. break;
  165. //订单发货提醒 2.1
  166. case 'DELIVER_GOODS_CODE':
  167. /*
  168. 订单编号:{{character_string2.DATA}}
  169. 商品名称:{{thing4.DATA}}
  170. 快递公司:{{thing13.DATA}}
  171. 快递单号:{{character_string14.DATA}}
  172. 发货时间:{{time12.DATA}}
  173. */
  174. $res = $order_make->get($id);
  175. if(!$res) return false;
  176. $link = '/pages/order_details/index?order_id=';
  177. if ($res->activity_type == 20) $link = '/pages/points_mall/integral_order_details?order_id=';
  178. $data[] = [
  179. 'tempCode' => 'DELIVER_GOODS_CODE',
  180. 'uid' => $res->uid ,
  181. 'data' => [
  182. 'character_string2' => $res['order_sn'],
  183. 'thing4' => mb_substr($res->orderProduct[0]->cart_info['product']['store_name'],0,15),
  184. 'thing13' => $res['delivery_name'],
  185. 'character_string14' => $res['delivery_id'],
  186. 'time12' => date('Y-m-d H:i:s',time()),
  187. ],
  188. 'link' => $stie_url .$link . $id,
  189. 'color' => null
  190. ];
  191. break;
  192. //订单配送提醒 2.1
  193. case 'ORDER_DELIVER_SUCCESS':
  194. /*
  195. 订单号:{{character_string1.DATA}}
  196. 商品名称:{{thing5.DATA}}
  197. 配送员:{{thing9.DATA}}
  198. 配送员电话:{{phone_number10.DATA}}
  199. 配送时间:{{time8.DATA}}
  200. */
  201. $res = $order_make->get($id);
  202. $link = '/pages/order_details/index?order_id=';
  203. if ($res->activity_type == 20) $link = '/pages/points_mall/integral_order_details?order_id=';
  204. $data[] = [
  205. 'tempCode' => 'ORDER_DELIVER_SUCCESS',
  206. 'uid' => $res->uid ,
  207. 'data' => [
  208. 'character_string1' => $res['order_sn'],
  209. 'thing5' => mb_substr($res->orderProduct[0]->cart_info['product']['store_name'],0,15),
  210. 'thing9' => $res['delivery_name'],
  211. 'phone_number10' => $res['delivery_id'],
  212. 'time8' => date('Y-m-d H:i:s',time()),
  213. ],
  214. 'link' => $stie_url .$link .$id,
  215. 'color' => null
  216. ];
  217. break;
  218. //确认收货提醒 2.1
  219. case 'ORDER_TAKE_SUCCESS':
  220. /*
  221. 订单编号:{{character_string2.DATA}}
  222. 商品名称:{{thing4.DATA}}
  223. 订单金额:{{amount9.DATA}}
  224. 商品数量:{{number5.DATA}}
  225. 签收时间:{{character_string7.DATA}}
  226. */
  227. $res = $order_make->get($id);
  228. if(!$res) return false;
  229. $link = '/pages/order_details/index?order_id='.$id;
  230. if ($res->activity_type == 20) $link = '';
  231. $data[] = [
  232. 'tempCode' => 'ORDER_TAKE_SUCCESS',
  233. 'uid' => $res->uid,
  234. 'data' => [
  235. 'character_string2' => $res['order_sn'],
  236. 'thing4' => mb_substr($res->orderProduct[0]->cart_info['product']['store_name'],0,15).'...等',
  237. 'amount9' => $res['pay_price'],
  238. 'number5' => $res['total_num'],
  239. 'character_string7' => date('Y-m-d H:i:s',time()),
  240. ],
  241. 'link' => $stie_url .$link ,
  242. 'color' => null
  243. ];
  244. break;
  245. case 'ADMIN_TAKE_DELIVERY_CODE':
  246. /*
  247. 订单编号:{{character_string2.DATA}}
  248. 商品名称:{{thing4.DATA}}
  249. 订单金额:{{amount9.DATA}}
  250. 商品数量:{{number5.DATA}}
  251. 签收时间:{{character_string7.DATA}}
  252. */
  253. $res = $order_make->get($id);
  254. if(!$res) return false;
  255. $link = '/pages/order_details/index?order_id=';
  256. if ($res->activity_type == 20) $link = '/pages/points_mall/integral_order_details?order_id=';
  257. $data[] = [
  258. 'tempCode' => 'ORDER_TAKE_SUCCESS',
  259. 'uid' => app()->make(StoreServiceRepository::class)->getNoticeServiceInfo($res->mer_id),
  260. 'data' => [
  261. 'character_string2' => $res['order_sn'],
  262. 'thing4' => mb_substr($res['orderProduct'][0]['product']['store_name'],0,15). '】等',
  263. 'amount9' =>$res['pay_price'],
  264. 'number5' =>$res['total_num'],
  265. 'character_string7' => date('Y-m-d H:i:s',time()),
  266. ],
  267. 'link' => $stie_url .$link .$id,
  268. 'color' => null
  269. ];
  270. break;
  271. //退款申请通知 2.1
  272. case 'ADMIN_RETURN_GOODS_CODE':
  273. /*
  274. 订单编号:{{character_string10.DATA}}
  275. 商品名称:{{thing8.DATA}}
  276. 退款金额:{{amount2.DATA}}
  277. 退款时间:{{time3.DATA}}
  278. */
  279. $res = $refund_make->get($id);
  280. if(!$res) return false;
  281. $data[] = [
  282. 'tempCode' => 'ADMIN_RETURN_GOODS_CODE',
  283. 'uid' => app()->make(StoreServiceRepository::class)->getNoticeServiceInfo($res->mer_id),
  284. 'data' => [
  285. 'character_string10' => $res['refund_order_sn'],
  286. 'thing8' => mb_substr($res['refundProduct'][0]['product']['cart_info']['product']['store_name'],0,15).'等',
  287. 'amount2' => $res['refund_price'],
  288. 'time3' => $res['create_time']
  289. ],
  290. 'link' => null,
  291. 'color' => null
  292. ];
  293. break;
  294. //商户同意退款 2.1
  295. case 'REFUND_SUCCESS_CODE':
  296. /*
  297. 订单编号:{{character_string8.DATA}}
  298. 申请类型:{{phrase3.DATA}}
  299. 处理结果:{{thing6.DATA}}
  300. 退款商品:{{thing4.DATA}}
  301. 退款金额:{{amount5.DATA}}
  302. */
  303. $res = $refund_make->get($id);
  304. if(!$res || $res['status'] != 1) return false;
  305. $data[] = [
  306. 'tempCode' => 'REFUND_SUCCESS_CODE',
  307. 'uid' => $res->uid,
  308. 'data' => [
  309. 'character_string8' => $res['refund_order_sn'],
  310. 'phrase3' => $res['refund_type'] == 1 ? '退款' : '退货退款',
  311. 'thing6' => '商家已同意',
  312. 'thing4' => mb_substr($res['refundProduct'][0]['product']['cart_info']['product']['store_name'],0,15).'等',
  313. 'amount5' => $res['refund_price'],
  314. ],
  315. 'link' => $stie_url .'/pages/users/refund/detail?id='.$id,
  316. 'color' => null
  317. ];
  318. break;
  319. //商户拒绝退款 2.1
  320. case 'REFUND_FAIL_CODE':
  321. /*
  322. 订单编号:{{character_string8.DATA}}
  323. 申请类型:{{phrase3.DATA}}
  324. 处理结果:{{thing6.DATA}}
  325. 退款商品:{{thing4.DATA}}
  326. 退款金额:{{amount5.DATA}}
  327. */
  328. $res = $refund_make->get($id);
  329. if(!$res || $res['status'] != -1) return false;
  330. $data[] = [
  331. 'tempCode' => 'REFUND_FAIL_CODE',
  332. 'uid' => $res->uid,
  333. 'data' => [
  334. 'character_string8' => $res['refund_order_sn'],
  335. 'phrase3' => $res['refund_type'] == 1 ? '退款' : '退货退款',
  336. 'thing6' => '商家已拒绝',
  337. 'thing4' => mb_substr($res['refundProduct'][0]['product']['cart_info']['product']['store_name'],0,15).'等',
  338. 'amount5' => $res['refund_price'],
  339. ],
  340. 'link' => $stie_url.'/pages/users/refund/detail?id='.$id,
  341. 'color' => null
  342. ];
  343. break;
  344. //退款成功通知 2.1
  345. case 'REFUND_CONFORM_CODE':
  346. /*
  347. 订单编号:{{character_string1.DATA}}
  348. 商品名称:{{thing2.DATA}}
  349. 退款金额:{{amount3.DATA}}
  350. 退款时间:{{time5.DATA}}
  351. */
  352. $res = $refund_make->get($id);
  353. if(!$res || $res['status'] != 3) return false;
  354. $data[] = [
  355. 'tempCode' => 'REFUND_CONFORM_CODE',
  356. 'uid' => $res->uid,
  357. 'data' => [
  358. 'character_string1' => $res['refund_order_sn'],
  359. 'thing2' => '「'.mb_substr($res['refundProduct'][0]['product']['cart_info']['product']['store_name'],0,15).'」等',
  360. 'amount3' => $res['refund_price'],
  361. 'time5' => $res['create_time'],
  362. ],
  363. 'link' => $stie_url.'/pages/users/refund/detail?id='.$id,
  364. 'color' => null
  365. ];
  366. break;
  367. //到货通知 2.1
  368. // case 'PRODUCT_INCREASE':
  369. // return false;
  370. // /*
  371. // {{first.DATA}}
  372. // 商品名称:{{keyword1.DATA}}
  373. // 到货地点:{{keyword2.DATA}}
  374. // 到货时间:{{keyword3.DATA}}
  375. // {{remark.DATA}
  376. // */
  377. // $make = app()->make(ProductTakeRepository::class);
  378. // $product = app()->make(ProductRepository::class)->getWhere(['product_id' => $id],'*',['attrValue']);
  379. // if(!$product) return false;
  380. // $unique[] = 1;
  381. // foreach ($product['attrValue'] as $item) {
  382. // if($item['stock'] > 0){
  383. // $unique[] = $item['unique'];
  384. // }
  385. // }
  386. // $res = $make->getSearch(['product_id' => $id,'status' =>0,'type' => 2])->where('unique','in',$unique)->column('uid,product_id,product_take_id');
  387. // $uids = array_unique(array_column($res,'uid'));
  388. // if (!$uids) return false;
  389. // $takeId = array_column($res,'product_take_id');
  390. // foreach ($uids as $uid) {
  391. // $data[] = [
  392. // 'tempCode' => 'PRODUCT_INCREASE',
  393. // 'uid' => $uid,
  394. // 'data' => [
  395. // 'first' => '亲,你想要的商品已到货,可以购买啦~',
  396. // 'keyword1' => '「'.$product->store_name.'」',
  397. // 'keyword2' => $product->merchant->mer_name,
  398. // 'keyword3' => date('Y-m-d H:i:s',time()),
  399. // 'remark' => '到货商品:【'.mb_substr($product['store_name'],0,15).'】等,点击查看'
  400. // ],
  401. // 'link' => $stie_url.'/pages/goods_details/index?id='.$id,
  402. // 'color' => null
  403. // ];
  404. // }
  405. // $make->updates($takeId,['status' => 1]);
  406. // break;
  407. // //余额变动 2.1
  408. // case 'USER_BALANCE_CHANGE':
  409. // return false;
  410. // /*
  411. // {{first.DATA}}
  412. // 用户名:{{keyword1.DATA}}
  413. // 变动时间:{{keyword2.DATA}}
  414. // 变动金额:{{keyword3.DATA}}
  415. // 可用余额:{{keyword4.DATA}}
  416. // 变动原因:{{keyword5.DATA}}
  417. // {{remark.DATA}}
  418. // */
  419. // $res = $bill_make->get($id);
  420. // if(!$res) return false;
  421. // $data[] = [
  422. // 'tempCode' => 'USER_BALANCE_CHANGE',
  423. // 'uid' => $res->uid,
  424. // 'data' => [
  425. // 'first' => '资金变动提醒',
  426. // 'keyword1' => $res->user->nickname,
  427. // 'keyword2' => $res['create_time'],
  428. // 'keyword3' => $res['number'],
  429. // 'keyword4' => $res['balance'],
  430. // 'keyword5' => $res['title'],
  431. // 'remark' => '请确认'
  432. // ],
  433. // 'link' => $stie_url.'/pages/users/user_money/index',
  434. // 'color' => null
  435. // ];
  436. // break;
  437. // //拼团成功 2.1
  438. // case 'GROUP_BUYING_SUCCESS':
  439. // return false;
  440. // /*
  441. // {{first.DATA}}
  442. // 订单编号:{{keyword1.DATA}}
  443. // 订单信息:{{keyword2.DATA}}
  444. // 注意信息:{{keyword3.DATA}}
  445. // {{remark.DATA}}
  446. // */
  447. // $res = app()->make(ProductGroupBuyingRepository::class)->get($id);
  448. // if(!$res) return false;
  449. // $ret = app()->make(ProductGroupUserRepository::class)->getSearch(['group_buying_id' => $id])->where('uid','>',0)->select();
  450. // foreach ($ret as $item){
  451. // $data[] = [
  452. // 'tempCode' => 'GROUP_BUYING_SUCCESS',
  453. // 'uid' => $item->uid,
  454. // 'data' => [
  455. // 'first' => '恭喜您拼团成功!',
  456. // 'keyword1' => $item->orderInfo['order_sn'],
  457. // 'keyword2' => '「'.mb_substr($res->productGroup->product['store_name'],0,15).'」',
  458. // 'keyword3' => '无',
  459. // 'remark' => ''
  460. // ],
  461. // 'link' => $stie_url.'/pages/order_details/index?order_id='.$item['order_id'],
  462. // 'color' => null
  463. // ];
  464. // }
  465. // break;
  466. // //客服消息 2.1
  467. // case'SERVER_NOTICE':
  468. // return false;
  469. // /*
  470. // {{first.DATA}}
  471. // 回复时间:{{keyword1.DATA}}
  472. // 回复内容:{{keyword2.DATA}}
  473. // {{remark.DATA}}
  474. // */
  475. // $first = '【平台】';
  476. // if ($params['mer_id']) {
  477. // $mer = app()->make(MerchantRepository::class)->get($params['mer_id']);
  478. // $first = '【' . $mer['mer_name'] . '】';
  479. // }
  480. // $data[] = [
  481. // 'tempCode' => 'SERVER_NOTICE',
  482. // 'uid' => $id,
  483. // 'data' => [
  484. // 'first' => $first .'亲,您有新的消息请注意查看~',
  485. // 'keyword1' => $params['keyword1'],
  486. // 'keyword2' => $params['keyword2'],
  487. // 'remark' => ''
  488. // ],
  489. // 'link' => $stie_url.$params['url'],
  490. // 'color' => null
  491. // ];
  492. // break;
  493. default:
  494. break;
  495. }
  496. return $data;
  497. }
  498. /**
  499. * 小程序订阅消息
  500. * @param $data
  501. * @param string|null $link
  502. * @param string|null $color
  503. * @return bool
  504. * @author Qinii
  505. * @day 2020-07-01
  506. */
  507. public function subscribeSendTemplate($data)
  508. {
  509. event('wechat.subscribeTemplate.before',compact('data'));
  510. Log::info('订阅消息发送 Data' . var_export($data, 1));
  511. $res = $this->subscribeTemplateMessage($data);
  512. Log::info('订阅消息发送 Res' . var_export($res, 1));
  513. if(!$res || !is_array($res))return true;
  514. foreach($res as $item){
  515. if(is_array($item['uid'])){
  516. foreach ($item['uid'] as $value){
  517. $openid = $this->getUserOpenID($value,'min');
  518. if (!$openid) {
  519. continue;
  520. }
  521. $this->send($openid,$item['tempCode'],$item['data'],'subscribe',$item['link'],$item['color']);
  522. }
  523. }else{
  524. $openid = $this->getUserOpenID($item['uid'],'min');
  525. if (!$openid) {
  526. continue;
  527. }
  528. $this->send($openid,$item['tempCode'],$item['data'],'subscribe',$item['link'],$item['color']);
  529. }
  530. }
  531. event('wechat.subscribeTemplate',compact('res'));
  532. }
  533. /**
  534. * 小程序订阅消息整理
  535. * @param string $tempCode
  536. * @param $id
  537. * @return array|bool
  538. * @author Qinii
  539. * @day 2020-07-01
  540. */
  541. public function subscribeTemplateMessage($params)
  542. {
  543. $data = [];
  544. $id = $params['id'];
  545. $tempId = $params['tempId'];
  546. $user_make = app()->make(UserRechargeRepository::class);
  547. $order_make = app()->make(StoreOrderRepository::class);
  548. $refund_make = app()->make(StoreRefundOrderRepository::class);
  549. $order_group_make = app()->make(StoreGroupOrderRepository::class);
  550. $extract_make = app()->make(UserExtractRepository::class);
  551. switch($tempId)
  552. {
  553. //订单支付成功
  554. case 'ORDER_PAY_SUCCESS':
  555. $res = $order_group_make->get($id);
  556. if(!$res) return false;
  557. $link = 'pages/users/order_list/index?status=1';
  558. if ($res->activity_type == 20) $link = 'pages/points_mall/exchange_record?delte=1';
  559. $data[] = [
  560. 'tempCode' => 'ORDER_PAY_SUCCESS',
  561. 'uid' => $res->uid,
  562. 'data' => [
  563. 'character_string1' => $res->group_order_sn,
  564. 'amount2' => $res->pay_price,
  565. 'date3' => $res->pay_time,
  566. 'amount5' => $res->total_price,
  567. ],
  568. 'link' => $link,
  569. 'color' => null
  570. ];
  571. break;
  572. //订单发货提醒(快递)
  573. case 'DELIVER_GOODS_CODE':
  574. $res = $order_make->getWith($id,'orderProduct');
  575. if(!$res) return false;
  576. $link = 'pages/order_details/index?order_id=';
  577. if ($res->activity_type == 20) $link = 'pages/points_mall/integral_order_details?order_id=';
  578. $name = mb_substr($res['orderProduct'][0]['cart_info']['product']['store_name'],0,10);
  579. $data[] = [
  580. 'tempCode' => 'DELIVER_GOODS_CODE',
  581. 'uid' => $res->uid,
  582. /**
  583. 快递单号{{character_string2.DATA}}
  584. 快递公司{{thing1.DATA}}
  585. 发货时间{{time3.DATA}}
  586. 订单商品{{thing5.DATA}}
  587. */
  588. 'data' => [
  589. 'character_string2' => $res->delivery_id,
  590. 'thing1' => $res->delivery_name,
  591. 'time3' => date('Y-m-d H:i:s',time()),
  592. 'thing5' => '「'.$name.'」等',
  593. ],
  594. 'link' => $link.$id,
  595. 'color' => null
  596. ];
  597. break;
  598. //订单发货提醒(送货)
  599. case 'ORDER_DELIVER_SUCCESS':
  600. $res = $order_make->getWith($id,'orderProduct');
  601. if(!$res) return false;
  602. $link = 'pages/order_details/index?order_id=';
  603. if ($res->activity_type == 20) $link = 'pages/points_mall/integral_order_details?order_id=';
  604. $name = mb_substr($res['orderProduct'][0]['cart_info']['product']['store_name'],0,10);
  605. $data[] = [
  606. 'tempCode' => 'ORDER_DELIVER_SUCCESS',
  607. 'uid' => $res->uid,
  608. 'data' => [
  609. 'thing8' => '「'.$name.'」等',
  610. 'character_string1' => $res->order_sn,
  611. 'name4' => $res->delivery_name,
  612. 'phone_number10' => $res->delivery_id,
  613. ],
  614. 'link' => $link.$id,
  615. 'color' => null
  616. ];
  617. break;
  618. //退款通知
  619. case 'REFUND_CONFORM_CODE':
  620. $res = $refund_make->get($id);
  621. if(!$res) return false;
  622. $name = mb_substr($res['refundProduct'][0]['product']['cart_info']['product']['store_name'],0,10);
  623. $data[] = [
  624. 'tempCode' => 'REFUND_CONFORM_CODE',
  625. 'uid' => $res->uid,
  626. 'data' => [
  627. 'thing1' => '退款成功',
  628. 'thing2' => '「'.$name.'」等',
  629. 'character_string6' => $res->refund_order_sn,
  630. 'amount3' => $res->refund_price,
  631. 'thing13' => $res->fail_message ?? '其他',
  632. ],
  633. 'link' => 'pages/users/refund/detail?id='.$id,
  634. 'color' => null
  635. ];
  636. break;
  637. //资金变动
  638. case 'USER_BALANCE_CHANGE':
  639. $res = $user_make->get($id);
  640. if(!$res) return false;
  641. $data[] = [
  642. 'tempCode' => 'USER_BALANCE_CHANGE',
  643. 'uid' => $res->uid,
  644. 'data' => [
  645. 'character_string1' => $res->order_id,
  646. 'amount3' => $res->price,
  647. 'amount6' => $res->give_price,
  648. 'date5' => $res->pay_time,
  649. ],
  650. 'link' => 'pages/users/user_money/index',
  651. 'color' => null
  652. ];
  653. break;
  654. //提现结果通知
  655. case 'EXTRACT_NOTICE':
  656. $res = $extract_make->get($id);
  657. if(!$res) return false;
  658. $data[] = [
  659. 'tempCode' => 'USER_EXTRACT',
  660. 'uid' => $res->uid,
  661. 'data' => [
  662. 'thing1' => $res->status == -1 ? '未通过' : '已通过',
  663. 'amount2' => empty($res->bank_code)?(empty($res->alipay_code)?$res->wechat:$res->alipay_code):$res->bank_code,
  664. 'thing3' => $res->give_price,
  665. 'date4' => $res->create_time,
  666. ],
  667. 'link' => 'pages/users/user_spread_user/index',
  668. 'color' => null
  669. ];
  670. break;
  671. //到货通知
  672. case'PRODUCT_INCREASE':
  673. /*
  674. 商品名称 {{thing1.DATA}}
  675. 商品价格:{{amount5.DATA}}
  676. 温馨提示:{{thing2.DATA}}
  677. */
  678. $make = app()->make(ProductTakeRepository::class);
  679. $product = app()->make(ProductRepository::class)->getWhere(['product_id' => $id],'*',['attrValue']);
  680. if(!$product) return false;
  681. $unique[] = 1;
  682. foreach ($product['attrValue'] as $item) {
  683. if($item['stock'] > 0){
  684. $unique[] = $item['unique'];
  685. }
  686. }
  687. $res = $make->getSearch(['product_id' => $id,'status' =>0,'type' => 3])->where('unique','in',$unique)->column('uid,product_id,product_take_id');
  688. $uids = array_unique(array_column($res,'uid'));
  689. if (!$uids) return false;
  690. $takeId = array_column($res,'product_take_id');
  691. foreach ($uids as $uid) {
  692. $data[] = [
  693. 'tempCode' => 'PRODUCT_INCREASE',
  694. 'uid' => $uid,
  695. 'data' => [
  696. 'thing1' => '「'.mb_substr($product->store_name,0,10) .'」',
  697. 'amount5' => $product->price,
  698. 'thing2' => '亲!你想要的商品已到货,可以购买啦~',
  699. ],
  700. 'link' => 'pages/goods_details/index?id='.$id,
  701. 'color' => null
  702. ];
  703. }
  704. if ($takeId) $make->updates($takeId,['status' => 1]);
  705. break;
  706. //提现结果通知
  707. case 'PAYMENT_RECEIVED':
  708. $res = $extract_make->get($id);
  709. if(!$res) return false;
  710. $data[] = [
  711. 'tempCode' => 'PAYMENT_RECEIVED',
  712. 'uid' => $res->uid,
  713. 'data' => [
  714. 'amount3' => $res->extract_price,
  715. 'thing4' => $res->status == -1 ? '审核未通过' : ($res->wechat_status == 'WAIT_USER_CONFIRM' ? '奖金提现已通过,请及时确认收款' : '审核通过'),
  716. 'time9' => $res->create_time,
  717. ],
  718. 'link' => 'pages/users/user_spread_money/index?type=1',
  719. 'color' => null
  720. ];
  721. break;
  722. default:
  723. return false;
  724. break;
  725. }
  726. return $data;
  727. }
  728. /**
  729. * 整理发送对象的openid
  730. * @param $uid
  731. * @return mixed
  732. * @author Qinii
  733. * @day 2020-06-29
  734. */
  735. public function getUserOpenID($uid,$type = 'wechat')
  736. {
  737. $user = app()->make(UserRepository::class)->get($uid);
  738. if (!$user) return null;
  739. $make = app()->make(WechatUserRepository::class);
  740. if($type == 'wechat') {
  741. return $make->idByOpenId((int)$user['wechat_user_id']);
  742. }else{
  743. return $make->idByRoutineId((int)$user['wechat_user_id']);
  744. }
  745. }
  746. /**
  747. * 发送模板消息/订阅消息
  748. * @param $openid
  749. * @param $tempCode
  750. * @param $data
  751. * @param $type
  752. * @param $link
  753. * @param $color
  754. * @return bool|mixed
  755. * @author Qinii
  756. * @day 2020-07-01
  757. */
  758. public function send($openid,$tempCode,$data,$type,$link,$color)
  759. {
  760. $template = new Template($type);
  761. $template->to($openid)->color($color);
  762. if ($link) $template->url($link);
  763. return $template->send($tempCode, $data);
  764. }
  765. }