hook.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import dayjs from "dayjs";
  2. import editForm from "../form.vue";
  3. import { handleTree } from "@/utils/tree";
  4. import { message } from "@/utils/message";
  5. import { ElMessageBox } from "element-plus";
  6. import { usePublicHooks } from "../../hooks";
  7. import { transformI18n } from "@/plugins/i18n";
  8. import { addDialog } from "@/components/ReDialog";
  9. import type { FormItemProps } from "../utils/types";
  10. import type { PaginationProps } from "@pureadmin/table";
  11. import { getKeyList, deviceDetection } from "@pureadmin/utils";
  12. import { getRoleList, getRoleMenu, getRoleMenuIds } from "@/api/system";
  13. import { type Ref, reactive, ref, onMounted, h, toRaw, watch } from "vue";
  14. export function useRole(treeRef: Ref) {
  15. const form = reactive({
  16. name: "",
  17. code: "",
  18. status: ""
  19. });
  20. const curRow = ref();
  21. const formRef = ref();
  22. const dataList = ref([]);
  23. const treeIds = ref([]);
  24. const treeData = ref([]);
  25. const isShow = ref(false);
  26. const loading = ref(true);
  27. const isLinkage = ref(false);
  28. const treeSearchValue = ref();
  29. const switchLoadMap = ref({});
  30. const isExpandAll = ref(false);
  31. const isSelectAll = ref(false);
  32. const { switchStyle } = usePublicHooks();
  33. const treeProps = {
  34. value: "id",
  35. label: "title",
  36. children: "children"
  37. };
  38. const pagination = reactive<PaginationProps>({
  39. total: 0,
  40. pageSize: 10,
  41. currentPage: 1,
  42. background: true
  43. });
  44. const columns: TableColumnList = [
  45. {
  46. label: "角色编号",
  47. prop: "id"
  48. },
  49. {
  50. label: "角色名称",
  51. prop: "name"
  52. },
  53. {
  54. label: "角色标识",
  55. prop: "code"
  56. },
  57. {
  58. label: "状态",
  59. cellRenderer: scope => (
  60. <el-switch
  61. size={scope.props.size === "small" ? "small" : "default"}
  62. loading={switchLoadMap.value[scope.index]?.loading}
  63. v-model={scope.row.status}
  64. active-value={1}
  65. inactive-value={0}
  66. active-text="已启用"
  67. inactive-text="已停用"
  68. inline-prompt
  69. style={switchStyle.value}
  70. onChange={() => onChange(scope as any)}
  71. />
  72. ),
  73. minWidth: 90
  74. },
  75. {
  76. label: "备注",
  77. prop: "remark",
  78. minWidth: 160
  79. },
  80. {
  81. label: "创建时间",
  82. prop: "createTime",
  83. minWidth: 160,
  84. formatter: ({ createTime }) =>
  85. dayjs(createTime).format("YYYY-MM-DD HH:mm:ss")
  86. },
  87. {
  88. label: "操作",
  89. fixed: "right",
  90. width: 210,
  91. slot: "operation"
  92. }
  93. ];
  94. // const buttonClass = computed(() => {
  95. // return [
  96. // "h-[20px]!",
  97. // "reset-margin",
  98. // "text-gray-500!",
  99. // "dark:text-white!",
  100. // "dark:hover:text-primary!"
  101. // ];
  102. // });
  103. function onChange({ row, index }) {
  104. ElMessageBox.confirm(
  105. `确认要<strong>${
  106. row.status === 0 ? "停用" : "启用"
  107. }</strong><strong style='color:var(--el-color-primary)'>${
  108. row.name
  109. }</strong>吗?`,
  110. "系统提示",
  111. {
  112. confirmButtonText: "确定",
  113. cancelButtonText: "取消",
  114. type: "warning",
  115. dangerouslyUseHTMLString: true,
  116. draggable: true
  117. }
  118. )
  119. .then(() => {
  120. switchLoadMap.value[index] = Object.assign(
  121. {},
  122. switchLoadMap.value[index],
  123. {
  124. loading: true
  125. }
  126. );
  127. setTimeout(() => {
  128. switchLoadMap.value[index] = Object.assign(
  129. {},
  130. switchLoadMap.value[index],
  131. {
  132. loading: false
  133. }
  134. );
  135. message(`已${row.status === 0 ? "停用" : "启用"}${row.name}`, {
  136. type: "success"
  137. });
  138. }, 300);
  139. })
  140. .catch(() => {
  141. row.status === 0 ? (row.status = 1) : (row.status = 0);
  142. });
  143. }
  144. function handleDelete(row) {
  145. message(`您删除了角色名称为${row.name}的这条数据`, { type: "success" });
  146. onSearch();
  147. }
  148. function handleSizeChange(val: number) {
  149. console.log(`${val} items per page`);
  150. }
  151. function handleCurrentChange(val: number) {
  152. console.log(`current page: ${val}`);
  153. }
  154. function handleSelectionChange(val) {
  155. console.log("handleSelectionChange", val);
  156. }
  157. async function onSearch() {
  158. loading.value = true;
  159. const { data } = await getRoleList(toRaw(form));
  160. dataList.value = data.list;
  161. pagination.total = data.total;
  162. pagination.pageSize = data.pageSize;
  163. pagination.currentPage = data.currentPage;
  164. setTimeout(() => {
  165. loading.value = false;
  166. }, 500);
  167. }
  168. const resetForm = formEl => {
  169. if (!formEl) return;
  170. formEl.resetFields();
  171. onSearch();
  172. };
  173. function openDialog(title = "新增", row?: FormItemProps) {
  174. addDialog({
  175. title: `${title}角色`,
  176. props: {
  177. formInline: {
  178. name: row?.name ?? "",
  179. code: row?.code ?? "",
  180. remark: row?.remark ?? ""
  181. }
  182. },
  183. width: "40%",
  184. draggable: true,
  185. fullscreen: deviceDetection(),
  186. fullscreenIcon: true,
  187. closeOnClickModal: false,
  188. contentRenderer: () => h(editForm, { ref: formRef, formInline: null }),
  189. beforeSure: (done, { options }) => {
  190. const FormRef = formRef.value.getRef();
  191. const curData = options.props.formInline as FormItemProps;
  192. function chores() {
  193. message(`您${title}了角色名称为${curData.name}的这条数据`, {
  194. type: "success"
  195. });
  196. done(); // 关闭弹框
  197. onSearch(); // 刷新表格数据
  198. }
  199. FormRef.validate(valid => {
  200. if (valid) {
  201. console.log("curData", curData);
  202. // 表单规则校验通过
  203. if (title === "新增") {
  204. // 实际开发先调用新增接口,再进行下面操作
  205. chores();
  206. } else {
  207. // 实际开发先调用修改接口,再进行下面操作
  208. chores();
  209. }
  210. }
  211. });
  212. }
  213. });
  214. }
  215. /** 菜单权限 */
  216. async function handleMenu(row?: any) {
  217. const { id } = row;
  218. if (id) {
  219. curRow.value = row;
  220. isShow.value = true;
  221. const { data } = await getRoleMenuIds({ id });
  222. treeRef.value.setCheckedKeys(data);
  223. } else {
  224. curRow.value = null;
  225. isShow.value = false;
  226. }
  227. }
  228. /** 高亮当前权限选中行 */
  229. function rowStyle({ row: { id } }) {
  230. return {
  231. cursor: "pointer",
  232. background: id === curRow.value?.id ? "var(--el-fill-color-light)" : ""
  233. };
  234. }
  235. /** 菜单权限-保存 */
  236. function handleSave() {
  237. const { id, name } = curRow.value;
  238. // 根据用户 id 调用实际项目中菜单权限修改接口
  239. console.log(id, treeRef.value.getCheckedKeys());
  240. message(`角色名称为${name}的菜单权限修改成功`, {
  241. type: "success"
  242. });
  243. }
  244. /** 数据权限 可自行开发 */
  245. // function handleDatabase() {}
  246. const onQueryChanged = (query: string) => {
  247. treeRef.value!.filter(query);
  248. };
  249. const filterMethod = (query: string, node) => {
  250. return transformI18n(node.title)!.includes(query);
  251. };
  252. onMounted(async () => {
  253. onSearch();
  254. const { data } = await getRoleMenu();
  255. treeIds.value = getKeyList(data, "id");
  256. treeData.value = handleTree(data);
  257. });
  258. watch(isExpandAll, val => {
  259. val
  260. ? treeRef.value.setExpandedKeys(treeIds.value)
  261. : treeRef.value.setExpandedKeys([]);
  262. });
  263. watch(isSelectAll, val => {
  264. val
  265. ? treeRef.value.setCheckedKeys(treeIds.value)
  266. : treeRef.value.setCheckedKeys([]);
  267. });
  268. return {
  269. form,
  270. isShow,
  271. curRow,
  272. loading,
  273. columns,
  274. rowStyle,
  275. dataList,
  276. treeData,
  277. treeProps,
  278. isLinkage,
  279. pagination,
  280. isExpandAll,
  281. isSelectAll,
  282. treeSearchValue,
  283. // buttonClass,
  284. onSearch,
  285. resetForm,
  286. openDialog,
  287. handleMenu,
  288. handleSave,
  289. handleDelete,
  290. filterMethod,
  291. transformI18n,
  292. onQueryChanged,
  293. // handleDatabase,
  294. handleSizeChange,
  295. handleCurrentChange,
  296. handleSelectionChange
  297. };
  298. }