SmsMessageService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service\sms;
  20. use app\common\enum\NoticeEnum;
  21. use app\common\enum\SmsEnum;
  22. use app\common\logic\NoticeLogic;
  23. use app\common\model\NoticeSetting;
  24. use app\common\model\SmsLog;
  25. use app\common\service\ConfigService;
  26. use think\facade\Log;
  27. /**
  28. * 短信服务
  29. * Class SmsMessageService
  30. * @package app\common\service
  31. */
  32. class SmsMessageService
  33. {
  34. protected $notice;
  35. protected $smsLog;
  36. public function send($params)
  37. {
  38. try {
  39. // 通知设置
  40. $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
  41. // 添加短信记录
  42. $content = $this->contentFormat($noticeSetting, $params);
  43. $this->smsLog = $this->addSmsLog($params, $content);
  44. // 添加通知记录
  45. $this->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::SMS, $content);
  46. // 发送短信
  47. $smsDriver = new SmsDriver();
  48. if(!is_null($smsDriver->getError())) {
  49. throw new \Exception($smsDriver->getError());
  50. }
  51. $result = $smsDriver->send($params['params']['mobile'], [
  52. 'template_id' => $noticeSetting['sms_notice']['template_id'],
  53. 'params' => $this->setSmsParams($noticeSetting, $params)
  54. ]);
  55. if ($result === false) {
  56. // 发送失败更新短信记录
  57. $this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_FAIL, $smsDriver->getError());
  58. throw new \Exception($smsDriver->getError());
  59. }
  60. // 发送成功更新短信记录
  61. $this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_SUCCESS, $result);
  62. return true;
  63. } catch (\Exception $e) {
  64. // throw new \Exception($e->getMessage());
  65. Log::write("短信消息发送失败:{$e->__toString()}");
  66. return true;
  67. }
  68. }
  69. /**
  70. * @notes 格式化消息(替换内容中的变量占位符)
  71. * @param $noticeSetting
  72. * @param $params
  73. * @return array|string|string[]
  74. * @author Tab
  75. * @date 2021/8/19 10:55
  76. */
  77. public function contentFormat($noticeSetting, $params)
  78. {
  79. $content = $noticeSetting['sms_notice']['content'];
  80. foreach($params['params'] as $k => $v) {
  81. $search = '${' . $k . '}';
  82. $content = str_replace($search, $v, $content);
  83. }
  84. return $content;
  85. }
  86. /**
  87. * @notes 添加短信记录
  88. * @param $params
  89. * @param $content
  90. * @return SmsLog|\think\Model
  91. * @author Tab
  92. * @date 2021/8/19 11:11
  93. */
  94. public function addSmsLog($params, $content)
  95. {
  96. $data = [
  97. 'scene_id' => $params['scene_id'],
  98. 'mobile' => $params['params']['mobile'],
  99. 'content' => $content,
  100. 'code' => $params['params']['code'] ?? '',
  101. 'send_status' => SmsEnum::SEND_ING,
  102. 'send_time' => time(),
  103. ];
  104. return SmsLog::create($data);
  105. }
  106. /**
  107. * @notes 腾讯云参数处理
  108. * @param $noticeSetting
  109. * @param $params
  110. * @return mixed
  111. * @author Tab
  112. * @date 2021/8/19 15:55
  113. */
  114. public function setSmsParams($noticeSetting, $params)
  115. {
  116. $defaultEngine = ConfigService::get('sms', 'engine', false);
  117. // 阿里云 且是 验证码类型
  118. if($defaultEngine != 'TENCENT' && in_array($params['scene_id'], NoticeEnum::SMS_SCENE)) {
  119. return ['code' => $params['params']['code']];
  120. }
  121. if($defaultEngine != 'TENCENT') {
  122. return $params['params'];
  123. }
  124. //腾讯云特殊处理
  125. $arr = [];
  126. $content = $noticeSetting['sms_notice']['content'];
  127. foreach ($params['params'] as $item => $val) {
  128. $search = '${' . $item . '}';
  129. if(strpos($content, $search) !== false && !in_array($item, $arr)) {
  130. //arr => 获的数组[nickname, order_sn] //顺序可能是乱的
  131. $arr[] = $item;
  132. }
  133. }
  134. //arr2 => 获得数组[nickname, order_sn] //调整好顺序的变量名数组
  135. $arr2 = [];
  136. if (!empty($arr)) {
  137. foreach ($arr as $v) {
  138. $key = strpos($content, $v);
  139. $arr2[$key] = $v;
  140. }
  141. }
  142. //格式化 arr2 => 以小到大的排序的数组
  143. ksort($arr2);
  144. $arr3 = array_values($arr2);
  145. //arr4 => 获取到变量数组的对应的值 [mofung, 123456789]
  146. $arr4 = [];
  147. foreach ($arr3 as $v2) {
  148. if(isset($params['params'][$v2])) {
  149. $arr4[] = $params['params'][$v2] . "";
  150. }
  151. }
  152. return $arr4;
  153. }
  154. /**
  155. * @notes 更新短信记录
  156. * @param $id
  157. * @param $status
  158. * @param $result
  159. * @author Tab
  160. * @date 2021/8/19 16:07
  161. */
  162. public function updateSmsLog($id, $status, $result)
  163. {
  164. SmsLog::update([
  165. 'id' => $id,
  166. 'send_status' => $status,
  167. 'results' => json_encode($result, JSON_UNESCAPED_UNICODE)
  168. ]);
  169. }
  170. }