list.vue 9.2 KB

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