This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nezha-fronted/nezha-fronted/src/components/common/rightBox/administration/userBox.vue

237 lines
8.7 KiB
Vue
Raw Normal View History

<template>
2021-06-02 17:16:00 +08:00
<div v-clickoutside="{obj: editUser, func: esc}" class="right-box right-box-user">
<div class="right-box__header">
<div class="header__title">{{editUser.id ? $t('config.user.editUser') : $t('config.user.createUser')}}</div>
<div class="header__operation">
<span v-cancel="{obj: editUser, func: esc}"><i class="nz-icon nz-icon-close"></i></span>
</div>
</div>
<div class="right-box__container">
<div class="container__form">
2021-05-19 23:17:24 +08:00
<el-form ref="userForm" :model="editUser" :rules="editUser.id ? rules2 : rules" label-position="top" label-width="120px">
<!--name-->
<el-form-item :label="$t('config.user.name')" prop="name">
<el-input id="account-input-name" v-model="editUser.name" :disabled="editUser.username==='admin' && editUser.id==1"
maxlength="64" placeholder="" show-word-limit size="small" type="text"></el-input>
</el-form-item>
<!--username-->
<el-form-item :label="$t('config.user.username')" prop="username">
<el-input id="account-input-username" v-model="editUser.username" :disabled="editUser.username==='admin' && editUser.id==1"
maxlength="64" placeholder="" show-word-limit size="small" type="text"></el-input>
</el-form-item>
<!--password-->
<el-form-item :label="$t('config.user.pin')" prop="pin">
<el-input id="account-input-password" v-model="editUser.pin" maxlength="64" placeholder=""
show-word-limit size="small" type="password" @blur="pinBlur" autocomplete="new-password"></el-input>
</el-form-item>
<!--pinChange-->
<el-form-item :label="$t('config.user.confirmPin')" label-width="200px" prop="pinChange">
<el-input id="account-input-pinChange" v-model="editUser.pinChange" maxlength="64" placeholder=""
show-word-limit size="small" type="password"></el-input>
</el-form-item>
<!--email-->
<el-form-item label="E-mail" prop="email">
<el-input id="account-input-email" v-model="editUser.email" maxlength="64" show-word-limit placeholder="" size="small" type="text"></el-input>
</el-form-item>
<!--mobile-->
<el-form-item :label="$t('config.user.mobile')" prop="mobile">
<el-input id="account-input-mobile" v-model.number="editUser.mobile" maxlength="64" show-word-limit placeholder="" size="small" type="text"></el-input>
</el-form-item>
<!--roles-->
<el-form-item :label="$t('config.user.roles')" prop="roleIds">
2021-05-19 23:17:24 +08:00
<el-select id="account-input-roleIds"
v-model="editUser.roleIds"
:disabled="(editUser.username==='admin') && editUser.id==1"
class="right-box__select"
clearable
collapse-tags
placeholder=""
popper-class="right-box-select-dropdown prevent-clickoutside"
size="small"
@change="()=>{this.$forceUpdate()}">
<template v-for="role in roles">
<!-- <el-option :key="role.id" :label="role.i18n?$t(role.i18n+''):role.name" :value="role.id"></el-option>-->
<el-option :key="role.id" :label="role.name" :value="role.id"></el-option>
</template>
</el-select>
</el-form-item>
<!--enable-->
<el-form-item :label="$t('config.user.enable')">
<el-switch id="account-input-status" v-model="editUser.status" :disabled="isCurrentUser(editUser.username) || (editUser.username==='admin' && editUser.id==1) " active-color="#ee9d3f" active-value="1"
inactive-value="0">
</el-switch>
</el-form-item>
<el-form-item v-if="editUser.id" :label="$t('config.user.createTime')">
<div class="right-box-form-content-txt">{{editUser.createAt}}</div>
</el-form-item>
</el-form>
</div>
</div>
<div class="right-box__footer">
<button id="asset-edit-cancel" v-cancel="{obj: editUser, func: esc}" class="footer__btn footer__btn--light">
<span>{{$t('overall.cancel')}}</span>
</button>
<button id="asset-edit-save" :class="{'footer__btn--disabled': prevent_opt.save}" :disabled="prevent_opt.save" class="footer__btn" @click="save">
<span>{{$t('overall.save')}}</span>
</button>
</div>
</div>
</template>
<script>
// import { host, port } from '@/components/common/js/validate'
2021-07-05 14:43:47 +08:00
import editRigthBox from '../../mixin/editRigthBox'
2021-04-22 18:03:56 +08:00
export default {
name: 'userBox',
components: {
},
props: {
obj: {
type: Object
}
},
2021-07-05 14:43:47 +08:00
mixins: [editRigthBox],
2021-04-22 18:03:56 +08:00
computed: {
isCurrentUser () {
return function (username) {
return localStorage.getItem('nz-username') == username
}
}
},
data () {
const vm = this
const validatePin = (rule, value, callback) => { // 确认密码的二次校验
2021-04-22 18:03:56 +08:00
if (value === '' && this.editUser.pin) {
callback(new Error(this.$t('config.user.inputConfirmPin')))
2021-04-22 18:03:56 +08:00
} else if (value !== this.editUser.pin) {
callback(new Error(this.$t('config.user.confirmPinErr')))
2021-04-22 18:03:56 +08:00
} else {
callback()
}
}
2021-04-22 18:03:56 +08:00
return {
editUser: {},
url: 'sys/user',
rightBox: { model: { show: false } },
rules: { // 表单校验规则
name: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
username: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
pin: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
pinChange: [
{ validator: validatePin, trigger: 'blur' },
2021-04-22 18:03:56 +08:00
{ required: true, message: '', trigger: 'blur' }
],
roleIds: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
email: [
{ type: 'email', message: this.$t('validate.email') }
]
},
2021-04-22 18:03:56 +08:00
rules2: { // 表单校验规则
username: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
pinChange: [
{ validator: validatePin, trigger: 'blur' }
2021-04-22 18:03:56 +08:00
],
roleIds: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
email: [
{ type: 'email', message: this.$t('validate.email') }
]
},
2021-04-22 18:03:56 +08:00
roles: []
}
},
watch: {
obj: {
deep: true,
immediate: true,
handler (n) {
this.isEdit = true
2021-04-22 18:03:56 +08:00
this.editUser = JSON.parse(JSON.stringify(n))
// if (this.editUser.roles && this.editUser.roles.length > 0) {
// this.editUser.roleIds = this.editUser.roles.map(t => t.id)
// }
}
2021-04-22 18:03:56 +08:00
}
},
mounted () {
this.getRoles()
},
methods: {
clickOutside () {
this.esc(false)
},
2021-04-22 18:03:56 +08:00
/* 关闭弹框 */
esc (refresh) {
this.prevent_opt.save = false
this.$emit('close', refresh)
},
2021-04-22 18:03:56 +08:00
/* 密码失去焦点 检验确认密码 */
pinBlur () {
2021-04-22 18:03:56 +08:00
if (this.editUser.pin && this.editUser.pinChange) {
this.$refs.accountForm.validateField('pinChange')
2021-04-22 18:03:56 +08:00
}
},
2021-04-22 18:03:56 +08:00
getRoles () {
this.roles = []
this.$get('sys/role?pageSize=-1').then(response => {
if (response.code === 200) {
this.roles = response.data.list
} else {
this.$message.error('load roles faild')
}
2021-04-22 18:03:56 +08:00
})
},
save () {
if (this.prevent_opt.save) { return } ;
this.prevent_opt.save = true
2021-04-22 18:03:56 +08:00
this.$refs.userForm.validate((valid) => {
if (valid) {
if (this.editUser.id) {
this.$put(this.url, this.editUser).then(res => {
this.prevent_opt.save = false
if (res.code === 200) {
this.$message({ duration: 2000, type: 'success', message: this.$t('tip.saveSuccess') })
this.esc(true)
} else {
this.$message.error(res.msg)
}
})
} else {
2021-04-22 18:03:56 +08:00
this.$post(this.url, this.editUser).then(res => {
this.prevent_opt.save = false
if (res.code === 200) {
this.$message({ duration: 2000, type: 'success', message: this.$t('tip.saveSuccess') })
this.esc(true)
} else {
this.$message.error(res.msg)
}
})
}
2021-04-22 18:03:56 +08:00
} else {
this.prevent_opt.save = false
return false
}
})
}
}
2021-04-22 18:03:56 +08:00
}
</script>
<style lang="scss">
@import '@/assets/css/common/rightBoxCommon.scss';
</style>