| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- namespace app\common\service;
- use app\common\model\User as UserModel;
- use app\common\model\Wxapp as WxappModel;
- use app\common\model\Setting as SettingModel;
- use app\common\model\dealer\Setting as DealerSettingModel;
- use app\common\model\sharing\Setting as SharingSettingModel;
- use app\common\service\wxapp\FormId as FormIdService;
- use app\common\enum\OrderType as OrderTypeEnum;
- use app\common\library\wechat\WxTplMsg;
- use app\common\library\sms\Driver as SmsDriver;
- /**
- * 消息通知服务
- * Class Message
- * @package app\common\service
- */
- class Message
- {
- /**
- * 订单支付成功后通知
- * @param \think\Model $order
- * @param int $orderType 订单类型 (10商城订单 20拼团订单)
- * @return bool
- * @throws \app\common\exception\BaseException
- * @throws \think\Exception
- * @throws \think\exception\DbException
- */
- public function payment($order, $orderType = OrderTypeEnum::MASTER)
- {
- // 1. 微信模板消息
- $template = SettingModel::getItem('tplMsg', $order['wxapp_id'])['payment'];
- if (!$template['is_enable'] || empty($template['template_id'])) {
- return false;
- }
- // 获取可用的formid
- if (!$formId = FormIdService::getAvailableFormId($order['user_id'])) {
- return false;
- }
- // 页面链接
- $urls = [
- OrderTypeEnum::MASTER => 'pages/order/detail',
- OrderTypeEnum::SHARING => 'pages/sharing/order/detail/detail',
- ];
- // 发送模板消息
- $status = $this->sendTemplateMessage($order['wxapp_id'], [
- 'touser' => $order['user']['open_id'],
- 'template_id' => $template['template_id'],
- 'page' => $urls[$orderType] . '?order_id=' . $order['order_id'],
- 'form_id' => $formId['form_id'],
- 'data' => [
- // 订单编号
- 'keyword1' => $order['order_no'],
- // 支付时间
- 'keyword2' => date('Y-m-d H:i:s', $order['pay_time']),
- // 订单金额
- 'keyword3' => $order['pay_price'],
- // 商品名称
- 'keyword4' => $this->formatGoodsName($order['goods']),
- ]
- ]);
- // 标记formid已使用
- $status === true && FormIdService::setIsUsed($formId['id']);
- // 2. 商家短信通知
- $smsConfig = SettingModel::getItem('sms', $order['wxapp_id']);
- $SmsDriver = new SmsDriver($smsConfig);
- return $SmsDriver->sendSms('order_pay', ['order_no' => $order['order_no']]);
- }
- /**
- * 后台发货通知
- * @param \think\Model $order
- * @param int $orderType 订单类型 (10商城订单 20拼团订单)
- * @return bool
- * @throws \app\common\exception\BaseException
- * @throws \think\Exception
- * @throws \think\exception\DbException
- */
- public function delivery($order, $orderType = OrderTypeEnum::MASTER)
- {
- // 微信模板消息
- $template = SettingModel::getItem('tplMsg', $order['wxapp_id'])['delivery'];
- if (!$template['is_enable'] || empty($template['template_id'])) {
- return false;
- }
- // 获取可用的formid
- if (!$formId = FormIdService::getAvailableFormId($order['user_id'])) {
- return false;
- }
- // 页面链接
- $urls = [
- OrderTypeEnum::MASTER => 'pages/order/detail',
- OrderTypeEnum::SHARING => 'pages/sharing/order/detail/detail',
- ];
- // 发送模板消息
- $status = $this->sendTemplateMessage($order['wxapp_id'], [
- 'touser' => $order['user']['open_id'],
- 'template_id' => $template['template_id'],
- 'page' => $urls[$orderType] . '?order_id=' . $order['order_id'],
- 'form_id' => $formId['form_id'],
- 'data' => [
- // 订单编号
- 'keyword1' => $order['order_no'],
- // 商品信息
- 'keyword2' => $this->formatGoodsName($order['goods']),
- // 收货人
- 'keyword3' => $order['address']['name'],
- // 收货地址
- 'keyword4' => implode('', $order['address']['region']) . $order['address']['detail'],
- // 物流公司
- 'keyword5' => $order['express']['express_name'],
- // 物流单号
- 'keyword6' => $order['express_no'],
- ]
- ]);
- // 标记formid已使用
- $status === true && FormIdService::setIsUsed($formId['id']);
- return $status;
- }
- /**
- * 后台售后单状态通知
- * @param \think\Model $refund
- * @param $order_no
- * @param int $orderType 订单类型 (10商城订单 20拼团订单)
- * @return bool
- * @throws \app\common\exception\BaseException
- * @throws \think\Exception
- * @throws \think\exception\DbException
- */
- public function refund($refund, $order_no, $orderType = OrderTypeEnum::MASTER)
- {
- // 微信模板消息
- $template = SettingModel::getItem('tplMsg', $refund['wxapp_id'])['refund'];
- if (!$template['is_enable'] || empty($template['template_id'])) {
- return false;
- }
- // 获取可用的formid
- if (!$formId = FormIdService::getAvailableFormId($refund['user_id'])) {
- return false;
- }
- // 页面链接
- $urls = [
- OrderTypeEnum::MASTER => 'pages/order/refund/index',
- OrderTypeEnum::SHARING => 'pages/sharing/order/refund/index',
- ];
- // 发送模板消息
- $status = $this->sendTemplateMessage($refund['wxapp_id'], [
- 'touser' => $refund['user']['open_id'],
- 'template_id' => $template['template_id'],
- 'page' => $urls[$orderType],
- 'form_id' => $formId['form_id'],
- 'data' => [
- // 售后类型
- 'keyword1' => $refund['type']['text'],
- // 状态
- 'keyword2' => $refund['status']['text'],
- // 订单号
- 'keyword3' => $order_no,
- // 商品名称
- 'keyword4' => $refund['order_goods']['goods_name'],
- // 申请时间
- 'keyword5' => $refund['create_time'],
- // 申请原因
- 'keyword6' => $refund['apply_desc'],
- ]
- ]);
- // 标记formid已使用
- FormIdService::setIsUsed($formId['id']);
- return $status;
- }
- /**
- * 拼团拼单状态通知
- * @param \app\common\model\sharing\Active $active
- * @param string $status_text
- * @return bool
- * @throws \app\common\exception\BaseException
- * @throws \think\exception\DbException
- */
- public function sharingActive($active, $status_text)
- {
- // 微信模板消息
- $config = SharingSettingModel::getItem('basic', $active['wxapp_id']);
- if (empty($config['tpl_msg_id'])) {
- return false;
- }
- foreach ($active['users'] as $item) {
- // 获取可用的formid
- if (!$formId = FormIdService::getAvailableFormId($item['user']['user_id'])) {
- continue;
- }
- // 发送模板消息
- $this->sendTemplateMessage($active['wxapp_id'], [
- 'touser' => $item['user']['open_id'],
- 'template_id' => $config['tpl_msg_id'],
- 'page' => 'pages/sharing/active/index?active_id=' . $active['active_id'],
- 'form_id' => $formId['form_id'],
- 'data' => [
- // 订单编号
- 'keyword1' => $item['sharing_order']['order_no'],
- // 商品名称
- 'keyword2' => $active['goods']['goods_name'],
- // 拼团价格
- 'keyword3' => $item['sharing_order']['pay_price'],
- // 拼团人数
- 'keyword4' => $active['people'],
- // 拼团时间
- 'keyword5' => $item['create_time'],
- // 拼团结果
- 'keyword6' => $status_text,
- ]
- ]);
- // 标记formid已使用
- FormIdService::setIsUsed($formId['id']);
- }
- return true;
- }
- /**
- * 分销商提现审核通知
- * @param \app\common\model\dealer\Withdraw $withdraw
- * @return bool
- * @throws \app\common\exception\BaseException
- * @throws \think\exception\DbException
- */
- public function withdraw($withdraw)
- {
- // 模板消息id
- $template = DealerSettingModel::getItem('template_msg', $withdraw['wxapp_id']);
- if (empty($template['withdraw_tpl'])) {
- return false;
- }
- // 获取可用的formid
- if (!$formId = FormIdService::getAvailableFormId($withdraw['user_id'])) {
- return false;
- }
- // 获取用户信息
- $user = UserModel::detail($withdraw['user_id']);
- // 发送模板消息
- $remark = '无';
- if ($withdraw['apply_status'] == 30) {
- $remark = $withdraw['reject_reason'];
- }
- $status = $this->sendTemplateMessage($withdraw['wxapp_id'], [
- 'touser' => $user['open_id'],
- 'template_id' => $template['withdraw_tpl'],
- 'page' => 'pages/dealer/withdraw/list/list',
- 'form_id' => $formId['form_id'],
- 'data' => [
- // 提现时间
- 'keyword1' => $withdraw['create_time'],
- // 提现方式
- 'keyword2' => $withdraw['pay_type']['text'],
- // 提现金额
- 'keyword3' => $withdraw['money'],
- // 提现状态
- 'keyword4' => $withdraw->applyStatus[$withdraw['apply_status']],
- // 备注
- 'keyword5' => $remark,
- ]
- ]);
- // 标记formid已使用
- FormIdService::setIsUsed($formId['id']);
- return $status;
- }
- /**
- * 分销商入驻审核通知
- * @param \app\common\model\dealer\Apply $dealer
- * @return bool
- * @throws \app\common\exception\BaseException
- * @throws \think\exception\DbException
- */
- public function dealer($dealer)
- {
- // 模板消息id
- $template = DealerSettingModel::getItem('template_msg', $dealer['wxapp_id']);
- if (empty($template['apply_tpl'])) {
- return false;
- }
- // 获取可用的formid
- if (!$formId = FormIdService::getAvailableFormId($dealer['user_id'])) {
- return false;
- }
- // 获取用户信息
- $user = UserModel::detail($dealer['user_id']);
- // 发送模板消息
- $remark = '分销商入驻审核通知';
- if ($dealer['apply_status'] == 30) {
- $remark .= "\n\n驳回原因:" . $dealer['reject_reason'];
- }
- $status = $this->sendTemplateMessage($dealer['wxapp_id'], [
- 'touser' => $user['open_id'],
- 'template_id' => $template['apply_tpl'],
- 'page' => 'pages/dealer/index/index',
- 'form_id' => $formId['form_id'],
- 'data' => [
- // 申请时间
- 'keyword1' => $dealer['apply_time'],
- // 审核状态
- 'keyword2' => $dealer->applyStatus[$dealer['apply_status']],
- // 审核时间
- 'keyword3' => $dealer['audit_time'],
- // 备注信息
- 'keyword4' => $remark,
- ]
- ]);
- // 标记formid已使用
- FormIdService::setIsUsed($formId['id']);
- return $status;
- }
- /**
- * 发送模板消息
- * @param $wxappId
- * @param $params
- * @return bool
- * @throws \app\common\exception\BaseException
- * @throws \think\exception\DbException
- */
- private function sendTemplateMessage($wxappId, $params)
- {
- // 微信模板消息
- $wxConfig = WxappModel::getWxappCache($wxappId);
- $WxTplMsg = new WxTplMsg($wxConfig['app_id'], $wxConfig['app_secret']);
- return $WxTplMsg->sendTemplateMessage($params);
- }
- /**
- * 格式化商品名称
- * @param $goodsData
- * @return string
- */
- private function formatGoodsName($goodsData)
- {
- $str = '';
- foreach ($goodsData as $goods) {
- $str .= $goods['goods_name'] . ' ';
- }
- return $str;
- }
- }
|