马宇豪
2024-08-16 3b75798cc41993f9016610296907d2338545a345
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<template>
    <div class="system-user-container">
        <el-card shadow="hover">
            <div class="system-user-search mb15">
                <el-cascader
                    v-model="userTableData.listQuery.searchParams.depId"
                    :props="props"
                    :options="departmentList"
                    :show-all-levels="false"
                    placeholder="请选择部门"
                    clearable
                    size="default"
                ></el-cascader>
                <el-input size="default" v-model.trim="userTableData.listQuery.searchParams.username" placeholder="请输入用户名" style="max-width: 180px;margin-left: 10px;margin-right: 10px"> </el-input>
<!--                <el-input size="default" v-model.trim="userTableData.listQuery.searchParams.realName" placeholder="请输入真实姓名" style="max-width: 180px"> </el-input>-->
                <el-button size="default" type="primary" class="ml10" @click="initUserTableData">
                    <el-icon>
                        <ele-Search />
                    </el-icon>
                    查询
                </el-button>
                <el-button size="default" type="success" class="ml10" @click="onOpenUserDialog('新增', '')">
                    <el-icon>
                        <ele-FolderAdd />
                    </el-icon>
                    新增用户
                </el-button>
            </div>
            <el-table :data="userTableData.data" style="width: 100%">
                <el-table-column type="index" label="序号" width="60" />
                <el-table-column prop="username" label="用户名" show-overflow-tooltip></el-table-column>
                <el-table-column prop="realName" label="真实姓名" show-overflow-tooltip></el-table-column>
                <el-table-column prop="gender" label="性别" show-overflow-tooltip>
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.gender == 1">男</el-tag>
                        <el-tag type="success" v-else-if="scope.row.gender == 0">女</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="positionId" label="职务" show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.positions?.map(i=>i.positionName).join('、') }}
                    </template>
                </el-table-column>
<!--                <el-table-column prop="type" label="职务" show-overflow-tooltip>-->
<!--                    <template #default="scope">-->
<!--                        {{ `${userTypeList.find(item => item.id === scope.row.type)?.name}` }}-->
<!--                    </template>-->
<!--                </el-table-column>-->
                <el-table-column prop="depName" label="部门" show-overflow-tooltip></el-table-column>
                <el-table-column prop="phone" label="手机号" show-overflow-tooltip></el-table-column>
                <el-table-column prop="email" label="邮箱" show-overflow-tooltip></el-table-column>
                <el-table-column prop="status" label="用户状态" show-overflow-tooltip>
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.status">启用</el-tag>
                        <el-tag type="info" v-else>禁用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="gmtCreate" label="创建时间" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" width="200">
                    <template #default="scope">
                        <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onOpenUserDialog('查看', scope.row)">查看</el-button>
                        <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onOpenUserDialog('修改', scope.row)">修改</el-button>
                        <el-button size="small" text type="primary" @click="onPwdDialog(scope.row.uid)">修改密码</el-button>
                        <el-button size="small" text type="primary" @click="onCertificate(scope.row)">证书管理</el-button>
                        <el-button style="color: red" :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br />
            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="page-position" :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="userTableData.listQuery.pageIndex" background v-model:page-size="userTableData.listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="userTableData.total"> </el-pagination>
            <br />
            <br />
        </el-card>
        <userDialog ref="userRef" @getUserList="initUserTableData" />
        <dialog-certificate ref="ctfRef" @getUserList="initUserTableData"></dialog-certificate>
      <el-dialog title="修改密码" v-model="showPwdDialog" width="500px">
        <el-form :model="pwdForm" size="default" ref="pwdRef" :rules="pwdFormRules" label-width="110px">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model.trim="pwdForm.newPassword" placeholder="请输入" type="password" show-password>
              </el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="rePassword">
              <el-input v-model.trim="pwdForm.rePassword" placeholder="请输入" type="password" show-password>
              </el-input>
            </el-form-item>
          </el-col>
        </el-form>
        <template #footer>
                <span class="dialog-footer">
                    <el-button @click="showPwdDialog = !showPwdDialog" size="default">取 消</el-button>
                    <el-button type="primary" v-throttle @click="onPwdSubmit" size="default">确 定</el-button>
                </span>
        </template>
      </el-dialog>
    </div>
