allproduct.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <!-- 商品分类列表 -->
  2. <template>
  3. <su-navbar :title="state.shopName" statusBar :color="color" :tools="tools" :opacityBgUi="opacityBgUi"
  4. @search="(e) => emits('search', e)" :defaultSearch="defaultSearch" />
  5. <!-- 情况一:单列布局 -->
  6. <view v-if="state.iconStatus && state.pagination.total > 0" class="goods-list ss-m-t-20">
  7. <view class="ss-p-l-20 ss-p-r-20 ss-m-b-20" v-for="item in state.pagination.list" :key="item.id">
  8. <s-goods-column class="" size="lg" :data="item" :topRadius="10" :bottomRadius="10"
  9. @click="sheep.$router.go('/pages/goods/index', { id: item.id })" />
  10. </view>
  11. </view>
  12. <!-- 情况二:双列布局 -->
  13. <view v-if="!state.iconStatus && state.pagination.total > 0"
  14. class="ss-flex ss-flex-wrap ss-p-x-20 ss-m-t-20 ss-col-top">
  15. <view class="goods-list-box">
  16. <view class="left-list" v-for="item in state.leftGoodsList" :key="item.id">
  17. <s-goods-column class="goods-md-box" size="md" :data="item" :topRadius="10" :bottomRadius="10"
  18. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  19. @getHeight="mountMasonry($event, 'left')">
  20. <template v-slot:cart>
  21. <button class="ss-reset-button cart-btn" />
  22. </template>
  23. </s-goods-column>
  24. </view>
  25. </view>
  26. <view class="goods-list-box">
  27. <view class="right-list" v-for="item in state.rightGoodsList" :key="item.id">
  28. <s-goods-column class="goods-md-box" size="md" :topRadius="10" :bottomRadius="10" :data="item"
  29. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  30. @getHeight="mountMasonry($event, 'right')">
  31. <template v-slot:cart>
  32. <button class="ss-reset-button cart-btn" />
  33. </template>
  34. </s-goods-column>
  35. </view>
  36. </view>
  37. </view>
  38. <uni-load-more v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{
  39. contentdown: '上拉加载更多',
  40. }" @tap="loadMore" />
  41. <s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无商品" />
  42. <s-tabbar path="/pages/shop/allproduct" :tabbar="tabbar" />
  43. <s-menu-tools />
  44. </template>
  45. <script setup>
  46. import secondOne from '@/pages/index/components/second-one.vue';
  47. import firstOne from '@/pages/index/components/first-one.vue';
  48. import firstTwo from '@/pages/index/components/first-two.vue';
  49. import sheep from '@/sheep';
  50. import CategoryApi from '@/sheep/api/product/category';
  51. import SpuApi from '@/sheep/api/product/spu';
  52. import {
  53. onLoad,
  54. onReachBottom
  55. } from '@dcloudio/uni-app';
  56. import {
  57. ref,
  58. computed,
  59. reactive
  60. } from 'vue';
  61. import _ from 'lodash';
  62. import {
  63. handleTree
  64. } from '@/sheep/util';
  65. import {
  66. t
  67. } from '@/locale'
  68. const tabbar = ref({
  69. "theme": "red",
  70. "style": {
  71. "bgType": "color",
  72. "bgColor": "#ffffff",
  73. "color": "#3c3c3c",
  74. "activeColor": "#1fa380"
  75. },
  76. "items": [{
  77. "text": t('common.shop_index'),
  78. "url": "/pages/shop/index",
  79. "iconUrl": sheep.$url.static('/static/shopIndex/shopIndex.svg'),
  80. "activeIconUrl": sheep.$url.static('/static/shopIndex/shopIndex-active.svg')
  81. },
  82. {
  83. "text": t('common.shop_category'),
  84. "url": "/pages/shop/category",
  85. "iconUrl": sheep.$url.static('/static/shopIndex/category.svg'),
  86. "activeIconUrl": sheep.$url.static('/static/shopIndex/category-active.svg')
  87. },
  88. {
  89. "text": t('common.all_product'),
  90. "url": "/pages/shop/allproduct",
  91. "iconUrl": sheep.$url.static('/static/shopIndex/allProduct.svg'),
  92. "activeIconUrl": sheep.$url.static('/static/shopIndex/allProduct-active.svg')
  93. },
  94. ]
  95. });
  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: t('common.recommended'),
  113. list: [{
  114. label: t('common.recommended')
  115. },
  116. {
  117. label: t('common.price_asc'),
  118. sort: 'price',
  119. order: true,
  120. },
  121. {
  122. label: t('common.price_desc'),
  123. sort: 'price',
  124. order: false,
  125. },
  126. ],
  127. },
  128. {
  129. name: t('common.sales'),
  130. sort: 'salesCount',
  131. order: false
  132. },
  133. {
  134. name: t('common.newest'),
  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 {
  220. code,
  221. data
  222. } = await SpuApi.getSpuPage({
  223. pageNo: state.pagination.pageNo,
  224. pageSize: state.pagination.pageSize,
  225. sortField: state.currentSort,
  226. sortAsc: state.currentOrder,
  227. categoryId: state.categoryId,
  228. keyword: state.keyword,
  229. shopId: state.shopId,
  230. merchantId: state.merchantId
  231. });
  232. if (code !== 0) {
  233. return;
  234. }
  235. state.pagination.list = _.concat(state.pagination.list, data.list)
  236. state.pagination.total = data.total;
  237. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  238. mountMasonry();
  239. }
  240. // 加载更多
  241. function loadMore() {
  242. if (state.loadStatus === 'noMore') {
  243. return;
  244. }
  245. state.pagination.pageNo++;
  246. getList(state.currentSort, state.currentOrder);
  247. }
  248. onLoad(async (options) => {
  249. state.merchantId = options.merchantId;
  250. state.shopId = options.shopId;
  251. state.shopName = options.shopName;
  252. state.categoryId = options.categoryId;
  253. state.keyword = options.keyword;
  254. tabbar.value.items = tabbar.value.items.map(item => {
  255. // 为每个 URL 添加参数
  256. item.url =
  257. `${item.url}?shopId=${state.shopId}&shopName=${encodeURIComponent(state.shopName)}&merchantId=${state.merchantId}`;
  258. return item;
  259. });
  260. getList(state.currentSort, state.currentOrder);
  261. });
  262. onReachBottom(() => {
  263. loadMore();
  264. });
  265. </script>
  266. <style lang="scss" scoped>
  267. .goods-list-box {
  268. width: 50%;
  269. box-sizing: border-box;
  270. .left-list {
  271. margin-right: 10rpx;
  272. margin-bottom: 20rpx;
  273. }
  274. .right-list {
  275. margin-left: 10rpx;
  276. margin-bottom: 20rpx;
  277. }
  278. }
  279. .goods-box {
  280. &:nth-last-of-type(1) {
  281. margin-bottom: 0 !important;
  282. }
  283. &:nth-child(2n) {
  284. margin-right: 0;
  285. }
  286. }
  287. .list-icon {
  288. width: 80rpx;
  289. .sicon-goods-card {
  290. font-size: 40rpx;
  291. }
  292. .sicon-goods-list {
  293. font-size: 40rpx;
  294. }
  295. }
  296. .goods-card {
  297. margin-left: 20rpx;
  298. }
  299. .list-filter-tabs {
  300. background-color: #fff;
  301. }
  302. .filter-list-box {
  303. padding: 28rpx 52rpx;
  304. .filter-item {
  305. font-size: 28rpx;
  306. font-weight: 500;
  307. color: #333333;
  308. line-height: normal;
  309. margin-bottom: 24rpx;
  310. &:nth-last-child(1) {
  311. margin-bottom: 0;
  312. }
  313. }
  314. .filter-item-active {
  315. color: var(--ui-BG-Main);
  316. }
  317. }
  318. .tab-item {
  319. height: 50px;
  320. position: relative;
  321. z-index: 11;
  322. .tab-title {
  323. font-size: 30rpx;
  324. }
  325. .cur-tab-title {
  326. font-weight: $font-weight-bold;
  327. }
  328. .tab-line {
  329. width: 60rpx;
  330. height: 6rpx;
  331. border-radius: 6rpx;
  332. position: absolute;
  333. left: 50%;
  334. transform: translateX(-50%);
  335. bottom: 10rpx;
  336. background-color: var(--ui-BG-Main);
  337. z-index: 12;
  338. }
  339. }
  340. </style>