permissions.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { useRouter } from '../core/utils/router'
  2. import { useUserStore } from '../store'
  3. import { storeToRefs } from 'pinia'
  4. import { computed } from 'vue'
  5. export const usePermissions = () => {
  6. const userStore = useUserStore()
  7. const { isLogined, isDesigner, userInfo } = storeToRefs(userStore)
  8. const routes = [
  9. { path: '/pages/mine/homepage/index', meta: { canNotLogin: false, canNotDesigner: true } },
  10. {
  11. path: '/pages/material/mini-class/index',
  12. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  13. },
  14. {
  15. path: '/pages/material/recommend/index',
  16. meta: { canNotLogin: false, canNotDesigner: false, toLogin: true },
  17. },
  18. {
  19. path: '/pages/publish/moment/index',
  20. meta: { canNotLogin: false, canNotDesigner: false, toLogin: true },
  21. },
  22. {
  23. path: '/pages/messages/index',
  24. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  25. },
  26. {
  27. path: '/pages/mine/setting/index',
  28. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  29. },
  30. {
  31. path: '/pages/mine/homepage/statistics/index',
  32. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  33. },
  34. {
  35. path: '/pages/mine/points/index',
  36. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  37. },
  38. {
  39. path: '/pages/mine/coupons/index',
  40. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  41. },
  42. {
  43. path: '/pages/mine/orders/index',
  44. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  45. },
  46. {
  47. path: '/pages/mine/agents/index',
  48. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  49. },
  50. {
  51. path: '/pages/mine/invite/index',
  52. meta: { canNotLogin: false, canNotDesigner: false, toLogin: true },
  53. },
  54. {
  55. path: '/pages/mall/confirm-order/index',
  56. meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
  57. },
  58. ]
  59. const features = computed(() => ({
  60. /**
  61. * 1分钟了解筑巢荟
  62. */
  63. about: isDesigner.value,
  64. toDesignerHomePage: isLogined.value,
  65. checkInAtStoreTask: isDesigner.value,
  66. shareMoment: isLogined.value && isDesigner.value && userInfo.value.level.level > 1,
  67. /**
  68. * 微信代运营兑换
  69. */
  70. wechatAgentExchange: isLogined.value,
  71. /**
  72. * 案例拍摄兑换
  73. */
  74. caseExchange: isLogined.value,
  75. /**
  76. * 商城兑换
  77. */
  78. mallExchange: [isLogined.value, isDesigner.value],
  79. }))
  80. /**
  81. * 按钮操作权限
  82. */
  83. const clickByPermission = (
  84. name: 'wechatAgentExchange' | 'caseExchange' | 'mallExchange',
  85. callback: () => void,
  86. ) => {
  87. const features = [
  88. { name: 'mallExchange', meta: { canNotLogin: false, canNotDesigner: false } },
  89. { name: 'wechatAgentExchange', meta: { canNotLogin: false, canNotDesigner: false } },
  90. ]
  91. const feature = features.find((item) => item.name === name)
  92. if (feature) {
  93. if (!feature.meta.canNotLogin && !isLogined.value) {
  94. useRouter().push('/pages/login/index')
  95. return
  96. }
  97. if (!feature.meta.canNotDesigner && !isDesigner.value) {
  98. useRouter().push('/pages/mine/authentication/index')
  99. return
  100. }
  101. }
  102. callback()
  103. }
  104. return { isLogined, isDesigner, routes, features, clickByPermission }
  105. }