WechatTemplateListService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\message\notice;
  12. use app\jobs\TemplateJob;
  13. use app\services\message\NoticeService;
  14. use app\services\kefu\service\StoreServiceServices;
  15. use app\services\user\UserServices;
  16. use app\services\wechat\WechatUserServices;
  17. use think\facade\Log;
  18. /**
  19. * 微信模版消息列表
  20. * Created by PhpStorm.
  21. * User: xurongyao <763569752@qq.com>
  22. * Date: 2021/9/22 1:23 PM
  23. */
  24. class WechatTemplateListService extends NoticeService
  25. {
  26. /**
  27. * 根据UID获取openid
  28. * @param int $uid
  29. * @return mixed
  30. */
  31. public function getOpenidByUid(int $uid)
  32. {
  33. $isDel = app()->make(UserServices::class)->value(['uid' => $uid], 'is_del');
  34. if ($isDel) {
  35. $openid = '';
  36. } else {
  37. $openid = app()->make(WechatUserServices::class)->uidToOpenid($uid, 'wechat');
  38. }
  39. return $openid;
  40. }
  41. /**
  42. * 发送模板消息
  43. * @param int $uid
  44. * @param array $data
  45. * @param string|null $link
  46. * @param string|null $color
  47. * @return bool|void
  48. */
  49. public function sendTemplate(int $uid, array $data, string $link = null, string $color = null)
  50. {
  51. try {
  52. if ($this->noticeInfo['is_wechat'] === 1) {
  53. $openid = $this->getOpenidByUid($uid);
  54. if ($openid != '') {
  55. //放入队列执行
  56. TemplateJob::dispatch('doJob', ['wechat', $openid, $this->noticeInfo['wechat_tempid'], $data, $link, $color]);
  57. }
  58. }
  59. } catch (\Exception $e) {
  60. Log::error($e->getMessage());
  61. return true;
  62. }
  63. }
  64. /**
  65. * 支付成功发送模板消息
  66. * @param $uid
  67. * @param $order
  68. * @return bool|void
  69. */
  70. public function sendOrderPaySuccess($uid, $order)
  71. {
  72. return $this->sendTemplate((int)$uid, [
  73. 'character_string2' => $order['order_id'],
  74. 'time4' => date('Y-m-d H:i:s', $order['pay_time']),
  75. 'thing3' => $order['storeName'],
  76. 'amount5' => $order['pay_price'],
  77. ], '/pages/goods/order_details/index?order_id=' . $order['order_id']);
  78. }
  79. /**
  80. * 订单配送通知
  81. * @param $uid
  82. * @param string $goodsName
  83. * @param $order
  84. * @param array $data
  85. * @return bool|void
  86. */
  87. public function sendOrderDeliver($uid, string $goodsName, $order)
  88. {
  89. return $this->sendTemplate((int)$uid, [
  90. 'character_string1' => $order['order_id'],
  91. 'time8' => date('Y-m-d H:i:s', time()),
  92. 'thing5' => $goodsName,
  93. 'thing9' => $order['delivery_name'],
  94. 'phone_number10' => $order['delivery_id'],
  95. ], '/pages/goods/order_details/index?order_id=' . $order['order_id']);
  96. }
  97. /**
  98. * 订单发货
  99. * @param $uid
  100. * @param $order
  101. * @param $storeTitle
  102. * @return bool|void
  103. */
  104. public function sendOrderPostage($uid, $order, $storeTitle)
  105. {
  106. return $this->sendTemplate((int)$uid, [
  107. 'character_string2' => $order['order_id'],
  108. 'time12' => date('Y-m-d H:i:s', time()),
  109. 'thing4' => $storeTitle,
  110. 'thing13' => $order['delivery_name'],
  111. 'character_string14' => $order['delivery_id'],
  112. ], '/pages/goods/order_details/index?order_id=' . $order['order_id']);
  113. }
  114. /**
  115. * 确认收货发送模板消息
  116. * @param $uid
  117. * @param $order
  118. * @param $title
  119. * @return bool|void
  120. */
  121. public function sendOrderTakeSuccess($uid, $order, $title)
  122. {
  123. return $this->sendTemplate((int)$uid, [
  124. 'character_string2' => $order['order_id'],
  125. 'character_string7' => date('Y-m-d H:i:s', time()),
  126. 'thing4' => $title,
  127. 'amount9' => $order['pay_price'],
  128. ], '/pages/goods/order_details/index?order_id=' . $order['order_id']);
  129. }
  130. /**
  131. * 发送退款模板消息
  132. * @param $uid
  133. * @param $order
  134. * @param $title
  135. * @return bool|void
  136. */
  137. public function sendOrderRefund($uid, $order, $title)
  138. {
  139. return $this->sendTemplate((int)$uid, [
  140. 'character_string1' => $order['refund_no'],
  141. 'time5' => date('Y-m-d H:i:s', time()),
  142. 'thing2' => $title,
  143. 'amount3' => $order['refund_price'],
  144. ], '/pages/goods/order_details/index?order_id=' . $order['refund_no'] . '&isReturen=1');
  145. }
  146. /**
  147. * 发送退款模板消息
  148. * @param $uid
  149. * @param $order
  150. * @param $title
  151. * @return bool|void
  152. */
  153. public function sendOrderNoRefund($uid, $order, $title)
  154. {
  155. return $this->sendTemplate((int)$uid, [
  156. 'character_string1' => $order['refund_no'],
  157. 'thing2' => $title,
  158. 'amount3' => $order['refund_price'],
  159. 'thing4' => $order['refuse_reason'],
  160. ], '/pages/goods/order_details/index?order_id=' . $order['refund_no'] . '&isReturen=1');
  161. }
  162. /**
  163. * 充值成功
  164. * @param $uid
  165. * @param $order
  166. * @return bool|void
  167. */
  168. public function sendRechargeSuccess($uid, $order)
  169. {
  170. return $this->sendTemplate((int)$uid, [
  171. 'time1' => date('Y-m-d H:i:s', $order['add_time']),
  172. 'amount3' => $order['price'],
  173. 'amount4' => $order['give_price'],
  174. 'amount5' => $order['now_money'],
  175. ]);
  176. }
  177. /**
  178. * 提现成功
  179. * @param $uid
  180. * @param $extractNumber
  181. * @return bool|void
  182. */
  183. public function sendUserExtract($uid, $extractNumber)
  184. {
  185. return $this->sendTemplate((int)$uid, [
  186. 'time3' => date('Y-m-d H:i:s', time()),
  187. 'amount2' => $extractNumber,
  188. ]);
  189. }
  190. /**
  191. * 订单给客服提醒
  192. * @param $orderId
  193. * @param $storeName
  194. * @param $title
  195. * @param $status
  196. * @param $link
  197. * @return bool
  198. */
  199. public function sendAdminOrder($orderId, $storeName, $title, $status, $link)
  200. {
  201. /** @var StoreServiceServices $StoreServiceServices */
  202. $StoreServiceServices = app()->make(StoreServiceServices::class);
  203. $adminList = $StoreServiceServices->getStoreServiceOrderNotice();
  204. foreach ($adminList as $item) {
  205. $this->sendTemplate((int)$item['uid'], [
  206. 'short_thing6' => $status,
  207. 'character_string1' => $orderId,
  208. 'time2' => date('Y-m-d H:i:s', time()),
  209. ], $link);
  210. }
  211. return true;
  212. }
  213. }