goods_cate.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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"/>
  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. if(value.name === '折扣券') return
  77. newArr[index] = value
  78. if (value.child) newArr[index].child = value.child.filter(item => item.status === true)
  79. })
  80. let listArr = newArr.filter(item => item.code !== 'bb_mall')
  81. productList.value = listArr.sort((a, b) => a.sort - b.sort);
  82. if(query.value.id){
  83. const index = productList.value.findIndex(item => item.id === query.value.id);
  84. tap(index,query.value.id)
  85. }
  86. // 使用 nextTick 确保 DOM 已更新
  87. nextTick(() => {
  88. setTimeout(() => {
  89. infoScroll();
  90. }, 500);
  91. });
  92. } catch (error) {
  93. console.error('获取分类列表失败:', error);
  94. uni.showToast({ title: '加载分类失败' });
  95. }
  96. };
  97. // 计算滚动相关数据
  98. const infoScroll = () => {
  99. let len = productList.value.length;
  100. let child = productList.value[len - 1] && productList.value[len - 1].child
  101. ? productList.value[len - 1].child
  102. : [];
  103. number.value = child ? child.length : 0;
  104. // 设置商品列表高度
  105. uni.getSystemInfo({
  106. success: (res) => {
  107. height.value = (res.windowHeight) * (750 / res.windowWidth) - 188;
  108. },
  109. });
  110. let heightTemp = 0;
  111. let hightArrTemp = [];
  112. for (let i = 0; i < len; i++) {
  113. // 获取元素所在位置
  114. let query = uni.createSelectorQuery();
  115. let idView = "#b" + i;
  116. query.select(idView).boundingClientRect();
  117. query.exec((res) => {
  118. if (res && res[0]) {
  119. let top = res[0].top;
  120. hightArrTemp.push(top);
  121. hightArr.value = hightArrTemp;
  122. console.log('hightArr.value')
  123. console.log(hightArr.value)
  124. }
  125. });
  126. }
  127. };
  128. // 点击左侧导航
  129. const tap = (index, id) => {
  130. toView.value = id;
  131. navActive.value = index;
  132. };
  133. // 右侧滚动事件
  134. const scroll = (e) => {
  135. let scrollTop = e.detail.scrollTop;
  136. let scrollArr = hightArr.value;
  137. if (!scrollArr || scrollArr.length === 0) return;
  138. for (let i = 0; i < scrollArr.length; i++) {
  139. if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
  140. navActive.value = 0;
  141. } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
  142. navActive.value = i;
  143. } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
  144. navActive.value = scrollArr.length - 1;
  145. }
  146. }
  147. };
  148. // 搜索提交
  149. const searchSubmitValue = (e) => {
  150. const searchValue = e.detail.value?.trim();
  151. // if (!searchValue) {
  152. // return uni.showToast({ title: '请填写要搜索的产品信息' });
  153. // }
  154. // if (searchValue.length > 0) {
  155. uni.navigateTo({
  156. url: '/pages/goods/goods_search/index?keyword=' + searchValue
  157. });
  158. // }
  159. };
  160. // 页面加载
  161. onShow(() => {
  162. const params = uni.getStorageSync('goods_cate');
  163. uni.removeStorageSync('goods_cate');
  164. query.value = params;
  165. getAllCategory();
  166. });
  167. // 页面显示
  168. onShow(() => {
  169. // 隐藏原生 tabBar
  170. uni.hideTabBar();
  171. });
  172. </script>
  173. <style scoped lang="scss">
  174. .productSort .header {
  175. width: 100%;
  176. height: 96rpx;
  177. background-color: #fff;
  178. position: fixed;
  179. left: 0;
  180. right: 0;
  181. top: 0;
  182. z-index: 9;
  183. border-bottom: 1rpx solid #f5f5f5;
  184. }
  185. .productSort .header .input {
  186. width: 700rpx;
  187. height: 88rpx;
  188. background-color: #f5f5f5;
  189. border-radius: 16rpx;
  190. box-sizing: border-box;
  191. padding: 0 25rpx;
  192. }
  193. .productSort .header .input .iconfont {
  194. font-size: 26rpx;
  195. color: #555;
  196. }
  197. .productSort .header .input .placeholder {
  198. color: #999;
  199. }
  200. .productSort .header .input input {
  201. font-size: 26rpx;
  202. height: 100%;
  203. width: 597rpx;
  204. }
  205. .productSort .aside {
  206. position: absolute;
  207. width: 180rpx;
  208. left: 0;
  209. top:0;
  210. background-color: #fff;
  211. overflow-y: scroll;
  212. overflow-x: hidden;
  213. height: auto;
  214. margin-top: 116rpx;
  215. border-radius: 16rpx;
  216. }
  217. .productSort .aside .item {
  218. height: 100rpx;
  219. width: 100%;
  220. font-size: 26rpx;
  221. color: #424242;
  222. }
  223. .productSort .aside .item.on {
  224. background-color: #fff;
  225. border-left: 6rpx solid #F8C008;
  226. width: 100%;
  227. text-align: center;
  228. color: #F8C008;
  229. font-weight: bold;
  230. }
  231. .productSort .conter {
  232. margin: 116rpx 0 0 200rpx;
  233. padding: 0 14rpx;
  234. background-color: #fff;
  235. border-radius: 16rpx;
  236. }
  237. .productSort .conter .listw {
  238. padding-top: 20rpx;
  239. }
  240. .productSort .conter .listw .title {
  241. height: 90rpx;
  242. }
  243. .productSort .conter .listw .title .line {
  244. width: 100rpx;
  245. height: 2rpx;
  246. background-color: #f0f0f0;
  247. }
  248. .productSort .conter .listw .title .name {
  249. font-size: 28rpx;
  250. color: #333;
  251. margin: 0 30rpx;
  252. font-weight: bold;
  253. }
  254. .productSort .conter .list {
  255. flex-wrap: wrap;
  256. }
  257. .productSort .conter .list .item {
  258. width: 177rpx;
  259. margin-top: 26rpx;
  260. }
  261. .productSort .conter .list .item .picture {
  262. width: 120rpx;
  263. height: 120rpx;
  264. border-radius: 50%;
  265. }
  266. .productSort .conter .list .item .picture image {
  267. width: 100%;
  268. height: 100%;
  269. border-radius: 50%;
  270. div{
  271. background-color: #f7f7f7;
  272. }
  273. }
  274. .productSort .conter .list .item .name {
  275. font-size: 24rpx;
  276. color: #333;
  277. height: 56rpx;
  278. line-height: 56rpx;
  279. width: 120rpx;
  280. text-align: center;
  281. }
  282. </style>