Message.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\User as UserModel;
  4. use app\common\model\Wxapp as WxappModel;
  5. use app\common\model\Setting as SettingModel;
  6. use app\common\model\dealer\Setting as DealerSettingModel;
  7. use app\common\model\sharing\Setting as SharingSettingModel;
  8. use app\common\service\wxapp\FormId as FormIdService;
  9. use app\common\enum\OrderType as OrderTypeEnum;
  10. use app\common\library\wechat\WxTplMsg;
  11. use app\common\library\sms\Driver as SmsDriver;
  12. /**
  13. * 消息通知服务
  14. * Class Message
  15. * @package app\common\service
  16. */
  17. class Message
  18. {
  19. /**
  20. * 订单支付成功后通知
  21. * @param \think\Model $order
  22. * @param int $orderType 订单类型 (10商城订单 20拼团订单)
  23. * @return bool
  24. * @throws \app\common\exception\BaseException
  25. * @throws \think\Exception
  26. * @throws \think\exception\DbException
  27. */
  28. public function payment($order, $orderType = OrderTypeEnum::MASTER)
  29. {
  30. // 1. 微信模板消息
  31. $template = SettingModel::getItem('tplMsg', $order['wxapp_id'])['payment'];
  32. if (!$template['is_enable'] || empty($template['template_id'])) {
  33. return false;
  34. }
  35. // 获取可用的formid
  36. if (!$formId = FormIdService::getAvailableFormId($order['user_id'])) {
  37. return false;
  38. }
  39. // 页面链接
  40. $urls = [
  41. OrderTypeEnum::MASTER => 'pages/order/detail',
  42. OrderTypeEnum::SHARING => 'pages/sharing/order/detail/detail',
  43. ];
  44. // 发送模板消息
  45. $status = $this->sendTemplateMessage($order['wxapp_id'], [
  46. 'touser' => $order['user']['open_id'],
  47. 'template_id' => $template['template_id'],
  48. 'page' => $urls[$orderType] . '?order_id=' . $order['order_id'],
  49. 'form_id' => $formId['form_id'],
  50. 'data' => [
  51. // 订单编号
  52. 'keyword1' => $order['order_no'],
  53. // 支付时间
  54. 'keyword2' => date('Y-m-d H:i:s', $order['pay_time']),
  55. // 订单金额
  56. 'keyword3' => $order['pay_price'],
  57. // 商品名称
  58. 'keyword4' => $this->formatGoodsName($order['goods']),
  59. ]
  60. ]);
  61. // 标记formid已使用
  62. $status === true && FormIdService::setIsUsed($formId['id']);
  63. // 2. 商家短信通知
  64. $smsConfig = SettingModel::getItem('sms', $order['wxapp_id']);
  65. $SmsDriver = new SmsDriver($smsConfig);
  66. return $SmsDriver->sendSms('order_pay', ['order_no' => $order['order_no']]);
  67. }
  68. /**
  69. * 后台发货通知
  70. * @param \think\Model $order
  71. * @param int $orderType 订单类型 (10商城订单 20拼团订单)
  72. * @return bool
  73. * @throws \app\common\exception\BaseException
  74. * @throws \think\Exception
  75. * @throws \think\exception\DbException
  76. */
  77. public function delivery($order, $orderType = OrderTypeEnum::MASTER)
  78. {
  79. // 微信模板消息
  80. $template = SettingModel::getItem('tplMsg', $order['wxapp_id'])['delivery'];
  81. if (!$template['is_enable'] || empty($template['template_id'])) {
  82. return false;
  83. }
  84. // 获取可用的formid
  85. if (!$formId = FormIdService::getAvailableFormId($order['user_id'])) {
  86. return false;
  87. }
  88. // 页面链接
  89. $urls = [
  90. OrderTypeEnum::MASTER => 'pages/order/detail',
  91. OrderTypeEnum::SHARING => 'pages/sharing/order/detail/detail',
  92. ];
  93. // 发送模板消息
  94. $status = $this->sendTemplateMessage($order['wxapp_id'], [
  95. 'touser' => $order['user']['open_id'],
  96. 'template_id' => $template['template_id'],
  97. 'page' => $urls[$orderType] . '?order_id=' . $order['order_id'],
  98. 'form_id' => $formId['form_id'],
  99. 'data' => [
  100. // 订单编号
  101. 'keyword1' => $order['order_no'],
  102. // 商品信息
  103. 'keyword2' => $this->formatGoodsName($order['goods']),
  104. // 收货人
  105. 'keyword3' => $order['address']['name'],
  106. // 收货地址
  107. 'keyword4' => implode('', $order['address']['region']) . $order['address']['detail'],
  108. // 物流公司
  109. 'keyword5' => $order['express']['express_name'],
  110. // 物流单号
  111. 'keyword6' => $order['express_no'],
  112. ]
  113. ]);
  114. // 标记formid已使用
  115. $status === true && FormIdService::setIsUsed($formId['id']);
  116. return $status;
  117. }
  118. /**
  119. * 后台售后单状态通知
  120. * @param \think\Model $refund
  121. * @param $order_no
  122. * @param int $orderType 订单类型 (10商城订单 20拼团订单)
  123. * @return bool
  124. * @throws \app\common\exception\BaseException
  125. * @throws \think\Exception
  126. * @throws \think\exception\DbException
  127. */
  128. public function refund($refund, $order_no, $orderType = OrderTypeEnum::MASTER)
  129. {
  130. // 微信模板消息
  131. $template = SettingModel::getItem('tplMsg', $refund['wxapp_id'])['refund'];
  132. if (!$template['is_enable'] || empty($template['template_id'])) {
  133. return false;
  134. }
  135. // 获取可用的formid
  136. if (!$formId = FormIdService::getAvailableFormId($refund['user_id'])) {
  137. return false;
  138. }
  139. // 页面链接
  140. $urls = [
  141. OrderTypeEnum::MASTER => 'pages/order/refund/index',
  142. OrderTypeEnum::SHARING => 'pages/sharing/order/refund/index',
  143. ];
  144. // 发送模板消息
  145. $status = $this->sendTemplateMessage($refund['wxapp_id'], [
  146. 'touser' => $refund['user']['open_id'],
  147. 'template_id' => $template['template_id'],
  148. 'page' => $urls[$orderType],
  149. 'form_id' => $formId['form_id'],
  150. 'data' => [
  151. // 售后类型
  152. 'keyword1' => $refund['type']['text'],
  153. // 状态
  154. 'keyword2' => $refund['status']['text'],
  155. // 订单号
  156. 'keyword3' => $order_no,
  157. // 商品名称
  158. 'keyword4' => $refund['order_goods']['goods_name'],
  159. // 申请时间
  160. 'keyword5' => $refund['create_time'],
  161. // 申请原因
  162. 'keyword6' => $refund['apply_desc'],
  163. ]
  164. ]);
  165. // 标记formid已使用
  166. FormIdService::setIsUsed($formId['id']);
  167. return $status;
  168. }
  169. /**
  170. * 拼团拼单状态通知
  171. * @param \app\common\model\sharing\Active $active
  172. * @param string $status_text
  173. * @return bool
  174. * @throws \app\common\exception\BaseException
  175. * @throws \think\exception\DbException
  176. */
  177. public function sharingActive($active, $status_text)
  178. {
  179. // 微信模板消息
  180. $config = SharingSettingModel::getItem('basic', $active['wxapp_id']);
  181. if (empty($config['tpl_msg_id'])) {
  182. return false;
  183. }
  184. foreach ($active['users'] as $item) {
  185. // 获取可用的formid
  186. if (!$formId = FormIdService::getAvailableFormId($item['user']['user_id'])) {
  187. continue;
  188. }
  189. // 发送模板消息
  190. $this->sendTemplateMessage($active['wxapp_id'], [
  191. 'touser' => $item['user']['open_id'],
  192. 'template_id' => $config['tpl_msg_id'],
  193. 'page' => 'pages/sharing/active/index?active_id=' . $active['active_id'],
  194. 'form_id' => $formId['form_id'],
  195. 'data' => [
  196. // 订单编号
  197. 'keyword1' => $item['sharing_order']['order_no'],
  198. // 商品名称
  199. 'keyword2' => $active['goods']['goods_name'],
  200. // 拼团价格
  201. 'keyword3' => $item['sharing_order']['pay_price'],
  202. // 拼团人数
  203. 'keyword4' => $active['people'],
  204. // 拼团时间
  205. 'keyword5' => $item['create_time'],
  206. // 拼团结果
  207. 'keyword6' => $status_text,
  208. ]
  209. ]);
  210. // 标记formid已使用
  211. FormIdService::setIsUsed($formId['id']);
  212. }
  213. return true;
  214. }
  215. /**
  216. * 分销商提现审核通知
  217. * @param \app\common\model\dealer\Withdraw $withdraw
  218. * @return bool
  219. * @throws \app\common\exception\BaseException
  220. * @throws \think\exception\DbException
  221. */
  222. public function withdraw($withdraw)
  223. {
  224. // 模板消息id
  225. $template = DealerSettingModel::getItem('template_msg', $withdraw['wxapp_id']);
  226. if (empty($template['withdraw_tpl'])) {
  227. return false;
  228. }
  229. // 获取可用的formid
  230. if (!$formId = FormIdService::getAvailableFormId($withdraw['user_id'])) {
  231. return false;
  232. }
  233. // 获取用户信息
  234. $user = UserModel::detail($withdraw['user_id']);
  235. // 发送模板消息
  236. $remark = '无';
  237. if ($withdraw['apply_status'] == 30) {
  238. $remark = $withdraw['reject_reason'];
  239. }
  240. $status = $this->sendTemplateMessage($withdraw['wxapp_id'], [
  241. 'touser' => $user['open_id'],
  242. 'template_id' => $template['withdraw_tpl'],
  243. 'page' => 'pages/dealer/withdraw/list/list',
  244. 'form_id' => $formId['form_id'],
  245. 'data' => [
  246. // 提现时间
  247. 'keyword1' => $withdraw['create_time'],
  248. // 提现方式
  249. 'keyword2' => $withdraw['pay_type']['text'],
  250. // 提现金额
  251. 'keyword3' => $withdraw['money'],
  252. // 提现状态
  253. 'keyword4' => $withdraw->applyStatus[$withdraw['apply_status']],
  254. // 备注
  255. 'keyword5' => $remark,
  256. ]
  257. ]);
  258. // 标记formid已使用
  259. FormIdService::setIsUsed($formId['id']);
  260. return $status;
  261. }
  262. /**
  263. * 分销商入驻审核通知
  264. * @param \app\common\model\dealer\Apply $dealer
  265. * @return bool
  266. * @throws \app\common\exception\BaseException
  267. * @throws \think\exception\DbException
  268. */
  269. public function dealer($dealer)
  270. {
  271. // 模板消息id
  272. $template = DealerSettingModel::getItem('template_msg', $dealer['wxapp_id']);
  273. if (empty($template['apply_tpl'])) {
  274. return false;
  275. }
  276. // 获取可用的formid
  277. if (!$formId = FormIdService::getAvailableFormId($dealer['user_id'])) {
  278. return false;
  279. }
  280. // 获取用户信息
  281. $user = UserModel::detail($dealer['user_id']);
  282. // 发送模板消息
  283. $remark = '分销商入驻审核通知';
  284. if ($dealer['apply_status'] == 30) {
  285. $remark .= "\n\n驳回原因:" . $dealer['reject_reason'];
  286. }
  287. $status = $this->sendTemplateMessage($dealer['wxapp_id'], [
  288. 'touser' => $user['open_id'],
  289. 'template_id' => $template['apply_tpl'],
  290. 'page' => 'pages/dealer/index/index',
  291. 'form_id' => $formId['form_id'],
  292. 'data' => [
  293. // 申请时间
  294. 'keyword1' => $dealer['apply_time'],
  295. // 审核状态
  296. 'keyword2' => $dealer->applyStatus[$dealer['apply_status']],
  297. // 审核时间
  298. 'keyword3' => $dealer['audit_time'],
  299. // 备注信息
  300. 'keyword4' => $remark,
  301. ]
  302. ]);
  303. // 标记formid已使用
  304. FormIdService::setIsUsed($formId['id']);
  305. return $status;
  306. }
  307. /**
  308. * 发送模板消息
  309. * @param $wxappId
  310. * @param $params
  311. * @return bool
  312. * @throws \app\common\exception\BaseException
  313. * @throws \think\exception\DbException
  314. */
  315. private function sendTemplateMessage($wxappId, $params)
  316. {
  317. // 微信模板消息
  318. $wxConfig = WxappModel::getWxappCache($wxappId);
  319. $WxTplMsg = new WxTplMsg($wxConfig['app_id'], $wxConfig['app_secret']);
  320. return $WxTplMsg->sendTemplateMessage($params);
  321. }
  322. /**
  323. * 格式化商品名称
  324. * @param $goodsData
  325. * @return string
  326. */
  327. private function formatGoodsName($goodsData)
  328. {
  329. $str = '';
  330. foreach ($goodsData as $goods) {
  331. $str .= $goods['goods_name'] . ' ';
  332. }
  333. return $str;
  334. }
  335. }