SmsDriver.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\enum\YesNoEnum;
  18. use app\common\model\Notice;
  19. use app\common\model\notice\SmsLog;
  20. use app\common\service\ConfigService;
  21. /**
  22. * 短信驱动
  23. * Class SmsDriver
  24. * @package app\common\service\sms
  25. */
  26. class SmsDriver
  27. {
  28. /**
  29. * 错误信息
  30. * @var
  31. */
  32. protected $error = null;
  33. /**
  34. * 默认短信引擎
  35. * @var
  36. */
  37. protected $defaultEngine;
  38. /**
  39. * 短信引擎
  40. * @var
  41. */
  42. protected $engine;
  43. /**
  44. * 架构方法
  45. * SmsDriver constructor.
  46. */
  47. public function __construct()
  48. {
  49. // 初始化
  50. $this->initialize();
  51. }
  52. /**
  53. * @notes 初始化
  54. * @return bool
  55. * @author 段誉
  56. * @date 2022/9/15 16:29
  57. */
  58. public function initialize()
  59. {
  60. try {
  61. $defaultEngine = ConfigService::get('sms', 'engine', false);
  62. if($defaultEngine === false) {
  63. throw new \Exception('请开启短信配置');
  64. }
  65. $this->defaultEngine = $defaultEngine;
  66. $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst(strtolower($defaultEngine)) . 'Sms';
  67. if (!class_exists($classSpace)) {
  68. throw new \Exception('没有相应的短信驱动类');
  69. }
  70. $engineConfig = ConfigService::get('sms', strtolower($defaultEngine), false);
  71. if($engineConfig === false) {
  72. throw new \Exception($defaultEngine . '未配置');
  73. }
  74. if ($engineConfig['status'] != 1) {
  75. throw new \Exception('短信服务未开启');
  76. }
  77. $this->engine = new $classSpace($engineConfig);
  78. if(!is_null($this->engine->getError())) {
  79. throw new \Exception($this->engine->getError());
  80. }
  81. return true;
  82. } catch (\Exception $e) {
  83. $this->error = $e->getMessage();
  84. return false;
  85. }
  86. }
  87. /**
  88. * @notes 获取错误信息
  89. * @return null
  90. * @author 段誉
  91. * @date 2022/9/15 16:29
  92. */
  93. public function getError()
  94. {
  95. return $this->error;
  96. }
  97. /**
  98. * @notes 发送短信
  99. * @param $mobile
  100. * @param $data
  101. * @return false
  102. * @author 段誉
  103. * @date 2022/9/15 16:29
  104. */
  105. public function send($mobile, $data)
  106. {
  107. try {
  108. // 发送频率限制
  109. $this->sendLimit($mobile);
  110. // 开始发送
  111. $result = $this->engine
  112. ->setMobile($mobile)
  113. ->setTemplateId($data['template_id'])
  114. ->setTemplateParams($data['params'])
  115. ->send();
  116. if(false === $result) {
  117. throw new \Exception($this->engine->getError());
  118. }
  119. return $result;
  120. } catch(\Exception $e) {
  121. $this->error = $e->getMessage();
  122. return false;
  123. }
  124. }
  125. /**
  126. * @notes 发送频率限制
  127. * @param $mobile
  128. * @throws \Exception
  129. * @author 段誉
  130. * @date 2022/9/15 16:29
  131. */
  132. public function sendLimit($mobile)
  133. {
  134. $smsLog = SmsLog::where([
  135. ['mobile', '=', $mobile],
  136. ['send_status', '=', SmsEnum::SEND_SUCCESS],
  137. ['scene_id', 'in', NoticeEnum::SMS_SCENE],
  138. ])
  139. ->order('send_time', 'desc')
  140. ->findOrEmpty()
  141. ->toArray();
  142. if(!empty($smsLog) && ($smsLog['send_time'] > time() - 60)) {
  143. throw new \Exception('同一手机号1分钟只能发送1条短信');
  144. }
  145. }
  146. /**
  147. * @notes 校验手机验证码
  148. * @param $mobile
  149. * @param $code
  150. * @return bool
  151. * @author 段誉
  152. * @date 2022/9/15 16:29
  153. */
  154. public function verify($mobile, $code, $sceneId = 0)
  155. {
  156. $where = [
  157. ['mobile', '=', $mobile],
  158. ['send_status', '=', SmsEnum::SEND_SUCCESS],
  159. ['scene_id', 'in', NoticeEnum::SMS_SCENE],
  160. ['is_verify', '=', YesNoEnum::NO],
  161. ];
  162. if (!empty($sceneId)) {
  163. $where[] = ['scene_id', '=', $sceneId];
  164. }
  165. $smsLog = SmsLog::where($where)
  166. ->order('send_time', 'desc')
  167. ->findOrEmpty();
  168. // 没有验证码 或 最新验证码已校验 或 已过期(有效期:5分钟)
  169. if($smsLog->isEmpty() || $smsLog->is_verify || ($smsLog->send_time < time() - 5 * 60) ) {
  170. return false;
  171. }
  172. // 更新校验状态
  173. if($smsLog->code == $code) {
  174. $smsLog->check_num = $smsLog->check_num + 1;
  175. $smsLog->is_verify = YesNoEnum::YES;
  176. $smsLog->save();
  177. return true;
  178. }
  179. // 更新验证次数
  180. $smsLog->check_num = $smsLog->check_num + 1;
  181. $smsLog->save();
  182. return false;
  183. }
  184. }