Register.vue 6.8 KB
Newer Older
yma16's avatar
yma16 已提交
1
<template>
Y
yma16 已提交
2
  <div class="register" style="text-align: center; margin: 0 auto">
yma16's avatar
yma16 已提交
3
    <el-card class="box-card" style="text-align: center; margin: 60px auto">
Y
yma16 已提交
4
      <!-- 弹出一个警告框 -->
yma16's avatar
yma16 已提交
5

Y
yma16 已提交
6 7 8 9 10 11 12
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        style="text-align: center; margin: 0 auto"
yma16's avatar
yma16 已提交
13
        v-loading="loading"
Y
yma16 已提交
14 15 16 17 18 19 20 21 22 23
      >
        <el-form-item>
          <!-- 弹出一个警告框 -->
          <el-alert
            title="这个用户名已经被使用!"
            type="warning"
            v-if="register_error"
          >
          </el-alert>
          <!-- 成功注册! -->
yma16's avatar
yma16 已提交
24

Y
yma16 已提交
25 26 27 28 29 30 31
          <el-alert title="欢迎!!!" type="success" v-if="register_success">
          </el-alert>
          <p style="font-size: 30px">注册</p>
          <el-avatar
            src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          ></el-avatar>
          <!-- <svg
yma16's avatar
yma16 已提交
32 33 34 35 36
          class="icon"
          aria-hidden="true"
        >
          <use xlink:href="#iconshejitouxiangai"></use>
        </svg> -->
Y
yma16 已提交
37
        </el-form-item>
yma16's avatar
yma16 已提交
38

yma16's avatar
yma16 已提交
39 40
        <el-form-item label="" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入账号"></el-input>
Y
yma16 已提交
41
        </el-form-item>
yma16's avatar
yma16 已提交
42

yma16's avatar
yma16 已提交
43
        <el-form-item label="" prop="pass">
Y
yma16 已提交
44 45 46
          <el-input
            type="password"
            v-model="ruleForm.pass"
yma16's avatar
yma16 已提交
47
            placeholder="请输入密码"
Y
yma16 已提交
48
            autocomplete="off"
yma16's avatar
yma16 已提交
49
            show-password
Y
yma16 已提交
50 51
          ></el-input>
        </el-form-item>
yma16's avatar
yma16 已提交
52
        <el-form-item label="" prop="checkPass">
Y
yma16 已提交
53 54 55 56
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
yma16's avatar
yma16 已提交
57
            placeholder="请确认密码"
yma16's avatar
yma16 已提交
58
            show-password
Y
yma16 已提交
59 60 61 62 63 64 65 66 67 68
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">清空</el-button>
        </el-form-item>
        <el-form-item>
          <el-link target="_blank" @click="$router.push({ path: '/login' })"
yma16's avatar
yma16 已提交
69
            >已有账号?</el-link
Y
yma16 已提交
70 71 72 73 74 75
          >
          <el-link type="primary" @click="$router.push({ path: '/login' })"
            >去登录</el-link
          >
        </el-form-item>
      </el-form>
yma16's avatar
yma16 已提交
76
    </el-card>
Y
yma16 已提交
77
  </div>
yma16's avatar
yma16 已提交
78 79 80 81
</template>

<script>
export default {
yma16's avatar
yma16 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    name: 'Register',
    data () {
        const checkName = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('账号不能为空'))
            }
            setTimeout(() => {
                // if (!Number.isInteger(value)) {
                //   callback(new Error('请输入密码'));
                // } if {
                if (value.length < 2) {
                    callback(new Error('名字至少两位'))
                } else {
                    callback()
                }
            }, 1000)
yma16's avatar
yma16 已提交
98
        }
yma16's avatar
yma16 已提交
99 100 101 102 103 104 105 106 107
        const validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('密码不能为空'))
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass')
                }
                callback()
            }
yma16's avatar
yma16 已提交
108
        }
yma16's avatar
yma16 已提交
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
        const validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请确认密码'))
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('密码不一致!'))
            } else {
                callback()
            }
        }
        return {
            loading: false,
            register_success: false,
            register_error: false,
            // baseurl: "http://127.0.0.1/",
            baseurl: '/api/',
            // baseurl: 'http://yongma16.xyz/',
            ruleForm: {
                pass: '',
                checkPass: '',
                name: ''
            },
            rules: {
                pass: [{ validator: validatePass, trigger: 'blur' }],
                checkPass: [{ validator: validatePass2, trigger: 'blur' }],
                name: [{ validator: checkName, trigger: 'blur' }]
            }
yma16's avatar
yma16 已提交
135
        }
Y
yma16 已提交
136
    },
yma16's avatar
yma16 已提交
137 138 139 140 141 142 143 144 145 146
    methods: {
        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                const that = this
                if (valid) {
                    that.loading = false
                    // 提交数据
                    // alert("yes submit!");
                    that.register_success = false // 初始化register的状态
                    that.register_error = false
yma16's avatar
yma16 已提交
147
                    that.$axios
yma16's avatar
yma16 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                        .post(that.baseurl + 'user/register/', {
                            // 传递的名字和密码
                            name: that.ruleForm.name,
                            password: that.ruleForm.pass
                        })
                        .then(function (res) {
                            if (res.data.code === 0) {
                                document.cookie = `user=${that.ruleForm.name}`
                                that.register_error = true
                                that.$message({
                                    message: '名称重复' + that.ruleForm.name + '!',
                                    type: 'error'
                                })
                            } else {
                                that.register_success = true
                                that.$message({
                                    message: '注册成功,欢迎您,' + that.ruleForm.name + '!',
                                    type: 'success'
                                })
                            }
                            that.loading = false
                        })
                        .catch(function (res) {
                            // 获取res中的name
                            that.loading = false
                            that.$message({
                                message: '注册失败' + that.ruleForm.name + '!',
                                type: 'error'
                            })
                        })
                } else {
                    return false
                }
            })
        },
        resetForm (formName) {
            this.$refs[formName].resetFields()
        }
    }
}
yma16's avatar
yma16 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
</script>

<style scoped>
.text {
  align-content: center;
  display: flex;
  margin: 0 auto;
  font-size: 14px;
}

.item {
  display: flex;
  margin: 0 auto;
  /* width: 50%; */
}

.box-card {
  align-self: center;
  align-content: center;
  display: flex;
  width: 450px;
  /* padding-left:25%; */
yma16's avatar
yma16 已提交
210
  margin: 0 auto;
yma16's avatar
yma16 已提交
211 212 213 214
  opacity: 1;
  margin-top: 80px;
  background-color: #ffffff;
}
yma16's avatar
yma16 已提交
215 216 217
/deep/ .el-card__body{
  width: 100% !important;
}
yma16's avatar
yma16 已提交
218
</style>