| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\common\service\sms;
- use app\common\enum\NoticeEnum;
- use app\common\enum\SmsEnum;
- use app\common\enum\YesNoEnum;
- use app\common\model\Notice;
- use app\common\model\SmsLog;
- use app\common\service\ConfigService;
- /**
- * 短信驱动
- * Class SmsDriver
- * @package app\common\service\sms
- */
- class SmsDriver
- {
- /**
- * 错误信息
- * @var
- */
- protected $error = null;
- /**
- * 默认短信引擎
- * @var
- */
- protected $defaultEngine;
- /**
- * 短信引擎
- * @var
- */
- protected $engine;
- /**
- * 架构方法
- * SmsDriver constructor.
- */
- public function __construct()
- {
- // 初始化
- $this->initialize();
- }
- /**
- * @notes 初始化
- * @return bool
- * @author Tab
- * @date 2021/8/19 14:43
- */
- public function initialize()
- {
- try {
- $defaultEngine = ConfigService::get('sms', 'engine', false);
- if($defaultEngine === false) {
- throw new \Exception('请开启短信配置');
- }
- $this->defaultEngine = $defaultEngine;
- $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst(strtolower($defaultEngine)) . 'Sms';
- outFileLog($classSpace,'noticeLogic','$classSpace');
- if (!class_exists($classSpace)) {
- throw new \Exception('没有相应的短信驱动类');
- }
- $engineConfig = ConfigService::get('sms', strtolower($defaultEngine), false);
- if($engineConfig === false) {
- throw new \Exception($defaultEngine . '未配置');
- }
- if ($engineConfig['status'] != 1) {
- throw new \Exception('短信服务未开启');
- }
- $this->engine = new $classSpace($engineConfig);
- if(!is_null($this->engine->getError())) {
- throw new \Exception($this->engine->getError());
- }
- return true;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- }
- /**
- * @notes 获取错误信息
- * @return mixed
- * @author Tab
- * @date 2021/8/19 14:42
- */
- public function getError()
- {
- return $this->error;
- }
- /**
- * @notes 发送短信
- * @param $mobile
- * @param $data
- * @return false
- * @author Tab
- * @date 2021/8/19 16:51
- */
- public function send($mobile, $data)
- {
- try {
- // 发送频率限制
- $this->sendLimit($mobile);
- // 开始发送
- $result = $this->engine
- ->setMobile($mobile)
- ->setTemplateId($data['template_id'])
- ->setTemplateParams($data['params'])
- ->send();
- outFileLog($result,'noticeLogic','短信通知$result2');
- if(false === $result) {
- outFileLog($this->engine->getError(),'noticeLogic','短信通知$result2-getError');
- throw new \Exception($this->engine->getError());
- }
- return $result;
- } catch(\Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- }
- /**
- * @notes 发送频率限制
- * @param $mobile
- * @throws \Exception
- * @author Tab
- * @date 2021/8/20 10:31
- */
- public function sendLimit($mobile)
- {
- $smsLog = SmsLog::where([
- ['mobile', '=', $mobile],
- ['send_status', '=', SmsEnum::SEND_SUCCESS],
- ['scene_id', 'in', NoticeEnum::SMS_SCENE],
- ])
- ->order('send_time', 'desc')
- ->findOrEmpty()
- ->toArray();
- if(!empty($smsLog) && ($smsLog['send_time'] > time() - 60)) {
- throw new \Exception('同一手机号1分钟只能发送1条短信');
- }
- }
- /**
- * @notes 校验手机验证码
- * @param $mobile
- * @param $code
- * @return bool
- * @author Tab
- * @date 2021/8/20 10:47
- */
- public function verify($mobile, $code)
- {
- $smsLog = SmsLog::where([
- ['mobile', '=', $mobile],
- ['send_status', '=', SmsEnum::SEND_SUCCESS],
- ['scene_id', 'in', NoticeEnum::SMS_SCENE],
- ['is_verify', '=', YesNoEnum::NO],
- ])
- ->order('send_time', 'desc')
- ->findOrEmpty();
- // 没有验证码 或 最新验证码已校验 或 已过期(有效期:5分钟)
- if($smsLog->isEmpty() || $smsLog->is_verify || ($smsLog->send_time < time() - 5 * 60) ) {
- return false;
- }
- if($smsLog->code == $code) {
- // 更新校验状态
- $smsLog->check_num = $smsLog->check_num + 1;
- $smsLog->is_verify = YesNoEnum::YES;
- $smsLog->save();
- return true;
- }
- // 更新验证次数
- $smsLog->check_num = $smsLog->check_num + 1;
- $smsLog->save();
- return false;
- }
- }
|