Message.php 12 KB

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