index.vue 11 KB

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