Переглянути джерело

fix pw length of register and add resetpassword

Starink 6 роки тому
батько
коміт
c3eaa524e7

+ 1 - 1
middleware/auth.js

@@ -41,7 +41,7 @@ export default function (context) {
     }
   }
 
-  else if (!token && mainPath !== "/" && mainPath !=="/login" && mainPath !== "/register" && mainPath !=="/error" && mainPath !=="/unauthorized" && mainPath !== "/api/receiveIDToken") {
+  else if (!token && mainPath !== "/" && mainPath !=="/login" && mainPath !== "/register" && mainPath !=="/error" && mainPath !=="/resetPassword" && mainPath !=="/unauthorized" && mainPath !== "/api/receiveIDToken") {
 
     // console.log("session:", req.session);
     app.$cookies.set('origin_path', path);

+ 3 - 3
pages/register.vue

@@ -121,7 +121,7 @@
           this.$refs.password.focus();
           return false;
         }
-        if(this.password.length > 10){
+        if(this.password.length > 20){
           this.$message({
             showClose: true,
             message: '密码过长',
@@ -214,14 +214,14 @@
 
       },
       register() {
+        this.loading = true;
+
         let isValid = this.checkValid();
 
         if(!isValid){
           return;
         }
 
-        this.loading = true;
-
         this.$api.$post('/user/register', {
           phone: this.phone,
           password: this.password,

+ 201 - 0
pages/resetPassword.vue

@@ -0,0 +1,201 @@
+<template>
+  <div class="page-container login-container">
+    <el-card class="login-card">
+      <h2 class="login-card__title login-card__item">重置密码</h2>
+      <el-input ref="password" v-model="password" type="password" placeholder="请输入密码, 长度为6-20个字符" maxlength="20" class="login-card__item"></el-input>
+      <el-input ref="passwordConfirm" v-model="passwordConfirm" type="password" placeholder="请再次输入密码" maxlength="20" class="login-card__item"></el-input>
+      <el-input ref="phone" v-model="phone"  oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入手机号" minlength='11' maxlength='11' class="login-card__item"></el-input>
+      <el-input ref="identifyCode" v-model="identifyCode" placeholder="请输入验证码" maxlength="6" class="login-card__short-item"></el-input>
+      <el-button :disabled="disabled" @click="sendMessage" class="login-card__right-button">{{buttonText}}</el-button>
+      <el-button type="primary" class="login-card__item" @click="resetPassword" :loading="loading" round>重置密码</el-button>
+      <div class="login-card__bottom"><router-link to="/login" class="login-card__link">记得密码?前往登录</router-link></div>
+    </el-card>
+    <div class="clear"></div>
+    <router-link to="/"><img src="/image/seec-logo--horizontal.png" class="login-card__logo"/></router-link>
+  </div>
+</template>
+
+<script>
+  export default {
+    name: "resetPassword",
+    layout: "blank",
+    data(){
+      return{
+        phone: '',
+        password: '',
+        identifyCode: '',
+        passwordConfirm: '',
+        buttonText: '获取验证码',
+        disabled: false,
+        loading: false,
+        email: ''
+      }
+    },
+    methods: {
+      sendMessage(){
+        if(this.phone.length < 11){
+          if(this.phone.length === 0){
+            this.$message({
+              showClose: true,
+              message: '请填写手机号码',
+              type: 'error'
+            });
+          }else{
+            this.$message({
+              showClose: true,
+              message: '手机号码格式有误',
+              type: 'error'
+            });
+          }
+          this.$refs.phone.focus();
+          return;
+        }
+        this.disabled = true;
+        let time=60;
+        let myScroll = setInterval(() => {
+          time--;
+          if(time>=0) {
+            this.buttonText = time + "s后可重发";
+          }else{
+            this.buttonText = '获取验证码';
+            this.disabled = false;  //倒计时结束能够重新点击发送的按钮
+            clearTimeout(myScroll);    //清除定时器
+            time = 60;   //设置循环重新开始条件
+          }
+        }, 1000);
+        this.$api.post("/user/sendMessage", {"phone":this.phone}).then(res => {
+          if(res.data.code === 0){
+            this.$message({
+              showClose: true,
+              message: '发送成功',
+              type: 'success'
+            });
+          }else{
+            this.$message({
+              showClose: true,
+              message: res.message,
+              type: 'error'
+            });
+          }
+        });
+      },
+      checkValid(){
+        if(this.password.length < 6){
+          if(this.password.length === 0){
+            this.$message({
+              showClose: true,
+              message: '密码不得为空',
+              type: 'error'
+            });
+          }else{
+            this.$message({
+              showClose: true,
+              message: '密码长度不足',
+              type: 'error'
+            });
+          }
+          this.$refs.password.focus();
+          return false;
+        }
+        if(this.password.length > 20){
+          this.$message({
+            showClose: true,
+            message: '密码过长',
+            type: 'error'
+          });
+          this.$refs.password.focus();
+          return false;
+        }
+
+        if(this.passwordConfirm !== this.password){
+          this.$message({
+            showClose: true,
+            message: '密码前后不一致',
+            type: 'error'
+          });
+          this.$refs.passwordConfirm.focus();
+          return false;
+        }
+
+        if(this.phone.length !== 11){
+          if(this.phone.length === 0){
+            this.$message({
+              showClose: true,
+              message: '手机号码不得为空',
+              type: 'error'
+            });
+          }else{
+            this.$message({
+              showClose: true,
+              message: '手机号码格式有误',
+              type: 'error'
+            });
+          }
+
+          this.$refs.phone.focus();
+          return false;
+        }
+
+        if(this.identifyCode.length === 0){
+          this.$message({
+            showClose: true,
+            message: '未填写验证码',
+            type: 'error'
+          });
+          this.$refs.identifyCode.focus();
+          return false;
+        }
+        if(this.identifyCode.length !== 6){
+          this.$message({
+            showClose: true,
+            message: '验证码长度错误',
+            type: 'error'
+          });
+          this.$refs.identifyCode.focus();
+          return false;
+        }
+
+        return true;
+
+      },
+      resetPassword() {
+
+        this.loading = true;
+        let isValid = this.checkValid();
+
+        if(!isValid){
+          return;
+        }
+
+        this.$api.$post('/user/resetPassword', {
+          phone: this.phone,
+          password: this.password,
+          identifyCode: this.identifyCode
+        }).then((res) =>
+          {
+            if(res.code === 0){
+              this.$message({
+                showClose: true,
+                message: "重置成功,前往登录",
+                type: 'success'
+              });
+              this.$router.push('/login');
+            }else{
+              this.$message({
+                showClose: true,
+                message: res.message,
+                type: 'error'
+              });
+            }
+
+          }
+        );
+        this.loading = false;
+      },
+    }
+  };
+</script>
+
+<style lang="scss" scoped>
+  @import '~/assets/css/login.scss';
+</style>

+ 22 - 0
server/controller/user.js

@@ -213,6 +213,28 @@ class UserController {
     }
   }
 
+  static async resetPassword(ctx) {
+    const data = ctx.request.body;
+
+    if(!ctx.session.verification_phone || !ctx.session.verification_code){
+      ctx.body = Response.failed('请重新获取验证码');
+    }else if(ctx.session.verification_code !== data.identifyCode || ctx.session.verification_phone !== data.phone){
+      ctx.body = Response.failed('验证码错误');
+    }else if( data.password.length<6 || data.password.length>20 || data.phone.length !== 11){
+      ctx.body = Response.failed('参数非法');
+    }else{
+      const hashPassword = bcrypt.hashSync(data.password, 10);
+      const result = await userModel.resetPassword(data.phone, hashPassword);
+      ctx.session.verification_phone = undefined;
+      ctx.session.verification_code = undefined;
+      if (result) {
+        ctx.body = Response.success({});
+      }  else {
+        ctx.body = Response.failed('此手机号未注册');
+      }
+    }
+  }
+
 }
 
 module.exports = UserController;

+ 1 - 1
server/index.js

@@ -160,7 +160,7 @@ async function start () {
   app.use(jwt({ secret: 'seec-portal' }).unless({
     // 设置login、register接口,可以不需要认证访问
     path: [
-      /\/api\/user\/login/, /\/api\/user\/register/,/\/api\/user\/sendMessage/, /\/api\/interaction/, /\/api\/receiveIDToken/, /\/api\/authenticate/,
+      /\/api\/user\/login/, /\/api\/user\/register/,/\/api\/user\/sendMessage/,,/\/api\/user\/resetPassword/, /\/api\/interaction/, /\/api\/receiveIDToken/, /\/api\/authenticate/,
       /^((?!\/api).)*$/   // 设置除了私有接口外的其它资源,可以不需要认证访问
     ]
   }));

+ 5 - 0
server/model/user.js

@@ -73,6 +73,11 @@ class UserModel {
     await User.update({role: 'TEACHER'}, {where: {id: uid}});
     return true;
   }
+
+  static async resetPassword(phone, newPassword){
+    let result = await User.update({password: newPassword}, {where: {phone: phone}});
+    return result[0]> 0;
+  }
 }
 
 module.exports = UserModel;

+ 1 - 0
server/routes/index.js

@@ -15,6 +15,7 @@ module.exports = () => {
   router.post('/user/saveName', UserController.saveName);
   router.post('/user/saveEmail', UserController.saveEmail);
   router.post('/user/saveSignature', UserController.saveSignature);
+  router.post('/user/resetPassword', UserController.resetPassword);
 
   router.get('/teacher/getInfo', TeacherController.getInfo);
   router.post('/teacher/apply', TeacherController.apply);