2021-06-11 10:00:22 +08:00
|
|
|
<template>
|
2022-06-08 15:31:41 +08:00
|
|
|
<div class="right-box right-box-user">
|
2021-06-11 10:00:22 +08:00
|
|
|
<div class="right-box__header">
|
2024-04-08 11:06:28 +08:00
|
|
|
<div class="header__title">{{ editObject.id ? $t('config.user.editUser') : $t('config.user.createUser') }}</div>
|
2021-06-11 10:00:22 +08:00
|
|
|
<div class="header__operation">
|
|
|
|
|
<span v-cancel="{object: editObject, func: esc}"><i class="cn-icon cn-icon-close"></i></span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="right-box__container">
|
|
|
|
|
<div class="container__form">
|
|
|
|
|
<el-form ref="userForm" :model="editObject" :rules="editObject.id ? rules2 : rules" label-position="top" label-width="120px">
|
|
|
|
|
<!--name-->
|
|
|
|
|
<el-form-item :label="$t('config.user.name')" prop="name">
|
2024-04-08 11:06:28 +08:00
|
|
|
<el-input id="account-input-name" v-model="editObject.name"
|
|
|
|
|
:disabled="editObject.username==='admin' && editObject.id === 1"
|
2024-04-10 16:50:39 +08:00
|
|
|
minlength="2" maxlength="64" placeholder="" show-word-limit type="text"></el-input>
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
<!--username-->
|
|
|
|
|
<el-form-item :label="$t('config.user.username')" prop="username">
|
2024-04-08 11:06:28 +08:00
|
|
|
<el-input id="account-input-username" v-model="editObject.username"
|
|
|
|
|
:disabled="editObject.username==='admin' && editObject.id === 1"
|
2024-04-10 16:50:39 +08:00
|
|
|
minlength="2" maxlength="64" placeholder="" show-word-limit type="text"></el-input>
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
<!--password-->
|
|
|
|
|
<el-form-item :label="$t('config.user.pin')" prop="pin">
|
2023-05-18 11:46:50 +08:00
|
|
|
<el-input id="account-input-password" v-model="editObject.pin" minlength="5" maxlength="64" placeholder=""
|
2024-04-10 16:50:39 +08:00
|
|
|
show-word-limit type="password" @blur="pinBlur" autocomplete="new-password"></el-input>
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
<!--pinChange-->
|
|
|
|
|
<el-form-item :label="$t('config.user.confirmPin')" label-width="200px" prop="pinChange">
|
2023-05-18 11:46:50 +08:00
|
|
|
<el-input id="account-input-pinChange" v-model="editObject.pinChange" minlength="5" maxlength="64" placeholder=""
|
2024-04-10 16:50:39 +08:00
|
|
|
show-word-limit type="password"></el-input>
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
<!--email-->
|
|
|
|
|
<el-form-item label="E-mail" prop="email">
|
2024-04-10 16:50:39 +08:00
|
|
|
<el-input id="account-input-email" v-model="editObject.email" maxlength="35" show-word-limit placeholder="" type="text"></el-input>
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
<!--mobile-->
|
|
|
|
|
<el-form-item :label="$t('config.user.mobile')" prop="mobile">
|
2024-04-10 16:50:39 +08:00
|
|
|
<el-input id="account-input-mobile" v-model.number="editObject.mobile" maxlength="11" show-word-limit placeholder="" type="text"></el-input>
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
<!--roles-->
|
|
|
|
|
<el-form-item :label="$t('config.user.roles')" prop="roleIds">
|
|
|
|
|
<el-select id="account-input-roleIds"
|
|
|
|
|
v-model="editObject.roleIds"
|
|
|
|
|
:disabled="(editObject.username === 'admin') && editObject.id === 1"
|
|
|
|
|
class="right-box__select"
|
|
|
|
|
clearable
|
|
|
|
|
collapse-tags
|
2023-10-17 10:01:08 +08:00
|
|
|
placeholder=" "
|
2021-06-11 10:00:22 +08:00
|
|
|
popper-class="right-box-select-dropdown prevent-clickoutside"
|
2024-04-10 16:50:39 +08:00
|
|
|
|
2021-06-11 10:00:22 +08:00
|
|
|
@change="()=>{ this.$forceUpdate() }">
|
|
|
|
|
<template v-for="role in roleData" :key="role.id">
|
|
|
|
|
<el-option :label="role.name" :value="role.id"></el-option>
|
|
|
|
|
</template>
|
|
|
|
|
</el-select>
|
|
|
|
|
</el-form-item>
|
2022-04-08 09:28:55 +08:00
|
|
|
<!--i18n-->
|
|
|
|
|
<el-form-item :label="$t('config.i18n.lang')" prop="i18n">
|
|
|
|
|
<el-select id="account-input-roleIds"
|
|
|
|
|
v-model="editObject.lang"
|
|
|
|
|
class="right-box__select"
|
|
|
|
|
clearable
|
|
|
|
|
collapse-tags
|
2023-10-17 10:01:08 +08:00
|
|
|
placeholder=" "
|
2022-04-08 09:28:55 +08:00
|
|
|
popper-class="right-box-select-dropdown prevent-clickoutside"
|
2024-04-10 16:50:39 +08:00
|
|
|
>
|
2022-04-08 09:28:55 +08:00
|
|
|
<template v-for="lang in langData" :key="lang.value">
|
|
|
|
|
<el-option :label="lang.label" :value="lang.value"></el-option>
|
|
|
|
|
</template>
|
|
|
|
|
</el-select>
|
|
|
|
|
</el-form-item>
|
2021-06-11 10:00:22 +08:00
|
|
|
<el-form-item :label="$t('config.user.enable')">
|
2021-06-16 15:01:58 +08:00
|
|
|
<el-switch
|
2024-04-08 11:06:28 +08:00
|
|
|
id="account-input-status"
|
|
|
|
|
v-model="editObject.status"
|
|
|
|
|
:disabled="(editObject.username === loginName) || (editObject.username === 'admin' && editObject.id === 1)"
|
|
|
|
|
:active-value="1"
|
|
|
|
|
:inactive-value="0">
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-switch>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
|
|
|
<el-form-item v-if="editObject.id" :label="$t('config.user.createTime')">
|
2024-04-08 11:06:28 +08:00
|
|
|
<div class="right-box-form-content-txt">{{ editObject.ctime }}</div>
|
2021-06-11 10:00:22 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
</el-form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="right-box__footer">
|
|
|
|
|
<button id="asset-edit-cancel" v-cancel="{object: editObject, func: esc}" class="footer__btn footer__btn--light">
|
2024-04-08 11:06:28 +08:00
|
|
|
<span>{{ $t('overall.cancel') }}</span>
|
2021-06-11 10:00:22 +08:00
|
|
|
</button>
|
|
|
|
|
<button id="asset-edit-save" :class="{'footer__btn--disabled': blockOperation.save}" :disabled="blockOperation.save" class="footer__btn" @click="save">
|
2024-04-08 11:06:28 +08:00
|
|
|
<span>{{ $t('overall.save') }}</span>
|
2021-06-11 10:00:22 +08:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2022-01-16 23:16:00 +08:00
|
|
|
import rightBoxMixin from '@/mixins/right-box'
|
2023-08-24 17:15:41 +08:00
|
|
|
import axios from 'axios'
|
|
|
|
|
import _ from 'lodash'
|
2022-04-14 15:52:07 +08:00
|
|
|
import { themeData, langData, storageKey } from '@/utils/constants'
|
|
|
|
|
import { api } from '@/utils/api'
|
|
|
|
|
|
2021-06-11 10:00:22 +08:00
|
|
|
export default {
|
|
|
|
|
name: 'UserBox',
|
|
|
|
|
mixins: [rightBoxMixin],
|
|
|
|
|
data () {
|
2023-09-15 15:15:14 +08:00
|
|
|
const validatePin = (rule, value, callback) => { // 确认密码
|
2023-11-17 16:09:18 +08:00
|
|
|
if (value && value.length < 5) {
|
2023-09-15 15:15:14 +08:00
|
|
|
callback(new Error(this.$t('validate.atLeastFive')))
|
|
|
|
|
} else {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const validateConfirmPin = (rule, value, callback) => { // 确认密码的二次校验
|
2023-11-30 09:16:45 +08:00
|
|
|
if (_.isEmpty(value) && !_.isEmpty(this.editObject.pin)) { // 密码有内容,确认密码没内容
|
2023-05-18 11:46:50 +08:00
|
|
|
callback(new Error(this.$t('config.user.confirmPin')))
|
2023-11-30 09:16:45 +08:00
|
|
|
} else if (!_.isEmpty(value) && value !== this.editObject.pin) { // 密码有内容,确认密码也有内容,内容不一致
|
2021-06-11 10:00:22 +08:00
|
|
|
callback(new Error(this.$t('config.user.confirmPinErr')))
|
|
|
|
|
} else {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-12 11:51:23 +08:00
|
|
|
const validateName = (rule, value, callback) => {
|
|
|
|
|
let validate = true
|
2023-09-18 16:09:13 +08:00
|
|
|
// const reg = /^[a-zA-Z0-9\u4e00-\u9fa5\u30a1-\u30f6\u3041-\u3093\uFF00-\uFFFF\u4e00-\u9fa5\u0400-\u04FF\s]{2,64}$/
|
|
|
|
|
const reg = /^[\u4E00-\u9FA5\a-zA-Z0-9\u3040-\u309F\u0800-\u4e00\u0400-\u04FF\u2000-\u206F\s]{2,64}$/
|
|
|
|
|
const emptyFlag = value[0] === ' ' || value[value.length - 1] === ' '
|
2023-07-12 11:51:23 +08:00
|
|
|
validate = reg.test(value)
|
|
|
|
|
if (value.length < 2) {
|
|
|
|
|
callback(new Error(this.$t('validate.atLeastTwo')))
|
2023-09-18 16:09:13 +08:00
|
|
|
} else if (!validate || value.trim() === '' || emptyFlag) {
|
|
|
|
|
// 除满足中英日俄、数字空格外,不能全为空格,前后也不可为空格
|
2023-07-12 11:51:23 +08:00
|
|
|
callback(new Error(this.$t('validate.name')))
|
|
|
|
|
} else {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-15 12:50:03 +08:00
|
|
|
const validateUserName = (rule, value, callback) => {
|
|
|
|
|
let validate = true
|
2023-09-18 16:09:13 +08:00
|
|
|
// const reg = /^[a-zA-Z0-9_\u4e00-\u9fa5\u30a1-\u30f6\u3041-\u3093\uFF00-\uFFFF\u4e00-\u9fa5\u0400-\u04FF\s]{2,64}$/
|
|
|
|
|
// 校验,只允许中、英、数字和下划线
|
|
|
|
|
const reg = /^[\u4e00-\u9fa5_a-zA-Z0-9]{2,64}$/
|
2023-07-15 12:50:03 +08:00
|
|
|
validate = reg.test(value)
|
|
|
|
|
if (value.length < 2) {
|
|
|
|
|
callback(new Error(this.$t('validate.atLeastTwo')))
|
|
|
|
|
} else if (!validate) {
|
|
|
|
|
callback(new Error(this.$t('validate.userName')))
|
|
|
|
|
} else {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
return {
|
2022-04-14 15:52:07 +08:00
|
|
|
url: api.user,
|
|
|
|
|
loginName: localStorage.getItem(storageKey.username),
|
2021-06-11 10:00:22 +08:00
|
|
|
rules: { // 表单校验规则
|
|
|
|
|
name: [
|
2023-05-18 11:46:50 +08:00
|
|
|
{ required: true, message: this.$t('validate.required'), trigger: 'blur' },
|
2023-09-18 16:09:13 +08:00
|
|
|
{ validator: validateName, trigger: 'blur' }
|
2021-06-11 10:00:22 +08:00
|
|
|
],
|
|
|
|
|
username: [
|
2023-05-18 11:46:50 +08:00
|
|
|
{ required: true, message: this.$t('validate.required'), trigger: 'blur' },
|
2023-09-18 16:09:13 +08:00
|
|
|
{ validator: validateUserName, trigger: 'blur' }
|
2021-06-11 10:00:22 +08:00
|
|
|
],
|
|
|
|
|
pin: [
|
2023-05-18 11:46:50 +08:00
|
|
|
{ required: true, message: this.$t('validate.required'), trigger: 'blur' },
|
2023-09-15 15:15:14 +08:00
|
|
|
{ validator: validatePin, trigger: 'blur' }
|
2021-06-11 10:00:22 +08:00
|
|
|
],
|
|
|
|
|
pinChange: [
|
2023-05-18 11:46:50 +08:00
|
|
|
{ required: true, message: this.$t('validate.required') },
|
2023-09-15 15:15:14 +08:00
|
|
|
{ validator: validatePin, trigger: 'blur' },
|
|
|
|
|
{ validator: validateConfirmPin, trigger: 'blur' }
|
2021-06-11 10:00:22 +08:00
|
|
|
],
|
|
|
|
|
roleIds: [
|
|
|
|
|
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
|
|
|
|
|
],
|
|
|
|
|
email: [
|
|
|
|
|
{ type: 'email', message: this.$t('validate.email') }
|
2023-05-18 11:46:50 +08:00
|
|
|
],
|
|
|
|
|
mobile: [
|
|
|
|
|
{ required: false, message: this.$t('validate.required') },
|
|
|
|
|
{ pattern: /^(1[345678]\d{9})$/, message: this.$t('validate.mobile') }
|
2021-06-11 10:00:22 +08:00
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
rules2: { // 表单校验规则
|
|
|
|
|
username: [
|
|
|
|
|
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
|
|
|
|
|
],
|
|
|
|
|
pinChange: [
|
2023-09-15 15:15:14 +08:00
|
|
|
{ validator: validateConfirmPin, trigger: 'blur' },
|
2023-10-30 16:41:37 +08:00
|
|
|
{ validator: validatePin, trigger: 'blur' }
|
2021-06-11 10:00:22 +08:00
|
|
|
],
|
|
|
|
|
roleIds: [
|
|
|
|
|
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
|
|
|
|
|
],
|
|
|
|
|
email: [
|
|
|
|
|
{ type: 'email', message: this.$t('validate.email') }
|
2023-05-18 11:46:50 +08:00
|
|
|
],
|
|
|
|
|
mobile: [
|
|
|
|
|
{ required: false, message: this.$t('validate.required') },
|
|
|
|
|
{ pattern: /^(1[345678]\d{9})$/, message: this.$t('validate.mobile') }
|
2021-06-11 10:00:22 +08:00
|
|
|
]
|
2021-06-16 10:35:09 +08:00
|
|
|
},
|
2022-04-08 09:28:55 +08:00
|
|
|
roleData: [],
|
|
|
|
|
themeData,
|
|
|
|
|
langData
|
2021-06-11 10:00:22 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
setup () {
|
2021-06-11 16:59:16 +08:00
|
|
|
},
|
2021-06-17 16:56:50 +08:00
|
|
|
mounted () {
|
2021-06-11 16:59:16 +08:00
|
|
|
this.getRoleData()
|
|
|
|
|
},
|
2021-06-11 10:00:22 +08:00
|
|
|
methods: {
|
2021-06-16 15:01:58 +08:00
|
|
|
isCurrentUser (username) {
|
2022-04-14 15:52:07 +08:00
|
|
|
return localStorage.getItem(storageKey.username) === username
|
2021-06-11 10:00:22 +08:00
|
|
|
},
|
|
|
|
|
/* 密码失去焦点 检验确认密码 */
|
|
|
|
|
pinBlur () {
|
|
|
|
|
if (this.editObject.pin && this.editObject.pinChange) {
|
|
|
|
|
this.$refs.userForm.validateField('pinChange')
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
save () {
|
2024-04-08 11:06:28 +08:00
|
|
|
if (this.blockOperation.save) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
this.blockOperation.save = true
|
|
|
|
|
|
|
|
|
|
this.$refs.userForm.validate((valid) => {
|
|
|
|
|
if (valid) {
|
|
|
|
|
if (this.editObject.id) {
|
2023-08-24 17:15:41 +08:00
|
|
|
axios.put(this.url, this.editObject).then(res => {
|
2021-06-11 10:00:22 +08:00
|
|
|
this.blockOperation.save = false
|
2023-08-24 17:15:41 +08:00
|
|
|
if (res.status === 200) {
|
2024-04-08 11:06:28 +08:00
|
|
|
this.$message({
|
|
|
|
|
duration: 2000,
|
|
|
|
|
type: 'success',
|
|
|
|
|
message: this.$t('tip.saveSuccess')
|
|
|
|
|
})
|
2023-09-21 19:49:35 +08:00
|
|
|
if (this.editObject.lang && this.editObject.id == localStorage.getItem(storageKey.userId)) {
|
|
|
|
|
const currentLang = localStorage.getItem(storageKey.language)
|
|
|
|
|
if (currentLang !== this.editObject.lang) {
|
|
|
|
|
localStorage.setItem(storageKey.language, this.editObject.lang)
|
|
|
|
|
window.location.reload()
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
this.esc(true)
|
|
|
|
|
} else {
|
2023-08-24 17:15:41 +08:00
|
|
|
this.$message.error(res.data.msg || res.data.message)
|
2021-06-11 10:00:22 +08:00
|
|
|
}
|
2023-09-15 15:15:14 +08:00
|
|
|
}).catch(err => {
|
|
|
|
|
this.blockOperation.save = false
|
2023-09-21 19:49:35 +08:00
|
|
|
const errMsg = err.response ? err.response : (err.message ? err.message : err.toString())
|
2023-09-15 15:15:14 +08:00
|
|
|
this.$message.error(_.get(errMsg, 'data.message', []))
|
2021-06-11 10:00:22 +08:00
|
|
|
})
|
|
|
|
|
} else {
|
2023-08-24 17:15:41 +08:00
|
|
|
axios.post(this.url, this.editObject).then(res => {
|
2021-06-11 10:00:22 +08:00
|
|
|
this.blockOperation.save = false
|
2023-08-24 17:15:41 +08:00
|
|
|
if (res.status === 200) {
|
2024-04-08 11:06:28 +08:00
|
|
|
this.$message({
|
|
|
|
|
duration: 2000,
|
|
|
|
|
type: 'success',
|
|
|
|
|
message: this.$t('tip.saveSuccess')
|
|
|
|
|
})
|
2021-06-11 10:00:22 +08:00
|
|
|
this.esc(true)
|
|
|
|
|
} else {
|
2023-08-24 17:15:41 +08:00
|
|
|
this.$message.error(res.data.msg || res.data.message)
|
2021-06-11 10:00:22 +08:00
|
|
|
}
|
2023-09-15 15:15:14 +08:00
|
|
|
}).catch((err) => {
|
|
|
|
|
this.blockOperation.save = false
|
2023-09-15 16:54:36 +08:00
|
|
|
if (_.get(err, 'response.data.code') === 511036) {
|
|
|
|
|
// TODO 改成国际化
|
|
|
|
|
this.$message.error('This login name is already in use')
|
|
|
|
|
} else {
|
|
|
|
|
const errMsg = err.response ? err.response : (err.message ? err.message : err.toString())
|
|
|
|
|
this.$message.error(_.get(errMsg, 'data.message', []))
|
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.blockOperation.save = false
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-06-16 10:35:09 +08:00
|
|
|
},
|
2021-06-17 16:56:50 +08:00
|
|
|
getRoleData () {
|
2023-08-24 17:15:41 +08:00
|
|
|
axios.get(api.role, { pageSize: -1 }).then(response => {
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
this.roleData = _.get(response, 'data.data.list', [])
|
2021-06-16 10:35:09 +08:00
|
|
|
}
|
|
|
|
|
})
|
2021-06-11 10:00:22 +08:00
|
|
|
}
|
2021-06-16 15:01:58 +08:00
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
object: {
|
|
|
|
|
deep: true,
|
|
|
|
|
immediate: true,
|
|
|
|
|
handler (n) {
|
|
|
|
|
this.editObject = JSON.parse(JSON.stringify(n))
|
2021-06-17 16:56:50 +08:00
|
|
|
if (!this.editObject.roleIds && this.editObject.roles) {
|
2021-06-16 15:01:58 +08:00
|
|
|
this.editObject.roleIds = this.editObject.roles[0].id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|