merchant_cate.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. let len = productList.value.length;
  92. let child = productList.value[len - 1] && productList.value[len - 1].child
  93. ? productList.value[len - 1].child
  94. : [];
  95. number.value = child ? child.length : 0;
  96. // 设置商品列表高度
  97. uni.getSystemInfo({
  98. success: (res) => {
  99. height.value = (res.windowHeight) * (750 / res.windowWidth) - 98;
  100. },
  101. });
  102. let heightTemp = 0;
  103. let hightArrTemp = [];
  104. for (let i = 0; i < len; i++) {
  105. // 获取元素所在位置
  106. let query = uni.createSelectorQuery();
  107. let idView = "#b" + i;
  108. query.select(idView).boundingClientRect();
  109. query.exec((res) => {
  110. if (res && res[0]) {
  111. let top = res[0].top;
  112. hightArrTemp.push(top);
  113. hightArr.value = hightArrTemp;
  114. }
  115. });
  116. }
  117. };
  118. // 点击左侧导航
  119. const tap = (index, id) => {
  120. toView.value = id;
  121. navActive.value = index;
  122. };
  123. // 右侧滚动事件
  124. const scroll = (e) => {
  125. let scrollTop = e.detail.scrollTop;
  126. let scrollArr = hightArr.value;
  127. if (!scrollArr || scrollArr.length === 0) return;
  128. for (let i = 0; i < scrollArr.length; i++) {
  129. if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
  130. navActive.value = 0;
  131. } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
  132. navActive.value = i;
  133. } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
  134. navActive.value = scrollArr.length - 1;
  135. }
  136. }
  137. };
  138. // 监听 merchantId 变化
  139. watch(() => props.merchantId, (newVal) => {
  140. console.log('merchantId 变化了:', newVal);
  141. if (newVal) {
  142. getAllCategory();
  143. }
  144. }, { immediate: true }); // 立即执行一次
  145. // 页面加载
  146. onLoad(() => {
  147. });
  148. // 页面显示
  149. onShow(() => {
  150. getAllCategory();
  151. });
  152. </script>
  153. <style scoped lang="scss">
  154. .productSort .aside {
  155. position: absolute;
  156. width: 180rpx;
  157. left: 0;
  158. top:0;
  159. background-color: #fff;
  160. overflow-y: scroll;
  161. overflow-x: hidden;
  162. height: auto;
  163. border-radius: 16rpx;
  164. }
  165. .productSort .aside .item {
  166. height: 100rpx;
  167. width: 100%;
  168. font-size: 26rpx;
  169. color: #424242;
  170. }
  171. .productSort .aside .item.on {
  172. background-color: #fff;
  173. border-left: 6rpx solid #F8C008;
  174. width: 100%;
  175. text-align: center;
  176. color: #F8C008;
  177. font-weight: bold;
  178. }
  179. .productSort .conter {
  180. margin: 0rpx 0 0 200rpx;
  181. padding: 0 14rpx;
  182. background-color: #fff;
  183. border-radius: 16rpx;
  184. }
  185. .productSort .conter .listw {
  186. padding-top: 20rpx;
  187. }
  188. .productSort .conter .listw .title {
  189. height: 90rpx;
  190. }
  191. .productSort .conter .listw .title .line {
  192. width: 100rpx;
  193. height: 2rpx;
  194. background-color: #f0f0f0;
  195. }
  196. .productSort .conter .listw .title .name {
  197. font-size: 28rpx;
  198. color: #333;
  199. margin: 0 30rpx;
  200. font-weight: bold;
  201. }
  202. .productSort .conter .list {
  203. flex-wrap: wrap;
  204. }
  205. .productSort .conter .list .item {
  206. width: 177rpx;
  207. margin-top: 26rpx;
  208. }
  209. .productSort .conter .list .item .picture {
  210. width: 120rpx;
  211. height: 120rpx;
  212. border-radius: 50%;
  213. }
  214. .productSort .conter .list .item .picture image {
  215. width: 100%;
  216. height: 100%;
  217. border-radius: 50%;
  218. div{
  219. background-color: #f7f7f7;
  220. }
  221. }
  222. .productSort .conter .list .item .name {
  223. font-size: 24rpx;
  224. color: #333;
  225. height: 56rpx;
  226. line-height: 56rpx;
  227. width: 120rpx;
  228. text-align: center;
  229. }
  230. </style>