hook.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import editForm from "../form.vue";
  2. import { handleTree } from "@/utils/tree";
  3. import { message } from "@/utils/message";
  4. import { getMenuList } from "@/api/system";
  5. import { transformI18n } from "@/plugins/i18n";
  6. import { addDialog } from "@/components/ReDialog";
  7. import { reactive, ref, onMounted, h } from "vue";
  8. import type { FormItemProps } from "../utils/types";
  9. import { useRenderIcon } from "@/components/ReIcon/src/hooks";
  10. import { cloneDeep, isAllEmpty, deviceDetection } from "@pureadmin/utils";
  11. export function useMenu() {
  12. const form = reactive({
  13. title: ""
  14. });
  15. const formRef = ref();
  16. const dataList = ref([]);
  17. const loading = ref(true);
  18. const getMenuType = (type, text = false) => {
  19. switch (type) {
  20. case 0:
  21. return text ? "菜单" : "primary";
  22. case 1:
  23. return text ? "iframe" : "warning";
  24. case 2:
  25. return text ? "外链" : "danger";
  26. case 3:
  27. return text ? "按钮" : "info";
  28. }
  29. };
  30. const columns: TableColumnList = [
  31. {
  32. label: "菜单名称",
  33. prop: "title",
  34. align: "left",
  35. cellRenderer: ({ row }) => (
  36. <>
  37. <span class="inline-block mr-1">
  38. {h(useRenderIcon(row.icon), {
  39. style: { paddingTop: "1px" }
  40. })}
  41. </span>
  42. <span>{transformI18n(row.title)}</span>
  43. </>
  44. )
  45. },
  46. {
  47. label: "菜单类型",
  48. prop: "menuType",
  49. width: 100,
  50. cellRenderer: ({ row, props }) => (
  51. <el-tag
  52. size={props.size}
  53. type={getMenuType(row.menuType)}
  54. effect="plain"
  55. >
  56. {getMenuType(row.menuType, true)}
  57. </el-tag>
  58. )
  59. },
  60. {
  61. label: "路由路径",
  62. prop: "path"
  63. },
  64. {
  65. label: "组件路径",
  66. prop: "component",
  67. formatter: ({ path, component }) =>
  68. isAllEmpty(component) ? path : component
  69. },
  70. {
  71. label: "权限标识",
  72. prop: "auths"
  73. },
  74. {
  75. label: "排序",
  76. prop: "rank",
  77. width: 100
  78. },
  79. {
  80. label: "隐藏",
  81. prop: "showLink",
  82. formatter: ({ showLink }) => (showLink ? "否" : "是"),
  83. width: 100
  84. },
  85. {
  86. label: "操作",
  87. fixed: "right",
  88. width: 210,
  89. slot: "operation"
  90. }
  91. ];
  92. function handleSelectionChange(val) {
  93. console.log("handleSelectionChange", val);
  94. }
  95. function resetForm(formEl) {
  96. if (!formEl) return;
  97. formEl.resetFields();
  98. onSearch();
  99. }
  100. async function onSearch() {
  101. loading.value = true;
  102. const { data } = await getMenuList(); // 这里是返回一维数组结构,前端自行处理成树结构,返回格式要求:唯一id加父节点parentId,parentId取父节点id
  103. let newData = data;
  104. if (!isAllEmpty(form.title)) {
  105. // 前端搜索菜单名称
  106. newData = newData.filter(item =>
  107. transformI18n(item.title).includes(form.title)
  108. );
  109. }
  110. dataList.value = handleTree(newData); // 处理成树结构
  111. setTimeout(() => {
  112. loading.value = false;
  113. }, 500);
  114. }
  115. function formatHigherMenuOptions(treeList) {
  116. if (!treeList || !treeList.length) return;
  117. const newTreeList = [];
  118. for (let i = 0; i < treeList.length; i++) {
  119. treeList[i].title = transformI18n(treeList[i].title);
  120. formatHigherMenuOptions(treeList[i].children);
  121. newTreeList.push(treeList[i]);
  122. }
  123. return newTreeList;
  124. }
  125. function openDialog(title = "新增", row?: FormItemProps) {
  126. addDialog({
  127. title: `${title}菜单`,
  128. props: {
  129. formInline: {
  130. menuType: row?.menuType ?? 0,
  131. higherMenuOptions: formatHigherMenuOptions(cloneDeep(dataList.value)),
  132. parentId: row?.parentId ?? 0,
  133. title: row?.title ?? "",
  134. name: row?.name ?? "",
  135. path: row?.path ?? "",
  136. component: row?.component ?? "",
  137. rank: row?.rank ?? 99,
  138. redirect: row?.redirect ?? "",
  139. icon: row?.icon ?? "",
  140. extraIcon: row?.extraIcon ?? "",
  141. enterTransition: row?.enterTransition ?? "",
  142. leaveTransition: row?.leaveTransition ?? "",
  143. activePath: row?.activePath ?? "",
  144. auths: row?.auths ?? "",
  145. frameSrc: row?.frameSrc ?? "",
  146. frameLoading: row?.frameLoading ?? true,
  147. keepAlive: row?.keepAlive ?? false,
  148. hiddenTag: row?.hiddenTag ?? false,
  149. fixedTag: row?.fixedTag ?? false,
  150. showLink: row?.showLink ?? true,
  151. showParent: row?.showParent ?? false
  152. }
  153. },
  154. width: "45%",
  155. draggable: true,
  156. fullscreen: deviceDetection(),
  157. fullscreenIcon: true,
  158. closeOnClickModal: false,
  159. contentRenderer: () => h(editForm, { ref: formRef, formInline: null }),
  160. beforeSure: (done, { options }) => {
  161. const FormRef = formRef.value.getRef();
  162. const curData = options.props.formInline as FormItemProps;
  163. function chores() {
  164. message(
  165. `您${title}了菜单名称为${transformI18n(curData.title)}的这条数据`,
  166. {
  167. type: "success"
  168. }
  169. );
  170. done(); // 关闭弹框
  171. onSearch(); // 刷新表格数据
  172. }
  173. FormRef.validate(valid => {
  174. if (valid) {
  175. console.log("curData", curData);
  176. // 表单规则校验通过
  177. if (title === "新增") {
  178. // 实际开发先调用新增接口,再进行下面操作
  179. chores();
  180. } else {
  181. // 实际开发先调用修改接口,再进行下面操作
  182. chores();
  183. }
  184. }
  185. });
  186. }
  187. });
  188. }
  189. function handleDelete(row) {
  190. message(`您删除了菜单名称为${transformI18n(row.title)}的这条数据`, {
  191. type: "success"
  192. });
  193. onSearch();
  194. }
  195. onMounted(() => {
  196. onSearch();
  197. });
  198. return {
  199. form,
  200. loading,
  201. columns,
  202. dataList,
  203. /** 搜索 */
  204. onSearch,
  205. /** 重置 */
  206. resetForm,
  207. /** 新增、修改菜单 */
  208. openDialog,
  209. /** 删除菜单 */
  210. handleDelete,
  211. handleSelectionChange
  212. };
  213. }