From c48059120d4c701ab5143c0779c71149df918607 Mon Sep 17 00:00:00 2001
From: vilson <545522390@qq.com>
Date: Sat, 20 Jul 2019 10:13:16 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BF=98=E8=AE=B0=E5=AF=86?=
 =?UTF-8?q?=E7=A0=81=E9=87=8D=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: vilson <545522390@qq.com>
---
 application/common.php                   |  8 +-
 application/project/controller/Login.php | 95 +++++++++++++++++++++---
 application/project/middleware/Auth.php  |  3 +-
 3 files changed, 93 insertions(+), 13 deletions(-)

diff --git a/application/common.php b/application/common.php
index d772ada..5233bf4 100644
--- a/application/common.php
+++ b/application/common.php
@@ -48,7 +48,13 @@ function getCurrentMember()
 
 function setCurrentMember($data)
 {
-    Cache::set('member:info:' . $data['code'], $data);
+
+    $key = 'member:info:' . $data['code'];
+    if (!$data) {
+        Cache::rm($key);
+    }else{
+        Cache::set($key, $data);
+    }
     return session('member', $data);
 }
 
diff --git a/application/project/controller/Login.php b/application/project/controller/Login.php
index 00d9755..e19c759 100644
--- a/application/project/controller/Login.php
+++ b/application/project/controller/Login.php
@@ -115,8 +115,7 @@ class Login extends BasicApi
                 $this->error('系统繁忙');
             }
         }
-        cache('captcha', $code);
-        cache('captchaMobile', $mobile);
+        cache('captcha:' . $mobile, $code, 60 * 15);
         $this->success('', config('sms.debug') ? $code : '');
     }
 
@@ -151,12 +150,12 @@ class Login extends BasicApi
         if ($member) {
             $this->error('该手机已被注册', 202);
         }
-        if (cache('captcha') != $data['captcha']) {
+        if (cache('captcha:' . $data['mobile']) != $data['captcha']) {
             $this->error('验证码错误', 203);
         }
-        if (cache('captchaMobile') != $data['mobile']) {
-            $this->error('手机号与验证码不匹配', 203);
-        }
+//        if (cache('captchaMobile') != $data['mobile']) {
+//            $this->error('手机号与验证码不匹配', 203);
+//        }
         $memberData = [
             'email' => $data['email'],
             'name' => $data['name'],
@@ -187,12 +186,12 @@ class Login extends BasicApi
     public function _bindMobile()
     {
         $mobile = $this->request->post('mobile', '');
-        if (cache('captcha') != Request::param('captcha')) {
+        if (cache('captcha:' . $mobile) != Request::param('captcha')) {
             $this->error('验证码错误', 203);
         }
-        if (cache('captchaMobile') != $mobile) {
-            $this->error('手机号与验证码不匹配', 203);
-        }
+//        if (cache('captchaMobile') != $mobile) {
+//            $this->error('手机号与验证码不匹配', 203);
+//        }
         $member = getCurrentMember();
         if ($mobile && $member['mobile'] == $mobile) {
             $this->error('你已绑定该手机', 203);
@@ -212,6 +211,45 @@ class Login extends BasicApi
         }
     }
 
+    /**
+     * 获取邮箱验证码
+     */
+    public function _getMailCaptcha()
+    {
+//        if (!config('mail.open')) {
+//            $this->error('系统尚未开启邮件服务');
+//        }
+        $email = $this->request->post('email', '');
+        $code = RandomService::numeric(6);
+        cache('captcha:' . $email, $code, 60 * 15);
+        $member = getCurrentMember();
+        if (config('mail.open')) {
+            $mailer = new Mail();
+            try {
+                $mail = $mailer->mail;
+                $mail->CharSet = 'utf-8';
+                $mail->setFrom(config('mail.Username'), 'pearProject');
+                $mail->addAddress($email, getCurrentMember()['name']);
+                //Content
+                $mail->isHTML(true);
+                $mail->Subject = '重置密码验证码:' . $code;
+//            $info = [
+//                'member_code' => $member['code'],
+//                'email' => $email,
+//            ];
+//            $accessToken = JwtService::getAccessToken($info);
+//            $link = Request::domain() . '/#/reset/email?token=' . $accessToken;
+                $mail->Body = '
+<p>系统检测到你正在尝试重置密码,验证码:' . $code . '。验证码15分钟内有效,请在重置页面输入验证码并进行下一步操作, 如非你本人操作,请忽略此邮件。</p>';
+                $mail->send();
+            } catch (Exception $e) {
+                ob_clean();
+                $this->error('发送失败 ');
+            }
+        }
+        $this->success('', !config('mail.open') ? $code : '');
+    }
+
     /**
      * 绑定邮箱
      */
@@ -307,6 +345,43 @@ class Login extends BasicApi
 
     }
 
+    /**
+     * 通过邮箱重置密码
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public function _resetPasswordByMail()
+    {
+        $data = Request::only('email,password,password2,mobile,captcha');
+        $validate = Validate::make([
+            'email' => 'require',
+            'password' => 'require|min:6',
+            'password2' => 'require|min:6',
+            'captcha' => 'require|min:6',
+        ], [
+            'email.require' => '邮箱账号不能为空!',
+            'password.require' => '登陆密码不能为空!',
+            'password.min' => '登录密码长度不能少于6位有效字符!',
+            'password2.require' => '确认密码不能为空!',
+            'password2.min' => '确认密码长度不能少于6位有效字符!',
+            'captcha.require' => '验证码不能为空!',
+            'captcha.min' => '验证码格式有误',
+        ]);
+        $validate->check($data) || $this->error($validate->getError());
+        $email = $this->request->post('email', '');
+        if (cache('captcha:' . $email) != Request::param('captcha')) {
+            $this->error('验证码错误', 203);
+        }
+        $member = Member::where(['email' => $email])->find();
+        if (!$member) {
+            $this->error('该邮箱账号不存在', 203);
+        }
+        $member->password = $data['password'];
+        $member->save();
+            $this->success('重置密码成功,请登录');
+    }
+
     /**
      * 检测登陆信息
      */
diff --git a/application/project/middleware/Auth.php b/application/project/middleware/Auth.php
index cae43d5..1e59e02 100644
--- a/application/project/middleware/Auth.php
+++ b/application/project/middleware/Auth.php
@@ -45,8 +45,7 @@ class Auth
                 $accessToken = explode(' ', $authorization)[1];
             }
             $data = JwtService::decodeToken($accessToken);
-            $isError = isError($data);
-            if ($isError) {
+            if (isError($data)) {
                 //TODO 启用refreshToken
                 if ($data['errno'] == 3) {
                     $msg = ['code' => 401, 'msg' => 'accessToken过期'];