list.vue 8.6 KB

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