hi-ucs/back/src/views/modules/sys/user-add-or-update.vue

398 lines
11 KiB
Vue
Raw Normal View History

2022-06-14 09:32:49 +08:00
<template>
2022-08-02 17:39:06 +08:00
<el-dialog
:visible.sync="visible"
:title="!dataForm.id ? $t('add') : $t('update')"
:close-on-click-modal="false"
:close-on-press-escape="false"
>
<el-form
:model="dataForm"
:rules="dataRule"
ref="dataForm"
@keyup.enter.native="dataFormSubmitHandle()"
label-width="120px"
>
<el-form-item
prop="username"
:label="$t('user.username')"
auto-complete="new-password"
>
<el-input
v-model="dataForm.username"
:placeholder="$t('user.username')"
auto-complete="new-password"
></el-input>
2022-06-14 09:32:49 +08:00
</el-form-item>
<el-form-item prop="deptName" :label="$t('user.deptName')">
2022-08-02 17:39:06 +08:00
<ren-dept-tree
v-model="dataForm.deptId"
:placeholder="$t('dept.title')"
:dept-name.sync="dataForm.deptName"
></ren-dept-tree>
2022-06-14 09:32:49 +08:00
</el-form-item>
2022-08-02 17:39:06 +08:00
<el-form-item
prop="password"
:label="$t('user.password')"
:class="{ 'is-required': !dataForm.id }"
auto-complete="new-password"
v-if="!dataForm.id"
>
<el-input
v-model="dataForm.password"
type="password"
:placeholder="$t('user.password')"
auto-complete="new-password"
@input="changeInput"
></el-input>
2022-06-14 09:32:49 +08:00
</el-form-item>
2022-08-02 17:39:06 +08:00
<el-form-item
prop="confirmPassword"
:label="$t('user.confirmPassword')"
:class="{ 'is-required': !dataForm.id }"
v-if="!dataForm.id"
>
<el-input
v-model="dataForm.confirmPassword"
type="password"
:placeholder="$t('user.confirmPassword')"
></el-input>
2022-06-14 09:32:49 +08:00
</el-form-item>
<el-form-item prop="realName" :label="$t('user.realName')">
2022-08-02 17:39:06 +08:00
<el-input
v-model="dataForm.realName"
:placeholder="$t('user.realName')"
></el-input>
2022-06-14 09:32:49 +08:00
</el-form-item>
<el-form-item prop="gender" :label="$t('user.gender')">
2022-08-02 17:39:06 +08:00
<ren-radio-group
v-model="dataForm.gender"
dict-type="gender"
></ren-radio-group>
2022-06-14 09:32:49 +08:00
</el-form-item>
<el-form-item prop="email" :label="$t('user.email')">
2022-08-02 17:39:06 +08:00
<el-input
v-model="dataForm.email"
:placeholder="$t('user.email')"
></el-input>
2022-06-14 09:32:49 +08:00
</el-form-item>
<el-form-item prop="mobile" :label="$t('user.mobile')">
2022-08-02 17:39:06 +08:00
<el-input
v-model="dataForm.mobile"
:placeholder="$t('user.mobile')"
></el-input>
2022-06-14 09:32:49 +08:00
</el-form-item>
2022-08-02 17:39:06 +08:00
<el-form-item
prop="roleIdList"
:label="$t('user.roleIdList')"
class="role-list"
>
<el-select
v-model="dataForm.roleIdList"
multiple
:placeholder="$t('user.roleIdList')"
>
<el-option
v-for="role in roleList"
:key="role.id"
:label="role.name"
:value="role.id"
></el-option>
2022-06-14 09:32:49 +08:00
</el-select>
</el-form-item>
2022-08-02 17:39:06 +08:00
<!-- 所在岗位屏蔽掉bug729 -->
<!-- <el-form-item prop="postIdList" :label="$t('user.postIdList')" class="role-list">
2022-06-14 09:32:49 +08:00
<el-select v-model="dataForm.postIdList" multiple :placeholder="$t('user.postIdList')">
<el-option v-for="post in postList" :key="post.id" :label="post.postName" :value="post.id"></el-option>
</el-select>
2022-08-02 17:39:06 +08:00
</el-form-item> -->
2022-06-14 09:32:49 +08:00
<el-form-item prop="status" :label="$t('user.status')" size="mini">
<el-radio-group v-model="dataForm.status">
<el-radio :label="0">{{ $t('user.status0') }}</el-radio>
<el-radio :label="1">{{ $t('user.status1') }}</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
<template slot="footer">
<el-button @click="visible = false">{{ $t('cancel') }}</el-button>
2022-08-02 17:39:06 +08:00
<el-button type="primary" @click="dataFormSubmitHandle()">{{
$t('confirm')
}}</el-button>
2022-06-14 09:32:49 +08:00
</template>
</el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
2022-06-24 17:58:43 +08:00
import { Encrypt } from '@/utils/crypto'
2022-06-14 09:32:49 +08:00
import { isEmail, isMobile } from '@/utils/validate'
export default {
data () {
return {
2022-07-28 17:12:30 +08:00
submitFlag: true,
2022-06-14 09:32:49 +08:00
visible: false,
roleList: [],
roleIdListDefault: [],
postList: [],
dataForm: {
id: '',
username: '',
deptId: '',
deptName: '',
password: '',
confirmPassword: '',
realName: '',
gender: 0,
email: '',
mobile: '',
roleIdList: [],
postIdList: [],
status: 1
}
}
},
computed: {
dataRule () {
var validatePassword = (rule, value, callback) => {
2022-06-24 17:58:43 +08:00
if (!value) {
this.showLevel = false
return callback(new Error(this.$t('validate.pwdStrength')))
}
if (value.length < 8) {
this.showLevel = false
return callback(new Error(this.$t('validate.pwdStrength')))
}
const num = /^.*[0-9]+.*/
const low = /^.*[a-z]+.*/
const up = /^.*[A-Z]+.*/
const spe = /^.*[^a-zA-Z0-9]+.*/
2022-08-02 17:39:06 +08:00
console.log(
'包含数字',
num.test(value),
'包含字母',
low.test(value),
up.test(value),
'包含特殊符号',
spe.test(value)
)
if (
!(
num.test(value) &&
(low.test(value) || up.test(value)) &&
spe.test(value)
)
) {
2022-06-24 17:58:43 +08:00
return callback(new Error(this.$t('validate.pwdStrength')))
2022-06-14 09:32:49 +08:00
}
2022-06-24 17:58:43 +08:00
// if (!this.dataForm.id && !/\S/.test(value)) {
// return callback(new Error(this.$t('validate.required')))
// }
2022-06-14 09:32:49 +08:00
callback()
}
var validateConfirmPassword = (rule, value, callback) => {
if (!this.dataForm.id && !/\S/.test(value)) {
return callback(new Error(this.$t('validate.required')))
}
if (this.dataForm.password !== value) {
return callback(new Error(this.$t('user.validate.confirmPassword')))
}
callback()
}
var validateEmail = (rule, value, callback) => {
if (value && !isEmail(value)) {
2022-08-02 17:39:06 +08:00
return callback(
new Error(
this.$t('validate.format', { attr: this.$t('user.email') })
)
)
2022-06-14 09:32:49 +08:00
}
callback()
}
var validateMobile = (rule, value, callback) => {
if (value && !isMobile(value)) {
2022-08-02 17:39:06 +08:00
return callback(
new Error(
this.$t('validate.format', { attr: this.$t('user.mobile') })
)
)
2022-06-14 09:32:49 +08:00
}
callback()
}
return {
username: [
2022-08-02 17:39:06 +08:00
{
required: true,
message: this.$t('validate.required'),
trigger: 'blur'
}
2022-06-14 09:32:49 +08:00
],
deptName: [
2022-08-02 17:39:06 +08:00
{
required: true,
message: this.$t('validate.required'),
trigger: 'change'
}
2022-06-14 09:32:49 +08:00
],
2022-08-02 17:39:06 +08:00
password: [{ validator: validatePassword, trigger: 'blur' }],
2022-06-14 09:32:49 +08:00
confirmPassword: [
{ validator: validateConfirmPassword, trigger: 'blur' }
],
realName: [
2022-08-02 17:39:06 +08:00
{
required: true,
message: this.$t('validate.required'),
trigger: 'blur'
}
2022-06-14 09:32:49 +08:00
],
2022-08-02 17:39:06 +08:00
email: [{ validator: validateEmail, trigger: 'blur' }],
mobile: [{ validator: validateMobile, trigger: 'blur' }]
2022-06-14 09:32:49 +08:00
}
}
},
methods: {
2022-06-24 17:58:43 +08:00
changeInput (value) {
if (value !== value.replace(/ /g, '')) {
this.$message({
message: '密码中不允许存在空格!',
type: 'warning'
})
this.dataForm.password = value.replace(/ /g, '')
// console.log('密码发生变化=============>', value)
}
},
2022-06-14 09:32:49 +08:00
init () {
this.visible = true
this.dataForm.deptId = ''
this.$nextTick(() => {
this.$refs.dataForm.resetFields()
this.roleIdListDefault = []
2022-08-02 17:39:06 +08:00
Promise.all([this.getRoleList(), this.getPostList()]).then(() => {
2022-06-14 09:32:49 +08:00
if (this.dataForm.id) {
this.getInfo()
2022-07-28 17:12:30 +08:00
} else {
this.dataForm = {
id: '',
username: '',
deptId: '',
deptName: '',
password: '',
confirmPassword: '',
realName: '',
gender: 0,
email: '',
mobile: '',
roleIdList: [],
postIdList: [],
status: 1
}
2022-06-14 09:32:49 +08:00
}
})
})
},
// 获取角色列表
getRoleList () {
2022-08-02 17:39:06 +08:00
return this.$http
.get('/sys/role/list')
.then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.roleList = res.data
})
.catch(() => {})
2022-06-14 09:32:49 +08:00
},
// 获取岗位列表
getPostList () {
2022-08-02 17:39:06 +08:00
return this.$http
.get('/sys/post/list')
.then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.postList = res.data
})
.catch(() => {})
2022-06-14 09:32:49 +08:00
},
// 获取信息
getInfo () {
2022-08-02 17:39:06 +08:00
this.$http
.get(`/sys/user/${this.dataForm.id}`)
.then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
2022-06-14 09:32:49 +08:00
}
2022-08-02 17:39:06 +08:00
this.dataForm = {
...this.dataForm,
...res.data,
roleIdList: []
}
// 角色配置, 区分是否为默认角色
for (var i = 0; i < res.data.roleIdList.length; i++) {
if (
this.roleList.filter(
(item) => item.id === res.data.roleIdList[i]
)[0]
) {
this.dataForm.roleIdList.push(res.data.roleIdList[i])
continue
}
this.roleIdListDefault.push(res.data.roleIdList[i])
}
})
.catch(() => {})
2022-06-14 09:32:49 +08:00
},
// 表单提交
2022-08-02 17:39:06 +08:00
dataFormSubmitHandle: debounce(
function () {
this.$refs.dataForm.validate((valid) => {
if (!valid) {
return false
2022-06-14 09:32:49 +08:00
}
2022-08-02 17:39:06 +08:00
if (this.submitFlag) {
this.submitFlag = false
this.dataForm.password = Encrypt(this.dataForm.password)
if (this.dataForm.id) {
delete this.dataForm.password
2022-06-14 09:32:49 +08:00
}
2022-08-02 17:39:06 +08:00
this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/user', {
...this.dataForm,
roleIdList: [
...this.dataForm.roleIdList,
...this.roleIdListDefault
]
2022-07-28 17:12:30 +08:00
})
2022-08-02 17:39:06 +08:00
.then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.$message({
message: this.$t('prompt.success'),
type: 'success',
duration: 500,
onClose: () => {
this.visible = false
this.$emit('refreshDataList')
}
})
this.submitFlag = true
})
.catch(() => {})
}
})
},
1000,
{ leading: true, trailing: false }
)
2022-06-14 09:32:49 +08:00
}
}
</script>
<style lang="scss">
.mod-sys__user {
.role-list {
.el-select {
width: 100%;
}
}
}
</style>