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