s-menu-grid.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!-- 装修基础组件:宫格导航 -->
  2. <template>
  3. <uni-grid :showBorder="Boolean(data.border)" :column="data.column" v-if="initData && initData.length > 0">
  4. <template v-for="(item, index) in initData">
  5. <uni-grid-item
  6. :key="index"
  7. @tap="sheep.$router.go(item.url)"
  8. v-if="item.isVisible"
  9. >
  10. <view class="grid-item-box ss-flex ss-flex-col ss-row-center ss-col-center">
  11. <view class="img-box">
  12. <view
  13. class="tag-box"
  14. v-if="item.badge.show"
  15. :style="[{ background: item.badge.bgColor, color: item.badge.textColor }]"
  16. >
  17. {{ item.badge.text }}
  18. </view>
  19. <image class="menu-image" :src="sheep.$url.cdn(item.iconUrl)"></image>
  20. </view>
  21. <view class="title-box ss-flex ss-flex-col ss-row-center ss-col-center">
  22. <view class="grid-text" :style="[{ color: item.titleColor }]">
  23. {{ item.title }}
  24. </view>
  25. <view class="grid-tip" :style="[{ color: item.subtitleColor }]">
  26. {{ item.subtitle }}
  27. </view>
  28. </view>
  29. </view>
  30. </uni-grid-item>
  31. </template>
  32. </uni-grid>
  33. </template>
  34. <script setup>
  35. import sheep from '@/sheep';
  36. import UserApi from '@/sheep/api/member/user';
  37. import { ref, watch, onMounted } from 'vue';
  38. const props = defineProps({
  39. data: {
  40. type: Object,
  41. default() {},
  42. },
  43. });
  44. // 使用 ref 来存储初始化后的数据
  45. const initData = ref([]);
  46. watch(
  47. () => props.data.list,
  48. (newList) => {
  49. if (Array.isArray(newList) && newList.length > 0) {
  50. initData.value = newList.map(item => ({
  51. ...item,
  52. isVisible: item.title === '商户入驻' ? false : (item.isVisible !== undefined ? item.isVisible : true),
  53. }));
  54. } else {
  55. initData.value = [];
  56. }
  57. },
  58. { immediate: true }
  59. );
  60. // 在组件挂载后获取用户信息并更新 initData
  61. onMounted(() => {
  62. try {
  63. UserApi.getUserInfo()
  64. .then((res) => {
  65. const userInfo = res.data;
  66. // 检查用户信息后修改 initData 的 isVisible 属性
  67. if (userInfo?.agent) {
  68. const targetIndex = initData.value.findIndex(
  69. (item) => item.title === '商户入驻'
  70. );
  71. if (targetIndex !== -1) {
  72. // 使用 splice 替换对象,确保响应性
  73. const updatedItem = {
  74. ...initData.value[targetIndex],
  75. isVisible: true,
  76. };
  77. initData.value.splice(targetIndex, 1, updatedItem);
  78. }
  79. }
  80. })
  81. .catch((error) => {
  82. console.error('获取用户信息时发生错误:', error);
  83. });
  84. } catch (error) {
  85. console.log(error);
  86. }
  87. // console.log(initData.value);
  88. });
  89. </script>
  90. <style lang="scss" scoped>
  91. .menu-image {
  92. width: 24px;
  93. height: 24px;
  94. }
  95. .grid-item-box {
  96. flex: 1;
  97. display: flex;
  98. flex-direction: column;
  99. align-items: center;
  100. justify-content: center;
  101. height: 100%;
  102. .img-box {
  103. position: relative;
  104. .tag-box {
  105. position: absolute;
  106. z-index: 2;
  107. top: 0;
  108. right: 0;
  109. font-size: 2em;
  110. line-height: 1;
  111. padding: 0.4em 0.6em 0.3em;
  112. transform: scale(0.4) translateX(0.5em) translatey(-0.6em);
  113. transform-origin: 100% 0;
  114. border-radius: 200rpx;
  115. white-space: nowrap;
  116. }
  117. }
  118. .title-box {
  119. .grid-tip {
  120. font-size: 24rpx;
  121. white-space: nowrap;
  122. text-align: center;
  123. }
  124. }
  125. }
  126. </style>