list.vue 9.1 KB

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