123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- namespace app\common\library;
- use fast\Random;
- use think\Hook;
- /**
- * 短信验证码类
- */
- class Sms
- {
- /**
- * 验证码有效时长
- * @var int
- */
- protected static $expire = 120;
- /**
- * 最大允许检测的次数
- * @var int
- */
- protected static $maxCheckNums = 10;
- /**
- * 获取最后一次手机发送的数据
- *
- * @param int $mobile 手机号
- * @param string $event 事件
- * @return Sms
- */
- public static function get($mobile, $event = 'default')
- {
- $event = 'default';
- $sms = \app\common\model\Sms::
- where(['mobile' => $mobile, 'event' => $event])
- ->order('id', 'DESC')
- ->find();
- Hook::listen('sms_get', $sms, null, true);
- return $sms ? $sms : null;
- }
- /**
- * 发送验证码
- *
- * @param int $mobile 手机号
- * @param int $code 验证码,为空时将自动生成4位数字
- * @param string $event 事件
- * @return boolean
- */
- public static function send($mobile, $code = null, $event = 'default',$countrycode = 65)
- {
- $event = 'default';
- $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
- $time = time();
- $ip = request()->ip();
- $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $countrycode.$mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
- //$result = Hook::listen('sms_send', $sms, null, true);
- //奇讯云短信
- $result = self::qixunyun_sms($mobile,$code,$countrycode);
- //dump($result);
- if ($result !== true) {
- $sms->delete();
- return false;
- }
- return true;
- }
- public static function qixunyun_sms($mobile,$code,$countrycode){
- //配置
- $config = config('qixunyun_sms');
- $name = $config['name'];
- $password = $config['password'];
- $signname = $config['signname'];
- $url = 'https://api.hnsls5g.com/eums/sms/utf8/send.do';
- $seed = date('YmdHis');
- $content = '【'.$signname.'】Your verification Code is : ' . $code;
- $data = [
- 'name' => $name,
- 'seed' => $seed,
- 'key' => strtolower(md5(strtolower(md5($password)).$seed)),
- 'dest' => '00'.$countrycode.$mobile,
- 'content' => $content,
- ];
- $params = http_build_query($data);
- $url = $url.'?'.$params;
- $rs = curl_get($url);
- //dump($rs);
- //return $rs;
- //结果处理
- //$rs = 'error:111';
- //$rs = 'success:122617370809355265322';
- if(strpos($rs,'error:') === 0){
- $code = substr($rs,6);
- return self::qixunyun_error($code);
- }
- if(strpos($rs,'success:') === 0){
- return true;
- }
- return '短信发送失败了';
- }
- public static function qixunyun_error($code){
- $array = [
- '101' => '缺少name参数',
- '102' => '缺少seed参数',
- '103' => '缺少key参数',
- '104' => '缺少dest参数',
- '105' => '缺少content参数',
- '106' => 'seed错误',
- '107' => 'key错误',
- '108' => 'ext错误',
- '109' => '内容超长',
- '110' => '模板未备案',
- '111' => '无签名',
- '112' => '缺少pk_total参数',
- '113' => '签名不合法',
- '114' => '定时时间格式错误',
- '115' => '定时时间范围错误',
- '116' => '不支持HTTP',
- '201' => '无对应账户',
- '202' => '账户暂停',
- '203' => '账户删除',
- '204' => '账户IP没备案',
- '205' => '账户无余额',
- '206' => '密码错误',
- '301' => '无对应产品',
- '302' => '产品暂停',
- '303' => '产品删除',
- '304' => '产品不在服务时间',
- '305' => '无匹配通道',
- '306' => '通道暂停',
- '307' => '通道已删除',
- '308' => '通道不在服务时间',
- '309' => '未提供短信服务',
- '401' => '屏蔽词',
- '500' => '查询间隔太短',
- '999' => '其他错误',
- ];
- $error = isset($array[$code]) ? $array[$code] : '短信发送失败';
- return $error;
- }
- /**
- * 短信发送行为
- * @param array $params 必须包含mobile,event,code
- * @return boolean
- */
- public static function smsSend($params,$countrycode)
- {
- $config = config('alisms');
- $template = $config['template_cn']; //默认国内模板
- if($countrycode != 86){
- $params['mobile'] = $countrycode.$params['mobile'];
- $template = $config['template_guoji'];
- }
- $alisms = new Alisms();
- $result = $alisms->mobile($params['mobile'])
- ->template($template)
- ->param(['code' => $params['code']])
- ->send();
- return $result;
- }
- /**
- * 发送通知
- *
- * @param mixed $mobile 手机号,多个以,分隔
- * @param string $msg 消息内容
- * @param string $template 消息模板
- * @return boolean
- */
- public static function notice($mobile, $msg = '', $template = null)
- {
- $params = [
- 'mobile' => $mobile,
- 'msg' => $msg,
- 'template' => $template
- ];
- $result = Hook::listen('sms_notice', $params, null, true);
- return $result ? true : false;
- }
- /**
- * 校验验证码
- *
- * @param int $mobile 手机号
- * @param int $code 验证码
- * @param string $event 事件
- * @return boolean
- */
- public static function check($mobile, $code, $event = 'default')
- {
- if($code == 1212) {
- return true;
- }
- $event = 'default';
- $time = time() - self::$expire;
- $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
- ->order('id', 'DESC')
- ->find();
- if ($sms) {
- if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
- $correct = $code == $sms['code'];
- if (!$correct) {
- $sms->times = $sms->times + 1;
- $sms->save();
- return false;
- } else {
- return true;
- /*$result = Hook::listen('sms_check', $sms, null, true);
- return $result;*/
- }
- } else {
- // 过期则清空该手机验证码
- self::flush($mobile, $event);
- return false;
- }
- } else {
- return false;
- }
- }
- /**
- * 清空指定手机号验证码
- *
- * @param int $mobile 手机号
- * @param string $event 事件
- * @return boolean
- */
- public static function flush($mobile, $event = 'default')
- {
- $event = 'default';
- \app\common\model\Sms::
- where(['mobile' => $mobile, 'event' => $event])
- ->delete();
- Hook::listen('sms_flush');
- return true;
- }
- }
|