|
@@ -0,0 +1,272 @@
|
|
|
+<template>
|
|
|
+ <el-dialog
|
|
|
+ v-model="show"
|
|
|
+ :title="visible === 1 ? '新增' : '编辑'"
|
|
|
+ :close-on-click-modal="false"
|
|
|
+ :close-on-press-escape="false"
|
|
|
+ :before-close="close"
|
|
|
+ >
|
|
|
+ <el-form
|
|
|
+ ref="formRef"
|
|
|
+ v-model:model="form"
|
|
|
+ :rules="rules"
|
|
|
+ label-width="100"
|
|
|
+ >
|
|
|
+ <el-form-item
|
|
|
+ prop="username"
|
|
|
+ label="登录账号"
|
|
|
+ >
|
|
|
+ <el-input
|
|
|
+ v-model="form.username"
|
|
|
+ :disabled="loading"
|
|
|
+ ></el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item
|
|
|
+ prop="status"
|
|
|
+ label="状态"
|
|
|
+ >
|
|
|
+ <el-switch
|
|
|
+ v-model="form.status"
|
|
|
+ :active-value="1"
|
|
|
+ :inactive-value="0"
|
|
|
+ ></el-switch>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item
|
|
|
+ prop="name"
|
|
|
+ label="姓名"
|
|
|
+ >
|
|
|
+ <el-input
|
|
|
+ v-model="form.name"
|
|
|
+ :disabled="loading"
|
|
|
+ ></el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item
|
|
|
+ prop="email"
|
|
|
+ label="邮箱地址"
|
|
|
+ >
|
|
|
+ <el-input
|
|
|
+ v-model="form.email"
|
|
|
+ :disabled="loading"
|
|
|
+ ></el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item
|
|
|
+ prop="password"
|
|
|
+ label="密码"
|
|
|
+ >
|
|
|
+ <el-input
|
|
|
+ v-model="form.password"
|
|
|
+ :placeholder="visible === 2 ? '不更改密码直接留空' : ''"
|
|
|
+ :disabled="loading"
|
|
|
+ ></el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item
|
|
|
+ v-if="visible === 1"
|
|
|
+ prop="crm_user_id"
|
|
|
+ label="CRM用户ID"
|
|
|
+ >
|
|
|
+ <el-input
|
|
|
+ v-model="form.crm_user_id"
|
|
|
+ :disabled="loading"
|
|
|
+ ></el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item
|
|
|
+ v-if="visible === 2"
|
|
|
+ prop="admin_ids"
|
|
|
+ label="下属"
|
|
|
+ >
|
|
|
+ <el-select-v2
|
|
|
+ v-model="form.admin_ids"
|
|
|
+ :options="userList"
|
|
|
+ filterable
|
|
|
+ multiple
|
|
|
+ ></el-select-v2>
|
|
|
+ </el-form-item>
|
|
|
+ <!-- <el-form-item> -->
|
|
|
+ <div class="flex justify-center w-[100%] mt-8">
|
|
|
+ <el-button
|
|
|
+ type="primary"
|
|
|
+ @click="onSave"
|
|
|
+ >
|
|
|
+ 保存
|
|
|
+ </el-button>
|
|
|
+ <el-button @click="close">取消</el-button>
|
|
|
+ </div>
|
|
|
+ <!-- </el-form-item> -->
|
|
|
+ </el-form>
|
|
|
+ </el-dialog>
|
|
|
+</template>
|
|
|
+<script lang="ts" setup>
|
|
|
+import {
|
|
|
+ defineComponent,
|
|
|
+ ref,
|
|
|
+ watch,
|
|
|
+ computed,
|
|
|
+ defineProps,
|
|
|
+ defineEmits,
|
|
|
+ nextTick,
|
|
|
+} from 'vue'
|
|
|
+import {
|
|
|
+ ElDialog,
|
|
|
+ ElButton,
|
|
|
+ ElForm,
|
|
|
+ ElFormItem,
|
|
|
+ ElInput,
|
|
|
+ ElSelectV2,
|
|
|
+ ElSwitch,
|
|
|
+ ElMessage,
|
|
|
+} from 'element-plus'
|
|
|
+import type { FormInstance } from 'element-plus'
|
|
|
+import { CirclePlus } from '@element-plus/icons-vue'
|
|
|
+import debounce from 'lodash.debounce'
|
|
|
+import cloneDeep from 'lodash.clonedeep'
|
|
|
+import { $t } from '@/i18n/index'
|
|
|
+import userAPI from '@/api/user'
|
|
|
+
|
|
|
+defineComponent({
|
|
|
+ name: 'ComponentEditIndentUser',
|
|
|
+})
|
|
|
+const { visible = 0, formData = {} } = defineProps<{
|
|
|
+ visible: number
|
|
|
+ formData: object
|
|
|
+}>()
|
|
|
+const $emit = defineEmits(['update:visible', 'save'])
|
|
|
+let show = ref(false)
|
|
|
+let loading = ref(false)
|
|
|
+const formRef = ref<FormInstance>()
|
|
|
+let form = ref({ password: '' } as any)
|
|
|
+let rules = ref({} as any)
|
|
|
+const defaultRules = {
|
|
|
+ name: [
|
|
|
+ {
|
|
|
+ required: true,
|
|
|
+ message: $t('text_please_input'),
|
|
|
+ trigger: 'change',
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ username: [
|
|
|
+ {
|
|
|
+ required: true,
|
|
|
+ message: $t('text_please_input'),
|
|
|
+ trigger: 'change',
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ email: [
|
|
|
+ {
|
|
|
+ required: true,
|
|
|
+ message: $t('text_please_input'),
|
|
|
+ trigger: 'change',
|
|
|
+ },
|
|
|
+ ],
|
|
|
+}
|
|
|
+let userList = ref([])
|
|
|
+const getUserSelectListFunc = () => {
|
|
|
+ userAPI
|
|
|
+ .getUserSelectList()
|
|
|
+ .then((res: any) => {
|
|
|
+ if (res.code === 1)
|
|
|
+ userList.value = res.result
|
|
|
+ .map((i: any) => ({
|
|
|
+ value: i.id,
|
|
|
+ label: i.username || i.name,
|
|
|
+ }))
|
|
|
+ .filter((i: any) => i.value !== form.value.id)
|
|
|
+ })
|
|
|
+ .catch(() => console.log('获取用户下拉列表异常'))
|
|
|
+}
|
|
|
+const checkPassword = (rule: any, value: any, cb: any) => {
|
|
|
+ const length = value.trim().length
|
|
|
+ if (length < 6 && length > 0) {
|
|
|
+ return cb(new Error('密码长度须大于6'))
|
|
|
+ }
|
|
|
+ cb()
|
|
|
+}
|
|
|
+watch(
|
|
|
+ () => visible,
|
|
|
+ () => {
|
|
|
+ show.value = visible > 0
|
|
|
+ if (show.value) {
|
|
|
+ form.value = Object.assign({ password: '' }, cloneDeep(formData))
|
|
|
+ }
|
|
|
+
|
|
|
+ if (visible === 1) {
|
|
|
+ rules.value = Object.assign({}, defaultRules, {
|
|
|
+ crm_user_id: [
|
|
|
+ {
|
|
|
+ required: true,
|
|
|
+ message: $t('text_please_input'),
|
|
|
+ trigger: 'change',
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ password: [
|
|
|
+ {
|
|
|
+ required: true,
|
|
|
+ validator: checkPassword,
|
|
|
+ trigger: 'change',
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ })
|
|
|
+ } else if (visible === 2) {
|
|
|
+ rules.value = Object.assign({}, defaultRules, {
|
|
|
+ password: [
|
|
|
+ {
|
|
|
+ required: false,
|
|
|
+ validator: checkPassword,
|
|
|
+ trigger: 'change',
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ })
|
|
|
+ getUserSelectListFunc()
|
|
|
+ }
|
|
|
+ nextTick(() => {
|
|
|
+ formRef.value?.clearValidate()
|
|
|
+ })
|
|
|
+ },
|
|
|
+)
|
|
|
+let close = (done = {} as any) => {
|
|
|
+ $emit('update:visible', 0)
|
|
|
+ if (typeof done === 'function') done()
|
|
|
+}
|
|
|
+const onSave = () => {
|
|
|
+ if (!formRef.value) return
|
|
|
+ formRef.value.validate((valid) => {
|
|
|
+ if (!valid) {
|
|
|
+ ElMessage.error('请检查必填项')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const f = cloneDeep(form.value)
|
|
|
+ if (f.password.trim().length === 0) {
|
|
|
+ delete f.password
|
|
|
+ }
|
|
|
+ console.log('pass')
|
|
|
+ loading.value = true
|
|
|
+ if (visible === 1) addFunc(f)
|
|
|
+ if (visible === 2) editFunc(f)
|
|
|
+ })
|
|
|
+}
|
|
|
+const editFunc = (f: any) => {
|
|
|
+ userAPI
|
|
|
+ .editUser(f)
|
|
|
+ .then((res: any) => {
|
|
|
+ if (res.code !== 1) {
|
|
|
+ ElMessage.error(res.message || '编辑用户出错')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ $emit('save')
|
|
|
+ close()
|
|
|
+ })
|
|
|
+ .finally(() => (loading.value = false))
|
|
|
+}
|
|
|
+const addFunc = (f: any) => {
|
|
|
+ userAPI
|
|
|
+ .addUser(f)
|
|
|
+ .then((res: any) => {
|
|
|
+ if (res.code !== 1) {
|
|
|
+ ElMessage.error(res.message || '新增用户出错')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ $emit('save')
|
|
|
+ close()
|
|
|
+ })
|
|
|
+ .finally(() => (loading.value = false))
|
|
|
+}
|
|
|
+</script>
|