merchant_cate.vue 6.2 KB

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