hook.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import {
  2. deleteMenuList,
  3. getMenuList,
  4. getPermissionList,
  5. postMenuList,
  6. putMenuList
  7. } from "@/api/system";
  8. import { ListDate } from "@/utils/formateDate";
  9. import {
  10. UseFormatDatePickerHook,
  11. UseValidTimeRangeHook
  12. } from "@/utils/hooks/format";
  13. import { type PaginationProps } from "@pureadmin/table";
  14. import { number } from "echarts";
  15. import { ElMessage, ElMessageBox } from "element-plus";
  16. import { reactive, ref, computed, onMounted } from "vue";
  17. export function useUser() {
  18. const UpdateForm = ref({
  19. name: "", // 菜单名
  20. path: "", // 组件路径
  21. title: "",
  22. descs: "", // 描述
  23. icon: "", // 图标
  24. parentId: "", //父id
  25. permissionId: "", //权限id
  26. menuStatus: "1", // 按钮标识
  27. version: "1", // 是否按钮
  28. id: ""
  29. });
  30. const dialogType = ref(1);
  31. const dataList = ref([]);
  32. const menuList = ref([]);
  33. const permissionList = ref([]);
  34. const loading = ref(false);
  35. const dialogUpdateVisible = ref(false);
  36. const pagination = reactive<PaginationProps>({
  37. total: 0,
  38. pageSize: 10,
  39. currentPage: 1,
  40. background: true
  41. });
  42. const columns: TableColumnList = [
  43. {
  44. type: "selection",
  45. width: 55,
  46. align: "left",
  47. hide: ({ checkList }) => !checkList.includes("勾选列")
  48. },
  49. {
  50. label: "序号",
  51. type: "index",
  52. width: 70,
  53. hide: ({ checkList }) => !checkList.includes("序号列")
  54. },
  55. {
  56. label: "ID",
  57. prop: "id",
  58. minWidth: 130
  59. },
  60. {
  61. label: "菜单名",
  62. prop: "name",
  63. minWidth: 130
  64. },
  65. {
  66. label: "组件路径",
  67. prop: "path",
  68. minWidth: 130
  69. },
  70. {
  71. label: "图标",
  72. prop: "icon",
  73. minWidth: 130
  74. },
  75. {
  76. label: "描述",
  77. prop: "descs",
  78. minWidth: 130
  79. },
  80. // {
  81. // label: "状态",
  82. // prop: "status",
  83. // minWidth: 130
  84. // },
  85. {
  86. label: "操作",
  87. fixed: "right",
  88. width: 200,
  89. slot: "operation"
  90. }
  91. ];
  92. const buttonClass = computed(() => {
  93. return [
  94. "!h-[20px]",
  95. "reset-margin",
  96. "!text-gray-500",
  97. "dark:!text-white",
  98. "dark:hover:!text-primary"
  99. ];
  100. });
  101. // 获取权限列表
  102. async function permission() {
  103. const { data } = await getPermissionList({});
  104. const permission = data.map(item => {
  105. return {
  106. label: item.name,
  107. Id: item.id
  108. };
  109. });
  110. permissionList.value = permission;
  111. permissionList.value.unshift({
  112. label: "作为菜单",
  113. Id: ""
  114. });
  115. }
  116. permission();
  117. // 树形结构格式化
  118. function TreeFormatter(data, flag = false) {
  119. const result = data.map(item => {
  120. if (item.children.length != 0) {
  121. return {
  122. label: item.name,
  123. value: item.id,
  124. children: TreeFormatter(item.children, true)
  125. };
  126. } else if (item.version != 0) {
  127. return {
  128. disabled: true,
  129. label: item.name,
  130. value: item.id
  131. };
  132. } else {
  133. return {
  134. label: item.name,
  135. value: item.id
  136. };
  137. }
  138. });
  139. return result;
  140. }
  141. // 获取全部菜单列表
  142. async function Menu() {
  143. const { data } = await getMenuList({ id: "" });
  144. menuList.value = TreeFormatter(data);
  145. console.log(menuList.value);
  146. menuList.value.unshift({
  147. label: "作为顶级菜单",
  148. value: "null"
  149. });
  150. }
  151. Menu();
  152. async function UpdateFormSubmit() {
  153. if (dialogType.value == 1) {
  154. const { status, message } = await postMenuList({
  155. name: UpdateForm.value.name, // 菜单名
  156. path: UpdateForm.value.path, // 组件路径
  157. descs: UpdateForm.value.descs, // 描述
  158. title: UpdateForm.value.title, // 名称
  159. icon: UpdateForm.value.icon, // 图标
  160. parentId:
  161. UpdateForm.value.parentId === "null"
  162. ? null
  163. : UpdateForm.value.parentId, //父id
  164. permissionId: UpdateForm.value.permissionId, //权限id
  165. menuStatus: UpdateForm.value.permissionId === "" ? "0" : "1" // 按钮标识
  166. });
  167. if (status == 1) {
  168. ElMessage({
  169. message: "新增菜单成功",
  170. type: "success"
  171. });
  172. dialogUpdateVisible.value = false;
  173. onSearch();
  174. permission();
  175. Menu();
  176. } else {
  177. ElMessageBox.alert(message, "提示", {
  178. confirmButtonText: "关闭",
  179. type: "warning"
  180. });
  181. }
  182. } else {
  183. const { status, message } = await putMenuList({
  184. name: UpdateForm.value.name, // 菜单名
  185. path: UpdateForm.value.path, // 组件路径
  186. descs: UpdateForm.value.descs, // 描述
  187. title: UpdateForm.value.title, // 名称
  188. icon: UpdateForm.value.icon, // 图标
  189. parentId:
  190. UpdateForm.value.parentId === "null"
  191. ? null
  192. : UpdateForm.value.parentId, //父id
  193. permissionId: UpdateForm.value.permissionId, //权限id
  194. menuStatus: UpdateForm.value.permissionId === "" ? "0" : "1", // 按钮标识
  195. id: UpdateForm.value.id
  196. });
  197. if (status == 1) {
  198. ElMessage({
  199. message: "菜单修改成功",
  200. type: "success"
  201. });
  202. dialogUpdateVisible.value = false;
  203. onSearch();
  204. permission();
  205. Menu();
  206. } else {
  207. ElMessageBox.alert(message, "提示", {
  208. confirmButtonText: "关闭",
  209. type: "warning"
  210. });
  211. }
  212. }
  213. }
  214. function handleDelete(row) {
  215. ElMessageBox.confirm(`是否删除名称为:${row.name}的菜单? `, "提示", {
  216. confirmButtonText: "删除",
  217. cancelButtonText: "取消",
  218. type: "warning"
  219. }).then(async () => {
  220. const { status, message } = await deleteMenuList({ id: row.id });
  221. if (status == 1) {
  222. ElMessage({
  223. message: "删除成功",
  224. type: "success"
  225. });
  226. onSearch();
  227. permission();
  228. Menu();
  229. } else {
  230. ElMessageBox.alert(message, "提示", {
  231. confirmButtonText: "关闭",
  232. type: "warning"
  233. });
  234. }
  235. });
  236. }
  237. function AddUser(row) {
  238. UpdateForm.value = {
  239. name: "", // 菜单名
  240. path: "", // 组件路径
  241. descs: "", // 描述
  242. title: "", // 名称
  243. icon: "", // 图标
  244. parentId: "", //父id
  245. permissionId: "", //权限id
  246. menuStatus: "1", // 按钮标识
  247. version: "" // 是否按钮
  248. };
  249. dialogType.value = 1;
  250. dialogUpdateVisible.value = true;
  251. }
  252. function handleUpdate(row) {
  253. console.log(row);
  254. dialogType.value = 2;
  255. dialogUpdateVisible.value = true;
  256. UpdateForm.value = {
  257. name: row.name, // 菜单名
  258. path: row.path, // 组件路径
  259. title: row.title, // 菜单名称
  260. descs: row.descs, // 描述
  261. icon: row.icon, // 图标
  262. parentId: row.parentId ? row.parentId : "null", //父id
  263. permissionId: row.permissionId === null ? "" : row.permissionId, //权限id
  264. menuStatus: row.version === 0 ? "0" : "1", // 按钮标识
  265. version: row.version, // 是否按钮
  266. id: row.id // 是否按钮
  267. };
  268. }
  269. function handleSizeChange(val: number) {
  270. console.log(`${val} items per page`);
  271. onSearch();
  272. }
  273. function handleCurrentChange(val: number) {
  274. if (typeof val !== "number") return;
  275. console.log(`current page: ${val}`);
  276. onSearch();
  277. }
  278. function handleSelectionChange(val) {
  279. console.log("handleSelectionChange", val);
  280. onSearch();
  281. }
  282. // 搜索函数(请求表格数据列表)
  283. function submit(e: { keyCode: number }) {
  284. if (e.keyCode == 229) {
  285. return;
  286. } else {
  287. onSearch();
  288. }
  289. }
  290. async function onSearch() {
  291. loading.value = true;
  292. const { data, other } = await getMenuList({
  293. pageSize: pagination.pageSize,
  294. pageNum: pagination.currentPage
  295. });
  296. dataList.value = data;
  297. pagination.total = other;
  298. setTimeout(() => {
  299. loading.value = false;
  300. }, 500);
  301. }
  302. onMounted(() => {
  303. onSearch();
  304. });
  305. return {
  306. loading,
  307. columns,
  308. dataList,
  309. pagination,
  310. buttonClass,
  311. dialogUpdateVisible,
  312. UpdateFormSubmit,
  313. submit,
  314. UpdateForm,
  315. menuList,
  316. permissionList,
  317. dialogType,
  318. AddUser,
  319. onSearch,
  320. handleUpdate,
  321. handleDelete,
  322. handleSizeChange,
  323. handleCurrentChange,
  324. handleSelectionChange
  325. };
  326. }