access.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * @Author: code4eat awesomedema@gmail.com
  3. * @Date: 2022-12-14 14:14:32
  4. * @LastEditors: code4eat awesomedema@gmail.com
  5. * @LastEditTime: 2024-01-19 11:26:51
  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. if(thisPageData){
  37. return thisPageData.function?thisPageData.function:[];
  38. }
  39. return [];
  40. }
  41. }
  42. const canIReadThisPage = (pagePath:string)=>{
  43. if(memuData){
  44. const thisPageData = findMenuItem(memuData,pagePath);
  45. // console.log({pagePath,thisPageData});
  46. if(thisPageData){
  47. return true;
  48. }
  49. return false;
  50. }
  51. }
  52. return {
  53. canSeeAdmin,
  54. whatCanIDoInThisPage,
  55. canIReadThisPage
  56. };
  57. };