123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use think\facade\Validate;
- use think\Request;
- /**
- * @title 发送短信
- * @controller Sms
- */
- class Sms extends Base
- {
- /**
- * @title 发送短信
- * @desc 发送短信
- * @url /api/Sms/send
- * @method POST
- * @tag 基础
- * @header
- * @param name:phone type:string require:1 default:-- desc:手机号
- * @param name:event type:string require:0 default:register desc:发送类型register:注册forgetpwd:找回密码
- */
- public function send()
- {
- $phone = input("phone");
- $event = input("event",'register');
- if (!$phone || !Validate::regex($phone, "^1\d{10}$")) {
- $this->error('手机号不正确');
- }
- $last = Db::name('store_sms')->where(['mobile' => $phone, 'event' => $event])
- ->order('id', 'DESC')
- ->find();
- // if ($last && time() - strtotime($last['createtime']) < 60) {
- // $this->error('发送频繁!');
- // }
- // $ipSendTotal = Db::name('store_sms')->where(['ip' => request()->ip()])->whereTime('createtime', '-1 hours')->count();
- // if ($ipSendTotal >= 5) {
- // $this->error('发送频繁!');
- // }
- $member = Db::name('store_member')
- ->where('phone',$phone)
- ->where('is_deleted',0)
- ->count();
- switch ($event){
- case 'register':
- if ($member) $this->error('手机号已注册');
- break;
- case 'forgetpwd':
- if (!$member) $this->error('手机号未注册');
- break;
- }
- $statusStr = array(
- "0" => "短信发送成功",
- "-1" => "参数不全",
- "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
- "30" => "密码错误",
- "40" => "账号不存在",
- "41" => "余额不足",
- "42" => "帐户已过期",
- "43" => "IP地址限制",
- "50" => "内容含有敏感词"
- );
- $smsapi = "http://api.smsbao.com/";
- $user = "xxuns"; //短信平台帐号
- $pass = md5("c7b18a37b98d4f9ebeb6d9c739da44a5"); //短信平台密码
- $code = mt_rand(100000, 999999);
- $content="【象寻科技】您的验证码是".$code."。如非本人操作,请忽略本短信";//要发送的短信内容
- $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
- $result =file_get_contents($sendurl);
- if ($result==0){
- $ip = request()->ip();
- Db::name('store_sms')->insert(['event' => $event, 'mobile' => $phone, 'createtime'=>time(),'code' => $code, 'ip' => $ip]);
- $this->success('发送成功');
- }
- $this->error($statusStr[$result]);
- }
- }
|