list.vue 9.2 KB

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