2019-12-22 09:37:52 +08:00
|
|
|
|
<template>
|
|
|
|
|
<a-modal
|
|
|
|
|
title="新建用户"
|
|
|
|
|
:width="640"
|
|
|
|
|
:visible="visible"
|
|
|
|
|
:confirmLoading="confirmLoading"
|
|
|
|
|
@ok="handleSubmit"
|
|
|
|
|
@cancel="handleCancel"
|
|
|
|
|
>
|
|
|
|
|
<a-spin :spinning="confirmLoading">
|
|
|
|
|
<a-form :form="form">
|
|
|
|
|
<a-form-item
|
|
|
|
|
label="用户名"
|
|
|
|
|
type="text"
|
|
|
|
|
:labelCol="labelCol"
|
|
|
|
|
:wrapperCol="wrapperCol"
|
|
|
|
|
>
|
|
|
|
|
<a-input v-decorator="['username', {rules: [{required: true, min: 3, message: '请输入至少3个字符!'}]}]" />
|
|
|
|
|
</a-form-item>
|
|
|
|
|
<a-form-item
|
|
|
|
|
label="邮箱"
|
|
|
|
|
:labelCol="labelCol"
|
|
|
|
|
:wrapperCol="wrapperCol"
|
|
|
|
|
>
|
|
|
|
|
<a-input v-decorator="['email', {rules: [{ validator: handleEmail }]}]" />
|
|
|
|
|
</a-form-item>
|
|
|
|
|
<a-form-item
|
|
|
|
|
label="密码"
|
|
|
|
|
:labelCol="labelCol"
|
|
|
|
|
:wrapperCol="wrapperCol"
|
|
|
|
|
>
|
|
|
|
|
<a-input type="password" v-decorator="['password', {rules: [{required: true, min: 5, message: '请输入密码'}]}]" />
|
|
|
|
|
</a-form-item>
|
|
|
|
|
<a-form-item
|
|
|
|
|
label="确认密码"
|
|
|
|
|
:labelCol="labelCol"
|
|
|
|
|
:wrapperCol="wrapperCol"
|
|
|
|
|
>
|
|
|
|
|
<a-input type="password" v-decorator="['passwordConfirm', {rules: [{required: true, min: 5, message: '请确认密码'}]}]" />
|
|
|
|
|
</a-form-item>
|
|
|
|
|
</a-form>
|
|
|
|
|
</a-spin>
|
|
|
|
|
</a-modal>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import { validEmail } from '@/utils/validate'
|
2019-12-25 22:12:16 +08:00
|
|
|
|
import { store, update } from '@/api/user'
|
|
|
|
|
import pick from 'lodash.pick'
|
2019-12-22 09:37:52 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
data () {
|
|
|
|
|
return {
|
|
|
|
|
labelCol: {
|
|
|
|
|
xs: { span: 24 },
|
|
|
|
|
sm: { span: 7 }
|
|
|
|
|
},
|
|
|
|
|
wrapperCol: {
|
|
|
|
|
xs: { span: 24 },
|
|
|
|
|
sm: { span: 13 }
|
|
|
|
|
},
|
|
|
|
|
visible: false,
|
|
|
|
|
confirmLoading: false,
|
2019-12-25 22:12:16 +08:00
|
|
|
|
id: null,
|
2019-12-22 09:37:52 +08:00
|
|
|
|
form: this.$form.createForm(this)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
add () {
|
|
|
|
|
this.visible = true
|
|
|
|
|
},
|
2019-12-25 22:12:16 +08:00
|
|
|
|
edit (record) {
|
|
|
|
|
this.visible = true
|
|
|
|
|
const { form: { setFieldsValue } } = this
|
|
|
|
|
this.id = record.id
|
|
|
|
|
setFieldsValue(pick(record, ['username', 'email']))
|
|
|
|
|
},
|
2019-12-22 09:37:52 +08:00
|
|
|
|
handleEmail (rule, value, callback) {
|
|
|
|
|
if (!validEmail(value)) {
|
|
|
|
|
callback(new Error('邮箱地址不正确'))
|
|
|
|
|
}
|
|
|
|
|
callback()
|
|
|
|
|
},
|
|
|
|
|
handleSubmit () {
|
|
|
|
|
const { form: { validateFields } } = this
|
|
|
|
|
this.confirmLoading = true
|
2019-12-25 22:12:16 +08:00
|
|
|
|
if (this.id) {
|
|
|
|
|
validateFields(['username', 'email'], (errors, values) => {
|
|
|
|
|
if (!errors) {
|
|
|
|
|
update(this.id, values).then((res) => {
|
|
|
|
|
this.refresh(res.message)
|
|
|
|
|
}).catch(err => this.failed(err))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
validateFields((errors, values) => {
|
|
|
|
|
if (!errors) {
|
|
|
|
|
store(values).then((res) => {
|
|
|
|
|
this.refresh(res.message)
|
|
|
|
|
}).catch(err => this.failed(err))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-12-22 09:37:52 +08:00
|
|
|
|
},
|
|
|
|
|
failed (errors) {
|
|
|
|
|
this.$notification['error']({
|
|
|
|
|
message: errors.message,
|
|
|
|
|
duration: 4
|
|
|
|
|
})
|
2019-12-25 22:12:16 +08:00
|
|
|
|
this.handleCancel()
|
|
|
|
|
},
|
|
|
|
|
handleCancel (message) {
|
|
|
|
|
this.id = null
|
|
|
|
|
this.visible = false
|
2019-12-22 09:37:52 +08:00
|
|
|
|
this.confirmLoading = false
|
2019-12-25 22:12:16 +08:00
|
|
|
|
this.form.resetFields()
|
2019-12-22 09:37:52 +08:00
|
|
|
|
},
|
2019-12-25 22:12:16 +08:00
|
|
|
|
refresh (message) {
|
|
|
|
|
this.$notification['success']({
|
|
|
|
|
message: message,
|
|
|
|
|
duration: 4
|
|
|
|
|
})
|
2019-12-22 09:37:52 +08:00
|
|
|
|
this.visible = false
|
2019-12-25 22:12:16 +08:00
|
|
|
|
this.form.resetFields()
|
|
|
|
|
this.id = null
|
|
|
|
|
this.$parent.$parent.handleOk()
|
2019-12-22 09:37:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|