list.vue 9.2 KB

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