|
|
@ -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 {
|
|
|
|
const ruleFormRef = ref<FormInstance>()
|
|
|
|
username: '',
|
|
|
|
|
|
|
|
password: '',
|
|
|
|
const validatePass = (rule: any, value: any, callback: any) => {
|
|
|
|
confirmPassword: ''
|
|
|
|
if (value === '') {
|
|
|
|
};
|
|
|
|
callback(new Error('请输入密码'))
|
|
|
|
},
|
|
|
|
} else {
|
|
|
|
methods: {
|
|
|
|
if (ruleForm.checkPass !== '') {
|
|
|
|
handleRegister() {
|
|
|
|
if (!ruleFormRef.value) return
|
|
|
|
if (this.password !== this.confirmPassword) {
|
|
|
|
ruleFormRef.value.validateField('checkPass')
|
|
|
|
alert('密码不一致,请重新输入');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
callback()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const validatePass2 = (rule: any, value: any, callback: any) => {
|
|
|
|
if (localStorage.getItem(this.username)) {
|
|
|
|
if (value === '') {
|
|
|
|
alert('用户名已存在,请选择其他用户名');
|
|
|
|
callback(new Error('请再次输入密码'))
|
|
|
|
return;
|
|
|
|
} else if (value !== ruleForm.pass) {
|
|
|
|
|
|
|
|
callback(new Error("两次输入不一致!"))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
callback()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const validateAccount = (rule: any, value: any, callback: any) => {
|
|
|
|
const newUser = {
|
|
|
|
const alphanumericRegex = /^[a-zA-Z0-9]+$/;
|
|
|
|
username: this.username,
|
|
|
|
if (value.length < 8 || !alphanumericRegex.test(value)) {
|
|
|
|
password: this.password
|
|
|
|
callback(new Error('账号需满足长度>8且仅由字母和数字组成'));
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
|
|
|
|
callback();
|
|
|
|
localStorage.setItem(this.username, JSON.stringify(newUser));
|
|
|
|
|
|
|
|
alert('注册成功!');
|
|
|
|
|
|
|
|
this.$router.push('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
const ruleForm = reactive({
|
|
|
|
<style scoped>
|
|
|
|
pass: '',
|
|
|
|
.register-container {
|
|
|
|
checkPass: '',
|
|
|
|
width: 300px;
|
|
|
|
account: '',
|
|
|
|
margin: 0 auto;
|
|
|
|
})
|
|
|
|
padding: 20px;
|
|
|
|
|
|
|
|
border: 1px solid #ddd;
|
|
|
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const rules = reactive({
|
|
|
|
h2 {
|
|
|
|
pass: [{ validator: validatePass, trigger: 'blur' }],
|
|
|
|
text-align: center;
|
|
|
|
checkPass: [{ validator: validatePass2, trigger: 'blur' }],
|
|
|
|
}
|
|
|
|
account: [{ validator: validateAccount, trigger: 'blur' }],
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const submitForm = (formEl: FormInstance | undefined) => {
|
|
|
|
form div {
|
|
|
|
if (!formEl) return
|
|
|
|
margin-bottom: 10px;
|
|
|
|
formEl.validate((valid) => {
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
|
|
|
|
console.log('提交成功!')
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log('提交失败!')
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
button {
|
|
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
|
|
padding: 10px;
|
|
|
|
|
|
|
|
background-color: #4CAF50;
|
|
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
|
|
border: none;
|
|
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const resetForm = (formEl: FormInstance | undefined) => {
|
|
|
|
button:hover {
|
|
|
|
if (!formEl) return
|
|
|
|
background-color: #45a049;
|
|
|
|
formEl.resetFields()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</style>
|
|
|
|