list.vue 9.3 KB

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