SmsMessageService.php 6.9 KB

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