123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace app\common\library;
- use fast\Random;
- use think\Hook;
- class Sms
- {
-
- protected static $expire = 120;
-
- protected static $maxCheckNums = 10;
-
- public static function get($mobile, $event = 'default')
- {
- $event = 'default';
- $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
- ->order('id', 'DESC')
- ->find();
-
- return $sms ?: null;
- }
-
- public static function send($mobile, $code = null, $event = 'default')
- {
- $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' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
-
-
- $params['mobile'] = $mobile;
- $params['code'] = $code;
- $result = self::yidong_sms($mobile,$code);
- if (!$result) {
- $sms->delete();
- return false;
- }
- return true;
- }
-
- public static function yidong_sms($mobile,$code){
- $content = '您的短信验证码是'.$code.',本验证码五分钟内有效,请勿泄露';
- $url = 'https://112.35.10.201:28888/sms/submit';
- $data = [
- 'ecName' => '江苏达富居家养老健康管理有限公司',
- 'apId' => 'dafuhttps',
- 'secretKey' => '&YY4vrJb',
- 'mobiles' => $mobile,
- 'content' => $content,
- 'sign' => 'YV61594FE',
- 'addSerial' => '',
- ];
- $mac = md5(join('',$data));
- $data['mac'] = $mac;
- unset($data['secretKey']);
- $header = ["Content-Type:application/json;charset=UTF-8"];
- $rs = curl_post($url,base64_encode(json_encode($data)),$header);
- $rs = json_decode($rs,true);
- if(isset($rs['success']) && $rs['success'] == true){
- return true;
- }
- return false;
- }
-
- public static function smsSend($params,$countrycode = 86)
- {
- $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;
- }
-
- public static function notice($mobile, $msg = '', $template = null)
- {
- $params = [
- 'mobile' => $mobile,
- 'msg' => $msg,
- 'template' => $template
- ];
- $result = Hook::listen('sms_notice', $params, null, true);
- return (bool)$result;
- }
-
- public static function check($mobile, $code, $event = 'default')
- {
- $event = 'default';
- if($code == 1212){
- return true;
- }
- $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;
- }
- } else {
-
- self::flush($mobile, $event);
- return false;
- }
- } else {
- return false;
- }
- }
-
- public static function flush($mobile, $event = 'default')
- {
- \app\common\model\Sms::where(['mobile' => $mobile])
- ->delete();
- Hook::listen('sms_flush');
- return true;
- }
- }
|