goods_cate.vue 7.5 KB

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