list.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <template>
  2. <view class="content">
  3. <view class="navbar" :style="{ position: headerPosition, top: headerTop }">
  4. <view class="nav-item" :class="{ current: filterIndex === 0 }" @click="tabClick(0)">综合排序</view>
  5. <view class="nav-item" :class="{ current: filterIndex === 1 }" @click="tabClick(1)">
  6. <text>销量优先</text>
  7. <view class="p-box">
  8. <text :class="{ active: numberOrder === 1 && filterIndex === 1 }" class="iconfont iconfold"></text>
  9. <text :class="{ active: numberOrder === 2 && filterIndex === 1 }" class="iconfont iconfold xia"></text>
  10. </view>
  11. </view>
  12. <view class="nav-item" :class="{ current: filterIndex === 2 }" @click="tabClick(2)">
  13. <text>价格</text>
  14. <view class="p-box">
  15. <text :class="{ active: priceOrder === 1 && filterIndex === 2 }" class="iconfont iconfold"></text>
  16. <text :class="{ active: priceOrder === 2 && filterIndex === 2 }" class="iconfont iconfold xia"></text>
  17. </view>
  18. </view>
  19. <text class="cate-item iconfont iconapps" @click="toggleCateMask('show')"></text>
  20. </view>
  21. <view class="goods-list">
  22. <view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navToDetailPage(item)">
  23. <view class="image-wrapper"><image :src="item.image" mode="aspectFill"></image></view>
  24. <text class="title clamp">{{ item.store_name }}</text>
  25. <view class="price-box">
  26. <text class="price">{{ item.price }}</text>
  27. <text>已售 {{ item.sales }}</text>
  28. </view>
  29. </view>
  30. </view>
  31. <uni-load-more :status="loadingType"></uni-load-more>
  32. <view class="cate-mask" :class="cateMaskState === 0 ? 'none' : cateMaskState === 1 ? 'show' : ''" @click="toggleCateMask">
  33. <view class="cate-content">
  34. <scroll-view scroll-y class="left-aside">
  35. <view v-for="item in cateList" :key="item.id" class="f-item b-b" :class="{ active: item.id === currentId }" @click.stop="tabtap(item)">
  36. {{ item.cate_name }}
  37. </view>
  38. </scroll-view>
  39. <scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
  40. <view v-for="item in cateList" :key="item.id" class="s-list" :id="'main-' + item.id">
  41. <text class="s-item">{{ item.cate_name }}</text>
  42. <view class="t-list ">
  43. <view @click.stop="changeCate(titem)" class="t-item" v-for="titem in item.children" :key="titem.id">
  44. <image :src="titem.pic"></image>
  45. <text>{{ titem.cate_name }}</text>
  46. </view>
  47. </view>
  48. </view>
  49. </scroll-view>
  50. </view>
  51. <!-- <view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
  52. <scroll-view scroll-y class="cate-list">
  53. <view v-for="item in cateList" :key="item.id">
  54. <view class="cate-item b-b two">{{ item.cate_name }}</view>
  55. <view v-for="tItem in item.children" :key="tItem.id" class="cate-item b-b" :class="{ active: tItem.id == cateId }" @click="changeCate(tItem)">
  56. {{ tItem.cate_name }}
  57. </view>
  58. </view>
  59. </scroll-view>
  60. </view> -->
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  66. import { getProducts } from '@/api/product.js';
  67. import { getCategoryList } from '@/api/product.js';
  68. export default {
  69. components: {
  70. uniLoadMore
  71. },
  72. data() {
  73. return {
  74. currentId: '',
  75. tabScrollTop: 0,
  76. cateMaskState: 0, //分类面板展开状态
  77. headerPosition: 'fixed',
  78. headerTop: '0px',
  79. loadingType: 'more', //加载更多状态
  80. filterIndex: 0, //查询类型
  81. numberOrder: 0, //1 销量从低到高 2销量从高到低
  82. limit: 6, //每次加载数据条数
  83. page: 1, //当前页数
  84. cateId: 0, //已选三级分类id
  85. priceOrder: 0, //1 价格从低到高 2价格从高到低
  86. cateList: [], //分类列表
  87. goodsList: [] //商品列表
  88. };
  89. },
  90. onLoad(options) {
  91. // #ifdef H5
  92. this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
  93. // #endif
  94. this.cateId = options.tid;
  95. this.loadCateList(options.fid, options.sid);
  96. this.loadData();
  97. },
  98. onPageScroll(e) {
  99. //兼容iOS端下拉时顶部漂移
  100. if (e.scrollTop >= 0) {
  101. this.headerPosition = 'fixed';
  102. } else {
  103. this.headerPosition = 'absolute';
  104. }
  105. },
  106. //下拉刷新
  107. onPullDownRefresh() {
  108. this.loadData('refresh');
  109. },
  110. //监听页面是否滚动到底部加载更多
  111. onReachBottom() {
  112. this.loadData();
  113. },
  114. methods: {
  115. //加载分类
  116. async loadCateList(fid, sid) {
  117. let obj = this;
  118. getCategoryList({}).then(function(e) {
  119. obj.cateList = e.data.filter(e => {
  120. return e.id != 1;
  121. });
  122. obj.currentId = obj.cateList[0].id;
  123. console.log(obj.cateList, '123456789');
  124. });
  125. },
  126. //一级分类点击
  127. tabtap(item) {
  128. console.log(item);
  129. // 判断有没有初始化页面高度对象数据
  130. if (!this.sizeCalcState) {
  131. this.calcSize();
  132. }
  133. // 获取当前点击的id
  134. this.currentId = item.id;
  135. console.log(item.top);
  136. this.tabScrollTop = item.top;
  137. console.log(this.tabScrollTop);
  138. },
  139. //右侧栏滚动
  140. asideScroll(e) {
  141. // 判断有没有初始化页面高度对象数据
  142. if (!this.sizeCalcState) {
  143. this.calcSize();
  144. }
  145. let scrollTop = e.detail.scrollTop;
  146. let box = 0; //列表包裹框高度初始化
  147. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  148. // 查询当前页面对象
  149. let view = uni.createSelectorQuery().select('.content');
  150. view.fields(
  151. {
  152. id: true,
  153. dataset: true,
  154. rect: true,
  155. size: true,
  156. scrollOffset: true
  157. },
  158. function(e) {
  159. // 保存包裹框高度
  160. box = e.height;
  161. }
  162. ).exec();
  163. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  164. let tabs = this.cateList.filter(item => item.top - 10 <= scrollTop).reverse();
  165. if (tabs.length > 0) {
  166. // 判断是否已经到达滚轮底部
  167. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  168. this.currentId = this.cateList[this.cateList.length - 1].id;
  169. } else {
  170. this.currentId = tabs[0].id;
  171. }
  172. }
  173. },
  174. //计算右侧栏每个tab的高度等信息
  175. calcSize() {
  176. let h = 0;
  177. this.cateList.forEach(item => {
  178. let view = uni.createSelectorQuery().select('#main-' + item.id);
  179. view.fields(
  180. {
  181. size: true
  182. },
  183. data => {
  184. item.top = h;
  185. h += data.height;
  186. item.bottom = h;
  187. }
  188. ).exec();
  189. });
  190. this.sizeCalcState = true;
  191. },
  192. //加载商品 ,带下拉刷新和上滑加载
  193. async loadData(type = 'add', loading) {
  194. let obj = this;
  195. let data = {
  196. page: obj.page,
  197. limit: obj.limit,
  198. sid: obj.cateId //分类id
  199. };
  200. //没有更多直接返回
  201. if (type === 'add') {
  202. if (obj.loadingType === 'nomore') {
  203. return;
  204. }
  205. obj.loadingType = 'loading';
  206. } else {
  207. obj.loadingType = 'more';
  208. }
  209. if (type === 'refresh') {
  210. // 清空数组
  211. obj.goodsList = [];
  212. obj.page = 1;
  213. }
  214. if (this.filterIndex == 1) {
  215. console.log(obj.salesOrder);
  216. data.salesOrder = obj.numberOrder == 1 ? 'asc' : 'desc';
  217. }
  218. if (this.filterIndex == 2) {
  219. console.log(obj.priceOrder);
  220. data.priceOrder = obj.priceOrder == 1 ? 'asc' : 'desc';
  221. }
  222. getProducts(data).then(function(e) {
  223. console.log(e.data);
  224. let arr = e.data.filter(info => {
  225. return info.cate_id != 2;
  226. });
  227. obj.goodsList = obj.goodsList.concat(arr);
  228. //判断是否还有下一页,有是more 没有是nomore
  229. if (obj.limit == e.data.length) {
  230. obj.page++;
  231. obj.loadingType = 'more';
  232. } else {
  233. obj.loadingType = 'nomore';
  234. }
  235. if (type === 'refresh') {
  236. if (loading == 1) {
  237. uni.hideLoading();
  238. } else {
  239. uni.stopPullDownRefresh();
  240. }
  241. }
  242. });
  243. },
  244. //筛选点击
  245. tabClick(index) {
  246. // 防止重复点击综合排序
  247. if (this.filterIndex === 0 && this.filterIndex === index) {
  248. return;
  249. }
  250. this.filterIndex = index;
  251. // 判断是否为销量优先
  252. if (index === 1) {
  253. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  254. }
  255. // 判断是否为价格优先
  256. if (index === 2) {
  257. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  258. }
  259. // 初始化页数
  260. this.page = 1;
  261. // 初始化数组
  262. uni.pageScrollTo({
  263. duration: 300,
  264. scrollTop: 0
  265. });
  266. this.loadData('refresh', 1);
  267. uni.showLoading({
  268. title: '正在加载'
  269. });
  270. },
  271. //显示分类面板
  272. toggleCateMask(type) {
  273. let timer = type === 'show' ? 10 : 300;
  274. let state = type === 'show' ? 1 : 0;
  275. this.cateMaskState = 2;
  276. setTimeout(() => {
  277. this.cateMaskState = state;
  278. }, timer);
  279. },
  280. //分类点击
  281. changeCate(item) {
  282. this.cateId = item.id;
  283. // 显示右侧分类
  284. this.toggleCateMask();
  285. // 滚轮返回顶部
  286. uni.pageScrollTo({
  287. duration: 300,
  288. scrollTop: 0
  289. });
  290. // 初始化查询页数
  291. this.page = 1;
  292. // 重新加载数据
  293. this.loadData('refresh', 1);
  294. uni.showLoading({
  295. title: '正在加载'
  296. });
  297. },
  298. //详情
  299. navToDetailPage(item) {
  300. let id = item.id;
  301. if (this.sid == 52) {
  302. uni.navigateTo({
  303. url: `/pages/product/product?id=${id}&isVip=1`
  304. });
  305. } else {
  306. uni.navigateTo({
  307. url: `/pages/product/product?id=${id}`
  308. });
  309. }
  310. }
  311. }
  312. };
  313. </script>
  314. <style lang="scss">
  315. page,
  316. .content {
  317. background: $page-color-base;
  318. }
  319. .content {
  320. padding-top: 96rpx;
  321. }
  322. .navbar {
  323. position: fixed;
  324. left: 0;
  325. top: var(--window-top);
  326. display: flex;
  327. width: 100%;
  328. height: 80rpx;
  329. background: #fff;
  330. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  331. z-index: 10;
  332. .nav-item {
  333. flex: 1;
  334. display: flex;
  335. justify-content: center;
  336. align-items: center;
  337. height: 100%;
  338. font-size: 30rpx;
  339. color: $font-color-dark;
  340. position: relative;
  341. &.current {
  342. color: #ff4c4c;
  343. &:after {
  344. content: '';
  345. position: absolute;
  346. left: 50%;
  347. bottom: 0;
  348. transform: translateX(-50%);
  349. width: 150rpx;
  350. height: 0;
  351. border-bottom: 4rpx solid #ff4c4c;
  352. }
  353. }
  354. }
  355. .p-box {
  356. display: flex;
  357. flex-direction: column;
  358. .iconfont {
  359. display: flex;
  360. align-items: center;
  361. justify-content: center;
  362. width: 30rpx;
  363. height: 14rpx;
  364. line-height: 1;
  365. margin-left: 4rpx;
  366. font-size: 26rpx;
  367. color: #888;
  368. &.active {
  369. color: $base-color;
  370. }
  371. }
  372. .xia {
  373. transform: scaleY(-1);
  374. }
  375. }
  376. .cate-item {
  377. display: flex;
  378. justify-content: center;
  379. align-items: center;
  380. height: 100%;
  381. width: 80rpx;
  382. position: relative;
  383. font-size: 44rpx;
  384. &:after {
  385. content: '';
  386. position: absolute;
  387. left: 0;
  388. top: 50%;
  389. transform: translateY(-50%);
  390. border-left: 1px solid #ddd;
  391. width: 0;
  392. height: 36rpx;
  393. }
  394. }
  395. }
  396. /* 分类 */
  397. .cate-mask {
  398. position: fixed;
  399. left: 0;
  400. top: var(--window-top);
  401. bottom: 0;
  402. width: 100%;
  403. background: rgba(0, 0, 0, 0);
  404. z-index: 95;
  405. transition: 0.3s;
  406. .cate-content {
  407. width: 630rpx;
  408. height: 100%;
  409. background: #fff;
  410. float: right;
  411. transform: translateX(100%);
  412. transition: 0.3s;
  413. display: flex;
  414. .left-aside {
  415. flex-shrink: 0;
  416. width: 200rpx;
  417. height: 100%;
  418. background-color: #fff;
  419. }
  420. .f-item {
  421. display: flex;
  422. align-items: center;
  423. justify-content: center;
  424. width: 100%;
  425. height: 100rpx;
  426. font-size: 28rpx;
  427. color: $font-color-base;
  428. position: relative;
  429. &.active {
  430. color: $base-color;
  431. background: #f8f8f8;
  432. &:before {
  433. content: '';
  434. position: absolute;
  435. left: 0;
  436. top: 50%;
  437. transform: translateY(-50%);
  438. height: 36rpx;
  439. width: 8rpx;
  440. background-color: $base-color;
  441. border-radius: 0 4px 4px 0;
  442. opacity: 0.8;
  443. }
  444. }
  445. }
  446. .right-aside {
  447. flex: 1;
  448. overflow: hidden;
  449. padding-left: 20rpx;
  450. padding-right: 20rpx;
  451. }
  452. .s-item {
  453. display: flex;
  454. align-items: center;
  455. height: 70rpx;
  456. padding-top: 8rpx;
  457. font-size: 28rpx;
  458. color: $font-color-dark;
  459. }
  460. .t-list {
  461. display: flex;
  462. flex-wrap: wrap;
  463. border-radius: 15rpx;
  464. width: 100%;
  465. background: #fff;
  466. padding-top: 12rpx;
  467. &:after {
  468. content: '';
  469. flex: 99;
  470. height: 0;
  471. }
  472. }
  473. .t-item {
  474. flex-shrink: 0;
  475. display: flex;
  476. justify-content: center;
  477. align-items: center;
  478. flex-direction: column;
  479. width: 171rpx;
  480. font-size: 26rpx;
  481. color: #666;
  482. padding-bottom: 20rpx;
  483. image {
  484. width: 140rpx;
  485. height: 140rpx;
  486. }
  487. }
  488. }
  489. &.none {
  490. display: none;
  491. }
  492. &.show {
  493. background: rgba(0, 0, 0, 0.4);
  494. .cate-content {
  495. transform: translateX(0);
  496. }
  497. }
  498. }
  499. .cate-list {
  500. display: flex;
  501. flex-direction: column;
  502. height: 100%;
  503. .cate-item {
  504. display: flex;
  505. align-items: center;
  506. height: 90rpx;
  507. padding-left: 30rpx;
  508. font-size: 28rpx;
  509. color: #555;
  510. position: relative;
  511. }
  512. .two {
  513. height: 64rpx;
  514. color: #303133;
  515. font-size: 30rpx;
  516. background: #f8f8f8;
  517. }
  518. .active {
  519. color: $base-color;
  520. }
  521. }
  522. /* 商品列表 */
  523. .goods-list {
  524. display: flex;
  525. flex-wrap: wrap;
  526. padding: 0 30rpx;
  527. background: #fff;
  528. .goods-item {
  529. display: flex;
  530. flex-direction: column;
  531. width: 48%;
  532. padding-bottom: 40rpx;
  533. &:nth-child(2n + 1) {
  534. margin-right: 4%;
  535. }
  536. }
  537. .image-wrapper {
  538. width: 100%;
  539. height: 330rpx;
  540. border-radius: 3px;
  541. overflow: hidden;
  542. position: relative;
  543. image {
  544. width: 100%;
  545. height: 100%;
  546. opacity: 1;
  547. }
  548. }
  549. .title {
  550. font-size: $font-lg;
  551. color: $font-color-dark;
  552. line-height: 80rpx;
  553. }
  554. .price-box {
  555. display: flex;
  556. align-items: center;
  557. justify-content: space-between;
  558. padding-right: 10rpx;
  559. font-size: 24rpx;
  560. color: $font-color-light;
  561. }
  562. .price {
  563. font-size: $font-lg;
  564. color: #ff4c4c;
  565. line-height: 1;
  566. &:before {
  567. content: '¥';
  568. font-size: 26rpx;
  569. }
  570. }
  571. }
  572. </style>