index.vue 11 KB

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