list.vue 9.6 KB

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