<!-- Profile.vue -->
<template>
<div
class="profile"
v-loading.fullscreen.lock="settingAllLoad"
>
<el-row>
<el-col>
<el-card>
<el-tabs
v-model="activeName"
tab-position="left"
@tab-click="activeClick"
>
<el-tab-pane name="first">
<span slot="label">
<i class="icon iconfont"></i>
个人信息
</span>
<!-- 个人信息内容 -->
<div class="userinfo-wrap">
<el-row :gutter="20">
<el-col
:md="15"
:sm="22"
:xs="22"
>
<el-form
label-position="right"
:rules="personalRules"
label-width="140px"
:model="userInfos"
ref="userInfos"
>
<el-form-item
label="姓名:"
prop="userFullname"
>
<el-input
placeholder="请输入姓名"
v-model.trim="userInfos.userFullname"
:disabled="editable"
></el-input>
</el-form-item>
<el-form-item
label="性别:"
prop="userSex"
>
<el-select
v-model="userInfos.userSex"
placeholder="请选择性别"
style="width:100%"
clearable
>
<el-option
label="男"
:value="0"
></el-option>
<el-option
label="女"
:value="1"
></el-option>
</el-select>
</el-form-item>
<el-form-item
label="出生年月:"
prop="userBirthday"
>
<el-date-picker
v-model="userInfos.userBirthday"
type="date"
placeholder="请选择出生年月"
style="width:100%"
format="yyyy-MM-dd"
value-format="timestamp"
:picker-options="pickerOptions"
>
</el-date-picker>
</el-form-item>
<el-form-item
label="证件类型:"
prop="userCertificatetypeDic.id"
>
<el-select
v-model="userInfos.userCertificatetypeDic.id"
placeholder="请选择证件类型"
style="width:100%"
:disabled="editable"
clearable
>
<el-option
v-for="(item, index) in idTypeList"
:key="index"
:label="item.dictionaryName"
:value="item.dictionaryCode"
></el-option>
</el-select>
</el-form-item>
<el-form-item
label="证件号码:"
prop="userCertificate"
>
<el-input
v-model.trim="userInfos.userCertificate"
minlength="3"
maxlength="21"
placeholder="请输入证件号码"
:disabled="visibleCertificate"
></el-input>
</el-form-item>
<div class="btn-footer">
<el-button
type="primary"
@click="savePersonal('userInfos')"
style="width:100%"
v-loading="userSaveLoad"
>保存</el-button>
</div>
</el-form>
</el-col>
</el-row>
</div>
</el-tab-pane>
<el-tab-pane name="second">
<span slot="label">
<i class="icon iconfont"></i>
安全中心
</span>
<!-- 安全中心内容 -->
<div class="safety">
<el-row>
<el-col>
<el-row class="item">
<el-col :span="20">
<i class="icon icon-name"></i>
<span class="title">用户名</span>
<span
class="title"
v-if="userInfos.userName"
>{{userInfos.userName}}</span>
</el-col>
<el-col
:span="4"
class="edit"
v-if="!userInfos.userName"
>
<a @click="editName('userName')">修改</a>
</el-col>
</el-row>
<el-row class="item">
<el-col :span="20">
<i class="icon icon-password"></i>
<span class="title">登录密码</span>
</el-col>
<el-col
:span="4"
class="edit"
>
<a @click="editPassword('password')">修改</a>
</el-col>
</el-row>
<el-row class="item">
<el-col :span="20">
<i class="icon icon-phone"></i>
<span class="title">手机号码</span>
<span
class="title"
v-if="userInfos.isCertification === 0"
>已绑定:{{userInfos.userMobile | formateMobile}}</span>
<span
class="title"
v-if="userInfos.isCertification === 1"
>实名认证手机:{{userInfos.userMobile | formateMobile}}</span>
</el-col>
<el-col
:span="4"
class="edit"
v-if="!userInfos.isCertification"
>
<a @click="editPhone('phone')">修改</a>
</el-col>
</el-row>
<el-row class="item">
<el-col :span="20">
<i class="icon icon-email"></i>
<span class="title">邮箱地址</span>
<span
class="title"
v-if="userInfos.userEmail"
>已绑定:{{userInfos.userEmail}}</span>
</el-col>
<el-col
:span="4"
class="edit"
>
<a @click="editEmail('email')">修改</a>
</el-col>
</el-row>
<el-row class="item">
<el-col :span="20">
<i class="icon icon-wx"></i>
<span class="title">微信</span>
<!-- <span class="title">已绑定微信</span> -->
</el-col>
<!-- <el-col :span="4" class="edit">
修改
</el-col> -->
</el-row>
</el-col>
</el-row>
</div>
</el-tab-pane>
</el-tabs>
</el-card>
</el-col>
</el-row>
<st-upfloat-panel
:panel="panel"
@panelClose="closeEvent"
class="userName"
>
<!-- 修改用户名 -->
<div v-if="name === 'userName'">
<el-row>
<el-col>
<el-form
key='userName'
:model="nameData"
label-position="right"
:rules="nameRules"
ref="nameData"
label-width="100px"
class="demo-ruleForm"
>
<el-form-item
label="用户名:"
prop="userName"
>
<el-input
v-model.trim="nameData.userName"
placeholder="请输入用户名"
maxlength="50"
></el-input>
</el-form-item>
<el-form-item>
<el-row
:gutter="20"
:span="24"
>
<el-col :span="12">
<el-button
type="primary"
class="btn"
@click="saveName('nameData')"
>确认</el-button>
</el-col>
<el-col :span="12">
<el-button
class="btn"
@click="cancelName()"
>取消</el-button>
</el-col>
</el-row>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
<!-- 修改登录密码 -->
<div v-if="name === 'password'">
<el-row justify="center">
<el-col>
<el-form
key="password"
:model="passwordData"
label-position="right"
:rules="passwordRules"
ref="passwordData"
label-width="100px"
class="demo-ruleForm"
>
<el-form-item
label="当前密码:"
prop="password"
>
<el-input
v-model="passwordData.password"
minlength="8"
maxlength="20"
type="password"
placeholder="请输入当前密码"
></el-input>
</el-form-item>
<el-form-item
label="新密码:"
prop="newPassword"
>
<el-input
v-model="passwordData.newPassword"
minlength="8"
maxlength="20"
type="password"
placeholder="请输入新密码"
></el-input>
</el-form-item>
<el-form-item
label="确认密码:"
prop="cofirmPassword"
>
<el-input
v-model="passwordData.cofirmPassword"
minlength="8"
maxlength="20"
type="password"
placeholder="请再次输入密码"
></el-input>
</el-form-item>
<el-form-item>
<el-row
:gutter="20"
:span="24"
>
<el-col :span="12">
<el-button
type="primary"
class="btn"
@click="savePassword('passwordData')"
>确认</el-button>
</el-col>
<el-col :span="12">
<el-button
class="btn"
@click="cancelPassword()"
>取消</el-button>
</el-col>
</el-row>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
<!-- 修改手机号码 -->
<div v-if="name === 'phone'">
<el-row>
<el-col>
<el-form
key="phone"
:model="phoneData"
label-position="right"
:rules="phoneRules"
ref="phoneData"
label-width="100px"
class="demo-ruleForm"
>
<el-form-item
label="手机号码:"
prop="phone"
>
<el-input
v-model.trim="phoneData.phone"
maxlength="11"
placeholder="请输入手机号码"
></el-input>
</el-form-item>
<el-form-item prop="code" label="验证码:">
<el-col :span="14">
<el-input v-model.trim="phoneData.code" maxlength="4" placeholder="请输入短信验证码"></el-input>
</el-col>
<el-col class="line" :span="1" > </el-col>
<el-col :span="9">
<getCode :childForm="phoneData" @updateForm="change"></getCode>
</el-col>
</el-form-item>
<el-form-item>
<el-row
:gutter="20"
:span="24"
>
<el-col :span="12">
<el-button
type="primary"
class="btn"
@click="savePhone('phoneData')"
>确认</el-button>
</el-col>
<el-col :span="12">
<el-button
class="btn"
@click="cancelPhone()"
>取消</el-button>
</el-col>
</el-row>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
<!-- 修改邮箱地址 -->
<div v-if="name === 'email'">
<el-row>
<el-col>
<el-form
key="email"
:model="emailData"
label-position="right"
:rules="emailRules"
ref="emailData"
label-width="120px"
class="demo-ruleForm"
>
<el-form-item
label="新邮箱地址:"
prop="address"
>
<el-input
:maxlength="50"
v-model.trim="emailData.address"
placeholder="请输入邮箱地址"
></el-input>
</el-form-item>
<el-form-item>
<el-row
:span="24"
:gutter="20"
>
<el-col :span="12">
<el-button
type="primary"
class="btn"
@click="saveEmail('emailData')"
>确认</el-button>
</el-col>
<el-col :span="12">
<el-button
class="btn"
@click="cancelEmail()"
>取消</el-button>
</el-col>
</el-row>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
</st-upfloat-panel>
</div>
</template>
<script>
import gql from 'graphql-tag'
import { getErrorByCode } from '@/utils/errors'
import StUpfloatPanel from '@/components/stupfloatpanel/StUpfloatPanel'
import Code from '@/components/code/Code'
import {
count, // 姓名
validateUserName, // 用户名
validateIdcard, // 身份证
validateEmail, // 邮箱
validatePassword, // 密码
validatePassCheckLegal, // 确认密码,
validateAuthCode, // 输入验证码
validatePhoneUser // 手机号
} from '@/utils/validate'
export default {
components: {
StUpfloatPanel,
getCode: Code
},
data () {
const valiDateCode = (rule, value, callback) => {
// 证件号
if (this.userInfos.userCertificatetypeDic.id === 'd82f57aaddb611e8b6a66c0b84d10001') {
if (!value) {
callback(new Error('请输入证件号码'))
} else if (!validateIdcard(value)) {
callback(new Error('请输入正确的身份证号'))
} else {
callback()
}
} else {
if (!value) {
callback(new Error('请输入证件号码'))
} else {
callback()
}
}
}
// 姓名的判断
const checkUserFullName = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入姓名'))
} else if (count(value) > 50) {
callback(new Error('请输入正确的姓名'))
} else {
callback()
}
}
// 邮箱的判断
const checkEmail = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入邮箱地址'))
} else if (!validateEmail(value)) {
callback(new Error('请输入正确的邮箱地址'))
} else {
callback()
}
}
// 密码验证输入密码
const checkPassword = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入当前密码'))
} else if (!validatePassword(value)) {
callback(new Error('请输入正确的密码(8-20位字符,必须由数字、字母组成)'))
} else {
callback()
}
}
// 密码验证新密码
const checkNewPassword = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入当前密码'))
} else if (!validatePassword(value)) {
callback(new Error('请输入正确的密码(8-20位字符,必须由数字、字母组成)'))
} else {
callback()
}
}
// 确认密码再次验证
const passCheckUpdate = (rule, value, callback) => {
if (!value) {
callback(new Error('请再次输入密码'))
} else if (!validatePassCheckLegal(value, this.passwordData.newPassword)) {
callback(new Error('两次输入的密码不一致'))
} else {
callback()
}
}
// 手机号码验证
const phoneCheck = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入手机号码'))
} else if (!validatePhoneUser(value)) {
callback(new Error('请输入正确的手机号码'))
} else {
callback()
}
}
// 验证码验证
const codeCheck = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入短信验证码'))
} else if (!validateAuthCode(value)) {
callback(new Error('请输入正确的验证码'))
} else {
callback()
}
}
// 用户名验证
const checkUserName = (rule, value, callback) => {
if (!value) {
callback(new Error('请输入用户名'))
} else if (!validateUserName(value)) {
callback(new Error('用户名必须以字母开头,数字、字母、_组合'))
} else {
callback()
}
}
return {
// 设置全局loading
settingAllLoad: false,
// 设置保存loading
userSaveLoad: false,
pickerOptions: {
disabledDate (time) {
return time.getTime() > Date.now()
}
},
// areaList: [], // 区域
idTypeList: [], // 证件类型
name: '',
activeName: 'first',
editable: false,
// 修改用户名
nameData: {
userName: ''
},
// 修改密码
passwordData: {
password: '',
newPassword: '',
cofirmPassword: ''
},
// 修改手机号
phoneData: {
phone: '',
code: ''
},
// 修改邮箱
emailData: {
address: '',
verificationCode: ''
},
// panel设置
panel: {
show: false,
title: '',
// 表单操作结果显示 type:(-1,0,1),msg
result: {
type: 0,
msg: ''
}
},
userInfos: {
id: '',
isCertification: 0,
userFullname: '',
userName: '',
userCertificatetypeDic: {
id: ''
},
userCertificate: '',
userSex: '',
userBirthday: '',
userEmail: '',
userMobile: ''
},
personalRules: {
userFullname: [
{ required: true, validator: checkUserFullName, trigger: 'blur' }
],
userSex: [{ required: true, message: '请选择性别', trigger: 'change' }],
userBirthday: [
{ type: 'date', required: true, message: '请选择出生年月', trigger: 'change' }
],
userCertificate: [
{ required: true, validator: valiDateCode, trigger: 'blur' }
],
userCertificatetypeDic: {
id: [{ required: true, message: '请选择证件类型', trigger: 'change' }]
}
},
nameRules: {
userName: [
{ required: true, validator: checkUserName, trigger: 'blur' }
]
},
passwordRules: {
password: [
{ validator: checkPassword, trigger: 'blur' }
],
newPassword: [
{ validator: checkNewPassword, trigger: 'blur' }
],
cofirmPassword: [
{ validator: passCheckUpdate, trigger: 'blur' }
]
},
phoneRules: {
phone: [{ required: true, validator: phoneCheck, trigger: 'blur' }],
code: [{ required: true, validator: codeCheck, trigger: 'blur' }]
},
emailRules: {
address: [
{
required: true,
validator: checkEmail,
trigger: 'blur'
}
]
}
}
},
filters: {
formateMobile (value) {
return value.substr(0, 3) + '****' + value.substr(-4, 4)
},
formateEmail (value) {
return value.substr(0, 3) + '**' + '@' + value.substr(-5, 5)
}
},
computed: {
visibleCertificate () {
if (this.userInfos.userCertificatetypeDic && this.userInfos.userCertificatetypeDic.id) {
return (
!this.userInfos.userCertificatetypeDic.id || this.userInfos.isCertification === 1
)
}
}
},
mounted () {
this.user()
this.dictionary()
},
methods: {
// 获取区域信息
dictionary () {
this.$apollo
.query({
query: gql`
query dictionaries($type: String!, $code: String!) {
dictionaries(type: $type, code: $code) {
dictionaryCode: id
dictionaryName: dictionary_name
}
}
`,
client: 'dictionary',
fetchPolicy: 'network-only',
variables: {
type: 'user_id_type',
code: 'user_id_type'
}
})
.then(res => {
if (res.data.dictionaries) {
this.idTypeList = res.data.dictionaries
}
})
.catch(err => {
this.$message.error(getErrorByCode(err))
})
},
// 获取用户信息
user () {
// 加载全局loading
this.settingAllLoad = true
this.$apollo
.query({
query: gql`
query user($accountCode: String) {
user(accountCode: $accountCode) {
id
userFullname
userName
userCertificate
userCertificatetypeDic{
id
dictionaryName
}
userSex
userBirthday
isCertification
userMobile
userEmail
}
}
`,
client: 'center',
fetchPolicy: 'network-only'
})
.then(res => {
// 取消全局loading
this.settingAllLoad = false
if (res.data.user) {
let data = JSON.parse(JSON.stringify(res.data.user))
if (!data.userCertificatetypeDic) {
data.userCertificatetypeDic = {}
this.$set(data.userCertificatetypeDic, 'id', '')
}
this.userInfos = data
if (this.userInfos.isCertification) {
this.editable = true
} else {
this.editable = false
}
}
}).catch(err => {
// 取消全局loading
this.settingAllLoad = false
this.$message.error(getErrorByCode(err))
})
},
saveUser (id, user) {
// 设置保存loading
this.userSaveLoad = true
this.$apollo
.mutate({
mutation: gql`
mutation saveUser($id: ID,$user: UserInput) {
saveUser(id: $id,user: $user) {
userBirthday
userCertificate
userCertificatetypeDic{
id
dictionaryName
}
userFullname
userSex
}
}
`,
client: 'center',
variables: {
id: id,
user: user
}
})
.then(res => {
// 设置保存loading
this.userSaveLoad = false
if (res.data.saveUser) {
this.$message({
type: 'success',
message: '更新成功!'
})
// 移除表单项的校验结果
this.$refs.userInfos.clearValidate()
this.user()
}
}).catch(err => {
// 设置保存loading
this.userSaveLoad = false
this.$message.error(getErrorByCode(err))
})
},
modifyAccount (ref, data) {
this.$apollo
.mutate({
mutation: gql`
mutation modifyAccount($userName:String,$accountEmail:AccountEmailInput,$accountMobile:AccountMobileInput) {
modifyAccount(userName:$userName,accountEmail:$accountEmail,accountMobile: $accountMobile)
}
`,
client: 'center',
variables: {
userName: data.userName || '',
accountEmail: {
userEmail: data.userEmail || '',
verificationCode: data.verificationCode || '1234'
},
accountMobile: {
verificationCode: data.verificationCode || '1234',
userMobile: data.userMobile || ''
}
}
}).then(res => {
if (res.data.modifyAccount) {
this.panel.result.type = 1
this.panel.show = true
this.user()
// this.resetClearForm(ref)
}
}).catch(err => {
this.$message.error(getErrorByCode(err))
})
},
// 个人信息保存
savePersonal (userInfos) {
this.$refs.userInfos.validate(valid => {
if (valid) {
this.hidePanel()
const form = {
userFullname: this.userInfos.userFullname,
userCertificate: this.userInfos.userCertificate,
userCertificatetype: this.userInfos.userCertificatetypeDic.id,
userSex: this.userInfos.userSex,
userBirthday: this.userInfos.userBirthday
}
this.saveUser(this.userInfos.id, form)
} else {
return false
}
})
},
activeClick (tab, event) {
if (tab.name === 'first') {
this.user()
}
this.$refs.userInfos.clearValidate()
},
// 关闭弹框
closeEvent () {
if (this.name === 'userName') {
// this.$nextTick(() => {
// // this.$refs.nameData && this.$refs.nameData.resetFields()
// this.nameData.userName = null
// this.$refs.nameData && this.$refs.nameData.clearValidate()
// })
this.nameData.userName = null
this.$refs.nameData && this.$refs.nameData.clearValidate()
} else if (this.name === 'password') {
// this.$nextTick(() => {
// // this.$refs.passwordData && this.$refs.passwordData.resetFields()
// this.passwordData.password = null
// this.passwordData.newPassword = null
// this.passwordData.cofirmPassword = null
// this.$refs.passwordData && this.$refs.passwordData.clearValidate()
// })
this.passwordData.password = null
this.passwordData.newPassword = null
this.passwordData.cofirmPassword = null
this.$refs.passwordData && this.$refs.passwordData.clearValidate()
} else if (this.name === 'phone') {
// this.$nextTick(() => {
// // this.$refs.phoneData && this.$refs.phoneData.resetFields()
// this.phoneData.phone = null
// this.phoneData.code = null
// this.$refs.phoneData && this.$refs.phoneData.clearValidate()
// })
this.phoneData.phone = null
this.phoneData.code = null
this.$refs.phoneData && this.$refs.phoneData.clearValidate()
} else if (this.name === 'email') {
// this.$nextTick(() => {
// // this.$refs.emailData && this.$refs.emailData.resetFields()
// this.emailData.address = null
// this.$refs.emailData && this.$refs.emailData.clearValidate()
// })
this.emailData.address = null
this.$refs.emailData && this.$refs.emailData.clearValidate()
}
this.panel.result.type = 0
this.panel.show = false
},
showPanel () {
this.panel.show = true
},
hidePanel () {
this.panel.show = false
},
cancelName () {
this.closeEvent()
},
saveName (nameData) {
this.$refs.nameData.validate((valid) => {
if (valid) {
this.modifyAccount(nameData, {
userName: this.nameData.userName,
userEmail: '',
verificationCode: '1111',
userMobile: ''
})
} else {
return false
}
})
},
editName (name) {
this.name = name
this.showPanel()
this.panel = Object.assign(this.panel, {
show: true,
title: '修改用户名',
result: {
type: 0,
msg: '您已成功修改用户名'
}
})
},
editPassword (name) {
this.name = name
this.showPanel()
this.panel = Object.assign(this.panel, {
show: true,
title: '修改登录密码',
result: {
type: 0,
msg: '您已成功修改密码'
}
})
},
savePassword (passwordData) {
this.$refs.passwordData.validate((valid) => {
if (valid) {
this.$apollo
.mutate({
mutation: gql`
mutation modifyPassword($password: String,$newPassword: String) {
modifyPassword(password: $password,newPassword: $newPassword)
}
`,
client: 'center',
variables: {
password: this.passwordData.password,
newPassword: this.passwordData.newPassword
}
})
.then(res => {
if (res.data.modifyPassword) {
this.panel.result.type = 1
this.panel.show = true
this.user()
// this.resetClearForm(passwordData)
}
}).catch(err => {
this.$message.error(getErrorByCode(err))
})
} else {
return false
}
})
},
resetClearForm (refName) {
// this.$refs[refName] && this.$refs[refName].resetFields()
this.$refs.refName && this.$refs.refName.clearValidate()
},
cancelPassword () {
this.closeEvent()
},
editPhone (name) {
this.name = name
this.showPanel()
this.panel = Object.assign(this.panel, {
show: true,
title: '修改手机号码',
result: {
type: 0,
msg: '您已成功修改手机号码'
}
})
},
savePhone (phoneData) {
this.$refs.phoneData.validate((valid) => {
if (valid) {
this.modifyAccount(phoneData, {
userName: '',
userEmail: '',
verificationCode: this.phoneData.code,
userMobile: this.phoneData.phone
})
} else {
return false
}
})
},
cancelPhone () {
this.closeEvent()
},
editEmail (name) {
this.name = name
this.showPanel()
this.panel = Object.assign(this.panel, {
show: true,
title: '修改邮箱地址',
result: {
type: 0,
msg: '您已成功修改邮箱地址'
}
})
},
saveEmail (emailData) {
this.$refs.emailData.validate((valid) => {
if (valid) {
this.modifyAccount(emailData, {
userName: '',
userEmail: this.emailData.address,
verificationCode: this.emailData.verificationCode || '1111',
userMobile: ''
})
} else {
return false
}
})
},
cancelEmail () {
this.closeEvent()
},
// 验证码返回值
change (msg) {
// 接收子组件的返回值
const code = msg.data.getMobileVerificationCode.code
if (code === 200) {
} else {
this.$message.error(getErrorByCode(code))
}
}
}
}
</script>
<style lang="stylus" scoped>
.profile
.btn-footer
margin-left 140px
.safety
margin-left 40px
.edit
font-size 14px
text-align right
color #1989fa
.item
height 80px
line-height 80px
border-bottom 1px solid #dcdfe6
.icon
width 40px
height 40px
display inline-block
position relative
top 13px
.icon-name
background-image url('./images/icon-name.svg')
.icon-password
background-image url('./images/icon-password.svg')
.icon-phone
background-image url('./images/icon-phone.svg')
.icon-email
background-image url('./images/icon-email.svg')
.icon-wx
background-image url('./images/icon-wx.svg')
.title
display inline-block
font-size 18px
color #303133
margin-left 20px
.userinfo-wrap
margin 20px 0
margin-left 50px
.userName
.btn
width 100%
</style>