list.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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: 10, //每次加载数据条数
  61. page: 1, //当前页数
  62. cateId: 0, //已选三级分类id
  63. priceOrder: 0, //1 价格从低到高 2价格从高到低
  64. cateList: [], //分类列表
  65. goodsList: [], //商品列表
  66. keyword: '', //搜索关键词
  67. mer_id: '', //商家id
  68. iszy: '' //是否是自营
  69. };
  70. },
  71. //下拉刷新
  72. onPullDownRefresh() {
  73. console.log('下拉刷新');
  74. this.loadData('refresh');
  75. },
  76. //加载更多
  77. onReachBottom() {
  78. this.loadData();
  79. },
  80. methods: {
  81. //加载分类
  82. async loadCateList(fid, sid) {
  83. let obj = this;
  84. getList({}).then(function(e) {
  85. console.log(e);
  86. e.data.forEach(function(e) {
  87. if (e.id == fid) {
  88. obj.cateList = e.children;
  89. return;
  90. }
  91. });
  92. console.log(obj.cateList);
  93. });
  94. },
  95. //加载商品 ,带下拉刷新和上滑加载
  96. async loadData(type = 'add', loading) {
  97. let obj = this;
  98. let data = {};
  99. console.log(obj.iszy, '123456');
  100. if (obj.iszy == 0) {
  101. data = {
  102. page: obj.page,
  103. limit: obj.limit,
  104. sid: obj.cateId, //分类id
  105. keyword: obj.keyword
  106. };
  107. } else {
  108. data = {
  109. page: obj.page,
  110. limit: obj.limit,
  111. sid: obj.cateId, //分类id
  112. keyword: obj.keyword,
  113. store_id: obj.mer_id
  114. };
  115. }
  116. //没有更多直接返回
  117. if (type === 'add') {
  118. if (obj.loadingType === 'nomore') {
  119. return;
  120. }
  121. obj.loadingType = 'loading';
  122. } else {
  123. obj.loadingType = 'more';
  124. }
  125. if (this.filterIndex == 1) {
  126. console.log(obj.salesOrder);
  127. data.salesOrder = obj.numberOrder == 1 ? 'asc' : 'desc';
  128. }
  129. if (this.filterIndex == 2) {
  130. console.log(obj.priceOrder);
  131. data.priceOrder = obj.priceOrder == 1 ? 'asc' : 'desc';
  132. }
  133. getProducts(data).then(function(e) {
  134. if (type === 'refresh') {
  135. console.log('jinru');
  136. // 清空数组
  137. obj.goodsList = [];
  138. }
  139. console.log(e.data);
  140. obj.goodsList = obj.goodsList.concat(e.data);
  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. this.toggleCateMask();
  197. uni.pageScrollTo({
  198. duration: 300,
  199. scrollTop: 0
  200. });
  201. this.loadData('refresh', 1);
  202. uni.showLoading({
  203. title: '正在加载'
  204. });
  205. },
  206. //详情
  207. navToDetailPage(item) {
  208. let id = item.id;
  209. uni.navigateTo({
  210. url: `/pages/product/product?id=${id}`
  211. });
  212. },
  213. stopPrevent() {}
  214. }
  215. };
  216. </script>
  217. <style lang="scss">
  218. page,
  219. .content {
  220. background: $page-color-base;
  221. }
  222. .navbar {
  223. // position: fixed;
  224. // left: 0;
  225. // top: var(--window-top);
  226. display: flex;
  227. width: 100%;
  228. height: 80rpx;
  229. background: #fff;
  230. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  231. z-index: 10;
  232. .nav-item {
  233. flex: 1;
  234. display: flex;
  235. justify-content: center;
  236. align-items: center;
  237. height: 100%;
  238. font-size: 30rpx;
  239. color: $font-color-dark;
  240. position: relative;
  241. &.current {
  242. color: $base-color;
  243. &:after {
  244. content: '';
  245. position: absolute;
  246. left: 50%;
  247. bottom: 0;
  248. transform: translateX(-50%);
  249. width: 120rpx;
  250. height: 0;
  251. border-bottom: 4rpx solid $base-color;
  252. }
  253. }
  254. }
  255. .p-box {
  256. display: flex;
  257. flex-direction: column;
  258. .iconfont {
  259. display: flex;
  260. align-items: center;
  261. justify-content: center;
  262. width: 30rpx;
  263. height: 14rpx;
  264. line-height: 1;
  265. margin-left: 4rpx;
  266. font-size: 26rpx;
  267. color: #888;
  268. &.active {
  269. color: $base-color;
  270. }
  271. }
  272. .xia {
  273. transform: scaleY(-1);
  274. }
  275. }
  276. .cate-item {
  277. display: flex;
  278. justify-content: center;
  279. align-items: center;
  280. height: 100%;
  281. width: 80rpx;
  282. position: relative;
  283. font-size: 44rpx;
  284. &:after {
  285. content: '';
  286. position: absolute;
  287. left: 0;
  288. top: 50%;
  289. transform: translateY(-50%);
  290. border-left: 1px solid #ddd;
  291. width: 0;
  292. height: 36rpx;
  293. }
  294. }
  295. }
  296. /* 分类 */
  297. .cate-mask {
  298. position: fixed;
  299. left: 0;
  300. top: var(--window-top);
  301. bottom: 0;
  302. width: 100%;
  303. background: rgba(0, 0, 0, 0);
  304. z-index: 9999;
  305. transition: 0.3s;
  306. .cate-content {
  307. width: 630rpx;
  308. height: 100%;
  309. background: #fff;
  310. float: right;
  311. transform: translateX(100%);
  312. transition: 0.3s;
  313. }
  314. &.none {
  315. display: none;
  316. }
  317. &.show {
  318. background: rgba(0, 0, 0, 0.4);
  319. .cate-content {
  320. transform: translateX(0);
  321. }
  322. }
  323. }
  324. .cate-list {
  325. display: flex;
  326. flex-direction: column;
  327. height: 100%;
  328. .cate-item {
  329. display: flex;
  330. align-items: center;
  331. height: 90rpx;
  332. padding-left: 30rpx;
  333. font-size: 28rpx;
  334. color: #555;
  335. position: relative;
  336. }
  337. .two {
  338. height: 64rpx;
  339. color: #303133;
  340. font-size: 30rpx;
  341. background: #f8f8f8;
  342. }
  343. .active {
  344. color: $base-color;
  345. }
  346. }
  347. /* 商品列表 */
  348. .goods-list {
  349. display: flex;
  350. flex-wrap: wrap;
  351. padding: 0 30rpx;
  352. padding-top: 20rpx;
  353. background: #fff;
  354. .goods-item {
  355. display: flex;
  356. flex-direction: column;
  357. width: 48%;
  358. padding-bottom: 40rpx;
  359. &:nth-child(2n + 1) {
  360. margin-right: 4%;
  361. }
  362. }
  363. .image-wrapper {
  364. width: 100%;
  365. height: 330rpx;
  366. border-radius: 3px;
  367. overflow: hidden;
  368. image {
  369. width: 100%;
  370. height: 100%;
  371. opacity: 1;
  372. }
  373. }
  374. .title {
  375. font-size: $font-lg;
  376. color: $font-color-dark;
  377. line-height: 80rpx;
  378. }
  379. .price-box {
  380. display: flex;
  381. align-items: center;
  382. justify-content: space-between;
  383. padding-right: 10rpx;
  384. font-size: 24rpx;
  385. color: $font-color-light;
  386. }
  387. .price {
  388. font-size: $font-lg;
  389. color: $uni-color-primary;
  390. line-height: 1;
  391. &:before {
  392. content: '¥';
  393. font-size: 26rpx;
  394. }
  395. }
  396. }
  397. </style>