merchant_cate.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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' :id="'b'+index">
  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/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{
  164. padding-right: 30rpx;
  165. }
  166. .productSort .aside {
  167. position: absolute;
  168. width: 180rpx;
  169. left: 0;
  170. top: 0;
  171. background-color: #fff;
  172. overflow-y: scroll;
  173. overflow-x: hidden;
  174. height: auto;
  175. border-radius: 16rpx;
  176. }
  177. .productSort .aside .item {
  178. height: 100rpx;
  179. width: 100%;
  180. font-size: 26rpx;
  181. color: #424242;
  182. }
  183. .productSort .aside .item.on {
  184. background-color: #fff;
  185. border-left: 6rpx solid #F8C008;
  186. width: 100%;
  187. text-align: center;
  188. color: #F8C008;
  189. font-weight: bold;
  190. }
  191. .productSort .conter {
  192. margin: 0rpx 0 0 200rpx;
  193. padding: 0 14rpx;
  194. background-color: #fff;
  195. border-radius: 16rpx;
  196. }
  197. .productSort .conter .listw {
  198. padding-top: 20rpx;
  199. }
  200. .productSort .conter .listw .title {
  201. height: 90rpx;
  202. }
  203. .productSort .conter .listw .title .line {
  204. width: 100rpx;
  205. height: 2rpx;
  206. background-color: #f0f0f0;
  207. }
  208. .productSort .conter .listw .title .name {
  209. font-size: 28rpx;
  210. color: #333;
  211. margin: 0 30rpx;
  212. font-weight: bold;
  213. }
  214. .productSort .conter .list {
  215. flex-wrap: wrap;
  216. }
  217. .productSort .conter .list .item {
  218. width: 177rpx;
  219. margin-top: 26rpx;
  220. }
  221. .productSort .conter .list .item .picture {
  222. width: 120rpx;
  223. height: 120rpx;
  224. border-radius: 50%;
  225. }
  226. .productSort .conter .list .item .picture image {
  227. width: 100%;
  228. height: 100%;
  229. border-radius: 50%;
  230. div {
  231. background-color: #f7f7f7;
  232. }
  233. }
  234. .productSort .conter .list .item .name {
  235. font-size: 24rpx;
  236. color: #333;
  237. height: 56rpx;
  238. line-height: 56rpx;
  239. width: 120rpx;
  240. text-align: center;
  241. }
  242. </style>