Sfoglia il codice sorgente

finish userinfo management

Starink 6 anni fa
parent
commit
7697cd155b
4 ha cambiato i file con 230 aggiunte e 7 eliminazioni
  1. 138 7
      pages/person/info.vue
  2. 67 0
      server/controller/user.js
  3. 22 0
      server/model/user.js
  4. 3 0
      server/routes/index.js

+ 138 - 7
pages/person/info.vue

@@ -15,13 +15,12 @@
           title="修改昵称"
           :visible.sync="nameDialogVisible"
           width="30%">
-          <el-input ref="name" v-model="newName" placeholder="请输入昵称, 长度为1-10个字符" maxlength="10" class="login-card__item" oninput="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\ ]/g,'')" autofocus></el-input>
+          <el-input ref="name" v-model="newName" placeholder="请输入昵称, 长度为1-10个字符" maxlength="10" oninput="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\ ]/g,'')" autofocus></el-input>
           <span slot="footer" class="dialog-footer">
             <el-button @click="nameDialogVisible = false">取 消</el-button>
-            <el-button type="primary" @click="saveName">确 定</el-button>
+            <el-button type="primary" @click="saveName" :loading="loading">确 定</el-button>
           </span>
         </el-dialog>
-
       </div>
       <div class="main-card__info-item">
         <div class="main-card__info-item__head">
@@ -29,13 +28,35 @@
           <a class="main-card__info-item__modify" @click="emailDialogVisible = true">修改</a>
         </div>
         <div class="main-card__info-item__value">{{email}}</div>
+
+        <el-dialog
+          title="更换邮箱"
+          :visible.sync="emailDialogVisible"
+          width="30%">
+          <el-input ref="name" v-model="newEmail" placeholder="请输入邮箱" maxlength="255" autofocus></el-input>
+          <span slot="footer" class="dialog-footer">
+            <el-button @click="emailDialogVisible = false">取 消</el-button>
+            <el-button type="primary" @click="saveEmail" :loading="loading">确 定</el-button>
+          </span>
+        </el-dialog>
       </div>
       <div class="main-card__info-item">
         <div class="main-card__info-item__head">
           <p class="main-card__info-item__name">个性签名</p>
-          <a class="main-card__info-item__modify" @click="signatueDialogVisible = true">修改</a>
+          <a class="main-card__info-item__modify" @click="signatureDialogVisible = true">修改</a>
         </div>
         <div class="main-card__info-item__value">{{signature}}</div>
+
+        <el-dialog
+          title="修改个性签名"
+          :visible.sync="signatureDialogVisible"
+          width="30%">
+          <el-input ref="name" v-model="newSignature" placeholder="请输入个性签名,长度1-20个字符" maxlength="20" autofocus></el-input>
+          <span slot="footer" class="dialog-footer">
+            <el-button @click="signatureDialogVisible = false">取 消</el-button>
+            <el-button type="primary" @click="saveSignature" :loading="loading">确 定</el-button>
+          </span>
+        </el-dialog>
       </div>
     </div>
   </div>
@@ -55,7 +76,8 @@
         newSignature: '',
         nameDialogVisible: false,
         emailDialogVisible: false,
