goods_cate.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <view class='productSort'>
  3. <view class='header acea-row row-center-wrapper'>
  4. <view class='acea-row row-between-wrapper input'>
  5. <text class='iconfont icon-sousuo'></text>
  6. <input type='text' placeholder='点击搜索商品信息' @confirm="searchSubmitValue" confirm-type='search' name="search"
  7. placeholder-class='placeholder' @focus="searchSubmitValue"></input>
  8. </view>
  9. </view>
  10. <view class='aside' :style="{bottom: tabbarH + 'px',height: height + 'rpx'}">
  11. <scroll-view scroll-y="true" scroll-with-animation='true' style="height: 100%;">
  12. <view class='item acea-row row-center-wrapper'
  13. :class='index==navActive?"on":""'
  14. v-for="(item,index) in productList"
  15. :key="index"
  16. @click='tap(index,"b"+index)'>
  17. <text>{{item.name}}</text>
  18. </view>
  19. </scroll-view>
  20. </view>
  21. <view class='conter'>
  22. <scroll-view scroll-y="true"
  23. :scroll-into-view="toView"
  24. :style='"height:"+height+"rpx;margin-top: 96rpx;"'
  25. @scroll="scroll"
  26. scroll-with-animation='true'>
  27. <view v-for="(item,index) in productList" :key="index">
  28. <view class='listw' :id="'b'+index">
  29. <view class='title acea-row row-center-wrapper'>
  30. <view class='line'></view>
  31. <view class='name'>{{item.name}}</view>
  32. <view class='line'></view>
  33. </view>
  34. <view class='list acea-row'>
  35. <view v-for="(itemn,indexn) in item.child" :key="indexn">
  36. <navigator hover-class='none'
  37. :url='"/pages/goods/goods_search/index?cid="+itemn.id+"&title="+itemn.name'
  38. class='item acea-row row-column row-middle'>
  39. <view class='name line1'>{{itemn.name}}</view>
  40. </navigator>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. <view :style='"height:"+(height-300)+"rpx;"' v-if="number<15"></view>
  46. </scroll-view>
  47. </view>
  48. <!-- 自定义 tabBar -->
  49. <customTabBar :current="1" :showBackTop="false" />
  50. </view>
  51. </template>
  52. <script setup>
  53. import { ref, onMounted, nextTick } from 'vue';
  54. import { onLoad, onShow } from '@dcloudio/uni-app';
  55. import { productCategory } from '@/api/merchant.js';
  56. import customTabBar from "@/components/customTabBar/index.vue";
  57. // 响应式数据
  58. const productList = ref([]);
  59. const navActive = ref(0);
  60. const number = ref("");
  61. const height = ref(0);
  62. const hightArr = ref([]);
  63. const toView = ref("");
  64. const tabbarH = ref(0);
  65. const query = ref({})
  66. // 获取分类列表
  67. const getAllCategory = async () => {
  68. try {
  69. let obj = {
  70. type: 1,
  71. status: 1
  72. }
  73. const { data }= await productCategory(obj);
  74. const newArr = []
  75. data.forEach((value, index) => {
  76. newArr[index] = value
  77. if (value.child) newArr[index].child = value.child.filter(item => item.status === true)
  78. })
  79. let listArr = newArr.filter(item => item.code !== 'bb_mall')
  80. productList.value = listArr.sort((a, b) => a.sort - b.sort);
  81. if(query.value.id){
  82. const index = productList.value.findIndex(item => item.id === query.value.id);
  83. tap(index,query.value.id)
  84. }
  85. // 使用 nextTick 确保 DOM 已更新
  86. nextTick(() => {
  87. setTimeout(() => {
  88. infoScroll();
  89. }, 500);
  90. });
  91. } catch (error) {
  92. console.error('获取分类列表失败:', error);
  93. uni.showToast({ title: '加载分类失败' });
  94. }
  95. };
  96. // 计算滚动相关数据
  97. const infoScroll = () => {
  98. let len = productList.value.length;
  99. let child = productList.value[len - 1] && productList.value[len - 1].child
  100. ? productList.value[len - 1].child
  101. : [];
  102. number.value = child ? child.length : 0;
  103. // 设置商品列表高度
  104. uni.getSystemInfo({
  105. success: (res) => {
  106. height.value = (res.windowHeight) * (750 / res.windowWidth) - 98;
  107. },
  108. });
  109. let heightTemp = 0;
  110. let hightArrTemp = [];
  111. for (let i = 0; i < len; i++) {
  112. // 获取元素所在位置
  113. let query = uni.createSelectorQuery();
  114. let idView = "#b" + i;
  115. query.select(idView).boundingClientRect();
  116. query.exec((res) => {
  117. if (res && res[0]) {
  118. let top = res[0].top;
  119. hightArrTemp.push(top);
  120. hightArr.value = hightArrTemp;
  121. console.log('hightArr.value')
  122. console.log(hightArr.value)
  123. }
  124. });
  125. }
  126. };
  127. // 点击左侧导航
  128. const tap = (index, id) => {
  129. toView.value = id;
  130. navActive.value = index;
  131. };
  132. // 右侧滚动事件
  133. const scroll = (e) => {
  134. let scrollTop = e.detail.scrollTop;
  135. let scrollArr = hightArr.value;
  136. if (!scrollArr || scrollArr.length === 0) return;
  137. for (let i = 0; i < scrollArr.length; i++) {
  138. if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
  139. navActive.value = 0;
  140. } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
  141. navActive.value = i;
  142. } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
  143. navActive.value = scrollArr.length - 1;
  144. }
  145. }
  146. };
  147. // 搜索提交
  148. const searchSubmitValue = (e) => {
  149. const searchValue = e.detail.value?.trim();
  150. // if (!searchValue) {
  151. // return uni.showToast({ title: '请填写要搜索的产品信息' });
  152. // }
  153. // if (searchValue.length > 0) {
  154. uni.navigateTo({
  155. url: '/pages/goods/goods_search/index?keyword=' + searchValue
  156. });
  157. // }
  158. };
  159. // 页面加载
  160. onShow(() => {
  161. const params = uni.getStorageSync('goods_cate');
  162. uni.removeStorageSync('goods_cate');
  163. query.value = params;
  164. getAllCategory();
  165. });
  166. // 页面显示
  167. onShow(() => {
  168. // 隐藏原生 tabBar
  169. uni.hideTabBar();
  170. });
  171. </script>
  172. <style scoped lang="scss">
  173. .productSort .header {
  174. width: 100%;
  175. height: 96rpx;
  176. background-color: #fff;
  177. position: fixed;
  178. left: 0;
  179. right: 0;
  180. top: 0;
  181. z-index: 9;
  182. border-bottom: 1rpx solid #f5f5f5;
  183. }
  184. .productSort .header .input {
  185. width: 700rpx;
  186. height: 88rpx;
  187. background-color: #f5f5f5;
  188. border-radius: 16rpx;
  189. box-sizing: border-box;
  190. padding: 0 25rpx;
  191. }
  192. .productSort .header .input .iconfont {
  193. font-size: 26rpx;
  194. color: #555;
  195. }
  196. .productSort .header .input .placeholder {
  197. color: #999;
  198. }
  199. .productSort .header .input input {
  200. font-size: 26rpx;
  201. height: 100%;
  202. width: 597rpx;
  203. }
  204. .productSort .aside {
  205. position: absolute;
  206. width: 180rpx;
  207. left: 0;
  208. top:0;
  209. background-color: #fff;
  210. overflow-y: scroll;
  211. overflow-x: hidden;
  212. height: auto;
  213. margin-top: 116rpx;
  214. border-radius: 16rpx;
  215. }
  216. .productSort .aside .item {
  217. height: 100rpx;
  218. width: 100%;
  219. font-size: 26rpx;
  220. color: #424242;
  221. }
  222. .productSort .aside .item.on {
  223. background-color: #fff;
  224. border-left: 6rpx solid #F8C008;
  225. width: 100%;
  226. text-align: center;
  227. color: #F8C008;
  228. font-weight: bold;
  229. }
  230. .productSort .conter {
  231. margin: 116rpx 0 0 200rpx;
  232. padding: 0 14rpx;
  233. background-color: #fff;
  234. border-radius: 16rpx;
  235. }
  236. .productSort .conter .listw {
  237. padding-top: 20rpx;
  238. }
  239. .productSort .conter .listw .title {
  240. height: 90rpx;
  241. }
  242. .productSort .conter .listw .title .line {
  243. width: 100rpx;
  244. height: 2rpx;
  245. background-color: #f0f0f0;
  246. }
  247. .productSort .conter .listw .title .name {
  248. font-size: 28rpx;
  249. color: #333;
  250. margin: 0 30rpx;
  251. font-weight: bold;
  252. }
  253. .productSort .conter .list {
  254. flex-wrap: wrap;
  255. }
  256. .productSort .conter .list .item {
  257. width: 177rpx;
  258. margin-top: 26rpx;
  259. }
  260. .productSort .conter .list .item .picture {
  261. width: 120rpx;
  262. height: 120rpx;
  263. border-radius: 50%;
  264. }
  265. .productSort .conter .list .item .picture image {
  266. width: 100%;
  267. height: 100%;
  268. border-radius: 50%;
  269. div{
  270. background-color: #f7f7f7;
  271. }
  272. }
  273. .productSort .conter .list .item .name {
  274. font-size: 24rpx;
  275. color: #333;
  276. height: 56rpx;
  277. line-height: 56rpx;
  278. width: 120rpx;
  279. text-align: center;
  280. }
  281. </style>