| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\common\service\sms;
- use app\common\enum\NoticeEnum;
- use app\common\enum\SmsEnum;
- use app\common\logic\NoticeLogic;
- use app\common\model\NoticeSetting;
- use app\common\model\SmsLog;
- use app\common\service\ConfigService;
- use think\facade\Log;
- /**
- * 短信服务
- * Class SmsMessageService
- * @package app\common\service
- */
- class SmsMessageService
- {
- protected $notice;
- protected $smsLog;
- public function send($params)
- {
- try {
- // 通知设置
- $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
- // 添加短信记录
- $content = $this->contentFormat($noticeSetting, $params);
- $this->smsLog = $this->addSmsLog($params, $content);
- // 添加通知记录
- $this->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::SMS, $content);
- // 发送短信
- $smsDriver = new SmsDriver();
- if(!is_null($smsDriver->getError())) {
- throw new \Exception($smsDriver->getError());
- }
- $result = $smsDriver->send($params['params']['mobile'], [
- 'template_id' => $noticeSetting['sms_notice']['template_id'],
- 'params' => $this->setSmsParams($noticeSetting, $params)
- ]);
- outFileLog($result,'noticeLogic','短信通知$result');
- if ($result === false) {
- // 发送失败更新短信记录
- $this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_FAIL, $smsDriver->getError());
- throw new \Exception($smsDriver->getError());
- }
- // 发送成功更新短信记录
- $this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_SUCCESS, $result);
- return true;
- } catch (\Exception $e) {
- // throw new \Exception($e->getMessage());
-
- Log::write("短信消息发送失败:{$e->__toString()}");
- return true;
- }
- }
- /**
- * @notes 格式化消息(替换内容中的变量占位符)
- * @param $noticeSetting
- * @param $params
- * @return array|string|string[]
- * @author Tab
- * @date 2021/8/19 10:55
- */
- public function contentFormat($noticeSetting, $params)
- {
- $content = $noticeSetting['sms_notice']['content'];
- foreach($params['params'] as $k => $v) {
- $search = '${' . $k . '}';
- $content = str_replace($search, $v, $content);
- }
- return $content;
- }
- /**
- * @notes 添加短信记录
- * @param $params
- * @param $content
- * @return SmsLog|\think\Model
- * @author Tab
- * @date 2021/8/19 11:11
- */
- public function addSmsLog($params, $content)
- {
- $data = [
- 'scene_id' => $params['scene_id'],
- 'mobile' => $params['params']['mobile'],
- 'content' => $content,
- 'code' => $params['params']['code'] ?? '',
- 'send_status' => SmsEnum::SEND_ING,
- 'send_time' => time(),
- ];
- return SmsLog::create($data);
- }
- /**
- * @notes 腾讯云参数处理
- * @param $noticeSetting
- * @param $params
- * @return mixed
- * @author Tab
- * @date 2021/8/19 15:55
- */
- public function setSmsParams($noticeSetting, $params)
- {
- $defaultEngine = ConfigService::get('sms', 'engine', false);
- // 阿里云 且是 验证码类型
- if($defaultEngine != 'TENCENT' && in_array($params['scene_id'], NoticeEnum::SMS_SCENE)) {
- return ['code' => $params['params']['code']];
- }
- if($defaultEngine != 'TENCENT') {
- return $params['params'];
- }
- //腾讯云特殊处理
- $arr = [];
- $content = $noticeSetting['sms_notice']['content'];
- foreach ($params['params'] as $item => $val) {
- $search = '${' . $item . '}';
- if(strpos($content, $search) !== false && !in_array($item, $arr)) {
- //arr => 获的数组[nickname, order_sn] //顺序可能是乱的
- $arr[] = $item;
- }
- }
- //arr2 => 获得数组[nickname, order_sn] //调整好顺序的变量名数组
- $arr2 = [];
- if (!empty($arr)) {
- foreach ($arr as $v) {
- $key = strpos($content, $v);
- $arr2[$key] = $v;
- }
- }
- //格式化 arr2 => 以小到大的排序的数组
- ksort($arr2);
- $arr3 = array_values($arr2);
- //arr4 => 获取到变量数组的对应的值 [mofung, 123456789]
- $arr4 = [];
- foreach ($arr3 as $v2) {
- if(isset($params['params'][$v2])) {
- $arr4[] = $params['params'][$v2] . "";
- }
- }
- return $arr4;
- }
- /**
- * @notes 更新短信记录
- * @param $id
- * @param $status
- * @param $result
- * @author Tab
- * @date 2021/8/19 16:07
- */
- public function updateSmsLog($id, $status, $result)
- {
- SmsLog::update([
- 'id' => $id,
- 'send_status' => $status,
- 'results' => json_encode($result, JSON_UNESCAPED_UNICODE)
- ]);
- }
- }
|