SmsMessageService.php 5.8 KB

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