accountList.vue
5.37 KB
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
<!--
* @Description: 账户管理页面
* @Author: huyue
* @Date: 2023-02-21 17:16:18
* @LastEditors: huyue
* @LastEditTime: 2023-07-24 18:23:05
-->
<template>
<div class="wrapper-content">
<div>
<el-input
:size="data.size"
class="search-input"
v-model="data.search"
clearable
placeholder="请输入要搜索的用户名"
></el-input>
<el-button
type="primary"
:size="data.size"
@click="searchList({ name: data.search })"
>搜索</el-button
><el-button type="primary" :size="data.size" @click="creatAccout"
>新建用户</el-button
>
</div>
<el-table
:data="data.tableData"
style="width: 100%"
border
:row-class-name="tableRowClassName"
>
<el-table-column prop="updateDate" label="更新时间" width="250" />
<el-table-column prop="acount" label="用户名" width="180" />
<el-table-column prop="auth" label="权限" width="180">
<template #default="scope">
{{ scope.row.auth == 'write' ? '编辑' : '查看' }}
</template>
</el-table-column>
<el-table-column label="操作">
<template #default="scope">
<el-input
v-show="data.showPassword[scope.$index]"
class="pwd-input"
:size="data.size"
v-model="data.password[scope.$index]"
placeholder="请输入新密码"
></el-input>
<el-button
:size="data.size"
v-show="
!data.showPassword[scope.$index] && scope.row.acount != 'admin'
"
@click="handleEdit(scope.row, scope.$index)"
>修改密码</el-button
>
<el-button
:size="data.size"
v-show="data.showPassword[scope.$index]"
@click="handleEdit(scope.row, scope.$index)"
>确认修改密码</el-button
>
<el-button
:size="data.size"
v-show="data.showPassword[scope.$index]"
@click="handleCancel(scope.$index)"
>取消</el-button
>
</template>
</el-table-column>
</el-table>
<createAccount
v-if="data.createModalVisible"
v-model="data.createModalVisible"
@confirm="confirm"
/>
</div>
</template>
<script lang="ts" setup>
import { onMounted, reactive } from "vue";
import { getAccountList, userEdit } from "@/api/login";
import { ElMessage, ElMessageBox } from "element-plus";
import createAccount from "./createAccount.vue";
// 表格内用户数据接口
interface User {
id: number;
acount: string;
name: string;
updateDate: string;
auth: string;
}
// data数据接口
interface DataType {
tableData: User[];
password: string[];
showPassword: boolean[];
search: string;
size: string;
createModalVisible: boolean;
}
const tableRowClassName = ({ rowIndex }: { row: User; rowIndex: number }) => {
if (rowIndex % 3 == 2) {
return "warning-row";
} else if (rowIndex % 3 == 1) {
return "success-row";
}
return "";
};
const data = reactive<DataType>({
tableData: [],
password: [],
showPassword: [],
search: "",
size: "small",
createModalVisible: false,
});
// 修改密码
const handleEdit = async (scope: User, index: number) => {
console.log(scope, data.showPassword[index]);
let showPassword = data.showPassword[index];
if (showPassword) {
if (!data.password[index]) {
ElMessage.error("请输入需要修改的用户新密码");
return;
}
ElMessageBox.confirm("您确定要修改此用户密码吗?", "Warning", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(async () => {
// 确认修改密码
const params = {
password: data.password[index],
name: scope.name,
id: scope.id,
};
console.log(params);
let { code, message } = await userEdit(params);
if (code == 200) {
ElMessage.success(message);
data.showPassword[index] = !data.showPassword[index];
data.password[index] = "";
} else {
ElMessage.error(message);
}
})
.catch(() => {
ElMessage({
type: "info",
message: "取消修改",
});
});
} else {
data.showPassword[index] = !data.showPassword[index];
}
};
const handleCancel = (index: number) => {
data.showPassword[index] = false;
data.password[index] = "";
};
// 搜索
const searchList = async (params?: { name: string }) => {
const list = await getAccountList(params);
data.tableData = list.data;
};
// 新建
const creatAccout = async () => {
data.createModalVisible = true;
};
const confirm = () => {
searchList();
};
onMounted(() => {
searchList();
});
</script>
<style>
.el-table .warning-row {
--el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
--el-table-tr-bg-color: var(--el-color-success-light-9);
}
.wrapper-content {
width: 800px;
margin: 20px auto;
}
.search-input {
width: 300px;
margin-bottom: 20px;
margin-right: 20px;
}
.pwd-input {
width: 150px;
/* margin-right: 10px; */
}
</style>