list.vue 8.7 KB

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