list.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 }"
  10. class="iconfont iconfold xia"></text>
  11. </view>
  12. </view>
  13. <view class="nav-item" :class="{ current: filterIndex === 2 }" @click="tabClick(2)">
  14. <text>价格</text>
  15. <view class="p-box">
  16. <text :class="{ active: priceOrder === 1 && filterIndex === 2 }" class="iconfont iconfold"></text>
  17. <text :class="{ active: priceOrder === 2 && filterIndex === 2 }"
  18. class="iconfont iconfold xia"></text>
  19. </view>
  20. </view>
  21. <text class="cate-item iconfont iconapps" @click="toggleCateMask('show')"></text>
  22. </view>
  23. <view class="goods-list">
  24. <view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navToDetailPage(item)">
  25. <view class="image-wrapper">
  26. <image :src="item.image" mode="aspectFill"></image>
  27. </view>
  28. <text class="title clamp">{{ item.title }}</text>
  29. <view class="price-box">
  30. <text class="price">{{ item.price }}</text>
  31. <text>已售 {{ item.sales }}</text>
  32. </view>
  33. </view>
  34. </view>
  35. <uni-load-more :status="loadingType"></uni-load-more>
  36. <view class="cate-mask" :class="cateMaskState === 0 ? 'none' : cateMaskState === 1 ? 'show' : ''"
  37. @click="toggleCateMask">
  38. <view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
  39. <scroll-view scroll-y class="cate-list">
  40. <view v-for="item in cateList" :key="item.id">
  41. <view class="cate-item b-b two">{{ item.cate_name }}</view>
  42. <view v-for="tItem in item.children" :key="tItem.id" class="cate-item b-b"
  43. :class="{ active: tItem.id == cateId }" @click="changeCate(tItem)">
  44. {{ tItem.cate_name }}
  45. </view>
  46. </view>
  47. </scroll-view>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  54. import {
  55. getProducts
  56. } from '@/api/product.js';
  57. import {
  58. getCategoryList
  59. } from '@/api/product.js';
  60. export default {
  61. components: {
  62. uniLoadMore
  63. },
  64. data() {
  65. return {
  66. cateMaskState: 0, //分类面板展开状态
  67. headerPosition: 'fixed',
  68. headerTop: '0px',
  69. loadingType: 'more', //加载更多状态
  70. filterIndex: 0, //查询类型
  71. numberOrder: 0, //1 销量从低到高 2销量从高到低
  72. limit: 6, //每次加载数据条数
  73. page: 0, //当前页数
  74. cateId: 0, //已选三级分类id
  75. priceOrder: 0, //1 价格从低到高 2价格从高到低
  76. cateList: [], //分类列表
  77. goodsList: [] //商品列表
  78. };
  79. },
  80. onLoad(options) {
  81. // #ifdef H5
  82. this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
  83. // #endif
  84. this.cateId = options.tid;
  85. this.loadCateList(options.fid, options.sid);
  86. this.loadData();
  87. },
  88. onPageScroll(e) {
  89. //兼容iOS端下拉时顶部漂移
  90. if (e.scrollTop >= 0) {
  91. this.headerPosition = 'fixed';
  92. } else {
  93. this.headerPosition = 'absolute';
  94. }
  95. },
  96. //下拉刷新
  97. onPullDownRefresh() {
  98. this.loadData('refresh');
  99. },
  100. //监听页面是否滚动到底部加载更多
  101. onReachBottom() {
  102. this.loadData();
  103. },
  104. methods: {
  105. //加载分类
  106. async loadCateList(fid, sid) {
  107. let obj = this;
  108. getCategoryList({}).then(function(e) {
  109. console.log(e);
  110. e.data.forEach(function(e) {
  111. if (e.id == fid) {
  112. obj.cateList = e.children;
  113. return;
  114. }
  115. });
  116. console.log(obj.cateList);
  117. });
  118. },
  119. //加载商品 ,带下拉刷新和上滑加载
  120. async loadData(type = 'add', loading) {
  121. let obj = this;
  122. let data = {
  123. page: obj.page,
  124. limit: obj.limit,
  125. sid: obj.cateId //分类id
  126. };
  127. //没有更多直接返回
  128. if (type === 'add') {
  129. if (obj.loadingType === 'nomore') {
  130. return;
  131. }
  132. obj.loadingType = 'loading';
  133. } else {
  134. obj.loadingType = 'more';
  135. }
  136. if (type === 'refresh') {
  137. // 清空数组
  138. obj.goodsList = [];
  139. obj.page = 1
  140. }
  141. if (this.filterIndex == 1) {
  142. console.log(obj.salesOrder);
  143. data.salesOrder = obj.numberOrder == 1 ? 'asc' : 'desc';
  144. }
  145. if (this.filterIndex == 2) {
  146. console.log(obj.priceOrder);
  147. data.priceOrder = obj.priceOrder == 1 ? 'asc' : 'desc';
  148. }
  149. getProducts(data).then(function(e) {
  150. console.log(e.data);
  151. obj.goodsList = obj.goodsList.concat(e.data);
  152. //判断是否还有下一页,有是more 没有是nomore
  153. if (obj.limit == e.data.length) {
  154. obj.page++
  155. obj.loadingType = 'more'
  156. } else {
  157. obj.loadingType = 'nomore'
  158. }
  159. if (type === 'refresh') {
  160. if (loading == 1) {
  161. uni.hideLoading();
  162. } else {
  163. uni.stopPullDownRefresh();
  164. }
  165. }
  166. });
  167. },
  168. //筛选点击
  169. tabClick(index) {
  170. // 防止重复点击综合排序
  171. if (this.filterIndex === 0 && this.filterIndex === index) {
  172. return;
  173. }
  174. this.filterIndex = index;
  175. // 判断是否为销量优先
  176. if (index === 1) {
  177. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  178. }
  179. // 判断是否为价格优先
  180. if (index === 2) {
  181. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  182. }
  183. // 初始化页数
  184. this.page = 1;
  185. // 初始化数组
  186. uni.pageScrollTo({
  187. duration: 300,
  188. scrollTop: 0
  189. });
  190. this.loadData('refresh', 1);
  191. uni.showLoading({
  192. title: '正在加载'
  193. });
  194. },
  195. //显示分类面板
  196. toggleCateMask(type) {
  197. let timer = type === 'show' ? 10 : 300;
  198. let state = type === 'show' ? 1 : 0;
  199. this.cateMaskState = 2;
  200. setTimeout(() => {
  201. this.cateMaskState = state;
  202. }, timer);
  203. },
  204. //分类点击
  205. changeCate(item) {
  206. this.cateId = item.id;
  207. // 显示右侧分类
  208. this.toggleCateMask();
  209. // 滚轮返回顶部
  210. uni.pageScrollTo({
  211. duration: 300,
  212. scrollTop: 0
  213. });
  214. // 初始化查询页数
  215. this.page = 1
  216. // 重新加载数据
  217. this.loadData('refresh', 1);
  218. uni.showLoading({
  219. title: '正在加载'
  220. });
  221. },
  222. //详情
  223. navToDetailPage(item) {
  224. let id = item.id;
  225. uni.navigateTo({
  226. url: `/pages/product/product?id=${id}`
  227. });
  228. },
  229. stopPrevent() {}
  230. }
  231. };
  232. </script>
  233. <style lang="scss">
  234. page,
  235. .content {
  236. background: $page-color-base;
  237. }
  238. .content {
  239. padding-top: 96rpx;
  240. }
  241. .navbar {
  242. position: fixed;
  243. left: 0;
  244. top: var(--window-top);
  245. display: flex;
  246. width: 100%;
  247. height: 80rpx;
  248. background: #fff;
  249. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  250. z-index: 10;
  251. .nav-item {
  252. flex: 1;
  253. display: flex;
  254. justify-content: center;
  255. align-items: center;
  256. height: 100%;
  257. font-size: 30rpx;
  258. color: $font-color-dark;
  259. position: relative;
  260. &.current {
  261. color: $base-color;
  262. &:after {
  263. content: '';
  264. position: absolute;
  265. left: 50%;
  266. bottom: 0;
  267. transform: translateX(-50%);
  268. width: 120rpx;
  269. height: 0;
  270. border-bottom: 4rpx solid $base-color;
  271. }
  272. }
  273. }
  274. .p-box {
  275. display: flex;
  276. flex-direction: column;
  277. .iconfont {
  278. display: flex;
  279. align-items: center;
  280. justify-content: center;
  281. width: 30rpx;
  282. height: 14rpx;
  283. line-height: 1;
  284. margin-left: 4rpx;
  285. font-size: 26rpx;
  286. color: #888;
  287. &.active {
  288. color: $base-color;
  289. }
  290. }
  291. .xia {
  292. transform: scaleY(-1);
  293. }
  294. }
  295. .cate-item {
  296. display: flex;
  297. justify-content: center;
  298. align-items: center;
  299. height: 100%;
  300. width: 80rpx;
  301. position: relative;
  302. font-size: 44rpx;
  303. &:after {
  304. content: '';
  305. position: absolute;
  306. left: 0;
  307. top: 50%;
  308. transform: translateY(-50%);
  309. border-left: 1px solid #ddd;
  310. width: 0;
  311. height: 36rpx;
  312. }
  313. }
  314. }
  315. /* 分类 */
  316. .cate-mask {
  317. position: fixed;
  318. left: 0;
  319. top: var(--window-top);
  320. bottom: 0;
  321. width: 100%;
  322. background: rgba(0, 0, 0, 0);
  323. z-index: 95;
  324. transition: 0.3s;
  325. .cate-content {
  326. width: 630rpx;
  327. height: 100%;
  328. background: #fff;
  329. float: right;
  330. transform: translateX(100%);
  331. transition: 0.3s;
  332. }
  333. &.none {
  334. display: none;
  335. }
  336. &.show {
  337. background: rgba(0, 0, 0, 0.4);
  338. .cate-content {
  339. transform: translateX(0);
  340. }
  341. }
  342. }
  343. .cate-list {
  344. display: flex;
  345. flex-direction: column;
  346. height: 100%;
  347. .cate-item {
  348. display: flex;
  349. align-items: center;
  350. height: 90rpx;
  351. padding-left: 30rpx;
  352. font-size: 28rpx;
  353. color: #555;
  354. position: relative;
  355. }
  356. .two {
  357. height: 64rpx;
  358. color: #303133;
  359. font-size: 30rpx;
  360. background: #f8f8f8;
  361. }
  362. .active {
  363. color: $base-color;
  364. }
  365. }
  366. /* 商品列表 */
  367. .goods-list {
  368. display: flex;
  369. flex-wrap: wrap;
  370. padding: 0 30rpx;
  371. background: #fff;
  372. .goods-item {
  373. display: flex;
  374. flex-direction: column;
  375. width: 48%;
  376. padding-bottom: 40rpx;
  377. &:nth-child(2n + 1) {
  378. margin-right: 4%;
  379. }
  380. }
  381. .image-wrapper {
  382. width: 100%;
  383. height: 330rpx;
  384. border-radius: 3px;
  385. overflow: hidden;
  386. image {
  387. width: 100%;
  388. height: 100%;
  389. opacity: 1;
  390. }
  391. }
  392. .title {
  393. font-size: $font-lg;
  394. color: $font-color-dark;
  395. line-height: 80rpx;
  396. }
  397. .price-box {
  398. display: flex;
  399. align-items: center;
  400. justify-content: space-between;
  401. padding-right: 10rpx;
  402. font-size: 24rpx;
  403. color: $font-color-light;
  404. }
  405. .price {
  406. font-size: $font-lg;
  407. color: $uni-color-primary;
  408. line-height: 1;
  409. &:before {
  410. content: '¥';
  411. font-size: 26rpx;
  412. }
  413. }
  414. }
  415. </style>