access.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * @Author: code4eat awesomedema@gmail.com
  3. * @Date: 2022-12-14 14:14:32
  4. * @LastEditors: code4eat awesomedema@gmail.com
  5. * @LastEditTime: 2023-06-08 16:26:11
  6. * @FilePath: /BudgetManaSystem/src/access.ts
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. interface MenuItem {
  10. function: any;
  11. path: string;
  12. name: string;
  13. children?: MenuItem[];
  14. }
  15. function findMenuItem(menu: MenuItem[], path: string): MenuItem | undefined {
  16. for (let i = 0; i < menu.length; i++) {
  17. if (menu[i].path === path) {
  18. return menu[i];
  19. } else if (menu[i].children) {
  20. let found = findMenuItem(menu[i].children as MenuItem[], path);
  21. if (found) return found;
  22. }
  23. }
  24. }
  25. export default (initialState: { name: string;memuData:any[] }) => {
  26. // 在这里按照初始化数据定义项目中的权限,统一管理
  27. // 参考文档 https://next.umijs.org/docs/max/access
  28. //console.log({initialState});
  29. const {memuData} = initialState;
  30. const canSeeAdmin = !!(
  31. initialState && initialState.name !== 'dontHaveAccess'
  32. );
  33. const whatCanIDoInThisPage = (pagePath:string)=>{
  34. if(memuData){
  35. const thisPageData = findMenuItem(memuData,pagePath);
  36. // console.log({pagePath,thisPageData});
  37. if(thisPageData){
  38. return thisPageData.function?thisPageData.function:[];
  39. }
  40. return [];
  41. }
  42. }
  43. return {
  44. canSeeAdmin,
  45. whatCanIDoInThisPage,
  46. };
  47. };