SmsDriver.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service\sms;
  20. use app\common\enum\NoticeEnum;
  21. use app\common\enum\SmsEnum;
  22. use app\common\enum\YesNoEnum;
  23. use app\common\model\Notice;
  24. use app\common\model\SmsLog;
  25. use app\common\service\ConfigService;
  26. /**
  27. * 短信驱动
  28. * Class SmsDriver
  29. * @package app\common\service\sms
  30. */
  31. class SmsDriver
  32. {
  33. /**
  34. * 错误信息
  35. * @var
  36. */
  37. protected $error = null;
  38. /**
  39. * 默认短信引擎
  40. * @var
  41. */
  42. protected $defaultEngine;
  43. /**
  44. * 短信引擎
  45. * @var
  46. */
  47. protected $engine;
  48. /**
  49. * 架构方法
  50. * SmsDriver constructor.
  51. */
  52. public function __construct()
  53. {
  54. // 初始化
  55. $this->initialize();
  56. }
  57. /**
  58. * @notes 初始化
  59. * @return bool
  60. * @author Tab
  61. * @date 2021/8/19 14:43
  62. */
  63. public function initialize()
  64. {
  65. try {
  66. $defaultEngine = ConfigService::get('sms', 'engine', false);
  67. if($defaultEngine === false) {
  68. throw new \Exception('请开启短信配置');
  69. }
  70. $this->defaultEngine = $defaultEngine;
  71. $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst(strtolower($defaultEngine)) . 'Sms';
  72. if (!class_exists($classSpace)) {
  73. throw new \Exception('没有相应的短信驱动类');
  74. }
  75. $engineConfig = ConfigService::get('sms', strtolower($defaultEngine), false);
  76. if($engineConfig === false) {
  77. throw new \Exception($defaultEngine . '未配置');
  78. }
  79. if ($engineConfig['status'] != 1) {
  80. throw new \Exception('短信服务未开启');
  81. }
  82. $this->engine = new $classSpace($engineConfig);
  83. if(!is_null($this->engine->getError())) {
  84. throw new \Exception($this->engine->getError());
  85. }
  86. return true;
  87. } catch (\Exception $e) {
  88. $this->error = $e->getMessage();
  89. return false;
  90. }
  91. }
  92. /**
  93. * @notes 获取错误信息
  94. * @return mixed
  95. * @author Tab
  96. * @date 2021/8/19 14:42
  97. */
  98. public function getError()
  99. {
  100. return $this->error;
  101. }
  102. /**
  103. * @notes 发送短信
  104. * @param $mobile
  105. * @param $data
  106. * @return false
  107. * @author Tab
  108. * @date 2021/8/19 16:51
  109. */
  110. public function send($mobile, $data)
  111. {
  112. try {
  113. // 发送频率限制
  114. $this->sendLimit($mobile);
  115. // 开始发送
  116. $result = $this->engine
  117. ->setMobile($mobile)
  118. ->setTemplateId($data['template_id'])
  119. ->setTemplateParams($data['params'])
  120. ->send();
  121. if(false === $result) {
  122. throw new \Exception($this->engine->getError());
  123. }
  124. return $result;
  125. } catch(\Exception $e) {
  126. $this->error = $e->getMessage();
  127. return false;
  128. }
  129. }
  130. /**
  131. * @notes 发送频率限制
  132. * @param $mobile
  133. * @throws \Exception
  134. * @author Tab
  135. * @date 2021/8/20 10:31
  136. */
  137. public function sendLimit($mobile)
  138. {
  139. $smsLog = SmsLog::where([
  140. ['mobile', '=', $mobile],
  141. ['send_status', '=', SmsEnum::SEND_SUCCESS],
  142. ['scene_id', 'in', NoticeEnum::SMS_SCENE],
  143. ])
  144. ->order('send_time', 'desc')
  145. ->findOrEmpty()
  146. ->toArray();
  147. if(!empty($smsLog) && ($smsLog['send_time'] > time() - 60)) {
  148. throw new \Exception('同一手机号1分钟只能发送1条短信');
  149. }
  150. }
  151. /**
  152. * @notes 校验手机验证码
  153. * @param $mobile
  154. * @param $code
  155. * @return bool
  156. * @author Tab
  157. * @date 2021/8/20 10:47
  158. */
  159. public function verify($mobile, $code)
  160. {
  161. $smsLog = SmsLog::where([
  162. ['mobile', '=', $mobile],
  163. ['send_status', '=', SmsEnum::SEND_SUCCESS],
  164. ['scene_id', 'in', NoticeEnum::SMS_SCENE],
  165. ['is_verify', '=', YesNoEnum::NO],
  166. ])
  167. ->order('send_time', 'desc')
  168. ->findOrEmpty();
  169. // 没有验证码 或 最新验证码已校验 或 已过期(有效期:5分钟)
  170. if($smsLog->isEmpty() || $smsLog->is_verify || ($smsLog->send_time < time() - 5 * 60) ) {
  171. return false;
  172. }
  173. if($smsLog->code == $code) {
  174. // 更新校验状态
  175. $smsLog->check_num = $smsLog->check_num + 1;
  176. $smsLog->is_verify = YesNoEnum::YES;
  177. $smsLog->save();
  178. return true;
  179. }
  180. // 更新验证次数
  181. $smsLog->check_num = $smsLog->check_num + 1;
  182. $smsLog->save();
  183. return false;
  184. }
  185. }