|
|
@ -1,97 +1,88 @@
|
|
|
|
<template>
|
|
|
|
<template>
|
|
|
|
<el-form
|
|
|
|
<div class="register-container">
|
|
|
|
ref="ruleFormRef"
|
|
|
|
<h2>注册</h2>
|
|
|
|
:model="ruleForm"
|
|
|
|
<form @submit.prevent="handleRegister">
|
|
|
|
status-icon
|
|
|
|
<div>
|
|
|
|
:rules="rules"
|
|
|
|
<label for="username">用户名:</label>
|
|
|
|
label-width="120px"
|
|
|
|
<input type="text" id="username" v-model="username" required />
|
|
|
|
class="demo-ruleForm"
|
|
|
|
</div>
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<el-form-item label="密码" prop="pass">
|
|
|
|
<label for="password">密码:</label>
|
|
|
|
<el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
|
|
|
|
<input type="password" id="password" v-model="password" required />
|
|
|
|
</el-form-item>
|
|
|
|
</div>
|
|
|
|
<el-form-item label="确认密码" prop="checkPass">
|
|
|
|
<div>
|
|
|
|
<el-input
|
|
|
|
<label for="confirmPassword">确认密码:</label>
|
|
|
|
v-model="ruleForm.checkPass"
|
|
|
|
<input type="password" id="confirmPassword" v-model="confirmPassword" required />
|
|
|
|
type="password"
|
|
|
|
</div>
|
|
|
|
autocomplete="off"
|
|
|
|
<button type="submit">注册</button>
|
|
|
|
/>
|
|
|
|
</form>
|
|
|
|
</el-form-item>
|
|
|
|
<p>
|
|
|
|
|
|
|
|
已有账号?<router-link to="/">点击登录</router-link>
|
|
|
|
|
|
|
|
</p>
|
|
|
|
<el-form-item label="账号" prop="account">
|
|
|
|
</div>
|
|
|
|
<el-input v-model="ruleForm.account" type="text" autocomplete="off" />
|
|
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<el-form-item>
|
|
|
|
|
|
|
|
<el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
|
|
|
|
|
|
|
|
<el-button @click="resetForm(ruleFormRef)">重置</el-button>
|
|
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
|
</el-form>
|
|
|
|
|
|
|
|
</template>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
|
|
|
<script>
|
|
|
|
import { reactive, ref } from 'vue'
|
|
|
|
export default {
|
|
|
|
import type { FormInstance } from 'element-plus'
|
|
|
|
data() {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
username: '',
|
|
|
|
|
|
|
|
password: '',
|
|
|
|
|
|
|
|
confirmPassword: ''
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
|
|
handleRegister() {
|
|
|
|
|
|
|
|
if (this.password !== this.confirmPassword) {
|
|
|
|
|
|
|
|
alert('密码不一致,请重新输入');
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ruleFormRef = ref<FormInstance>()
|
|
|
|
if (localStorage.getItem(this.username)) {
|
|
|
|
|
|
|
|
alert('用户名已存在,请选择其他用户名');
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const validatePass = (rule: any, value: any, callback: any) => {
|
|
|
|
const newUser = {
|
|
|
|
if (value === '') {
|
|
|
|
username: this.username,
|
|
|
|
callback(new Error('请输入密码'))
|
|
|
|
password: this.password
|
|
|
|
} else {
|
|
|
|
};
|
|
|
|
if (ruleForm.checkPass !== '') {
|
|
|
|
|
|
|
|
if (!ruleFormRef.value) return
|
|
|
|
localStorage.setItem(this.username, JSON.stringify(newUser));
|
|
|
|
ruleFormRef.value.validateField('checkPass')
|
|
|
|
alert('注册成功!');
|
|
|
|
|
|
|
|
this.$router.push('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
const validatePass2 = (rule: any, value: any, callback: any) => {
|
|
|
|
<style scoped>
|
|
|
|
if (value === '') {
|
|
|
|
.register-container {
|
|
|
|
callback(new Error('请再次输入密码'))
|
|
|
|
width: 300px;
|
|
|
|
} else if (value !== ruleForm.pass) {
|
|
|
|
margin: 0 auto;
|
|
|
|
callback(new Error("两次输入不一致!"))
|
|
|
|
padding: 20px;
|
|
|
|
} else {
|
|
|
|
border: 1px solid #ddd;
|
|
|
|
callback()
|
|
|
|
border-radius: 5px;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const validateAccount = (rule: any, value: any, callback: any) => {
|
|
|
|
h2 {
|
|
|
|
const alphanumericRegex = /^[a-zA-Z0-9]+$/;
|
|
|
|
text-align: center;
|
|
|
|
if (value.length < 8 || !alphanumericRegex.test(value)) {
|
|
|
|
|
|
|
|
callback(new Error('账号需满足长度>8且仅由字母和数字组成'));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ruleForm = reactive({
|
|
|
|
form div {
|
|
|
|
pass: '',
|
|
|
|
margin-bottom: 10px;
|
|
|
|
checkPass: '',
|
|
|
|
}
|
|
|
|
account: '',
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rules = reactive({
|
|
|
|
|
|
|
|
pass: [{ validator: validatePass, trigger: 'blur' }],
|
|
|
|
|
|
|
|
checkPass: [{ validator: validatePass2, trigger: 'blur' }],
|
|
|
|
|
|
|
|
account: [{ validator: validateAccount, trigger: 'blur' }],
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const submitForm = (formEl: FormInstance | undefined) => {
|
|
|
|
button {
|
|
|
|
if (!formEl) return
|
|
|
|
width: 100%;
|
|
|
|
formEl.validate((valid) => {
|
|
|
|
padding: 10px;
|
|
|
|
if (valid) {
|
|
|
|
background-color: #4CAF50;
|
|
|
|
console.log('提交成功!')
|
|
|
|
color: white;
|
|
|
|
} else {
|
|
|
|
border: none;
|
|
|
|
console.log('提交失败!')
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const resetForm = (formEl: FormInstance | undefined) => {
|
|
|
|
button:hover {
|
|
|
|
if (!formEl) return
|
|
|
|
background-color: #45a049;
|
|
|
|
formEl.resetFields()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</style>
|
|
|
|