list.vue 9.5 KB

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