소스 검색

验证码

chenhao 4 년 전
부모
커밋
21fad6500a
1개의 변경된 파일40개의 추가작업 그리고 10개의 파일을 삭제
  1. 40 10
      application/api/controller/User.php

+ 40 - 10
application/api/controller/User.php

@@ -68,10 +68,19 @@ class User extends Api
         if (!Validate::regex($mobile, "^1\d{10}$")) {
             $this->error(__('Mobile is incorrect'));
         }
-        $ret = session($mobile);
-        if (!$ret || $ret != $captcha) {
+//        $ret = session($mobile);
+        $ret = Db::name('captcha')->where('mobile',$mobile)->order('create_time desc')->find();
+        if (!$ret) {
             $this->error(__('Captcha is incorrect'));
         }
+        if ($ret) {
+            if ($ret['number'] != $captcha) {
+                $this->error('验证码不正确');
+            }
+            if(time()-$ret['create_time'] > 300) {
+                $this->error('验证码超时');
+            }
+        }
         $user = \app\common\model\User::getByMobile($mobile);
         if ($user) {
             if ($user->status != '1') {
@@ -115,10 +124,19 @@ class User extends Api
         if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
             $this->error(__('Mobile is incorrect'));
         }
-        $ret = session($mobile);
-        if (!$ret || $ret != $code) {
+//        $ret = session($mobile);
+        $ret = Db::name('captcha')->where('mobile',$mobile)->order('create_time desc')->find();
+        if (!$ret) {
             $this->error(__('Captcha is incorrect'));
         }
+        if ($ret) {
+            if ($ret['number'] != $code) {
+                $this->error('验证码不正确');
+            }
+            if(time()-$ret['create_time'] > 300) {
+                $this->error('验证码超时');
+            }
+        }
         if (!$group_id) {
             $group_id = 0;
         }
@@ -269,11 +287,19 @@ class User extends Api
         if (!$user) {
             $this->error(__('User not found'));
         }
-        $ret = session($mobile);
-
-        if (!$ret || $ret != $captcha) {
+//        $ret = session($mobile);
+        $ret = Db::name('captcha')->where('mobile',$mobile)->order('create_time desc')->find();
+        if (!$ret) {
             $this->error(__('Captcha is incorrect'));
         }
+        if ($ret) {
+            if ($ret['number'] != $captcha) {
+                $this->error('验证码不正确');
+            }
+            if(time()-$ret['create_time'] > 300) {
+                $this->error('验证码超时');
+            }
+        }
 
         //模拟一次登录
         $this->auth->direct($user->id);
@@ -295,7 +321,7 @@ class User extends Api
     {
 
         $mobile = $this->request->param('mobile');
-        echo session($mobile);die;
+
         $type = $this->request->param('type');
         if (!isset($type) || empty($type)) return $this->error('参数错误');
 
@@ -315,8 +341,12 @@ class User extends Api
 
         $res = send_sms($mobile, 1, ['code' => $number]);
         if (isset($res['Message']) && $res['Message'] == "OK") {
-            session($mobile, $number);
-
+            $data = [
+                'mobile' =>$mobile,
+                'number' =>$number,
+                'create_time' =>time(),
+            ];
+            Db::name('captcha')->insert($data);
             return $this->success('发送成功', $number);
         } else {
             return $this->error('发送失败');