-        signatureDialogVisible: false
+        signatureDialogVisible: false,
+        loading: false
       }
     },
     mounted(){
@@ -83,12 +105,32 @@
         });
       },
       saveName(){
+        this.loading = true;
+
+        if(this.newName.length === 0){
+          this.$message({
+            showClose: true,
+            message: '昵称不得为空',
+            type: 'error'
+          });
+          this.loading = false;
+          return;
+        }
+        if(this.newName === this.name){
+          this.$message({
+            showClose: true,
+            message: '新昵称不得与原昵称相同',
+            type: 'error'
+          });
+          this.loading = false;
+          return;
+        }
         this.$api.$post('/user/saveName', {
-          name: this.newName
+          newName: this.newName
         }).then((res) =>
           {
             if(res.code === 0){
-              let user = this.$store.state.user;
+              let user = JSON.parse(JSON.stringify(this.$store.state.user));
               user.name = this.newName;
               this.$store.commit("saveUser", user);
               this.name = this.newName;
@@ -109,11 +151,100 @@
         ).catch((e)=>{
           console.log(e);
         });
+
+        this.loading = false;
       },
       saveEmail(){
+        this.loading = true;
+        if(this.newEmail.length === 0){
+          this.$message({
+            showClose: true,
+            message: '邮箱不得为空',
+            type: 'error'
+          });
+          this.loading = false;
+          return;
+        }
+
+        this.$api.$post('/user/saveEmail', {
+          newEmail: this.newEmail
+        }).then((res) =>
+          {
+            if(res.code === 0){
+              let user = JSON.parse(JSON.stringify(this.$store.state.user));
+              user.email = this.newEmail;
+              this.$store.commit("saveUser", user);
+              this.email = this.newEmail;
+              this.emailDialogVisible = false;
+              this.$message({
+                showClose: true,
+                message: "修改成功",
+                type: 'success'
+              });
+            }else{
+              this.$message({
+                showClose: true,
+                message: res.message,
+                type: 'error'
+              });
+            }
+          }
+        ).catch((e)=>{
+          console.log(e);
+        });
+
+        this.loading = false;
 
       },
       saveSignature(){
+        this.loading = true;
+        if(this.newSignature.length === 0){
+          this.$message({
+            showClose: true,
+            message: '个性签名不得为空',
+            type: 'error'
+          });
+          this.loading = false;
+          return;
+        }
+        if(this.newSignature.length > 20){
+          this.$message({
+            showClose: true,
+            message: '个性签名过长',
+            type: 'error'
+          });
+          this.loading = false;
+          return;
+        }
+
+        this.$api.$post('/user/saveSignature', {
+          newSignature: this.newSignature
+        }).then((res) =>
+          {
+            if(res.code === 0){
+              let user = JSON.parse(JSON.stringify(this.$store.state.user));
+              user.signature = this.newSignature;
+              this.$store.commit("saveUser", user);
+              this.signature = this.newSignature;
+              this.signatureDialogVisible = false;
+              this.$message({
+                showClose: true,
+                message: "修改成功",
+                type: 'success'
+              });
+            }else{
+              this.$message({
+                showClose: true,
+                message: res.message,
+                type: 'error'
+              });
+            }
+          }
+        ).catch((e)=>{
+          console.log(e);
+        });
+
+        this.loading = false;
 
       }
     }

+ 67 - 0
server/controller/user.js

@@ -60,11 +60,15 @@ class UserController {
      * @returns {Promise.<void>}
      */
     static async register(ctx) {
+        const myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
         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(!myReg.test(data.email)){
+          ctx.body = Response.failed('邮箱格式有误');
         }else{
           const hashPassword = bcrypt.hashSync(data.password, 10);
           const result = await userModel.create(data.name, hashPassword, data.phone, data.email);
@@ -136,8 +140,71 @@ class UserController {
       }
     }
 
+  }
 
+  static async saveName(ctx){
+    let theUser = await getTokenUser(ctx);
+    if(!theUser){
+      ctx.status = 401;
+    }else{
+      let id = theUser.id;
+      let newName = ctx.request.body.newName;
+      let result = await userModel.updateName(id, newName);
+      if(result.code === 0){
+        ctx.body = Response.success({});
+      }else{
+        ctx.body = Response.failed('此昵称已被占用');
+      }
+    }
   }
+
+  static async saveEmail(ctx){
+    let theUser = await getTokenUser(ctx);
+    if(!theUser){
+      ctx.status = 401;
+    }else{
+      let id = theUser.id;
+      let newEmail = ctx.request.body.newEmail;
+
+      const myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
+
+      if(!myReg.test(newEmail)){
+        ctx.body = Response.failed('邮箱格式有误');
+      }else{
+        let result = await userModel.updateEmail(id, newEmail);
+        if(result.code === 0){
+          ctx.body = Response.success({});
+        }else{
+          ctx.body = Response.failed('修改失败');
+        }
+      }
+
+    }
+  }
+
+  static async saveSignature(ctx){
+    let theUser = await getTokenUser(ctx);
+    if(!theUser){
+      ctx.status = 401;
+    }else{
+      let id = theUser.id;
+      let newSignature = ctx.request.body.newSignature;
+
+      if(newSignature === 0){
+        ctx.body = Response.failed('个性签名不得为空');
+      }else if(newSignature > 20){
+        ctx.body = Response.failed('个性签名过长');
+      }else {
+        let result = await userModel.updateSignature(id, newSignature);
+        if(result.code === 0){
+          ctx.body = Response.success({});
+        }else{
+          ctx.body = Response.failed('修改失败');
+        }
+      }
+    }
+  }
+
 }
 
 module.exports = UserController;

+ 22 - 0
server/model/user.js

@@ -46,6 +46,28 @@ class UserModel {
       where: {id:id}
     });
   }
+
+  static async updateName(id, newName){
+    let user = await User.findOne({
+      where: {name: newName}
+    });
+    if(user){
+      return {code: -1};
+    }
+
+    await User.update({name: newName}, {where: {id: id}});
+    return {code: 0};
+  }
+
+  static async updateEmail(id, newEmail){
+    await User.update({email: newEmail}, {where: {id: id}});
+    return {code: 0};
+  }
+
+  static async updateSignature(id, newSignature){
+    await User.update({signature: newSignature}, {where: {id: id}});
+    return {code: 0};
+  }
 }
 
 module.exports = UserModel;

+ 3 - 0
server/routes/index.js

@@ -10,6 +10,9 @@ module.exports = () => {
     router.post('/user/register', UserController.register);
   router.post('/user/sendMessage', UserController.sendMessage);
   router.get('/user/getInfo', UserController.getInfo);
+  router.post('/user/saveName', UserController.saveName);
+  router.post('/user/saveEmail', UserController.saveEmail);
+  router.post('/user/saveSignature', UserController.saveSignature);
 
     router.get('/course/getMyCourses',  CourseController.getMyCourses);
     router.get('/course/getHotCourses',  CourseController.getHotCourses);