list.vue 8.9 KB

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