merchant_cate.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. let listArr = newArr.filter(item => item.code !== 'bb_mall')
  77. productList.value = listArr.sort((a, b) => a.sort - b.sort);
  78. // 使用 nextTick 确保 DOM 已更新
  79. nextTick(() => {
  80. setTimeout(() => {
  81. infoScroll();
  82. }, 500);
  83. });
  84. } catch (error) {
  85. console.error('获取分类列表失败:', error);
  86. uni.showToast({ title: '加载分类失败' });
  87. }
  88. };
  89. // 计算滚动相关数据
  90. const infoScroll = () => {
  91. console.log('infoScroll++++++++')
  92. let len = productList.value.length;
  93. let child = productList.value[len - 1] && productList.value[len - 1].child
  94. ? productList.value[len - 1].child
  95. : [];
  96. number.value = child ? child.length : 0;
  97. // 设置商品列表高度
  98. uni.getSystemInfo({
  99. success: (res) => {
  100. height.value = (res.windowHeight) * (750 / res.windowWidth) - 98;
  101. },
  102. });
  103. let heightTemp = 0;
  104. let hightArrTemp = [];
  105. for (let i = 0; i < len; i++) {
  106. // 获取元素所在位置
  107. let query = uni.createSelectorQuery();
  108. let idView = "#b" + i;
  109. query.select(idView).boundingClientRect();
  110. query.exec((res) => {
  111. if (res && res[0]) {
  112. let top = res[0].top;
  113. hightArrTemp.push(top);
  114. hightArr.value = hightArrTemp;
  115. console.log('hightArr.value======')
  116. console.log(hightArr.value)
  117. }
  118. });
  119. }
  120. };
  121. // 点击左侧导航
  122. const tap = (index, id) => {
  123. toView.value = id;
  124. navActive.value = index;
  125. };
  126. // 右侧滚动事件
  127. const scroll = (e) => {
  128. let scrollTop = e.detail.scrollTop;
  129. let scrollArr = hightArr.value;
  130. console.log('scrollTop:', scrollTop, 'scrollArr:', scrollArr);
  131. if (!scrollArr || scrollArr.length === 0 || scrollArr.length < 2) return;
  132. // 修正逻辑:减去第一个元素的高度作为偏移量
  133. const baseOffset = scrollArr[0] || 0;
  134. const adjustedScrollTop = scrollTop + baseOffset;
  135. // 找到当前应该激活的索引
  136. let currentIndex = 0;
  137. for (let i = 0; i < scrollArr.length; i++) {
  138. if (i === 0 && adjustedScrollTop < scrollArr[0]) {
  139. currentIndex = 0;
  140. break;
  141. } else if (i === scrollArr.length - 1 && adjustedScrollTop >= scrollArr[i]) {
  142. currentIndex = i;
  143. break;
  144. } else if (adjustedScrollTop >= scrollArr[i] && adjustedScrollTop < scrollArr[i + 1]) {
  145. currentIndex = i;
  146. break;
  147. }
  148. }
  149. if (navActive.value !== currentIndex) {
  150. navActive.value = currentIndex;
  151. }
  152. };
  153. // 监听 merchantId 变化
  154. watch(() => props.merchantId, (newVal) => {
  155. console.log('merchantId 变化了:', newVal);
  156. if (newVal) {
  157. getAllCategory();
  158. }
  159. }, { immediate: true }); // 立即执行一次
  160. // 页面加载
  161. onLoad(() => {
  162. });
  163. // 页面显示
  164. onShow(() => {
  165. getAllCategory();
  166. });
  167. </script>
  168. <style scoped lang="scss">
  169. .productSort .aside {
  170. position: absolute;
  171. width: 180rpx;
  172. left: 0;
  173. top:0;
  174. background-color: #fff;
  175. overflow-y: scroll;
  176. overflow-x: hidden;
  177. height: auto;
  178. border-radius: 16rpx;
  179. }
  180. .productSort .aside .item {
  181. height: 100rpx;
  182. width: 100%;
  183. font-size: 26rpx;
  184. color: #424242;
  185. }
  186. .productSort .aside .item.on {
  187. background-color: #fff;
  188. border-left: 6rpx solid #F8C008;
  189. width: 100%;
  190. text-align: center;
  191. color: #F8C008;
  192. font-weight: bold;
  193. }
  194. .productSort .conter {
  195. margin: 0rpx 0 0 200rpx;
  196. padding: 0 14rpx;
  197. background-color: #fff;
  198. border-radius: 16rpx;
  199. }
  200. .productSort .conter .listw {
  201. padding-top: 20rpx;
  202. }
  203. .productSort .conter .listw .title {
  204. height: 90rpx;
  205. }
  206. .productSort .conter .listw .title .line {
  207. width: 100rpx;
  208. height: 2rpx;
  209. background-color: #f0f0f0;
  210. }
  211. .productSort .conter .listw .title .name {
  212. font-size: 28rpx;
  213. color: #333;
  214. margin: 0 30rpx;
  215. font-weight: bold;
  216. }
  217. .productSort .conter .list {
  218. flex-wrap: wrap;
  219. }
  220. .productSort .conter .list .item {
  221. width: 177rpx;
  222. margin-top: 26rpx;
  223. }
  224. .productSort .conter .list .item .picture {
  225. width: 120rpx;
  226. height: 120rpx;
  227. border-radius: 50%;
  228. }
  229. .productSort .conter .list .item .picture image {
  230. width: 100%;
  231. height: 100%;
  232. border-radius: 50%;
  233. div{
  234. background-color: #f7f7f7;
  235. }
  236. }
  237. .productSort .conter .list .item .name {
  238. font-size: 24rpx;
  239. color: #333;
  240. height: 56rpx;
  241. line-height: 56rpx;
  242. width: 120rpx;
  243. text-align: center;
  244. }
  245. </style>