elementUI form表单完成密码修改页面
<template>
<div>
<el-row>
<el-col :span="6"> </el-col>
<el-col :span="8">
<h4 type="success" effect="dark" style="margin: 20px 150px;">短信设置密码</h4>
<el-form ref="form" :rules="rules" :model="form" label-width="150px">
<el-form-item label="手机号码" required prop="phone">
<el-col :span="18">
<el-input style="position: fixed;bottom: -99999px" maxlength="11" type="text" />
<el-input v-model="form.phone" maxlength="11" type="text" placeholder="请输入手机号" @input="oninputphone('form')" />
</el-col>
<!-- <el-col v-if="istrue" :span="5" style="margin-left: 10px;color: red"><i class="el-icon-warning" style="margin-right: 5px" />{{ phonemsg }}</el-col>-->
<!-- <el-col v-else :span="5" style="margin-left: 10px;color: green"><i class="el-icon-success" style="margin-right: 5px" />{{ phonemsg }}</el-col>-->
</el-form-item>
<el-form-item label="验证码" required prop="smscodes">
<el-row>
<el-col :span="12">
<el-input v-model="form.smscodes" type="text" placeholder="请输入验证码" @input="oninputsms" />
<el-input style="position: fixed;bottom: -99999px" type="text" />
</el-col>
<el-col :span="6" style="margin-left: 10px">
<el-button v-if="show" type="primary" @click="getsmscodeone">获取验证码</el-button>
<el-button v-else style="width: 112px;">{{ count }}</el-button>
</el-col>
<!-- <el-col v-if="false" :span="5" style="color: red"><i class="el-icon-warning" style="margin-right: 5px" />请输入验证码</el-col>-->
<!-- <el-col v-else :span="5" style="color: green"><i class="el-icon-success" style="margin-right: 5px" /></el-col>-->
</el-row>
</el-form-item>
<el-form-item label="设置新密码" required prop="password">
<el-col :span="18">
<el-input style="position: fixed;bottom: -99999px" type="password" />
<el-input v-model="form.password" type="text" autocomplete="off" placeholder="请输入新密码" show-password @keyup.enter.native="oninputpassword" />
</el-col>
<!-- <el-col v-if="false" :span="5" style="margin-left: 10px;color: red"><i class="el-icon-warning" style="margin-right: 5px" />请输入新密码</el-col>-->
<!-- <el-col v-else :span="5" style="margin-left: 10px;color: green"><i class="el-icon-success" style="margin-right: 5px" /></el-col>-->
</el-form-item>
<el-form-item label="再次的输入新密码" required prop="newpassword">
<el-col :span="18">
<el-input v-model="form.newpassword" type="text" autocomplete="off" placeholder="请再次输入新密码" show-password @keyup.enter.native="oninputnewpassword" />
</el-col>
<!-- <el-col v-if="false" :span="5" style="margin-left: 10px;color: red"><i class="el-icon-warning" style="margin-right: 5px" />请再次输入新密码</el-col>-->
<!-- <el-col v-else :span="5" style="margin-left: 10px;color: green"><i class="el-icon-success" style="margin-right: 5px" /></el-col>-->
</el-form-item>
<el-form-item>
<el-button type="success" @click="oninputPassWord('form')">确认修改</el-button>
<el-button type="primary" @click="tologin">返回登录</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
</template>
<script>
import { selectphone, getsmscode, verification } from '@/api/chengepasswore/passwore'
import md5 from 'js-md5'
export default {
name: 'SetUrl',
data() {
var checkPhone = (rule, value, callback) => {
const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
if (!value) {
return callback(new Error('电话号码不能为空'))
}
setTimeout(() => {
if (!Number.isInteger(+value)) {
callback(new Error('请输入数字值'))
} else {
if (phoneReg.test(value)) {
callback()
} else {
callback(new Error('电话号码格式不正确'))
}
}
}, 100)
}
var validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入密码'))
} else {
if (this.form.newpassword !== '') {
this.$refs.form.validateField('newpassword')
}
callback()
}
}
var validatePass2 = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'))
} else if (value !== this.form.password) {
callback(new Error('两次输入密码不一致!'))
} else {
callback()
}
}
return {
istrue: false,
phonemsg: '',
count: '',
show: true,
form: {
phone: '',
smscodes: '',
password: '',
newpassword: ''
},
rules: {
smscodes: [
{ required: true, message: '请输入验证码', trigger: 'blur' },
{ min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
],
phone: [
{ required: true, validator: checkPhone, trigger: 'change' },
{ max: 11, message: '手机号长度为11位,以13/14/15/16/17/18/19开头', trigger: 'change' }
],
password: [
{ min: 7, max: 16, message: '请输入7-16位密码,包含数字和字母,字母区分大小写', trigger: 'blur' },
{ validator: validatePass, trigger: 'blur' }
],
newpassword: [
{ validator: validatePass2, trigger: 'blur' }
]
}
}
},
created() {
const users = sessionStorage.getItem('factoryMsg')
this.form.phone = JSON.parse(users).phone
console.log(JSON.parse(users))
},
methods: {
async tologin() {
await this.$store.dispatch('user/logout')
this.$router.push(`/login`)
},
oninputPassWord(form) {
this.$refs[form].validate((valid) => {
if (valid) {
console.log('error submit!!', valid, this.form)
verification({ value: { phone: this.form.phone, code: this.form.smscodes, password: md5(this.form.password) }}).then(res => {
this.$message.success(res.msg)
this.$router.push(`/login`)
})
} else {
console.log('error submit!!')
return false
}
})
},
oninputphone(form) {
this.$refs[form].validate(async(valid) => {
if (valid) {
selectphone({ value: { phone: this.form.phone }}).then(res => {
if (res.code === 1) {
this.phonemsg = res.msg
this.istrue = true
}
if (res.code === 0) {
this.phonemsg = res.msg
this.istrue = false
}
}).catch(e => {
console.log(e)
})
console.log('正确!', this.form.phone)
} else {
console.log('error submit!!')
return false
}
})
},
getsmscodeone() {
const TIME_COUNT = 60
getsmscode({ value: { phone: this.form.phone }}).then(res => {
if (res.code === 1) {
this.phonemsg = res.msg
this.istrue = true
}
}).catch(e => {
console.log(e)
})
if (!this.timer) {
this.count = TIME_COUNT
this.show = false
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--
} else {
this.show = true
clearInterval(this.timer)
this.timer = null
}
}, 1000)
}
},
oninputsms(val) {
const reg = /[^\d]/g
const str = reg.test(val)
console.log('正确!', this.form.phone)
},
oninputpassword() {
},
oninputnewpassword() {
}
}
}
</script>
<style scoped>
</style>