WechatTemplate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace app\models\wechat;
  3. use app\models\store\StoreBargain;
  4. use app\models\store\StorePink;
  5. use crmeb\services\template\Template;
  6. use crmeb\traits\ModelTrait;
  7. use crmeb\basic\BaseModel;
  8. use app\models\user\WechatUser;
  9. use think\facade\Route;
  10. /**
  11. * 微信模板消息model
  12. * Class WechatTemplate
  13. * @package app\models\wechat
  14. */
  15. class WechatTemplate extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'template_message';
  27. use ModelTrait;
  28. /**
  29. * 获取系统分页数据 分类
  30. * @param array $where
  31. * @return array
  32. */
  33. public static function systemPage($where = array())
  34. {
  35. $model = self::where('type', 1);
  36. if ($where['name'] !== '') $model = $model->where('name', 'LIKE', "%$where[name]%");
  37. if ($where['status'] !== '') $model = $model->where('status', $where['status']);
  38. $count = $model->count();
  39. $list = $model->page((int)$where['page'], (int)$where['limit'])
  40. ->select()
  41. ->each(function ($item) {
  42. if ($item['content']) $item['content'] = explode("\n", $item['content']);
  43. });
  44. return compact('count', 'list');
  45. }
  46. /**
  47. * 支付成功发送模板消息
  48. * @param $order
  49. * @return bool
  50. */
  51. public function sendOrderPaySuccess($order)
  52. {
  53. return $this->sendTemplate('ORDER_PAY_SUCCESS', $order['uid'], [
  54. 'first' => '亲,您购买的商品已支付成功',
  55. 'keyword1' => $order['order_id'],
  56. 'keyword2' => $order['pay_price'],
  57. 'remark' => '点击查看订单详情'
  58. ], Route::buildUrl('/pages/order_details/index?order_id=' . $order['order_id'])->suffix('')->domain(true)->build());
  59. }
  60. /**
  61. * 发送客服消息
  62. * @param $order
  63. * @param string|null $link
  64. * @return bool
  65. */
  66. public function sendServiceNotice($order, string $link = null)
  67. {
  68. $kefuIds = ServiceModel::where('notify', 1)->column('uid', 'uid');
  69. if (empty($kefuIds)) {
  70. return true;
  71. }
  72. $data = [
  73. 'first' => "亲,您有一个新订单 \n订单号:{$order['order_id']}",
  74. 'keyword1' => '新订单',
  75. 'keyword2' => '已支付',
  76. 'keyword3' => date('Y/m/d H:i', time()),
  77. 'remark' => '请及时处理'
  78. ];
  79. foreach ($kefuIds as $uid) {
  80. $this->sendTemplate('ADMIN_NOTICE', $uid, $data, $link);
  81. }
  82. }
  83. /**
  84. * 退款发送客服消息
  85. * @param $order
  86. * @param string|null $link
  87. * @return bool
  88. */
  89. public function sendRefundServiceNotice($order, string $link = null)
  90. {
  91. $kefuIds = ServiceModel::where('notify', 1)->column('uid', 'uid');
  92. if (empty($kefuIds)) {
  93. return true;
  94. }
  95. $data = [
  96. 'first' => "亲,您有一个订单申请退款 \n订单号:{$order['order_id']}",
  97. 'keyword1' => '申请退款',
  98. 'keyword2' => '待处理',
  99. 'keyword3' => date('Y/m/d H:i', time()),
  100. 'remark' => '请及时处理'
  101. ];
  102. foreach ($kefuIds as $uid) {
  103. $this->sendTemplate('ADMIN_NOTICE', $uid, $data, $link);
  104. }
  105. }
  106. /**
  107. * 确认收货发送模板消息
  108. * @param $order
  109. * @return bool|mixed
  110. */
  111. public function sendOrderTakeSuccess($order, $title)
  112. {
  113. return $this->sendTemplate('ORDER_TAKE_SUCCESS', $order['uid'], [
  114. 'first' => '亲,您的订单已收货',
  115. 'keyword1' => $order['order_id'],
  116. 'keyword2' => '已收货',
  117. 'keyword3' => date('Y-m-d H:i:s', time()),
  118. 'keyword4' => $title,
  119. 'remark' => '感谢您的光临!'
  120. ]);
  121. }
  122. /**
  123. * 发送退款模板消息
  124. * @param array $data
  125. * @param $order
  126. * @return bool|mixed
  127. */
  128. public function sendOrderRefundStatus(array $data, $order)
  129. {
  130. return $this->sendTemplate('ORDER_REFUND_STATUS', $order['uid'], [
  131. 'first' => '亲,您购买的商品已退款,本次退款' . $data['refund_price'] . '金额',
  132. 'keyword1' => $order['order_id'],
  133. 'keyword2' => $order['pay_price'],
  134. 'keyword3' => date('Y-m-d H:i:s', $order['add_time']),
  135. 'remark' => '点击查看订单详情'
  136. ], Route::buildUrl('/pages/order_details/index?order_id=' . $order['order_id'])->suffix('')->domain(true)->build());
  137. }
  138. /**
  139. * 发送用户充值退款模板消息
  140. * @param array $data
  141. * @param $userRecharge
  142. * @return bool|mixed
  143. */
  144. public function sendRechargeRefundStatus(array $data, $userRecharge)
  145. {
  146. return $this->sendTemplate('ORDER_REFUND_STATUS', $userRecharge['uid'], [
  147. 'first' => '亲,您充值的金额已退款,本次退款' .
  148. $data['refund_price'] . '金额',
  149. 'keyword1' => $userRecharge['order_id'],
  150. 'keyword2' => $userRecharge['price'],
  151. 'keyword3' => date('Y-m-d H:i:s', $userRecharge['add_time']),
  152. 'remark' => '点击查看订单详情'
  153. ], Route::buildUrl('/pages/users/user_bill/index')->domain(true)->suffix(false)->build());
  154. }
  155. /**
  156. * 佣金提现失败发送模板消息
  157. * @param $uid
  158. * @param $extract_number
  159. * @param $fail_msg
  160. * @return bool|mixed
  161. */
  162. public function sendUserBalanceChangeFial($uid, $extract_number, $fail_msg)
  163. {
  164. return $this->sendTemplate('USER_BALANCE_CHANGE', $uid, [
  165. 'first' => '提现失败,退回佣金' . $extract_number . '元',
  166. 'keyword1' => '佣金提现',
  167. 'keyword2' => date('Y-m-d H:i:s', time()),
  168. 'keyword3' => $extract_number,
  169. 'remark' => '错误原因:' . $fail_msg
  170. ], Route::buildUrl('/pages/users/user_spread_money/index?type=1')->suffix(false)->domain(true)->build());
  171. }
  172. /**
  173. * 佣金提现成功发送模板消息
  174. * @param $uid
  175. * @param $extractNumber
  176. * @return bool|mixed
  177. */
  178. public function sendUserBalanceChangeSuccess($uid, $extractNumber)
  179. {
  180. return $this->sendTemplate('USER_BALANCE_CHANGE', $uid, [
  181. 'first' => '成功提现佣金' . $extractNumber . '元',
  182. 'keyword1' => '佣金提现',
  183. 'keyword2' => date('Y-m-d H:i:s', time()),
  184. 'keyword3' => $extractNumber,
  185. 'remark' => '点击查看我的佣金明细'
  186. ], Route::buildUrl('/pages/users/user_spread_money/index?type=1')->suffix(false)->domain(true)->build());
  187. }
  188. /**
  189. * 拼团成功发送模板消息
  190. * @param $uid
  191. * @param $order_id
  192. * @param $title
  193. * @return bool|mixed
  194. */
  195. public function sendOrderPinkSuccess($uid, $order_id, $title)
  196. {
  197. return $this->sendTemplate('ORDER_USER_GROUPS_SUCCESS', $uid, [
  198. 'first' => '亲,您的拼团已经完成了',
  199. 'keyword1' => $order_id,
  200. 'keyword2' => $title,
  201. 'remark' => '点击查看订单详情'
  202. ], Route::buildUrl('/pages/activity/goods_combination_status/index?id=' . $order_id)->suffix(false)->domain(true)->build());
  203. }
  204. /**
  205. * 参团成功发送模板消息
  206. * @param $uid
  207. * @param $order_id
  208. * @param $title
  209. * @return bool|mixed
  210. */
  211. public function sendOrderPinkUseSuccess($uid, string $order_id, string $title, string $pink_id)
  212. {
  213. return $this->sendTemplate('ORDER_USER_GROUPS_SUCCESS', $uid, [
  214. 'first' => '亲,您已成功参与拼团',
  215. 'keyword1' => $order_id,
  216. 'keyword2' => $title,
  217. 'remark' => '点击查看订单详情'
  218. ], Route::buildUrl('/pages/activity/goods_combination_status/index?id=' . $pink_id)->suffix(false)->domain(true)->build());
  219. }
  220. /**
  221. * 取消拼团发送模板消息
  222. * @param $uid
  223. * @param StorePink $order_id
  224. * @param $price
  225. * @param string $title
  226. * @return bool|mixed
  227. */
  228. public function sendOrderPinkClone($uid, StorePink $pink, $title)
  229. {
  230. return $this->sendTemplate('ORDER_USER_GROUPS_LOSE', $uid, [
  231. 'first' => '亲,您的拼团取消',
  232. 'keyword1' => $title,
  233. 'keyword2' => $pink->price,
  234. 'keyword3' => $pink->price,
  235. 'remark' => '点击查看订单详情'
  236. ], Route::buildUrl('/pages/activity/goods_combination_status/index?id=' . $pink->id)->suffix(false)->domain(true)->build());
  237. }
  238. /**
  239. * 拼团失败发送模板消息
  240. * @param $uid
  241. * @param StorePink $pink
  242. * @param $title
  243. * @return bool|mixed
  244. */
  245. public function sendOrderPinkFial($uid, StorePink $pink, $title)
  246. {
  247. return $this->sendTemplate('ORDER_USER_GROUPS_LOSE', $uid, [
  248. 'first' => '亲,您的拼团失败',
  249. 'keyword1' => $title,
  250. 'keyword2' => $pink->price,
  251. 'keyword3' => $pink->price,
  252. 'remark' => '点击查看订单详情'
  253. ], Route::buildUrl('/pages/activity/goods_combination_status/index?id=' . $pink->id)->suffix(false)->domain(true)->build());
  254. }
  255. /**
  256. * 开团成功发送模板消息
  257. * @param $uid
  258. * @param StorePink $pink
  259. * @param $title
  260. * @return bool|mixed
  261. */
  262. public function sendOrderPinkOpenSuccess($uid, $pink, $title)
  263. {
  264. return $this->sendTemplate('OPEN_PINK_SUCCESS', $uid, [
  265. 'first' => '您好,您已成功开团!赶紧与小伙伴们分享吧!!!',
  266. 'keyword1' => $title,
  267. 'keyword2' => $pink['total_price'],
  268. 'keyword3' => $pink['people'],
  269. 'remark' => '点击查看订单详情'
  270. ], Route::buildUrl('/pages/activity/goods_combination_status/index?id=' . $pink['id'])->suffix(false)->domain(true)->build());
  271. }
  272. /**
  273. * 助力成功发送模板消息
  274. * @param $uid
  275. * @param $order_id
  276. * @param $title
  277. * @return bool|mixed
  278. */
  279. public function sendOrderAssistanceUseSuccess($uid, string $order_id, string $title, string $assistance_id)
  280. {
  281. return $this->sendTemplate('ORDER_USER_GROUPS_SUCCESS', $uid, [
  282. 'first' => '亲,您已成功参与助力',
  283. 'keyword1' => $order_id,
  284. 'keyword2' => $title,
  285. 'remark' => '点击查看订单详情'
  286. ], Route::buildUrl('/pages/activity/goods_assistance_status/index?id=' . $assistance_id)->suffix(false)->domain(true)->build());
  287. }
  288. /**
  289. * 取消助力发送模板消息
  290. * @param $uid
  291. * @param StoreAssistance $order_id
  292. * @param $price
  293. * @param string $title
  294. * @return bool|mixed
  295. */
  296. public function sendOrderAssistanceClone($uid, StoreAssistance $assistance, $title)
  297. {
  298. return $this->sendTemplate('ORDER_USER_GROUPS_LOSE', $uid, [
  299. 'first' => '亲,您的拼团取消',
  300. 'keyword1' => $title,
  301. 'keyword2' => $assistance->price,
  302. 'keyword3' => $assistance->price,
  303. 'remark' => '点击查看订单详情'
  304. ], Route::buildUrl('/pages/activity/goods_assistance_status/index?id=' . $assistance->id)->suffix(false)->domain(true)->build());
  305. }
  306. /**
  307. * 砍价成功发送模板消息
  308. * @param $uid
  309. * @param StoreBargain $bargain
  310. * @return bool|mixed
  311. */
  312. public function sendBrgainSuccess($uid, StoreBargain $bargain)
  313. {
  314. return $this->sendTemplate('BARGAIN_SUCCESS', $uid, [
  315. 'first' => '好腻害!你的朋友们已经帮你砍到底价了!',
  316. 'keyword1' => $bargain['title'],
  317. 'keyword2' => $bargain['min_price'],
  318. 'remark' => '点击查看订单详情'
  319. ], Route::buildUrl('/pages/activity/goods_bargain_details/index?id=' . $bargain['id'] . '&bargain=' . $uid)->suffix(false)->domain(true)->build());
  320. }
  321. /**
  322. * 发送模板消息
  323. * @param string $tempCode 模板消息常量名称
  324. * @param $uid 用户uid
  325. * @param array $data 模板内容
  326. * @param string $link 跳转链接
  327. * @param string|null $color 文字颜色
  328. * @return bool|mixed
  329. */
  330. public function sendTemplate(string $tempCode, $uid, array $data, string $link = '', string $color = null)
  331. {
  332. try {
  333. $openid = WechatUser::uidToOpenid($uid, 'openid');
  334. if (!$openid) return false;
  335. $template = new Template('wechat');
  336. return $template->to($openid)->color($color)->url($link)->send($tempCode, $data);
  337. } catch (\Exception $e) {
  338. return false;
  339. }
  340. }
  341. }