1
0

SmsMessageService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. use think\facade\Log;
  22. /**
  23. * 短信服务
  24. * Class SmsMessageService
  25. * @package app\common\service
  26. */
  27. class SmsMessageService
  28. {
  29. protected $notice;
  30. protected $smsLog;
  31. public function send($params)
  32. {
  33. try {
  34. // 通知设置
  35. $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
  36. // 替换通知模板参数
  37. $content = $this->contentFormat($noticeSetting, $params);
  38. // 添加短信记录
  39. $this->smsLog = $this->addSmsLog($params, $content);
  40. // 添加通知记录
  41. $this->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::SMS, $content);
  42. // 发送短信
  43. $smsDriver = new SmsDriver();
  44. if(!is_null($smsDriver->getError())) {
  45. throw new \Exception($smsDriver->getError());
  46. }
  47. $result = $smsDriver->send($params['params']['mobile'], [
  48. 'template_id' => $noticeSetting['sms_notice']['template_id'],
  49. 'params' => $this->setSmsParams($noticeSetting, $params)
  50. ]);
  51. if ($result === false) {
  52. // 发送失败更新短信记录
  53. $this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_FAIL, $smsDriver->getError());
  54. throw new \Exception($smsDriver->getError());
  55. }
  56. // 发送成功更新短信记录
  57. $this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_SUCCESS, $result);
  58. return true;
  59. } catch (\Exception $e) {
  60. throw new \Exception($e->getMessage());
  61. }
  62. }
  63. public function sendBusiness($params)
  64. {
  65. try {
  66. // 通知设置
  67. $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
  68. // 替换通知模板参数
  69. $content = $this->contentFormat($noticeSetting, $params);
  70. // 添加通知记录
  71. $this->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::SMS, $content);
  72. // 发送短信
  73. $smsDriver = new SmsDriver();
  74. if(!is_null($smsDriver->getError())) {
  75. throw new \Exception($smsDriver->getError());
  76. }
  77. Log::info('SmsMessageService-sendBusiness:'.json_encode([$noticeSetting,$params,$this->setSmsParams($noticeSetting, $params)]));
  78. $result = $smsDriver->send($params['params']['mobile'], [
  79. 'template_id' => $noticeSetting['sms_notice']['template_id'],
  80. 'params' => $this->setSmsParams($noticeSetting, $params)
  81. ]);
  82. if ($result === false) {
  83. throw new \Exception($smsDriver->getError());
  84. }
  85. return true;
  86. } catch (\Exception $e) {
  87. Log::info('SmsMessageService-sendBusiness:'.$e->getMessage());
  88. throw new \Exception($e->getMessage());
  89. }
  90. }
  91. /**
  92. * @notes 格式化消息内容
  93. * @param $noticeSetting
  94. * @param $params
  95. * @return array|mixed|string|string[]
  96. * @author 段誉
  97. * @date 2022/9/15 16:24
  98. */
  99. public function contentFormat($noticeSetting, $params)
  100. {
  101. $content = $noticeSetting['sms_notice']['content'];
  102. foreach($params['params'] as $k => $v) {
  103. $search = '${' . $k . '}';
  104. $content = str_replace($search, $v, $content);
  105. }
  106. return $content;
  107. }
  108. /**
  109. * @notes 添加短信记录
  110. * @param $params
  111. * @param $content
  112. * @return SmsLog|\think\Model
  113. * @author 段誉
  114. * @date 2022/9/15 16:24
  115. */
  116. public function addSmsLog($params, $content)
  117. {
  118. $data = [
  119. 'scene_id' => $params['scene_id'],
  120. 'mobile' => $params['params']['mobile'],
  121. 'content' => $content,
  122. 'code' => $params['params']['code'] ?? '',
  123. 'send_status' => SmsEnum::SEND_ING,
  124. 'send_time' => time(),
  125. ];
  126. return SmsLog::create($data);
  127. }
  128. /**
  129. * @notes 处理腾讯云短信参数
  130. * @param $noticeSetting
  131. * @param $params
  132. * @return array|mixed
  133. * @author 段誉
  134. * @date 2022/9/15 16:25
  135. */
  136. public function setSmsParams($noticeSetting, $params)
  137. {
  138. $defaultEngine = ConfigService::get('sms', 'engine', false);
  139. // 阿里云 且是 验证码类型
  140. if($defaultEngine != 'TENCENT' && in_array($params['scene_id'], NoticeEnum::SMS_SCENE)) {
  141. return ['code' => $params['params']['code']];
  142. }
  143. if($defaultEngine != 'TENCENT') {
  144. return $params['params'];
  145. }
  146. //腾讯云特殊处理
  147. $arr = [];
  148. $content = $noticeSetting['sms_notice']['content'];
  149. foreach ($params['params'] as $item => $val) {
  150. $search = '${' . $item . '}';
  151. if(strpos($content, $search) !== false && !in_array($item, $arr)) {
  152. //arr => 获的数组[nickname, order_sn] //顺序可能是乱的
  153. $arr[] = $item;
  154. }
  155. }
  156. //arr2 => 获得数组[nickname, order_sn] //调整好顺序的变量名数组
  157. $arr2 = [];
  158. if (!empty($arr)) {
  159. foreach ($arr as $v) {
  160. $key = strpos($content, $v);
  161. $arr2[$key] = $v;
  162. }
  163. }
  164. //格式化 arr2 => 以小到大的排序的数组
  165. ksort($arr2);
  166. $arr3 = array_values($arr2);
  167. //arr4 => 获取到变量数组的对应的值 [mofung, 123456789]
  168. $arr4 = [];
  169. foreach ($arr3 as $v2) {
  170. if(isset($params['params'][$v2])) {
  171. $arr4[] = $params['params'][$v2] . "";
  172. }
  173. }
  174. return $arr4;
  175. }
  176. /**
  177. * @notes 更新短信记录
  178. * @param $id
  179. * @param $status
  180. * @param $result
  181. * @author 段誉
  182. * @date 2022/9/15 16:25
  183. */
  184. public function updateSmsLog($id, $status, $result)
  185. {
  186. SmsLog::update([
  187. 'id' => $id,
  188. 'send_status' => $status,
  189. 'results' => json_encode($result, JSON_UNESCAPED_UNICODE)
  190. ]);
  191. }
  192. }