permissions.ts 3.8 KB

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