SmsMessageServer.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server;
  20. use app\common\enum\NoticeEnum;
  21. use app\common\enum\SmsEnum;
  22. use app\common\logic\MessageNoticeLogic;
  23. use app\common\model\NoticeSetting;
  24. use app\common\model\SmsLog;
  25. use app\common\server\sms\Driver;
  26. use think\facade\Log;
  27. class SmsMessageServer
  28. {
  29. protected $sms_log;
  30. protected $notice;
  31. public function send($params)
  32. {
  33. try{
  34. // 手机号为空 直接返回
  35. if (empty($params['mobile'])) {
  36. return true;
  37. }
  38. //场景对应短信模板信息
  39. $scene_config = NoticeSetting::where(['scene' => $params['scene']])->findOrEmpty();
  40. //增加短信记录
  41. $content = $this->getContentInfo($scene_config, $params);
  42. $code = $this->getCodeInfo($params['scene'], $scene_config, $params['params']);
  43. $this->sms_log = $this->addSmsLog($params, $content, $code);
  44. //增加通知记录
  45. $this->notice = MessageNoticeLogic::addNoticeLog($params, $scene_config['sms_notice'], NoticeEnum::SMS_NOTICE, $content);
  46. //发送短信
  47. $SmsDriver = new Driver();
  48. $res = $SmsDriver->send($params['mobile'], [
  49. 'template_id' => $scene_config['sms_notice']['template_code'],
  50. 'param' => $this->setSmsParams($scene_config, $params),
  51. ]);
  52. if (false === $res) {
  53. $this->updateSmsLog($this->sms_log['id'], SmsEnum::SEND_FAIL, $SmsDriver->getError());
  54. throw new \Exception($SmsDriver->getError());
  55. }
  56. $this->updateSmsLog($this->sms_log['id'], SmsEnum::SEND_SUCCESS, $res);
  57. return true;
  58. } catch (\Throwable $e) {
  59. if (!empty($this->sms_log['id'])) {
  60. $this->updateSmsLog($this->sms_log['id'], SmsEnum::SEND_FAIL, $e->getMessage());
  61. }
  62. if (!empty($this->notice['id'])) {
  63. MessageNoticeLogic::updateNotice($this->notice['id'], $e->getMessage());
  64. }
  65. Log::write($e->__toString(), 'sms_send_error');
  66. return $e->getMessage();
  67. }
  68. }
  69. public function addSmsLog($params, $content, $code)
  70. {
  71. return SmsLog::create([
  72. 'message_key' => $params['scene'],
  73. 'mobile' => $params['mobile'],
  74. 'content' => $content,
  75. 'code' => $code,
  76. 'send_status' => SmsEnum::SEND_ING,
  77. 'send_time' => time(),
  78. ]);
  79. }
  80. public function updateSmsLog($id, $status, $result)
  81. {
  82. SmsLog::update([
  83. 'send_status' => $status,
  84. 'results' => json_encode($result, JSON_UNESCAPED_UNICODE)
  85. ],['id' => $id]);
  86. }
  87. //发送内容(替换设置好的模板变量)
  88. public function getContentInfo($scene_config, $params)
  89. {
  90. $content = $scene_config['sms_notice']['content'];
  91. foreach ($params['params'] as $item => $val) {
  92. $search_replace = '{' . $item . '}';
  93. $content = str_replace($search_replace, $val, $content);
  94. }
  95. return $content;
  96. }
  97. //短信验证码
  98. public function getCodeInfo($scene, $scene_config, $sms_params)
  99. {
  100. $code = '';
  101. if (in_array($scene, NoticeEnum::NOTICE_NEED_CODE)) {
  102. $code = array_intersect_key($sms_params, $scene_config['variable']);
  103. if ($code) {
  104. return array_shift($code);
  105. }
  106. }
  107. return $code;
  108. }
  109. /**
  110. * @notes 腾讯云参数设置
  111. * @param $scene_config
  112. * @param $params
  113. * @return array|mixed
  114. * @author 段誉
  115. * @date 2021/8/4 14:09
  116. */
  117. public function setSmsParams($scene_config, $params)
  118. {
  119. $sms_driver = ConfigServer::get('sms_driver', 'default', '');
  120. if ($sms_driver != 'tc') {
  121. return $params['params'];
  122. }
  123. //腾讯云特殊处理
  124. $arr = [];
  125. $content = $scene_config['sms_notice']['content'];
  126. foreach ($params['params'] as $item => $val) {
  127. $search = '{' . $item . '}';
  128. if(strpos($content, $search) !== false
  129. && !in_array($item, $arr)
  130. ) {
  131. //arr => 获的数组[nickname, order_sn] //顺序可能是乱的
  132. $arr[] = $item;
  133. }
  134. }
  135. //arr2 => 获得数组[nickname, order_sn] //调整好顺序的变量名数组
  136. $arr2 = [];
  137. if (!empty($arr)) {
  138. foreach ($arr as $v) {
  139. $key = strpos($content, $v);
  140. $arr2[$key] = $v;
  141. }
  142. }
  143. //格式化 arr2 => 以小到大的排序的数组
  144. ksort($arr2);
  145. $arr3 = array_values($arr2);
  146. //arr4 => 获取到变量数组的对应的值 [mofung, 123456789]
  147. $arr4 = [];
  148. foreach ($arr3 as $v2) {
  149. if(isset($params['params'][$v2])) {
  150. $arr4[] = $params['params'][$v2];
  151. }
  152. }
  153. return $arr4;
  154. }
  155. }