list.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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, ref } 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. import OrderApi from '@/sheep/api/trade/order';
  125. import { appendSettlementProduct } from '@/sheep/hooks/useGoods';
  126. const sys_navBar = sheep.$platform.navbar;
  127. const emits = defineEmits(['close', 'change']);
  128. const state = reactive({
  129. pagination: {
  130. list: [],
  131. total: 0,
  132. pageNo: 1,
  133. pageSize: 6,
  134. },
  135. currentSort: undefined,
  136. currentOrder: undefined,
  137. currentTab: 0, // 当前选中的 tab
  138. curFilter: 0, // 当前选中的 list 筛选项
  139. showFilter: false,
  140. iconStatus: false, // true - 单列布局;false - 双列布局
  141. keyword: '',
  142. categoryId: 0,
  143. tabList: [
  144. {
  145. name: '综合推荐',
  146. list: [
  147. {
  148. label: '综合推荐',
  149. },
  150. {
  151. label: '价格升序',
  152. sort: 'price',
  153. order: true,
  154. },
  155. {
  156. label: '价格降序',
  157. sort: 'price',
  158. order: false,
  159. },
  160. ],
  161. },
  162. {
  163. name: '销量',
  164. sort: 'salesCount',
  165. order: false,
  166. },
  167. {
  168. name: '新品优先',
  169. value: 'createTime',
  170. order: false,
  171. },
  172. ],
  173. loadStatus: '',
  174. leftGoodsList: [], // 双列布局 - 左侧商品
  175. rightGoodsList: [], // 双列布局 - 右侧商品
  176. });
  177. // 加载瀑布流
  178. let count = 0;
  179. let leftHeight = 0;
  180. let rightHeight = 0;
  181. // 处理双列布局 leftGoodsList + rightGoodsList
  182. function mountMasonry(height = 0, where = 'left') {
  183. if (!state.pagination.list[count]) {
  184. return;
  185. }
  186. if (where === 'left') {
  187. leftHeight += height;
  188. } else {
  189. rightHeight += height;
  190. }
  191. if (leftHeight <= rightHeight) {
  192. state.leftGoodsList.push(state.pagination.list[count]);
  193. } else {
  194. state.rightGoodsList.push(state.pagination.list[count]);
  195. }
  196. count++;
  197. }
  198. // 清空列表
  199. function emptyList() {
  200. resetPagination(state.pagination);
  201. state.leftGoodsList = [];
  202. state.rightGoodsList = [];
  203. count = 0;
  204. leftHeight = 0;
  205. rightHeight = 0;
  206. }
  207. // 搜索
  208. function onSearch(e) {
  209. state.keyword = e;
  210. emptyList();
  211. getList(state.currentSort, state.currentOrder);
  212. }
  213. // 点击
  214. function onTabsChange(e) {
  215. // 如果点击的是【综合推荐】,则直接展开或者收起筛选项
  216. if (state.tabList[e.index].list) {
  217. state.currentTab = e.index;
  218. state.showFilter = !state.showFilter;
  219. return;
  220. }
  221. state.showFilter = false;
  222. // 如果点击的是【销量】或者【新品优先】,则直接切换 tab
  223. if (e.index === state.currentTab) {
  224. return;
  225. }
  226. state.currentTab = e.index;
  227. state.currentSort = e.sort;
  228. state.currentOrder = e.order;
  229. emptyList();
  230. getList(e.sort, e.order);
  231. }
  232. // 点击 tab 的 list 筛选项
  233. const onFilterItem = (val) => {
  234. // 如果点击的是当前的筛选项,则直接收起筛选项,不要加载数据
  235. // 这里选择 tabList[0] 的原因,是目前只有它有 list
  236. if (
  237. state.currentSort === state.tabList[0].list[val].sort &&
  238. state.currentOrder === state.tabList[0].list[val].order
  239. ) {
  240. state.showFilter = false;
  241. return;
  242. }
  243. state.showFilter = false;
  244. // 设置筛选条件
  245. state.curFilter = val;
  246. state.tabList[0].name = state.tabList[0].list[val].label;
  247. state.currentSort = state.tabList[0].list[val].sort;
  248. state.currentOrder = state.tabList[0].list[val].order;
  249. // 清空 + 加载数据
  250. emptyList();
  251. getList();
  252. };
  253. async function getList() {
  254. state.loadStatus = 'loading';
  255. const { code, data } = await SpuApi.getSpuPage({
  256. pageNo: state.pagination.pageNo,
  257. pageSize: state.pagination.pageSize,
  258. sortField: state.currentSort,
  259. sortAsc: state.currentOrder,
  260. categoryId: state.categoryId,
  261. keyword: state.keyword,
  262. });
  263. if (code !== 0) {
  264. return;
  265. }
  266. // 拼接结算信息(营销)
  267. await OrderApi.getSettlementProduct(data.list.map((item) => item.id).join(',')).then((res) => {
  268. if (res.code !== 0) {
  269. return;
  270. }
  271. appendSettlementProduct(data.list, res.data);
  272. });
  273. state.pagination.list = _.concat(state.pagination.list, data.list);
  274. state.pagination.total = data.total;
  275. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  276. mountMasonry();
  277. }
  278. // 加载更多
  279. function loadMore() {
  280. if (state.loadStatus === 'noMore') {
  281. return;
  282. }
  283. state.pagination.pageNo++;
  284. getList(state.currentSort, state.currentOrder);
  285. }
  286. onLoad((options) => {
  287. state.categoryId = options.categoryId;
  288. state.keyword = options.keyword;
  289. getList(state.currentSort, state.currentOrder);
  290. });
  291. // 上拉加载更多
  292. onReachBottom(() => {
  293. loadMore();
  294. });
  295. </script>
  296. <style lang="scss" scoped>
  297. .goods-list-box {
  298. width: 50%;
  299. box-sizing: border-box;
  300. .left-list {
  301. margin-right: 10rpx;
  302. margin-bottom: 20rpx;
  303. }
  304. .right-list {
  305. margin-left: 10rpx;
  306. margin-bottom: 20rpx;
  307. }
  308. }
  309. .goods-box {
  310. &:nth-last-of-type(1) {
  311. margin-bottom: 0 !important;
  312. }
  313. &:nth-child(2n) {
  314. margin-right: 0;
  315. }
  316. }
  317. .list-icon {
  318. width: 80rpx;
  319. .sicon-goods-card {
  320. font-size: 40rpx;
  321. }
  322. .sicon-goods-list {
  323. font-size: 40rpx;
  324. }
  325. }
  326. .goods-card {
  327. margin-left: 20rpx;
  328. }
  329. .list-filter-tabs {
  330. background-color: #fff;
  331. }
  332. .filter-list-box {
  333. padding: 28rpx 52rpx;
  334. .filter-item {
  335. font-size: 28rpx;
  336. font-weight: 500;
  337. color: #333333;
  338. line-height: normal;
  339. margin-bottom: 24rpx;
  340. &:nth-last-child(1) {
  341. margin-bottom: 0;
  342. }
  343. }
  344. .filter-item-active {
  345. color: var(--ui-BG-Main);
  346. }
  347. }
  348. .tab-item {
  349. height: 50px;
  350. position: relative;
  351. z-index: 11;
  352. .tab-title {
  353. font-size: 30rpx;
  354. }
  355. .cur-tab-title {
  356. font-weight: $font-weight-bold;
  357. }
  358. .tab-line {
  359. width: 60rpx;
  360. height: 6rpx;
  361. border-radius: 6rpx;
  362. position: absolute;
  363. left: 50%;
  364. transform: translateX(-50%);
  365. bottom: 10rpx;
  366. background-color: var(--ui-BG-Main);
  367. z-index: 12;
  368. }
  369. }
  370. </style>