马宇豪
2023-03-24 df39b348c7743e3275aca6053a46c2d63efc5bfb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<template>
    <div class="system-add-dept-container">
        <el-dialog :title="title" v-model="isShowDialog" width="600px">
            <el-form ref="ruleFormRef" :model="departmentForm" :rules="rules" size="default" label-width="90px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="部门等级" prop="depLevel">
                            <el-select v-model="departmentForm.depLevel" placeholder="请选择部门等级" class="input-add" clearable>
                                <el-option
                                    v-for="item in depLevelList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="上级部门" prop="parentDepId">
                            <el-cascader :options="deptData" class="input-add" :props="{ emitPath: false, checkStrictly: true, value: 'depId', label: 'depName' }" placeholder="请选择部门" clearable v-model="departmentForm.parentDepId"> </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="部门名称" prop="depName">
                            <el-input v-model="departmentForm.depName" class="input-add" placeholder="请输入部门名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                      <el-form-item label="部门编号" prop="depCode">
                        <el-input v-model="departmentForm.depCode" class="input-add" placeholder="请输入部门编号" clearable></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="部门描述">
                            <el-input v-model="departmentForm.depInfo" class="input-add" type="textarea" placeholder="请输入部门描述" maxlength="150"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel" size="default">取 消</el-button>
                    <el-button type="primary" v-throttle @click="onSubmit(ruleFormRef)" size="default">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
 
<script lang="ts">
import { ElMessage } from 'element-plus';
import { ref, reactive, toRefs, onMounted, defineComponent } from 'vue';
import { departmentApi } from '/@/api/systemManage/department';
import type { FormInstance, FormRules } from 'element-plus'
// 定义接口来定义对象的类型
interface TableDataRow {
    name: string;
    info: string;
    parentId: string;
    id: number;
}
interface DeptSate {
    title: string;
    isShowDialog: boolean;
    departmentForm: {
        depName: string;
        depCode: string;
        depInfo: string;
        parentDepId: string;
        depLevel:null | number
    };
    deptData: Array<TableDataRow>;
    depLevelList: Array<Type>
}
interface Type{
    id:number;
    name:string
}
 
export default defineComponent({
    name: 'systemAddDept',
    setup(prop, context) {
        const state = reactive<DeptSate>({
            title: '',
            isShowDialog: false,
            departmentForm: {
                depName: '',
                depCode: '',
                parentDepId: '',
                depInfo: '',
                depLevel:null,
            },
            deptData: [], // 部门数据
            depLevelList: [
                {id:1,name:'公司'},
                {id:2,name:'事业部'},
                {id:3,name:'车间'},
            ] // 部门数据
        });
        const checkCode = (rule: any, value: any, callback: any) => {
          if (!value) {
            return callback(new Error('请输入部门编号'))
          }
          setTimeout(() => {
            const regex = /^[a-zA-Z0-9]+$/
            if (regex.test(value)) {
              callback()
            } else {
              callback(new Error('部门编号只能有英文字母和数字构成'))
            }
          }, 1000)
        }
      const ruleFormRef = ref<FormInstance>()
        const rules = reactive<FormRules>({
          depName: [{ required: true, message: '请填写部门名称', trigger: 'blur' }],
          parentDepId: [{ required: true, message: '请选择上级部门', trigger: 'blur' }],
          depLevel: [{ required: true, message: '请选择部门等级', trigger: 'blur' }],
          depCode: [{ required: true, validator: checkCode, trigger: 'blur' }]
        });
        // 打开弹窗
        const openDialog = (type: string, value: any, departmentList: []) => {
            state.isShowDialog = true;
            state.deptData = JSON.parse(JSON.stringify(departmentList));
            if (type === '新增') {
                state.title = '新增部门';
                state.departmentForm = {
                    depName: '',
                    depCode: '',
                    parentDepId: '',
                    depLevel:null,
                    depInfo: ''
                };
            } else {
                state.title = '修改部门';
                state.departmentForm = JSON.parse(JSON.stringify(value));
            }
        };
        // 关闭弹窗
        const closeDialog = () => {
            state.isShowDialog = false;
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = async (formEl: FormInstance | undefined) => {
            if (!formEl) return
            await formEl.validate(async (valid, fields) => {
              if (valid) {
                if (state.title === '新增部门') {
                  let res = await departmentApi().addDepartment(state.departmentForm);
                  if (res.data.code === '200') {
                    ElMessage({
                      type: 'success',
                      message: '部门新增成功',
                      duration: 2000
                    });
                    closeDialog();
                    context.emit('getDepartmentList');
                  } else {
                    ElMessage({
                      type: 'warning',
                      message: res.data.msg
                    });
                  }
                } else {
                  let res = await departmentApi().modDepartment(state.departmentForm);
                  if (res.data.code === '200') {
                    ElMessage({
                      type: 'success',
                      message: '部门修改成功',
                      duration: 2000
                    });
                    closeDialog();
                    context.emit('getDepartmentList');
                  } else {
                    ElMessage({
                      type: 'warning',
                      message: res.data.msg
                    });
                  }
                }
              } else {
                console.log('error submit!', fields)
              }
            })
        };
        // 初始化部门数据
        const initTableData = () => {};
        // 页面加载时
        onMounted(() => {
            initTableData();
        });
        return {
            ruleFormRef,
            rules,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state)
        };
    }
});
</script>