list.vue 9.9 KB

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