list.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. <template v-slot:cart>
  55. <button class="ss-reset-button cart-btn"> </button>
  56. </template>
  57. </s-goods-column>
  58. </view>
  59. </view>
  60. <view class="goods-list-box">
  61. <view class="right-list" v-for="item in state.rightGoodsList" :key="item.id">
  62. <s-goods-column
  63. class="goods-md-box"
  64. size="md"
  65. :topRadius="10"
  66. :bottomRadius="10"
  67. :data="item"
  68. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  69. @getHeight="mountMasonry($event, 'right')">
  70. <template v-slot:cart>
  71. <button class="ss-reset-button cart-btn" />
  72. </template>
  73. </s-goods-column>
  74. </view>
  75. </view>
  76. </view>
  77. <uni-load-more v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{
  78. contentdown: '上拉加载更多',
  79. }" @tap="loadMore" />
  80. <s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无商品" />
  81. </s-layout>
  82. </template>
  83. <script setup>
  84. import { reactive } from 'vue';
  85. import {
  86. onLoad,
  87. onReachBottom
  88. } from '@dcloudio/uni-app';
  89. import sheep from '@/sheep';
  90. import _ from 'lodash';
  91. import { resetPagination } from '@/sheep/util';
  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: 'weigh',
  102. // currentOrder: 'desc',
  103. currentTab: 0,
  104. curFilter: 0,
  105. showFilter: false,
  106. iconStatus: false, // true - 单列布局;false - 双列布局
  107. categoryId: 0,
  108. tabList: [{
  109. name: '综合推荐',
  110. // value: '',
  111. list: [{
  112. label: '综合推荐',
  113. // sort: '',
  114. // order: true,
  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. // value: 'salesCount',
  133. },
  134. {
  135. name: '新品优先',
  136. // value: 'create_time',
  137. },
  138. ],
  139. loadStatus: '',
  140. keyword: '',
  141. leftGoodsList: [], // 双列布局 - 左侧商品
  142. rightGoodsList: [], // 双列布局 - 右侧商品
  143. });
  144. // 加载瀑布流
  145. let count = 0;
  146. let leftHeight = 0;
  147. let rightHeight = 0;
  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. if (state.tabList[e.index].list) {
  182. state.currentTab = e.index;
  183. state.showFilter = !state.showFilter;
  184. return;
  185. } else {
  186. state.showFilter = false;
  187. }
  188. if (e.index === state.currentTab) {
  189. return;
  190. } else {
  191. state.currentTab = e.index;
  192. }
  193. emptyList();
  194. console.log(e, '6666')
  195. getList(e.sort, e.order);
  196. }
  197. // 点击筛选项
  198. const onFilterItem = (val) => {
  199. console.log(val)
  200. if (
  201. state.currentSort === state.tabList[0].list[val].sort &&
  202. state.currentOrder === state.tabList[0].list[val].order
  203. ) {
  204. state.showFilter = false;
  205. return;
  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. emptyList();
  212. getList(state.currentSort, state.currentOrder);
  213. state.showFilter = false;
  214. };
  215. async function getList(Sort, Order) {
  216. state.loadStatus = 'loading';
  217. const { code, data } = await sheep.$api.goods.list({
  218. pageNo: state.pagination.pageNo,
  219. pageSize: state.pagination.pageSize,
  220. sortField: Sort,
  221. sortAsc: Order,
  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>