list.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <template>
  2. <s-layout navbar="normal" :leftWidth="0" :rightWidth="0" tools="search" :defaultSearch="state.keyword"
  3. @search="onSearch">
  4. <!-- 筛选 -->
  5. <su-sticky bgColor="#fff">
  6. <view class="ss-flex">
  7. <view class="ss-flex-1">
  8. <su-tabs :list="state.tabList" :scrollable="false" @change="onTabsChange"
  9. :current="state.currentTab"></su-tabs>
  10. </view>
  11. <view class="list-icon" @tap="state.iconStatus = !state.iconStatus">
  12. <text v-if="state.iconStatus" class="sicon-goods-list"></text>
  13. <text v-else class="sicon-goods-card"></text>
  14. </view>
  15. </view>
  16. </su-sticky>
  17. <!-- 弹窗 -->
  18. <su-popup :show="state.showFilter" type="top" round="10" :space="sys_navBar + 38" backgroundColor="#F6F6F6"
  19. :zIndex="10" @close="state.showFilter = false">
  20. <view class="filter-list-box">
  21. <view class="filter-item" v-for="(item, index) in state.tabList[state.currentTab].list"
  22. :key="item.value" :class="[{ 'filter-item-active': index == state.curFilter }]"
  23. @tap="onFilterItem(index)">
  24. {{ item.label }}
  25. </view>
  26. </view>
  27. </su-popup>
  28. <view v-if="state.iconStatus && state.pagination.total > 0" class="goods-list ss-m-t-20">
  29. <view class="ss-p-l-20 ss-p-r-20 ss-m-b-20" v-for="item in state.pagination.data" :key="item.id">
  30. <s-goods-column class="" size="lg" :data="item" :topRadius="10" :bottomRadius="10"
  31. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"></s-goods-column>
  32. </view>
  33. </view>
  34. <view v-if="!state.iconStatus && state.pagination.total > 0"
  35. class="ss-flex ss-flex-wrap ss-p-x-20 ss-m-t-20 ss-col-top">
  36. <view class="goods-list-box">
  37. <view class="left-list" v-for="item in state.leftGoodsList" :key="item.id">
  38. <s-goods-column class="goods-md-box" size="md" :data="item" :topRadius="10" :bottomRadius="10"
  39. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  40. @getHeight="mountMasonry($event, 'left')">
  41. <template v-slot:cart>
  42. <button class="ss-reset-button cart-btn"> </button>
  43. </template>
  44. </s-goods-column>
  45. </view>
  46. </view>
  47. <view class="goods-list-box">
  48. <view class="right-list" v-for="item in state.rightGoodsList" :key="item.id">
  49. <s-goods-column class="goods-md-box" size="md" :topRadius="10" :bottomRadius="10" :data="item"
  50. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  51. @getHeight="mountMasonry($event, 'right')">
  52. <template v-slot:cart>
  53. <button class="ss-reset-button cart-btn"> </button>
  54. </template>
  55. </s-goods-column>
  56. </view>
  57. </view>
  58. </view>
  59. <uni-load-more v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{
  60. contentdown: '上拉加载更多',
  61. }" @tap="loadmore" />
  62. <s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无商品">
  63. </s-empty>
  64. </s-layout>
  65. </template>
  66. <script setup>
  67. import {
  68. reactive
  69. } from 'vue';
  70. import {
  71. onLoad,
  72. onReachBottom
  73. } from '@dcloudio/uni-app';
  74. import sheep from '@/sheep';
  75. import _ from 'lodash';
  76. const sys_navBar = sheep.$platform.navbar;
  77. const emits = defineEmits(['close', 'change']);
  78. const pagination = {
  79. data: [],
  80. current_page: 1,
  81. total: 1,
  82. last_page: 1,
  83. };
  84. const state = reactive({
  85. pagination: {
  86. data: [],
  87. current_page: 1,
  88. total: 1,
  89. last_page: 1,
  90. },
  91. // currentSort: 'weigh',
  92. // currentOrder: 'desc',
  93. currentTab: 0,
  94. filterParams: {},
  95. curFilter: 0,
  96. showFilter: false,
  97. iconStatus: false,
  98. categoryId: 0,
  99. tabList: [{
  100. name: '综合推荐',
  101. // value: '',
  102. list: [{
  103. label: '综合推荐',
  104. // sort: '',
  105. // order: true,
  106. },
  107. {
  108. label: '价格升序',
  109. sort: 'price',
  110. order: true,
  111. },
  112. {
  113. label: '价格降序',
  114. sort: 'price',
  115. order: false,
  116. },
  117. ],
  118. },
  119. {
  120. name: '销量',
  121. sort: 'salesCount',
  122. order: false,
  123. // value: 'salesCount',
  124. },
  125. {
  126. name: '新品优先',
  127. // value: 'create_time',
  128. },
  129. ],
  130. loadStatus: '',
  131. keyword: '',
  132. leftGoodsList: [],
  133. rightGoodsList: [],
  134. });
  135. // 加载瀑布流
  136. let count = 0;
  137. let leftHeight = 0;
  138. let rightHeight = 0;
  139. function mountMasonry(height = 0, where = 'left') {
  140. if (!state.pagination.data[count]) return;
  141. if (where === 'left') {
  142. leftHeight += height;
  143. } else {
  144. rightHeight += height;
  145. }
  146. if (leftHeight <= rightHeight) {
  147. state.leftGoodsList.push(state.pagination.data[count]);
  148. } else {
  149. state.rightGoodsList.push(state.pagination.data[count]);
  150. }
  151. count++;
  152. }
  153. function emptyList() {
  154. state.pagination = pagination
  155. state.leftGoodsList = [];
  156. state.rightGoodsList = [];
  157. count = 0;
  158. leftHeight = 0;
  159. rightHeight = 0;
  160. }
  161. //搜索
  162. function onSearch(e) {
  163. state.keyword = e;
  164. emptyList();
  165. getList(state.currentSort, state.currentOrder, state.categoryId, e);
  166. }
  167. // 点击
  168. function onTabsChange(e) {
  169. if (state.tabList[e.index].list) {
  170. state.currentTab = e.index;
  171. state.showFilter = !state.showFilter;
  172. return;
  173. } else {
  174. state.showFilter = false;
  175. }
  176. if (e.index === state.currentTab) {
  177. return;
  178. } else {
  179. state.currentTab = e.index;
  180. }
  181. emptyList();
  182. console.log(e, '6666')
  183. getList(e.sort, e.order, state.categoryId, state.keyword);
  184. }
  185. // 点击筛选项
  186. const onFilterItem = (val) => {
  187. console.log(val)
  188. if (
  189. state.currentSort === state.tabList[0].list[val].sort &&
  190. state.currentOrder === state.tabList[0].list[val].order
  191. ) {
  192. state.showFilter = false;
  193. return;
  194. }
  195. state.curFilter = val;
  196. state.tabList[0].name = state.tabList[0].list[val].label;
  197. state.currentSort = state.tabList[0].list[val].sort;
  198. state.currentOrder = state.tabList[0].list[val].order;
  199. emptyList();
  200. getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword);
  201. state.showFilter = false;
  202. };
  203. async function getList(Sort, Order, categoryId, keyword, page = 1, list_rows = 6) {
  204. state.loadStatus = 'loading';
  205. const res = await sheep.$api.goods.list({
  206. sortField: Sort,
  207. sortAsc: Order,
  208. category_id: !keyword ? categoryId : '',
  209. pageSize: list_rows,
  210. keyword: keyword,
  211. pageNo: page,
  212. });
  213. if (res.code === 0) {
  214. let couponList = _.concat(state.pagination.data, res.data.list);
  215. state.pagination = {
  216. ...res.data,
  217. data: couponList,
  218. };
  219. mountMasonry();
  220. if (state.pagination.current_page < state.pagination.last_page) {
  221. state.loadStatus = 'more';
  222. } else {
  223. state.loadStatus = 'noMore';
  224. }
  225. }
  226. }
  227. // 加载更多
  228. function loadmore() {
  229. if (state.loadStatus !== 'noMore') {
  230. getList(
  231. state.currentSort,
  232. state.currentOrder,
  233. state.categoryId,
  234. state.keyword,
  235. state.pagination.current_page + 1,
  236. );
  237. }
  238. }
  239. onLoad((options) => {
  240. state.categoryId = options.categoryId;
  241. state.keyword = options.keyword;
  242. getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword);
  243. });
  244. // 上拉加载更多
  245. onReachBottom(() => {
  246. loadmore();
  247. });
  248. </script>
  249. <style lang="scss" scoped>
  250. .goods-list-box {
  251. width: 50%;
  252. box-sizing: border-box;
  253. .left-list {
  254. margin-right: 10rpx;
  255. margin-bottom: 20rpx;
  256. }
  257. .right-list {
  258. margin-left: 10rpx;
  259. margin-bottom: 20rpx;
  260. }
  261. }
  262. .goods-box {
  263. &:nth-last-of-type(1) {
  264. margin-bottom: 0 !important;
  265. }
  266. &:nth-child(2n) {
  267. margin-right: 0;
  268. }
  269. }
  270. .list-icon {
  271. width: 80rpx;
  272. .sicon-goods-card {
  273. font-size: 40rpx;
  274. }
  275. .sicon-goods-list {
  276. font-size: 40rpx;
  277. }
  278. }
  279. .goods-card {
  280. margin-left: 20rpx;
  281. }
  282. .list-filter-tabs {
  283. background-color: #fff;
  284. }
  285. .filter-list-box {
  286. padding: 28rpx 52rpx;
  287. .filter-item {
  288. font-size: 28rpx;
  289. font-weight: 500;
  290. color: #333333;
  291. line-height: normal;
  292. margin-bottom: 24rpx;
  293. &:nth-last-child(1) {
  294. margin-bottom: 0;
  295. }
  296. }
  297. .filter-item-active {
  298. color: var(--ui-BG-Main);
  299. }
  300. }
  301. .tab-item {
  302. height: 50px;
  303. position: relative;
  304. z-index: 11;
  305. .tab-title {
  306. font-size: 30rpx;
  307. }
  308. .cur-tab-title {
  309. font-weight: $font-weight-bold;
  310. }
  311. .tab-line {
  312. width: 60rpx;
  313. height: 6rpx;
  314. border-radius: 6rpx;
  315. position: absolute;
  316. left: 50%;
  317. transform: translateX(-50%);
  318. bottom: 10rpx;
  319. background-color: var(--ui-BG-Main);
  320. z-index: 12;
  321. }
  322. }
  323. </style>