list.vue 9.1 KB

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