</template>
 
<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import userDialog from '/@/views/system/user/component/userDialog.vue';
import dialogCertificate from "/@/views/system/user/component/dialogCertificate.vue";
import { userApi } from '/@/api/systemManage/user';
import { dutyApi } from '/@/api/systemManage/duty';
import { departmentApi } from '/@/api/systemManage/department';
import { useRoleApi } from '/@/api/systemManage/role';
import {verifyPwd} from "/@/utils/toolsValidate";
import {useLoginApi} from "/@/api/login";
import {Session} from "/@/utils/storage";
import Cookies from "js-cookie";
 
// 定义接口来定义对象的类型
interface TableDataRow {
    userName: string;
    userNickname: string;
    roleSign: string;
    department: string[];
    phone: string;
    email: string;
    sex: string;
    password: string;
    overdueTime: Date;
    status: boolean;
    describe: string;
    createTime: string;
}
interface DepartmentDataRow {}
interface TableDataState {
    userTableData: {
        data: Array<TableDataRow>;
        total: number;
        loading: boolean;
        listQuery: {
            searchParams: {
                depId: string | null;
                username: string | null;
                realName: string | null;
            };
            pageIndex: number;
            pageSize: number;
        };
    };
    departmentList: [];
    roleList: [];
    dutyList: [];
    userTypeList: Array<{id:number,name:string}>;
    props:{}
    showPwdDialog: boolean
    pwdForm: {
      uid: number|string| null
      newPassword: string
      rePassword: string
    }
    pwdFormRules: {}
}
 
