merchant_cate.vue 6.1 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' :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. try {
  70. let obj = {
  71. type: 1,
  72. status: 1
  73. }
  74. const {
  75. data
  76. } = await productCategory(obj);
  77. const newArr = []
  78. data.forEach((value, index) => {
  79. newArr[index] = value
  80. if (value.child) newArr[index].child = value.child.filter(item => item.status === true)
  81. })
  82. let listArr = newArr.filter(item => item.code !== 'bb_mall')
  83. productList.value = listArr.sort((a, b) => a.sort - b.sort);
  84. // 使用 nextTick 确保 DOM 已更新
  85. // nextTick(() => {
  86. // setTimeout(() => {
  87. // infoScroll();
  88. // }, 500);
  89. // });
  90. } catch (error) {
  91. console.error('获取分类列表失败:', error);
  92. uni.showToast({
  93. title: '加载分类失败'
  94. });
  95. }
  96. };
  97. // 计算滚动相关数据
  98. const infoScroll = () => {
  99. nextTick(() => {
  100. setTimeout(() => {
  101. const len = productList.value.length;
  102. // 创建查询对象,并指定在当前组件实例中查询
  103. const query = uni.createSelectorQuery().in(instance);
  104. // 先查询所有元素
  105. query.selectAll('.listw').boundingClientRect((rects) => {
  106. if (rects) {
  107. hightArr.value = rects.map(rect => rect.top);
  108. console.log('hightArr:', hightArr.value);
  109. }
  110. }).exec();
  111. }, 500);
  112. });
  113. };
  114. // 点击左侧导航
  115. const tap = (index, id) => {
  116. toView.value = id;
  117. navActive.value = index;
  118. };
  119. // 右侧滚动事件
  120. const scroll = (e) => {
  121. let scrollTop = e.detail.scrollTop;
  122. let scrollArr = hightArr.value;
  123. if (!scrollArr || scrollArr.length === 0) return;
  124. for (let i = 0; i < scrollArr.length; i++) {
  125. if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
  126. navActive.value = 0;
  127. } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
  128. navActive.value = i;
  129. } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
  130. navActive.value = scrollArr.length - 1;
  131. }
  132. }
  133. };
  134. // 监听 merchantId 变化
  135. watch(() => props.merchantId, (newVal) => {
  136. if (newVal) {
  137. getAllCategory();
  138. }
  139. }, {
  140. immediate: true
  141. }); // 立即执行一次
  142. // 页面加载
  143. onLoad(() => {
  144. });
  145. // 页面显示
  146. onShow(() => {
  147. getAllCategory();
  148. // // 设置商品列表高度
  149. uni.getSystemInfo({
  150. success: (res) => {
  151. height.value = (res.windowHeight) * (750 / res.windowWidth) - (88 + 30 + 30);
  152. },
  153. });
  154. });
  155. defineExpose({
  156. infoScroll
  157. })
  158. </script>
  159. <style scoped lang="scss">
  160. .productSort{
  161. padding-right: 30rpx;
  162. }
  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>