list.vue 9.9 KB

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