merchant_cate.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view class='productSort'>
  3. <view class='aside' :style="{bottom: tabbarH + 'px',height: height + 'rpx'}">
  4. <scroll-view scroll-y="true" scroll-with-animation='true' style="height: 100%;">
  5. <view class='item acea-row row-center-wrapper'
  6. :class='index==navActive?"on":""'
  7. v-for="(item,index) in productList"
  8. :key="index"
  9. @click='tap(index,"b"+index)'>
  10. <text>{{item.name}}</text>
  11. </view>
  12. </scroll-view>
  13. </view>
  14. <view class='conter'>
  15. <scroll-view scroll-y="true"
  16. :scroll-into-view="toView"
  17. :style='"height:"+height+"rpx;"'
  18. @scroll="scroll"
  19. scroll-with-animation='true'>
  20. <view v-for="(item,index) in productList" :key="index">
  21. <view class='listw' :id="'b'+index">
  22. <view class='title acea-row row-center-wrapper'>
  23. <view class='line'></view>
  24. <view class='name'>{{item.name}}</view>
  25. <view class='line'></view>
  26. </view>
  27. <view class='list acea-row'>
  28. <view v-for="(itemn,indexn) in item.child" :key="indexn">
  29. <navigator hover-class='none'
  30. :url='"/pages/goods/goods_search/index?cid="+itemn.id+"&title="+itemn.name+"&merchantId="+props.merchantId'
  31. class='item acea-row row-column row-middle'>
  32. <view class='name line1'>{{itemn.name}}</view>
  33. </navigator>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view :style='"height:"+(height-300)+"rpx;"' v-if="number<15"></view>
  39. </scroll-view>
  40. </view>
  41. </view>
  42. </template>
  43. <script setup>
  44. import { ref, onMounted, nextTick,watch} from 'vue';
  45. import { onLoad, onShow } from '@dcloudio/uni-app';
  46. import { productCategory } from '@/api/merchant.js';
  47. const props = defineProps({
  48. merchantId: {
  49. type: [String, Number],
  50. required: true
  51. }
  52. });
  53. // 响应式数据
  54. const productList = ref([]);
  55. const navActive = ref(0);
  56. const number = ref("");
  57. const height = ref(0);
  58. const hightArr = ref([]);
  59. const toView = ref("");
  60. const tabbarH = ref(0);
  61. // 获取分类列表
  62. const getAllCategory = async () => {
  63. console.log('============')
  64. console.log(props.merchantId)
  65. try {
  66. let obj = {
  67. type: 1,
  68. status: -1
  69. }
  70. const { data }= await productCategory(obj);
  71. const newArr = []
  72. data.forEach((value, index) => {
  73. newArr[index] = value
  74. if (value.child) newArr[index].child = value.child.filter(item => item.status === true)
  75. })
  76. productList.value = newArr.filter(item => item.code !== 'bb_mall')
  77. // 使用 nextTick 确保 DOM 已更新
  78. nextTick(() => {
  79. setTimeout(() => {
  80. infoScroll();
  81. }, 500);
  82. });
  83. } catch (error) {
  84. console.error('获取分类列表失败:', error);
  85. uni.showToast({ title: '加载分类失败' });
  86. }
  87. };
  88. // 计算滚动相关数据
  89. const infoScroll = () => {
  90. let len = productList.value.length;
  91. let child = productList.value[len - 1] && productList.value[len - 1].child
  92. ? productList.value[len - 1].child
  93. : [];
  94. number.value = child ? child.length : 0;
  95. // 设置商品列表高度
  96. uni.getSystemInfo({
  97. success: (res) => {
  98. height.value = (res.windowHeight) * (750 / res.windowWidth) - 98;
  99. },
  100. });
  101. let heightTemp = 0;
  102. let hightArrTemp = [];
  103. for (let i = 0; i < len; i++) {
  104. // 获取元素所在位置
  105. let query = uni.createSelectorQuery();
  106. let idView = "#b" + i;
  107. query.select(idView).boundingClientRect();
  108. query.exec((res) => {
  109. if (res && res[0]) {
  110. let top = res[0].top;
  111. hightArrTemp.push(top);
  112. hightArr.value = hightArrTemp;
  113. }
  114. });
  115. }
  116. };
  117. // 点击左侧导航
  118. const tap = (index, id) => {
  119. toView.value = id;
  120. navActive.value = index;
  121. };
  122. // 右侧滚动事件
  123. const scroll = (e) => {
  124. let scrollTop = e.detail.scrollTop;
  125. let scrollArr = hightArr.value;
  126. if (!scrollArr || scrollArr.length === 0) return;
  127. for (let i = 0; i < scrollArr.length; i++) {
  128. if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
  129. navActive.value = 0;
  130. } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
  131. navActive.value = i;
  132. } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
  133. navActive.value = scrollArr.length - 1;
  134. }
  135. }
  136. };
  137. // 监听 merchantId 变化
  138. watch(() => props.merchantId, (newVal) => {
  139. console.log('merchantId 变化了:', newVal);
  140. if (newVal) {
  141. getAllCategory();
  142. }
  143. }, { immediate: true }); // 立即执行一次
  144. // 页面加载
  145. onLoad(() => {
  146. });
  147. // 页面显示
  148. onShow(() => {
  149. getAllCategory();
  150. });
  151. </script>
  152. <style scoped lang="scss">
  153. .productSort .aside {
  154. position: absolute;
  155. width: 180rpx;
  156. left: 0;
  157. top:0;
  158. background-color: #fff;
  159. overflow-y: scroll;
  160. overflow-x: hidden;
  161. height: auto;
  162. border-radius: 16rpx;
  163. }
  164. .productSort .aside .item {
  165. height: 100rpx;
  166. width: 100%;
  167. font-size: 26rpx;
  168. color: #424242;
  169. }
  170. .productSort .aside .item.on {
  171. background-color: #fff;
  172. border-left: 6rpx solid #F8C008;
  173. width: 100%;
  174. text-align: center;
  175. color: #F8C008;
  176. font-weight: bold;
  177. }
  178. .productSort .conter {
  179. margin: 0rpx 0 0 200rpx;
  180. padding: 0 14rpx;
  181. background-color: #fff;
  182. border-radius: 16rpx;
  183. }
  184. .productSort .conter .listw {
  185. padding-top: 20rpx;
  186. }
  187. .productSort .conter .listw .title {
  188. height: 90rpx;
  189. }
  190. .productSort .conter .listw .title .line {
  191. width: 100rpx;
  192. height: 2rpx;
  193. background-color: #f0f0f0;
  194. }
  195. .productSort .conter .listw .title .name {
  196. font-size: 28rpx;
  197. color: #333;
  198. margin: 0 30rpx;
  199. font-weight: bold;
  200. }
  201. .productSort .conter .list {
  202. flex-wrap: wrap;
  203. }
  204. .productSort .conter .list .item {
  205. width: 177rpx;
  206. margin-top: 26rpx;
  207. }
  208. .productSort .conter .list .item .picture {
  209. width: 120rpx;
  210. height: 120rpx;
  211. border-radius: 50%;
  212. }
  213. .productSort .conter .list .item .picture image {
  214. width: 100%;
  215. height: 100%;
  216. border-radius: 50%;
  217. div{
  218. background-color: #f7f7f7;
  219. }
  220. }
  221. .productSort .conter .list .item .name {
  222. font-size: 24rpx;
  223. color: #333;
  224. height: 56rpx;
  225. line-height: 56rpx;
  226. width: 120rpx;
  227. text-align: center;
  228. }
  229. </style>