RoutineTemplateJob.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\notice\template;
  12. use app\services\message\TemplateMessageServices;
  13. use crmeb\basic\BaseJobs;
  14. use crmeb\services\wechat\MiniProgram;
  15. use crmeb\traits\QueueTrait;
  16. use crmeb\services\template\Template;
  17. use think\facade\Log;
  18. /**
  19. * 小程序模板消息消息队列
  20. * Class RoutineTemplateJob
  21. * @package app\jobs
  22. */
  23. class RoutineTemplateJob extends BaseJobs
  24. {
  25. use QueueTrait;
  26. /**
  27. * 同步订阅消息
  28. * @return bool
  29. */
  30. public function doJob(array $template, array $errMessage)
  31. {
  32. $time = time();
  33. $works = [];
  34. try {
  35. $works = MiniProgram::getSubscribeTemplateKeyWords($template['tempkey']);
  36. } catch (\Throwable $e) {
  37. $wechatErr = $e->getMessage();
  38. if (is_string($wechatErr)) {
  39. Log::error('同步订阅消息,获取关键词列表失败:' . $wechatErr);
  40. } elseif (in_array($wechatErr->getCode(), array_keys($errMessage))) {
  41. Log::error('同步订阅消息,获取关键词列表失败:' . $wechatErr->getMessage());
  42. }
  43. }
  44. $kid = [];
  45. if ($works) {
  46. $works = array_combine(array_column($works, 'name'), $works);
  47. $content = is_array($template['content']) ? $template['content'] : explode("\n", $template['content']);
  48. foreach ($content as $c) {
  49. $name = explode('{{', $c)[0] ?? '';
  50. if ($name && isset($works[$name])) {
  51. $kid[] = $works[$name]['kid'];
  52. }
  53. }
  54. }
  55. if ($kid && isset($template['kid']) && !$template['kid']) {
  56. $tempid = '';
  57. try {
  58. $tempid = MiniProgram::addSubscribeTemplate($template['tempkey'], $kid, $template['name']);
  59. } catch (\Throwable $e) {
  60. $wechatErr = $e->getMessage();
  61. if (is_string($wechatErr)) {
  62. Log::error('同步订阅消息,添加订阅消息模版失败:' . $wechatErr);
  63. } elseif (in_array($wechatErr->getCode(), array_keys($errMessage))) {
  64. Log::error('同步订阅消息,添加订阅消息模版失败:' . $wechatErr->getMessage());
  65. }
  66. }
  67. if ($tempid != $template['tempid']) {
  68. /** @var TemplateMessageServices $templateMessageServices */
  69. $templateMessageServices = app()->make(TemplateMessageServices::class);
  70. $templateMessageServices->update($template['id'], ['tempid' => $tempid, 'kid' => json_encode($kid), 'add_time' => $time], 'id');
  71. }
  72. }
  73. return true;
  74. }
  75. /**
  76. * 确认收货
  77. * @param $openid
  78. * @param $order
  79. * @param $title
  80. * @return bool
  81. */
  82. public function sendOrderTakeOver($openid, $order, $title)
  83. {
  84. return $this->sendTemplate('ORDER_TAKEVER', $openid, [
  85. 'thing1' => $order['order_id'],
  86. 'thing2' => $title,
  87. 'date5' => date('Y-m-d H:i:s', time()),
  88. ], '/pages/users/order_details/index?order_id=' . $order['order_id']);
  89. }
  90. /**
  91. * @param $openid
  92. * @param $order
  93. * @param $storeTitle
  94. * @param int $isGive 0 = 同城配送, 1 = 快递发货
  95. * @return bool
  96. */
  97. public function sendOrderPostage($openid, $order, $storeTitle, int $isGive = 0)
  98. {
  99. if ($isGive) {//快递发货
  100. return $this->sendTemplate('ORDER_DELIVER_SUCCESS', $openid, [
  101. 'character_string2' => $order['delivery_id'],
  102. 'thing1' => $order['delivery_name'],
  103. 'time3' => date('Y-m-d H:i:s', time()),
  104. 'thing5' => $storeTitle,
  105. ], '/pages/users/order_details/index?order_id=' . $order['order_id']);
  106. } else {//同城配送
  107. return $this->sendTemplate('ORDER_POSTAGE_SUCCESS', $openid, [
  108. 'thing8' => $storeTitle,
  109. 'character_string1' => $order['order_id'],
  110. 'name4' => $order['delivery_name'],
  111. 'phone_number10' => $order['delivery_id']
  112. ], '/pages/users/order_details/index?order_id=' . $order['order_id']);
  113. }
  114. }
  115. /**
  116. * 充值金额退款
  117. * @param $UserRecharge
  118. * @param $refund_price
  119. * @return bool
  120. */
  121. public function sendRechargeSuccess($openid, $UserRecharge, $now_money)
  122. {
  123. return $this->sendTemplate('RECHARGE_SUCCESS', $openid, [
  124. 'character_string1' => $UserRecharge['order_id'],
  125. 'amount3' => $UserRecharge['price'],
  126. 'amount4' => $now_money,
  127. 'date5' => date('Y-m-d H:i:s', time()),
  128. ], '/pages/users/user_bill/index?type=2');
  129. }
  130. /**
  131. * 订单退款成功发送消息
  132. * @param string $openid
  133. * @param array $order
  134. * @return bool
  135. */
  136. public function sendOrderRefundSuccess($openid, $order, $storeTitle)
  137. {
  138. return $this->sendTemplate('ORDER_REFUND', $openid, [
  139. 'thing1' => '已成功退款',
  140. 'thing2' => $storeTitle,
  141. 'amount3' => $order['pay_price'],
  142. 'character_string6' => $order['order_id']
  143. ], '/pages/users/order_details/index?order_id=' . $order['order_id'] . '&isReturen=1');
  144. }
  145. /**
  146. * 订单退款失败
  147. * @param string $openid
  148. * @param $order
  149. * @return bool
  150. */
  151. public function sendOrderRefundFail($openid, $order, $storeTitle)
  152. {
  153. return $this->sendTemplate('ORDER_REFUND', $openid, [
  154. 'thing1' => '退款失败',
  155. 'thing2' => $storeTitle,
  156. 'amount3' => $order['pay_price'],
  157. 'character_string6' => $order['order_id']
  158. ], '/pages/users/order_details/index?order_id=' . $order['order_id'] . '&isReturen=1');
  159. }
  160. /**
  161. * 用户申请退款给管理员发送消息
  162. * @param array $order
  163. * @param string $refundReasonWap
  164. * @param array $adminList
  165. */
  166. public function sendOrderRefundStatus($openid, $order)
  167. {
  168. $data['character_string4'] = $order['order_id'];
  169. $data['date5'] = date('Y-m-d H:i:s', time());
  170. $data['amount2'] = $order['pay_price'];
  171. $data['phrase7'] = '申请退款中';
  172. $data['thing8'] = '请及时处理';
  173. return $this->sendTemplate('ORDER_REFUND_STATUS', $openid, $data);
  174. }
  175. /**
  176. * 砍价成功通知
  177. * @param array $bargain
  178. * @param array $bargainUser
  179. * @param int $bargainUserId
  180. * @return bool
  181. */
  182. public function sendBargainSuccess($openid, $bargain = [], $bargainUser = [], $bargainUserId = 0)
  183. {
  184. $data['thing1'] = $bargain['title'];
  185. $data['amount2'] = $bargain['min_price'];
  186. $data['thing3'] = '恭喜您,已经砍到最低价了';
  187. return $this->sendTemplate('BARGAIN_SUCCESS', $openid, $data, '/pages/activity/goods_bargain_details/index?id=' . $bargain['id'] . '&bargain=' . $bargainUserId);
  188. }
  189. /**
  190. * 订单支付成功发送模板消息
  191. * @param $openidf
  192. * @param $pay_price
  193. * @param $orderId
  194. * @param $payTime
  195. * @return bool|void
  196. */
  197. public function sendOrderSuccess($openid, $pay_price, $orderId)
  198. {
  199. if ($orderId == '') return true;
  200. $data['character_string1'] = $orderId;
  201. $data['amount2'] = $pay_price . '元';
  202. $data['date3'] = date('Y-m-d H:i:s', time());
  203. return $this->sendTemplate('ORDER_PAY_SUCCESS', $openid, $data, '/pages/users/order_details/index?order_id=' . $orderId);
  204. }
  205. /**
  206. * 会员订单支付成功发送消息
  207. * @param $openid
  208. * @param $pay_price
  209. * @param $orderId
  210. * @return bool
  211. */
  212. public function sendMemberOrderSuccess($openid, $pay_price, $orderId)
  213. {
  214. if ($orderId == '') return true;
  215. $data['character_string1'] = $orderId;
  216. $data['amount2'] = $pay_price . '元';
  217. $data['date3'] = date('Y-m-d H:i:s', time());
  218. return $this->sendTemplate('ORDER_PAY_SUCCESS', $openid, $data, '/pages/annex/vip_paid/index');
  219. }
  220. /**
  221. * 提现失败
  222. * @param $openid
  223. * @param $msg
  224. * @param $extract_number
  225. * @param $extract_type
  226. * @return bool
  227. */
  228. public function sendExtractFail($openid, $msg, $extract_number, $nickname)
  229. {
  230. return $this->sendTemplate('USER_EXTRACT', $openid, [
  231. 'thing1' => '提现失败:' . $msg,
  232. 'amount2' => $extract_number . '元',
  233. 'thing3' => $nickname,
  234. 'date4' => date('Y-m-d H:i:s', time())
  235. ], '/pages/users/user_spread_money/index?type=2');
  236. }
  237. /**
  238. * 提现成功
  239. * @param $openid
  240. * @param $extract_number
  241. * @param $nickname
  242. * @return bool
  243. */
  244. public function sendExtractSuccess($openid, $extract_number, $nickname)
  245. {
  246. return $this->sendTemplate('USER_EXTRACT', $openid, [
  247. 'thing1' => '提现成功',
  248. 'amount2' => $extract_number . '元',
  249. 'thing3' => $nickname,
  250. 'date4' => date('Y-m-d H:i:s', time())
  251. ], '/pages/users/user_spread_money/index?type=2');
  252. }
  253. /**
  254. * 拼团成功通知
  255. * @param $uid
  256. * @param $pinkTitle
  257. * @param $nickname
  258. * @param $pinkTime
  259. * @param $count
  260. * @return bool
  261. */
  262. public function sendPinkSuccess($openid, $pinkTitle, $nickname, $pinkTime, $count, string $link = '')
  263. {
  264. return $this->sendTemplate('PINK_TRUE', $openid, [
  265. 'thing1' => $pinkTitle,
  266. 'name3' => $nickname,
  267. 'date5' => date('Y-m-d H:i:s', $pinkTime),
  268. 'number2' => $count
  269. ], $link);
  270. }
  271. /**
  272. * 拼团状态通知
  273. * @param $openid
  274. * @param $pinkTitle
  275. * @param $count
  276. * @param $remarks
  277. * @return bool
  278. */
  279. public function sendPinkFail($openid, $pinkTitle, $count, $remarks, $link)
  280. {
  281. return $this->sendTemplate('PINK_STATUS', $openid, [
  282. 'thing2' => $pinkTitle,
  283. 'thing1' => $count,
  284. 'thing3' => $remarks
  285. ], $link);
  286. }
  287. /**
  288. * 赠送积分消息提醒
  289. * @param $openid
  290. * @param $order
  291. * @param $storeTitle
  292. * @param $gainIntegral
  293. * @param $integral
  294. * @return bool
  295. */
  296. public function sendUserIntegral($openid, $order, $storeTitle, $gainIntegral, $integral)
  297. {
  298. if (!$order || !$openid) return true;
  299. if (is_string($order['cart_id']))
  300. $order['cart_id'] = json_decode($order['cart_id'], true);
  301. return $this->sendTemplate('INTEGRAL_ACCOUT', $openid, [
  302. 'character_string2' => $order['order_id'],
  303. 'thing3' => $storeTitle,
  304. 'amount4' => $order['pay_price'],
  305. 'number5' => $gainIntegral,
  306. 'number6' => $integral
  307. ], '/pages/users/user_integral/index');
  308. }
  309. /**
  310. * 发送模板消息
  311. * @param string $TempCode 模板消息常量名称
  312. * @param int $openid 用户openid
  313. * @param array $data 模板内容
  314. * @param string $link 跳转链接
  315. * @return bool
  316. */
  317. public function sendTemplate(string $tempCode, $openid, array $data, string $link = '')
  318. {
  319. try {
  320. if (!$openid) return true;
  321. $template = new Template('subscribe');
  322. $res = $template->to($openid)->url($link)->send($tempCode, $data);
  323. if (!$res) {
  324. Log::error('订阅消息发送失败,原因:' . $template->getError() . '----参数:' . json_encode(compact('tempCode', 'openid', 'data', 'link')));
  325. }
  326. return true;
  327. } catch (\Exception $e) {
  328. Log::error('订阅消息发送失败,原因:' . $e->getMessage() . '----参数:' . json_encode(compact('tempCode', 'openid', 'data', 'link')));
  329. return true;
  330. }
  331. }
  332. /**
  333. * 获得推广佣金发送提醒
  334. * @param string $openid
  335. * @param string $brokeragePrice
  336. * @param string $goods_name
  337. * @return bool
  338. */
  339. public function sendOrderBrokerageSuccess(string $openid, string $brokeragePrice, string $goods_name)
  340. {
  341. return $this->sendTemplate('ORDER_BROKERAGE', $openid, [
  342. 'thing2' => $goods_name,
  343. 'amount4' => $brokeragePrice . '元',
  344. 'time1' => date('Y-m-d H:i:s', time())
  345. ], '/pages/users/user_spread_user/index');
  346. }
  347. /**
  348. * 绑定推广关系发送消息提醒
  349. * @param string $openid
  350. * @param string $userName
  351. * @return bool|mixed
  352. */
  353. public function sendBindSpreadUidSuccess(string $openid, string $userName)
  354. {
  355. return $this->sendTemplate('BIND_SPREAD_UID', $openid, [
  356. 'name3' => $userName . "加入您的团队",
  357. 'date4' => date('Y-m-d H:i:s', time())
  358. ], '/pages/users/user_spread_user/index');
  359. }
  360. }