list.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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: 1, //当前页数
  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. store_region: 1,
  126. sid: obj.cateId //分类id
  127. };
  128. //没有更多直接返回
  129. if (type === 'add') {
  130. if (obj.loadingType === 'nomore') {
  131. return;
  132. }
  133. obj.loadingType = 'loading';
  134. } else {
  135. obj.loadingType = 'more';
  136. }
  137. if (type === 'refresh') {
  138. // 清空数组
  139. obj.goodsList = [];
  140. obj.page = 1
  141. }
  142. if (this.filterIndex == 1) {
  143. console.log(obj.salesOrder);
  144. data.salesOrder = obj.numberOrder == 1 ? 'asc' : 'desc';
  145. }
  146. if (this.filterIndex == 2) {
  147. console.log(obj.priceOrder);
  148. data.priceOrder = obj.priceOrder == 1 ? 'asc' : 'desc';
  149. }
  150. getProducts(data).then(function(e) {
  151. console.log(e.data);
  152. obj.goodsList = obj.goodsList.concat(e.data);
  153. //判断是否还有下一页,有是more 没有是nomore
  154. if (obj.limit == e.data.length) {
  155. obj.page++
  156. obj.loadingType = 'more'
  157. } else {
  158. obj.loadingType = 'nomore'
  159. }
  160. if (type === 'refresh') {
  161. if (loading == 1) {
  162. uni.hideLoading();
  163. } else {
  164. uni.stopPullDownRefresh();
  165. }
  166. }
  167. });
  168. },
  169. //筛选点击
  170. tabClick(index) {
  171. // 防止重复点击综合排序
  172. if (this.filterIndex === 0 && this.filterIndex === index) {
  173. return;
  174. }
  175. this.filterIndex = index;
  176. // 判断是否为销量优先
  177. if (index === 1) {
  178. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  179. }
  180. // 判断是否为价格优先
  181. if (index === 2) {
  182. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  183. }
  184. // 初始化页数
  185. this.page = 1;
  186. // 初始化数组
  187. uni.pageScrollTo({
  188. duration: 300,
  189. scrollTop: 0
  190. });
  191. this.loadData('refresh', 1);
  192. uni.showLoading({
  193. title: '正在加载'
  194. });
  195. },
  196. //显示分类面板
  197. toggleCateMask(type) {
  198. let timer = type === 'show' ? 10 : 300;
  199. let state = type === 'show' ? 1 : 0;
  200. this.cateMaskState = 2;
  201. setTimeout(() => {
  202. this.cateMaskState = state;
  203. }, timer);
  204. },
  205. //分类点击
  206. changeCate(item) {
  207. this.cateId = item.id;
  208. // 显示右侧分类
  209. this.toggleCateMask();
  210. // 滚轮返回顶部
  211. uni.pageScrollTo({
  212. duration: 300,
  213. scrollTop: 0
  214. });
  215. // 初始化查询页数
  216. this.page = 1
  217. // 重新加载数据
  218. this.loadData('refresh', 1);
  219. uni.showLoading({
  220. title: '正在加载'
  221. });
  222. },
  223. //详情
  224. navToDetailPage(item) {
  225. let id = item.id;
  226. uni.navigateTo({
  227. url: `/pages/product/product?id=${id}`
  228. });
  229. },
  230. stopPrevent() {}
  231. }
  232. };
  233. </script>
  234. <style lang="scss">
  235. page,
  236. .content {
  237. background: $page-color-base;
  238. }
  239. .content {
  240. padding-top: 96rpx;
  241. }
  242. .navbar {
  243. position: fixed;
  244. left: 0;
  245. top: var(--window-top);
  246. display: flex;
  247. width: 100%;
  248. height: 80rpx;
  249. background: #fff;
  250. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  251. z-index: 10;
  252. .nav-item {
  253. flex: 1;
  254. display: flex;
  255. justify-content: center;
  256. align-items: center;
  257. height: 100%;
  258. font-size: 30rpx;
  259. color: $font-color-dark;
  260. position: relative;
  261. &.current {
  262. color: $base-color;
  263. &:after {
  264. content: '';
  265. position: absolute;
  266. left: 50%;
  267. bottom: 0;
  268. transform: translateX(-50%);
  269. width: 120rpx;
  270. height: 0;
  271. border-bottom: 4rpx solid $base-color;
  272. }
  273. }
  274. }
  275. .p-box {
  276. display: flex;
  277. flex-direction: column;
  278. .iconfont {
  279. display: flex;
  280. align-items: center;
  281. justify-content: center;
  282. width: 30rpx;
  283. height: 14rpx;
  284. line-height: 1;
  285. margin-left: 4rpx;
  286. font-size: 26rpx;
  287. color: #888;
  288. &.active {
  289. color: $base-color;
  290. }
  291. }
  292. .xia {
  293. transform: scaleY(-1);
  294. }
  295. }
  296. .cate-item {
  297. display: flex;
  298. justify-content: center;
  299. align-items: center;
  300. height: 100%;
  301. width: 80rpx;
  302. position: relative;
  303. font-size: 44rpx;
  304. &:after {
  305. content: '';
  306. position: absolute;
  307. left: 0;
  308. top: 50%;
  309. transform: translateY(-50%);
  310. border-left: 1px solid #ddd;
  311. width: 0;
  312. height: 36rpx;
  313. }
  314. }
  315. }
  316. /* 分类 */
  317. .cate-mask {
  318. position: fixed;
  319. left: 0;
  320. top: var(--window-top);
  321. bottom: 0;
  322. width: 100%;
  323. background: rgba(0, 0, 0, 0);
  324. z-index: 95;
  325. transition: 0.3s;
  326. .cate-content {
  327. width: 630rpx;
  328. height: 100%;
  329. background: #fff;
  330. float: right;
  331. transform: translateX(100%);
  332. transition: 0.3s;
  333. }
  334. &.none {
  335. display: none;
  336. }
  337. &.show {
  338. background: rgba(0, 0, 0, 0.4);
  339. .cate-content {
  340. transform: translateX(0);
  341. }
  342. }
  343. }
  344. .cate-list {
  345. display: flex;
  346. flex-direction: column;
  347. height: 100%;
  348. .cate-item {
  349. display: flex;
  350. align-items: center;
  351. height: 90rpx;
  352. padding-left: 30rpx;
  353. font-size: 28rpx;
  354. color: #555;
  355. position: relative;
  356. }
  357. .two {
  358. height: 64rpx;
  359. color: #303133;
  360. font-size: 30rpx;
  361. background: #f8f8f8;
  362. }
  363. .active {
  364. color: $base-color;
  365. }
  366. }
  367. /* 商品列表 */
  368. .goods-list {
  369. display: flex;
  370. flex-wrap: wrap;
  371. padding: 0 30rpx;
  372. background: #fff;
  373. .goods-item {
  374. display: flex;
  375. flex-direction: column;
  376. width: 48%;
  377. padding-bottom: 40rpx;
  378. &:nth-child(2n + 1) {
  379. margin-right: 4%;
  380. }
  381. }
  382. .image-wrapper {
  383. width: 100%;
  384. height: 330rpx;
  385. border-radius: 3px;
  386. overflow: hidden;
  387. image {
  388. width: 100%;
  389. height: 100%;
  390. opacity: 1;
  391. }
  392. }
  393. .title {
  394. font-size: $font-lg;
  395. color: $font-color-dark;
  396. line-height: 80rpx;
  397. }
  398. .price-box {
  399. margin-top: 10rpx;
  400. display: flex;
  401. align-items: center;
  402. justify-content: space-between;
  403. padding-right: 10rpx;
  404. font-size: 24rpx;
  405. color: $font-color-light;
  406. }
  407. .price {
  408. font-size: $font-lg;
  409. color: $base-color;
  410. line-height: 1;
  411. &:before {
  412. content: '¥';
  413. font-size: 26rpx;
  414. }
  415. }
  416. }
  417. </style>