All files / vue-koa-demo/src/components Register.vue

0% Statements 0/46
0% Branches 0/12
0% Functions 0/10
0% Lines 0/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                                                                                                                                                                                                                                                                 
<template>
  <div class="register">
    <el-form ref="form" status-icon :rules="registerRules" :model="form" label-width="80px">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="密 码" prop="pwd">
        <el-input type="password" v-model="form.pwd"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="repwd">
        <el-input type="password" v-model="form.repwd"></el-input>
      </el-form-item>
      <div class="btns">
        <el-button @click="$router.push('/login')">取消注册</el-button>
        <el-button type="primary" @click="confirmRegister('form')">确认注册</el-button>
      </div>
    </el-form>
  </div>
</template>
 
<script>
import axios from 'axios'
 
export default {
  data () {
    const validatePwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    const validateRepwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('再次输入密码'))
      } else if (value !== this.form.pwd) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        pwd: '',
        repwd: ''
      },
      registerRules: {
        name: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符之间', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePwd, trigger: 'blur' }
        ],
        repwd: [
          { required: true, message: '再次输入密码', trigger: 'blur' },
          { validator: validateRepwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    confirmRegister (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('/api/register', this.form)
            .then((res) => {
              console.log(res)
              if (res.data.success) {
                this.$message({
                  message: '注册成功,快去登录吧 😉',
                  type: 'success',
                  duration: 1500
                })
                setTimeout(() => {
                  this.$router.push('/login')
                }, 1000)
              } else {
                this.$message.error({
                  message: '用户名已被占用',
                  duration: 1500
                })
                this.resetForm('form')
              }
            })
            .catch((err) => { console.error(err) })
        } else {
          this.$message.error({
            message: '注册失败 😥',
            duration: 1500
          })
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  },
  created () {
    if (sessionStorage.username) {
      this.$router.replace('/todo')
    }
  }
}
</script>
 
<style lang="scss" scoped>
.register {
  flex: 1 1 auto;
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-form {
  width: 420px;
  padding: 90px 40px;
  padding-bottom: 120px;
  margin: 0 auto;
  margin-top: -20px;
  background: white;
  box-shadow: 0 0 20px 3px rgba(0, 0, 0, .1);
  position: relative;
  .btns {
    width: 100%;
    position: absolute;
    bottom: 32px;
    left: 0;
    .el-button:nth-child(1) {
      margin-right: 100px;
    }
  }
}
</style>