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