permission.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { localGet, localSet } from "@/utils/index";
  2. import { ElMessage } from "element-plus";
  3. import { getUserByPhone, getDetail, checkMenuPermissions } from "@/api/modules/homeEntry";
  4. /**
  5. * @description 判断是否有操作权限
  6. * @returns {Boolean} 是否有权限
  7. */
  8. export async function usePermission(tip?: string) {
  9. let type = true;
  10. // if (localGet("createdId") || localGet("geeker-user").userInfo.storeId) {
  11. // // return type;
  12. // // }
  13. let params = {
  14. phone: localGet("iphone") || localGet("geeker-user").userInfo.phone
  15. };
  16. const res: any = await getUserByPhone(params);
  17. if (res.data && res.data.storeId) {
  18. localSet("createdId", res.data.storeId);
  19. const resD: any = await getDetail({
  20. id: res.data.storeId
  21. });
  22. if (resD.data && resD.data.storeApplicationStatus != 1) {
  23. return false;
  24. }
  25. if (resD.data && resD.data.commissionRate) {
  26. localSet("commissionRate", resD.data.commissionRate);
  27. }
  28. if (resD.data && resD.data.businessSection) {
  29. localSet("businessSection", resD.data.businessSection);
  30. }
  31. } else {
  32. type = false;
  33. if (tip) {
  34. ElMessage.warning(`请完成商家入驻后再进行${tip}`);
  35. }
  36. return type;
  37. }
  38. if (!localGet("businessSection") && !localGet("geeker-user").userInfo.businessSection) {
  39. type = false;
  40. if (tip) {
  41. ElMessage.warning(`请完成商家入驻后重新登录再进行${tip}`);
  42. }
  43. return type;
  44. }
  45. return type;
  46. }
  47. /**
  48. * @description 检查菜单访问权限(新方法)
  49. * @returns {Object} 返回合同管理和食品经营许可证的权限状态
  50. */
  51. export async function checkMenuAccessPermission(): Promise<{
  52. contractManagement: boolean;
  53. foodBusinessLicense: boolean;
  54. }> {
  55. try {
  56. // 调用单个API,返回两个字段
  57. const res: any = await checkMenuPermissions({
  58. storeId: localGet("createdId")
  59. });
  60. if (res) {
  61. const data = res || {};
  62. // 解析合同管理权限
  63. let contractPermission = false;
  64. if (data.expirationTime !== undefined) {
  65. const value = data.expirationTime;
  66. contractPermission = value == "0";
  67. }
  68. // 解析食品经营许可证权限
  69. let foodPermission = false;
  70. if (data.foodLicenceExpirationTime !== undefined) {
  71. const value = data.foodLicenceExpirationTime;
  72. foodPermission = value == "0";
  73. }
  74. return {
  75. contractManagement: contractPermission,
  76. foodBusinessLicense: foodPermission
  77. };
  78. }
  79. // 如果API调用失败或返回格式不正确,默认返回两个都为false(不做限制)
  80. return {
  81. contractManagement: false,
  82. foodBusinessLicense: false
  83. };
  84. } catch (error) {
  85. console.error("检查菜单权限失败:", error);
  86. // 如果API调用失败,默认返回两个都为false(不做限制)
  87. return {
  88. contractManagement: false,
  89. foodBusinessLicense: false
  90. };
  91. }
  92. }
  93. /**
  94. * @description 检查菜单项是否可以点击
  95. * @param {string} path 菜单路径
  96. * @returns {Promise<Object>} 返回包含canClick、contractManagement、foodBusinessLicense的对象
  97. */
  98. export async function checkMenuClickPermission(path: string): Promise<{
  99. canClick: boolean;
  100. contractManagement: boolean;
  101. foodBusinessLicense: boolean;
  102. }> {
  103. // 页面路径常量
  104. const CONTRACT_MANAGEMENT_PATH = "/licenseManagement/contractManagement"; // 合同管理
  105. const FOOD_BUSINESS_LICENSE_PATH = "/licenseManagement/foodBusinessLicense"; // 食品经营许可证
  106. // 调用权限检查方法,获取两个权限状态
  107. const permissions = await checkMenuAccessPermission();
  108. const { contractManagement, foodBusinessLicense } = permissions;
  109. // 如果两者都为false,不做限制,所有页面都可以点击
  110. if (!contractManagement && !foodBusinessLicense) {
  111. return {
  112. canClick: true,
  113. contractManagement: false,
  114. foodBusinessLicense: false
  115. };
  116. }
  117. // 如果至少有一个为true,需要检查权限
  118. // 构建允许访问的路径列表
  119. const allowedPaths: string[] = [];
  120. if (contractManagement) {
  121. allowedPaths.push(CONTRACT_MANAGEMENT_PATH);
  122. }
  123. if (foodBusinessLicense) {
  124. allowedPaths.push(FOOD_BUSINESS_LICENSE_PATH);
  125. }
  126. // 检查当前路径是否在允许访问的列表中
  127. const canClick = allowedPaths.includes(path);
  128. // 如果不可点击,根据权限状态显示相应的提示信息
  129. if (!canClick) {
  130. let message = "";
  131. // 如果两者都为true,显示两行提示
  132. if (contractManagement && foodBusinessLicense) {
  133. message = "合同已到期,请上传最新合同。\n经营许可证已到期,请上传最新许可证。";
  134. } else if (contractManagement) {
  135. // 只有合同管理为true
  136. message = "合同已到期,请上传最新合同。";
  137. } else if (foodBusinessLicense) {
  138. // 只有食品经营许可证为true
  139. message = "经营许可证已到期,请上传最新许可证。";
  140. }
  141. if (message) {
  142. ElMessage.warning(message);
  143. }
  144. }
  145. return {
  146. canClick,
  147. contractManagement,
  148. foodBusinessLicense
  149. };
  150. }