list.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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">
  24. <image :src="item.image" mode="aspectFill"></image>
  25. <!-- <view class="fanli" v-if="item.give_integral != 0">
  26. <view class="fanli-bg"><image src="../../static/img/index-fanl.png" mode=""></image></view>
  27. <view class="fanli-font flex">
  28. <view class="font-left">获得响亮积分</view>
  29. <view class="font-right">{{ item.give_integral * 1 }}</view>
  30. </view>
  31. </view> -->
  32. </view>
  33. <text class="title clamp">{{ item.store_name }}</text>
  34. <view class="price-box">
  35. <text class="price">{{ item.price }}</text>
  36. <text>已售 {{ item.sales }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. <uni-load-more :status="loadingType"></uni-load-more>
  41. <view class="cate-mask" :class="cateMaskState === 0 ? 'none' : cateMaskState === 1 ? 'show' : ''" @click="toggleCateMask">
  42. <view class="cate-content">
  43. <scroll-view scroll-y class="left-aside">
  44. <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>
  45. </scroll-view>
  46. <scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
  47. <view v-for="item in cateList" :key="item.id" class="s-list" :id="'main-' + item.id">
  48. <text class="s-item">{{ item.cate_name }}</text>
  49. <view class="t-list ">
  50. <view @click.stop="changeCate(titem)" class="t-item" v-for="titem in item.children" :key="titem.id">
  51. <image :src="titem.pic"></image>
  52. <text>{{ titem.cate_name }}</text>
  53. </view>
  54. </view>
  55. </view>
  56. </scroll-view>
  57. </view>
  58. <!-- <view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
  59. <scroll-view scroll-y class="cate-list">
  60. <view v-for="item in cateList" :key="item.id">
  61. <view class="cate-item b-b two">{{ item.cate_name }}</view>
  62. <view v-for="tItem in item.children" :key="tItem.id" class="cate-item b-b" :class="{ active: tItem.id == cateId }" @click="changeCate(tItem)">
  63. {{ tItem.cate_name }}
  64. </view>
  65. </view>
  66. </scroll-view>
  67. </view> -->
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  73. import { getProducts,getCategoryList } from '@/api/product.js';
  74. export default {
  75. components: {
  76. uniLoadMore
  77. },
  78. data() {
  79. return {
  80. currentId: '',
  81. tabScrollTop: 0,
  82. cateMaskState: 0, //分类面板展开状态
  83. headerPosition: 'fixed',
  84. headerTop: '0px',
  85. loadingType: 'more', //加载更多状态
  86. filterIndex: 0, //查询类型
  87. numberOrder: 0, //1 销量从低到高 2销量从高到低
  88. limit: 6, //每次加载数据条数
  89. page: 1, //当前页数
  90. cateId: 0, //已选三级分类id
  91. priceOrder: 0, //1 价格从低到高 2价格从高到低
  92. cateList: [], //分类列表
  93. goodsList: [] //商品列表
  94. };
  95. },
  96. onLoad(options) {
  97. // #ifdef H5
  98. // this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
  99. // #endif
  100. this.cateId = options.tid;
  101. this.loadCateList(options.fid, options.sid);
  102. this.loadData();
  103. },
  104. onPageScroll(e) {
  105. //兼容iOS端下拉时顶部漂移
  106. if (e.scrollTop >= 0) {
  107. this.headerPosition = 'fixed';
  108. } else {
  109. this.headerPosition = 'absolute';
  110. }
  111. },
  112. //下拉刷新
  113. onPullDownRefresh() {
  114. this.loadData('refresh');
  115. },
  116. //监听页面是否滚动到底部加载更多
  117. onReachBottom() {
  118. this.loadData();
  119. },
  120. methods: {
  121. //加载分类
  122. async loadCateList(fid, sid) {
  123. let obj = this;
  124. getCategoryList({}).then(function(e) {
  125. obj.cateList = e.data.filter(e => {
  126. return e.id != 1;
  127. });
  128. obj.currentId = obj.cateList[0].id
  129. console.log(obj.cateList, '123456789');
  130. });
  131. },
  132. //一级分类点击
  133. tabtap(item) {
  134. console.log(item);
  135. // 判断有没有初始化页面高度对象数据
  136. if (!this.sizeCalcState) {
  137. this.calcSize();
  138. }
  139. // 获取当前点击的id
  140. this.currentId = item.id;
  141. console.log(item.top);
  142. this.tabScrollTop = item.top;
  143. console.log(this.tabScrollTop);
  144. },
  145. //右侧栏滚动
  146. asideScroll(e) {
  147. // 判断有没有初始化页面高度对象数据
  148. if (!this.sizeCalcState) {
  149. this.calcSize();
  150. }
  151. let scrollTop = e.detail.scrollTop;
  152. let box = 0; //列表包裹框高度初始化
  153. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  154. // 查询当前页面对象
  155. let view = uni.createSelectorQuery().select('.content');
  156. view.fields(
  157. {
  158. id: true,
  159. dataset: true,
  160. rect: true,
  161. size: true,
  162. scrollOffset: true
  163. },
  164. function(e) {
  165. // 保存包裹框高度
  166. box = e.height;
  167. }
  168. ).exec();
  169. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  170. let tabs = this.cateList.filter(item =>( item.top-10) <= scrollTop).reverse();
  171. if (tabs.length > 0) {
  172. // 判断是否已经到达滚轮底部
  173. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  174. this.currentId = this.cateList[this.cateList.length - 1].id;
  175. } else {
  176. this.currentId = tabs[0].id;
  177. }
  178. }
  179. },
  180. //计算右侧栏每个tab的高度等信息
  181. calcSize() {
  182. let h = 0;
  183. this.cateList.forEach(item => {
  184. let view = uni.createSelectorQuery().select('#main-' + item.id);
  185. view.fields(
  186. {
  187. size: true
  188. },
  189. data => {
  190. item.top = h;
  191. h += data.height;
  192. item.bottom = h;
  193. }
  194. ).exec();
  195. });
  196. this.sizeCalcState = true;
  197. },
  198. //加载商品 ,带下拉刷新和上滑加载
  199. async loadData(type = 'add', loading) {
  200. let obj = this;
  201. let data = {
  202. page: obj.page,
  203. limit: obj.limit,
  204. sid: obj.cateId //分类id
  205. };
  206. //没有更多直接返回
  207. if (type === 'add') {
  208. if (obj.loadingType === 'nomore') {
  209. return;
  210. }
  211. obj.loadingType = 'loading';
  212. } else {
  213. obj.loadingType = 'more';
  214. }
  215. if (type === 'refresh') {
  216. // 清空数组
  217. obj.goodsList = [];
  218. obj.page = 1;
  219. }
  220. if (this.filterIndex == 1) {
  221. console.log(obj.salesOrder);
  222. data.salesOrder = obj.numberOrder == 1 ? 'asc' : 'desc';
  223. }
  224. if (this.filterIndex == 2) {
  225. console.log(obj.priceOrder);
  226. data.priceOrder = obj.priceOrder == 1 ? 'asc' : 'desc';
  227. }
  228. getProducts(data).then(function(e) {
  229. console.log(e.data);
  230. let arr = e.data.filter(info => {
  231. return info.cate_id != 2;
  232. });
  233. obj.goodsList = obj.goodsList.concat(arr);
  234. //判断是否还有下一页,有是more 没有是nomore
  235. if (obj.limit == e.data.length) {
  236. obj.page++;
  237. obj.loadingType = 'more';
  238. } else {
  239. obj.loadingType = 'nomore';
  240. }
  241. if (type === 'refresh') {
  242. if (loading == 1) {
  243. uni.hideLoading();
  244. } else {
  245. uni.stopPullDownRefresh();
  246. }
  247. }
  248. });
  249. },
  250. //筛选点击
  251. tabClick(index) {
  252. // 防止重复点击综合排序
  253. if (this.filterIndex === 0 && this.filterIndex === index) {
  254. return;
  255. }
  256. this.filterIndex = index;
  257. // 判断是否为销量优先
  258. if (index === 1) {
  259. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  260. }
  261. // 判断是否为价格优先
  262. if (index === 2) {
  263. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  264. }
  265. // 初始化页数
  266. this.page = 1;
  267. // 初始化数组
  268. uni.pageScrollTo({
  269. duration: 300,
  270. scrollTop: 0
  271. });
  272. this.loadData('refresh', 1);
  273. uni.showLoading({
  274. title: '正在加载'
  275. });
  276. },
  277. //显示分类面板
  278. toggleCateMask(type) {
  279. let timer = type === 'show' ? 10 : 300;
  280. let state = type === 'show' ? 1 : 0;
  281. this.cateMaskState = 2;
  282. setTimeout(() => {
  283. this.cateMaskState = state;
  284. }, timer);
  285. },
  286. //分类点击
  287. changeCate(item) {
  288. this.cateId = item.id;
  289. // 显示右侧分类
  290. this.toggleCateMask();
  291. // 滚轮返回顶部
  292. uni.pageScrollTo({
  293. duration: 300,
  294. scrollTop: 0
  295. });
  296. // 初始化查询页数
  297. this.page = 1;
  298. // 重新加载数据
  299. this.loadData('refresh', 1);
  300. uni.showLoading({
  301. title: '正在加载'
  302. });
  303. },
  304. //详情
  305. navToDetailPage(item) {
  306. let id = item.id;
  307. uni.navigateTo({
  308. url: `/pages/product/product?id=${id}`
  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. .fanli {
  549. position: absolute;
  550. bottom: 0;
  551. left: 0;
  552. width: 344rpx;
  553. height: 96rpx;
  554. .fanli-bg {
  555. position: absolute;
  556. bottom: 0;
  557. left: 0;
  558. width: 344rpx;
  559. height: 96rpx;
  560. }
  561. .fanli-font {
  562. position: relative;
  563. z-index: 10;
  564. color: #ffffff;
  565. height: 96rpx;
  566. align-items: flex-end;
  567. padding: 36rpx 10rpx 10rpx;
  568. .font-left {
  569. width: 226rpx;
  570. font-size: 20rpx;
  571. font-family: PingFang SC;
  572. font-weight: 500;
  573. color: #ffffff;
  574. }
  575. .font-right {
  576. width: 118rpx;
  577. text-align: center;
  578. font-size: 36rpx;
  579. font-family: Microsoft YaHei;
  580. font-weight: bold;
  581. color: #ffffff;
  582. }
  583. }
  584. }
  585. }
  586. .title {
  587. font-size: $font-lg;
  588. color: $font-color-dark;
  589. line-height: 80rpx;
  590. }
  591. .price-box {
  592. display: flex;
  593. align-items: center;
  594. justify-content: space-between;
  595. padding-right: 10rpx;
  596. font-size: 24rpx;
  597. color: $font-color-light;
  598. }
  599. .price {
  600. font-size: $font-lg;
  601. color: #ff4c4c;
  602. line-height: 1;
  603. &:before {
  604. content: '¥';
  605. font-size: 26rpx;
  606. }
  607. }
  608. }
  609. </style>