export default defineComponent({
    name: 'systemUser',
    components: { userDialog,dialogCertificate },
    setup() {
        const userRef = ref();
        const pwdRef = ref();
        const ctfRef = ref()
        let validatePwd = (rule: any, value: any, callback: any)=>{
          if(value === ''){
            callback(new Error('请输入密码'))
          }else{
            if(!verifyPwd(value)){
              callback(new Error('密码须包含字母、数字、特殊字符,长度在6-16之间'))
            }else{
              callback()
            }
          }
        }
      const equalToPassword = (rule: any, value: any, callback: any) => {
        if(value == ''){
          callback(new Error("请再次确认密码"))
        }else if (state.pwdForm.newPassword !== value) {
          callback(new Error("两次输入的密码不一致"))
        } else {
          callback();
        }
      };
        const state = reactive<TableDataState>({
            userTableData: {
                data: [],
                total: 0,
                loading: false,
                listQuery: {
                    searchParams: {
                        depId: null,
                        username: null,
                        realName: null
                    },
                    pageIndex: 1,
                    pageSize: 10
                }
            },
            departmentList: [],
            roleList: [],
            dutyList: [],
            props: {
                label: 'depName',
                value: 'depId',
                checkStrictly: true,
                emitPath: false
            },
            userTypeList: [
                { id: 1, name: '超级管理员' },
                { id: 2, name: '管理员' },
                { id: 3, name: '普通员工' }
            ],
          showPwdDialog: false,
          pwdForm: {
            uid: null,
            newPassword: '',
            rePassword: ''
          },
          pwdFormRules: {
            newPassword: [{ required: true, validator: validatePwd, trigger: 'blur' }],
            rePassword: [{ required: true, validator: equalToPassword, trigger: "blur" }]
          }
        });
        // 初始化表格数据
        const initUserTableData = async () => {
            let res = await userApi().getUserList(state.userTableData.listQuery);
            if (res.data.code === '200') {
                state.userTableData.data = res.data.data;
                state.userTableData.total = res.data.total;
            } else {
                ElMessage({
                    type: 'warning',
                    message: res.data.msg
                });
            }
        };
 
        // 回显职务信息
        const parseNumber = (value: number) => {
            return state.dutyList.find((i) => i.positionId === value)?.positionName;
        };
        const getDepartmentData = async () => {
            let res = await departmentApi().getDepartmentList();
            if (res.data.code === '200') {
                state.departmentList = res.data.data;
            } else {
                ElMessage({
                    type: 'warning',
                    message: res.data.msg
                });
            }
        };
 
        const getRoleData = async () => {
            let res = await useRoleApi().getRoleList();
            if (res.data.code === '200') {
                state.roleList = res.data.data;
            } else {
                ElMessage({
                    type: 'warning',
                    message: res.data.msg
                });
            }
        };
 
        const getDutyData = async () => {
            let res = await dutyApi().getAllList({positionName: '',positionCode: ''});
            if (res.data.code === '200') {
                state.dutyList = res.data.data;
            } else {
                ElMessage({
                    type: 'warning',
                    message: res.data.msg
                });
            }
        };
 
        // 打开新增修改用户弹窗
        const onOpenUserDialog = (type: string, value: any) => {
            userRef.value.openDialog(type, value, state.departmentList, state.roleList, state.dutyList);
        };
 
        const onPwdDialog = (uid: string)=>{
          state.pwdForm = {
            uid: uid,
            newPassword: '',
            rePassword: ''
          }
          state.showPwdDialog = true
        }
 
        const onPwdSubmit = async () => {
          pwdRef.value.validate(async (valid:Boolean) => {
            if(valid){
              const {rePassword,...data} = state.pwdForm
              let res = await userApi().pwdMod(data);
              if (res.data.code === '200') {
                state.showPwdDialog = false;
                if(state.pwdForm.uid == Cookies.get('uid')){
                  ElMessage({
                    type: 'success',
                    message: '密码修改成功,请重新登录',
                    duration: 2000
                  })
                  setTimeout(()=>{
                    useLoginApi()
                        .signOut()
                        .then(() => {
                          Session.clear()
                          window.location.href = '/'
                        })
                  },2000)
                }else{
                  ElMessage({
                    type: 'success',
                    message: '修改密码成功',
                    duration: 2000
                  })
                }
              } else {
                ElMessage({
                  type: 'warning',
                  message: res.data.msg
                });
              }
            }else{
              ElMessage({
                type:'warning',
                message:'请完善信息'
              })
            }
          })
        }
        const onCertificate = (value: any)=>{
          ctfRef.value.openDialog(value);
        }
 
        // 删除用户
        const onRowDel = (row: TableDataRow) => {
            ElMessageBox.confirm(`此操作将永久删除账户名称:“${row.realName}”,是否继续?`, '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    let res = await userApi().deleteUser({ uid: row.uid });
                    if (res.data.code === '200') {
                        ElMessage({
                            type: 'success',
                            duration: 2000,
                            message: '删除成功'
                        });
                        await initUserTableData();
                    } else {
                        ElMessage({
                            type: 'warning',
                            message: res.data.msg
                        });
                    }
                })
                .catch(() => {});
        };
        // 分页改变
        const onHandleSizeChange = (val: number) => {
            state.userTableData.listQuery.pageSize = val;
            initUserTableData();
        };
        // 分页改变
        const onHandleCurrentChange = (val: number) => {
            state.userTableData.listQuery.pageIndex = val;
            initUserTableData();
        };
        // 页面加载时
        onMounted(() => {
            initUserTableData();
            getDepartmentData();
            getRoleData();
            getDutyData()
        });
        return {
            userRef,
            ctfRef,
          pwdRef,
          onPwdDialog,
          onPwdSubmit,
            onOpenUserDialog,
            onCertificate,
            onRowDel,
            parseNumber,
            onHandleSizeChange,
            initUserTableData,
            onHandleCurrentChange,
            ...toRefs(state)
        };
    }
});
</script>
<style  scoped>
:deep(.el-textarea.is-disabled .el-textarea__inner) {
    background-color: var(--el-card-bg-color);
    color: var(--el-input-text-color, var(--el-text-color-regular));
}
:deep(.el-input.is-disabled .el-input__inner) {
    color: var(--el-input-text-color, var(--el-text-color-regular));
}
:deep(.el-input.is-disabled .el-input__wrapper) {
    background-color: var(--el-card-bg-color);
}
</style>