list.vue 9.4 KB

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