list.vue 8.6 KB

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