index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <div class="system-user-container">
  3. <el-row :gutter="10">
  4. <el-col :span="5">
  5. <el-card shadow="hover">
  6. <el-scrollbar>
  7. <el-input :prefix-icon="search" v-model="filterText" placeholder="请输入组织名称" clearable size="default" style="width: 100%;" />
  8. <el-tree ref="treeRef" class="filter-tree mt-4" :data="deptData" :props="deptProps" default-expand-all :filter-node-method="deptFilterNode" @node-click="handleNodeClick">
  9. <template #default="{ node, data }">
  10. <div class="custom-tree-node" :title="node.label">
  11. {{ node.label }}
  12. </div>
  13. </template></el-tree>
  14. </el-scrollbar>
  15. </el-card>
  16. </el-col>
  17. <el-col :span="19">
  18. <el-card shadow="hover">
  19. <div class="system-user-search mb15">
  20. <el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="68px">
  21. <el-form-item label="关键字" prop="keyWords">
  22. <el-input v-model="tableData.param.keyWords" placeholder="请输入用户名或姓名" clearable size="default" style="width: 240px" @keyup.enter.native="userList" />
  23. </el-form-item>
  24. <!-- <el-form-item label="手机号码" prop="mobile">-->
  25. <!-- <el-input v-model="tableData.param.mobile" placeholder="请输入手机号码" clearable size="default" style="width: 240px" @keyup.enter.native="userList" />-->
  26. <!-- </el-form-item>-->
  27. <el-form-item label="状态" prop="status" style="width: 200px;">
  28. <el-select v-model="tableData.param.status" placeholder="用户状态" size="default" style="width: 240px">
  29. <el-option label="全部" :value="-1" />
  30. <el-option label="启用" :value="1" />
  31. <el-option label="禁用" :value="0" />
  32. <el-option label="未验证" :value="2" />
  33. </el-select>
  34. </el-form-item>
  35. <el-form-item label="创建时间" prop="dateRange">
  36. <el-date-picker v-model="tableData.param.dateRange" size="default" style="width: 240px" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
  37. </el-form-item>
  38. <el-form-item>
  39. <el-button size="default" type="primary" class="ml10" @click="userList">
  40. <el-icon>
  41. <ele-Search />
  42. </el-icon>
  43. 查询
  44. </el-button>
  45. <el-button size="default" @click="resetQuery(queryRef)">
  46. <el-icon>
  47. <ele-Refresh />
  48. </el-icon>
  49. 重置
  50. </el-button>
  51. <el-button size="default" type="primary" class="ml10" @click="onOpenAddUser" v-auth="'add'">
  52. <el-icon>
  53. <ele-FolderAdd />
  54. </el-icon>
  55. 新增用户
  56. </el-button>
  57. <!-- <el-button size="default" type="info" class="ml10" @click="onRowDel(null)">
  58. <el-icon>
  59. <ele-Delete />
  60. </el-icon>
  61. 删除用户
  62. </el-button> -->
  63. </el-form-item>
  64. </el-form>
  65. </div>
  66. <el-table :data="tableData.data" style="width: 100%" v-loading="loading" @selection-change="handleSelectionChange">
  67. <!-- <el-table-column type="selection" width="55" align="center" /> -->
  68. <el-table-column type="index" label="序号" width="60" align="center" />
  69. <el-table-column prop="userName" label="用户名" v-col="'userName'" min-width="120" show-overflow-tooltip></el-table-column>
  70. <el-table-column prop="userNickname" label="姓名" v-col="'userNickname'" width="160" show-overflow-tooltip></el-table-column>
  71. <el-table-column prop="dept.deptName" label="组织" v-col="'deptName'" show-overflow-tooltip></el-table-column>
  72. <el-table-column label="角色" min-width="120" prop="rolesNames" v-col="'rolesNames'" :show-overflow-tooltip="true"></el-table-column>
  73. <el-table-column prop="mobile" label="手机号" v-col="'mobile'" width="120" align="center"></el-table-column>
  74. <el-table-column prop="status" label="用户状态" width="120" v-col="'status'" align="center">
  75. <template #default="scope">
  76. <el-switch v-model="scope.row.status" :disabled="scope.row.id === 1" v-auth="'change-status'" inline-prompt :active-value="1" :inactive-value="0" active-text="启" inactive-text="禁" @change="handleStatusChange(scope.row)">
  77. </el-switch>
  78. </template>
  79. </el-table-column>
  80. <el-table-column prop="createdAt" label="创建时间" width="180" v-col="'createdAt'" align="center"></el-table-column>
  81. <el-table-column label="操作" width="180" align="center" v-col="'handle'" fixed="right">
  82. <template #default="scope">
  83. <!-- <el-button size="small" text type="warning" @click="onOpenEditUser(scope.row)" v-auths="['edit','del']">修改</el-button>
  84. <el-button size="small" text type="warning" @click="onOpenEditUser(scope.row)" v-auth-all="['edit','del']">修改</el-button> -->
  85. <el-button size="small" text type="warning" @click="onOpenEditUser(scope.row)" v-auth="'edit'">修改</el-button>
  86. <el-button size="small" text type="info" @click="onRowDel(scope.row)" v-if="scope.row.id !== 1" v-auth="'del'">删除</el-button>
  87. <el-button size="small" text type="success" @click="handleResetPwd(scope.row)" v-auth="'reset'">重置</el-button>
  88. </template>
  89. </el-table-column>
  90. </el-table>
  91. <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="userList" />
  92. </el-card>
  93. </el-col>
  94. </el-row>
  95. <EditUser ref="editUserRef" :dept-data="deptData" :post-data="postData" :role-data="roleData" @getUserList="userList" />
  96. <EditPer ref="editPerRef" @getUserList="userList" />
  97. </div>
  98. </template>
  99. <script lang="ts">
  100. import { toRefs, reactive, onMounted, ref, defineComponent, watch } from 'vue';
  101. import { ElMessageBox, ElMessage, ElTree, FormInstance } from 'element-plus';
  102. import { Search } from '@element-plus/icons-vue';
  103. import EditUser from './component/editUser.vue';
  104. import api from '/@/api/system';
  105. import useCommon from '/@/hooks/useCommon';
  106. interface Tree {
  107. id: number;
  108. label: string;
  109. children?: Tree[];
  110. }
  111. interface TableDataState {
  112. ids: number[];
  113. loading: boolean;
  114. deptProps: {};
  115. deptData: any[];
  116. roleData: any[];
  117. postData: any[];
  118. tableData: {
  119. data: any[];
  120. total: number;
  121. param: {
  122. pageNum: number;
  123. pageSize: number;
  124. status: number;
  125. deptId: string;
  126. keyWords: string;
  127. mobile: string;
  128. userNickname: string;
  129. userName: string;
  130. dateRange: string[];
  131. };
  132. };
  133. }
  134. export default defineComponent({
  135. name: 'systemUser',
  136. components: { EditUser },
  137. setup() {
  138. const editUserRef = ref();
  139. const queryRef = ref();
  140. const filterText = ref('');
  141. const treeRef = ref<InstanceType<typeof ElTree>>();
  142. const search = Search;
  143. const { statusParams } = useCommon();
  144. const state = reactive<TableDataState>({
  145. loading: false,
  146. ids: [],
  147. deptProps: {
  148. id: 'deptId',
  149. children: 'children',
  150. label: 'deptName',
  151. },
  152. deptData: [],
  153. postData: [],
  154. roleData: [],
  155. tableData: {
  156. data: [],
  157. total: 0,
  158. param: {
  159. status: -1,
  160. pageNum: 1,
  161. pageSize: 10,
  162. deptId: '',
  163. userNickname: '',
  164. userName: '',
  165. keyWords: '',
  166. mobile: '',
  167. dateRange: [],
  168. },
  169. },
  170. });
  171. // 初始化表格数据
  172. const initTableData = () => {
  173. api.dept.getList(statusParams).then((res: any) => {
  174. state.deptData = res;
  175. });
  176. api.post.getList(statusParams).then((res: any) => {
  177. state.postData = res;
  178. });
  179. api.role.getList(statusParams).then((res: any) => {
  180. state.roleData = res;
  181. });
  182. userList();
  183. };
  184. const userList = () => {
  185. state.loading = true;
  186. api.user
  187. .getList(state.tableData.param)
  188. .then((res: any) => {
  189. state.tableData.data = res.list;
  190. state.tableData.total = res.total;
  191. })
  192. .finally(() => {
  193. state.loading = false;
  194. });
  195. };
  196. // 打开新增用户弹窗
  197. const onOpenAddUser = () => {
  198. editUserRef.value.openDialog();
  199. };
  200. // 打开修改用户弹窗
  201. const onOpenEditUser = (row: any) => {
  202. editUserRef.value.openDialog(row);
  203. };
  204. // 删除用户
  205. const onRowDel = (row: any) => {
  206. let msg = '你确定要删除所选用户?';
  207. // let ids: number[] = [];
  208. // if (row) {
  209. msg = `此操作将永久删除用户:“${row.userName}”,是否继续?`;
  210. // ids = [row.id];
  211. // } else {
  212. // ids = state.ids;
  213. // }
  214. // if (ids.length === 0) {
  215. // ElMessage.error('请选择要删除的数据。');
  216. // return;
  217. // }
  218. ElMessageBox.confirm(msg, '提示', {
  219. confirmButtonText: '确认',
  220. cancelButtonText: '取消',
  221. type: 'warning',
  222. })
  223. .then(() => {
  224. api.user.del(row.id).then(() => {
  225. ElMessage.success('删除成功');
  226. userList();
  227. });
  228. })
  229. .catch(() => { });
  230. };
  231. // 分页改变
  232. const onHandleSizeChange = (val: number) => {
  233. state.tableData.param.pageSize = val;
  234. };
  235. // 分页改变
  236. const onHandleCurrentChange = (val: number) => {
  237. state.tableData.param.pageNum = val;
  238. };
  239. // 页面加载时
  240. onMounted(() => {
  241. initTableData();
  242. });
  243. watch(filterText, (val) => {
  244. treeRef.value!.filter(val);
  245. });
  246. const deptFilterNode = (value: string, data: any) => {
  247. if (!value) return true;
  248. return data.deptName.includes(value);
  249. };
  250. // 多选框选中数据
  251. const handleSelectionChange = (selection: any[]) => {
  252. state.ids = selection.map((item) => item.id);
  253. };
  254. // 节点单击事件
  255. const handleNodeClick = (data: any) => {
  256. state.tableData.param.deptId = data.deptId;
  257. userList();
  258. };
  259. /** 重置密码按钮操作 */
  260. const handleResetPwd = (row: any) => {
  261. ElMessageBox.prompt('请输入"' + row.userName + '"的新密码', '提示', {
  262. confirmButtonText: '确定',
  263. cancelButtonText: '取消',
  264. })
  265. .then(({ value }) => {
  266. if (!value || value == '') {
  267. ElMessage.success('密码不能为空');
  268. return;
  269. }
  270. api.user.resetPassword(row.id, value).then(() => {
  271. ElMessage.success('修改成功,新密码是:' + value);
  272. });
  273. })
  274. .catch(() => { });
  275. };
  276. // 用户状态修改
  277. const handleStatusChange = (row: any) => {
  278. let text = row.status === 1 ? '启用' : '停用';
  279. ElMessageBox.confirm('确认要"' + text + '":"' + row.userName + '"用户吗?', '警告', {
  280. confirmButtonText: '确定',
  281. cancelButtonText: '取消',
  282. type: 'warning',
  283. })
  284. .then(function () {
  285. return api.user.setStatus(row.id, row.status);
  286. })
  287. .then(() => {
  288. ElMessage.success(text + '成功');
  289. })
  290. .catch(function () {
  291. row.status = row.status === 0 ? 1 : 0;
  292. });
  293. };
  294. /** 重置按钮操作 */
  295. const resetQuery = (formEl: FormInstance | undefined) => {
  296. if (!formEl) return;
  297. formEl.resetFields();
  298. userList();
  299. };
  300. return {
  301. queryRef,
  302. editUserRef,
  303. onOpenAddUser,
  304. onOpenEditUser,
  305. onRowDel,
  306. onHandleSizeChange,
  307. onHandleCurrentChange,
  308. deptFilterNode,
  309. filterText,
  310. treeRef,
  311. search,
  312. userList,
  313. handleSelectionChange,
  314. handleNodeClick,
  315. handleResetPwd,
  316. handleStatusChange,
  317. resetQuery,
  318. ...toRefs(state),
  319. };
  320. },
  321. });
  322. </script>