list.vue 15 KB

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