SmsDriver.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. outFileLog($classSpace,'noticeLogic','$classSpace');
  73. if (!class_exists($classSpace)) {
  74. throw new \Exception('没有相应的短信驱动类');
  75. }
  76. $engineConfig = ConfigService::get('sms', strtolower($defaultEngine), false);
  77. if($engineConfig === false) {
  78. throw new \Exception($defaultEngine . '未配置');
  79. }
  80. if ($engineConfig['status'] != 1) {
  81. throw new \Exception('短信服务未开启');
  82. }
  83. $this->engine = new $classSpace($engineConfig);
  84. if(!is_null($this->engine->getError())) {
  85. throw new \Exception($this->engine->getError());
  86. }
  87. return true;
  88. } catch (\Exception $e) {
  89. $this->error = $e->getMessage();
  90. return false;
  91. }
  92. }
  93. /**
  94. * @notes 获取错误信息
  95. * @return mixed
  96. * @author Tab
  97. * @date 2021/8/19 14:42
  98. */
  99. public function getError()
  100. {
  101. return $this->error;
  102. }
  103. /**
  104. * @notes 发送短信
  105. * @param $mobile
  106. * @param $data
  107. * @return false
  108. * @author Tab
  109. * @date 2021/8/19 16:51
  110. */
  111. public function send($mobile, $data)
  112. {
  113. try {
  114. // 发送频率限制
  115. $this->sendLimit($mobile);
  116. // 开始发送
  117. $result = $this->engine
  118. ->setMobile($mobile)
  119. ->setTemplateId($data['template_id'])
  120. ->setTemplateParams($data['params'])
  121. ->send();
  122. outFileLog($result,'noticeLogic','短信通知$result2');
  123. if(false === $result) {
  124. outFileLog($this->engine->getError(),'noticeLogic','短信通知$result2-getError');
  125. throw new \Exception($this->engine->getError());
  126. }
  127. return $result;
  128. } catch(\Exception $e) {
  129. $this->error = $e->getMessage();
  130. return false;
  131. }
  132. }
  133. /**
  134. * @notes 发送频率限制
  135. * @param $mobile
  136. * @throws \Exception
  137. * @author Tab
  138. * @date 2021/8/20 10:31
  139. */
  140. public function sendLimit($mobile)
  141. {
  142. $smsLog = SmsLog::where([
  143. ['mobile', '=', $mobile],
  144. ['send_status', '=', SmsEnum::SEND_SUCCESS],
  145. ['scene_id', 'in', NoticeEnum::SMS_SCENE],
  146. ])
  147. ->order('send_time', 'desc')
  148. ->findOrEmpty()
  149. ->toArray();
  150. if(!empty($smsLog) && ($smsLog['send_time'] > time() - 60)) {
  151. throw new \Exception('同一手机号1分钟只能发送1条短信');
  152. }
  153. }
  154. /**
  155. * @notes 校验手机验证码
  156. * @param $mobile
  157. * @param $code
  158. * @return bool
  159. * @author Tab
  160. * @date 2021/8/20 10:47
  161. */
  162. public function verify($mobile, $code)
  163. {
  164. $smsLog = SmsLog::where([
  165. ['mobile', '=', $mobile],
  166. ['send_status', '=', SmsEnum::SEND_SUCCESS],
  167. ['scene_id', 'in', NoticeEnum::SMS_SCENE],
  168. ['is_verify', '=', YesNoEnum::NO],
  169. ])
  170. ->order('send_time', 'desc')
  171. ->findOrEmpty();
  172. // 没有验证码 或 最新验证码已校验 或 已过期(有效期:5分钟)
  173. if($smsLog->isEmpty() || $smsLog->is_verify || ($smsLog->send_time < time() - 5 * 60) ) {
  174. return false;
  175. }
  176. if($smsLog->code == $code) {
  177. // 更新校验状态
  178. $smsLog->check_num = $smsLog->check_num + 1;
  179. $smsLog->is_verify = YesNoEnum::YES;
  180. $smsLog->save();
  181. return true;
  182. }
  183. // 更新验证次数
  184. $smsLog->check_num = $smsLog->check_num + 1;
  185. $smsLog->save();
  186. return false;
  187. }
  188. }