index.vue 10 KB

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