list.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <template>
  2. <s-layout
  3. navbar="normal"
  4. :leftWidth="0"
  5. :rightWidth="0"
  6. tools="search"
  7. :defaultSearch="state.keyword"
  8. @search="onSearch"
  9. >
  10. <!-- 筛选 -->
  11. <su-sticky bgColor="#fff">
  12. <view class="ss-flex">
  13. <view class="ss-flex-1">
  14. <su-tabs
  15. :list="state.tabList"
  16. :scrollable="false"
  17. @change="onTabsChange"
  18. :current="state.currentTab"
  19. ></su-tabs>
  20. </view>
  21. <view class="list-icon" @tap="state.iconStatus = !state.iconStatus">
  22. <text v-if="state.iconStatus" class="sicon-goods-list"></text>
  23. <text v-else class="sicon-goods-card"></text>
  24. </view>
  25. </view>
  26. </su-sticky>
  27. <!-- 弹窗 -->
  28. <su-popup
  29. :show="state.showFilter"
  30. type="top"
  31. round="10"
  32. :space="sys_navBar + 38"
  33. backgroundColor="#F6F6F6"
  34. :zIndex="10"
  35. @close="state.showFilter = false"
  36. >
  37. <view class="filter-list-box">
  38. <view
  39. class="filter-item"
  40. v-for="(item, index) in state.tabList[state.currentTab].list"
  41. :key="item.value"
  42. :class="[{ 'filter-item-active': index == state.curFilter }]"
  43. @tap="onFilterItem(index)"
  44. >
  45. {{ item.label }}
  46. </view>
  47. </view>
  48. </su-popup>
  49. <view v-if="state.iconStatus && state.pagination.total > 0" class="goods-list ss-m-t-20">
  50. <view
  51. class="ss-p-l-20 ss-p-r-20 ss-m-b-20"
  52. v-for="item in state.pagination.data"
  53. :key="item.id"
  54. >
  55. <s-goods-column
  56. class=""
  57. size="lg"
  58. :data="item"
  59. :topRadius="10"
  60. :bottomRadius="10"
  61. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  62. ></s-goods-column>
  63. </view>
  64. </view>
  65. <view
  66. v-if="!state.iconStatus && state.pagination.total > 0"
  67. class="ss-flex ss-flex-wrap ss-p-x-20 ss-m-t-20 ss-col-top"
  68. >
  69. <view class="goods-list-box">
  70. <view class="left-list" v-for="item in state.leftGoodsList" :key="item.id">
  71. <s-goods-column
  72. class="goods-md-box"
  73. size="md"
  74. :data="item"
  75. :topRadius="10"
  76. :bottomRadius="10"
  77. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  78. @getHeight="mountMasonry($event, 'left')"
  79. >
  80. <template v-slot:cart>
  81. <button class="ss-reset-button cart-btn"> </button>
  82. </template>
  83. </s-goods-column>
  84. </view>
  85. </view>
  86. <view class="goods-list-box">
  87. <view class="right-list" v-for="item in state.rightGoodsList" :key="item.id">
  88. <s-goods-column
  89. class="goods-md-box"
  90. size="md"
  91. :topRadius="10"
  92. :bottomRadius="10"
  93. :data="item"
  94. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  95. @getHeight="mountMasonry($event, 'right')"
  96. >
  97. <template v-slot:cart>
  98. <button class="ss-reset-button cart-btn"> </button>
  99. </template>
  100. </s-goods-column>
  101. </view>
  102. </view>
  103. </view>
  104. <uni-load-more
  105. v-if="state.pagination.total > 0"
  106. :status="state.loadStatus"
  107. :content-text="{
  108. contentdown: '上拉加载更多',
  109. }"
  110. @tap="loadmore"
  111. />
  112. <s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无商品">
  113. </s-empty>
  114. </s-layout>
  115. </template>
  116. <script setup>
  117. import { reactive } from 'vue';
  118. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  119. import sheep from '@/sheep';
  120. import _ from 'lodash';
  121. const sys_navBar = sheep.$platform.navbar;
  122. const emits = defineEmits(['close', 'change']);
  123. const state = reactive({
  124. pagination: {
  125. data: [],
  126. current_page: 1,
  127. total: 1,
  128. last_page: 1,
  129. },
  130. currentSort: 'weigh',
  131. currentOrder: 'desc',
  132. currentTab: 0,
  133. filterParams: {},
  134. curFilter: 0,
  135. showFilter: false,
  136. iconStatus: false,
  137. categoryId: 0,
  138. tabList: [
  139. {
  140. name: '综合推荐',
  141. value: 'weigh',
  142. list: [
  143. {
  144. label: '综合推荐',
  145. sort: 'weigh',
  146. order: 'desc',
  147. },
  148. {
  149. label: '价格升序',
  150. sort: 'price',
  151. order: 'asc',
  152. },
  153. {
  154. label: '价格降序',
  155. sort: 'price',
  156. order: 'desc',
  157. },
  158. ],
  159. },
  160. {
  161. name: '销量',
  162. value: 'total_sales',
  163. },
  164. {
  165. name: '新品优先',
  166. value: 'create_time',
  167. },
  168. ],
  169. loadStatus: '',
  170. keyword: '',
  171. leftGoodsList: [],
  172. rightGoodsList: [],
  173. });
  174. // 加载瀑布流
  175. let count = 0;
  176. let leftHeight = 0;
  177. let rightHeight = 0;
  178. function mountMasonry(height = 0, where = 'left') {
  179. if (!state.pagination.data[count]) return;
  180. if (where === 'left') {
  181. leftHeight += height;
  182. } else {
  183. rightHeight += height;
  184. }
  185. if (leftHeight <= rightHeight) {
  186. state.leftGoodsList.push(state.pagination.data[count]);
  187. } else {
  188. state.rightGoodsList.push(state.pagination.data[count]);
  189. }
  190. count++;
  191. }
  192. function emptyList() {
  193. state.pagination = {
  194. data: [],
  195. current_page: 1,
  196. total: 1,
  197. last_page: 1,
  198. };
  199. state.leftGoodsList = [];
  200. state.rightGoodsList = [];
  201. count = 0;
  202. }
  203. //搜索
  204. function onSearch(e) {
  205. state.keyword = e;
  206. emptyList();
  207. getList(state.currentSort, state.currentOrder, state.categoryId, e);
  208. }
  209. // 点击
  210. function onTabsChange(e) {
  211. if (state.tabList[e.index].list) {
  212. state.currentTab = e.index;
  213. state.showFilter = !state.showFilter;
  214. return;
  215. } else {
  216. state.showFilter = false;
  217. }
  218. if (e.index === state.currentTab) {
  219. return;
  220. } else {
  221. state.currentTab = e.index;
  222. }
  223. emptyList();
  224. getList(e.value, state.currentOrder, state.categoryId, state.keyword);
  225. }
  226. // 点击筛选项
  227. const onFilterItem = (val) => {
  228. state.curFilter = val;
  229. state.tabList[0].name = state.tabList[0].list[val].label;
  230. state.currentSort = state.tabList[0].list[val].sort;
  231. state.currentOrder = state.tabList[0].list[val].order;
  232. emptyList();
  233. getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword);
  234. state.showFilter = false;
  235. };
  236. async function getList(Sort, Order, categoryId, keyword, page = 1, list_rows = 6) {
  237. state.loadStatus = 'loading';
  238. const res = await sheep.$api.goods.list({
  239. sort: Sort,
  240. order: Order,
  241. category_id: !keyword ? categoryId : '',
  242. list_rows,
  243. keyword: keyword,
  244. page,
  245. });
  246. if (res.error === 0) {
  247. if (page >= 2) {
  248. let couponList = _.concat(state.pagination.data, res.data.data);
  249. state.pagination = {
  250. ...res.data,
  251. data: couponList,
  252. };
  253. } else {
  254. state.pagination = res.data;
  255. }
  256. mountMasonry();
  257. if (state.pagination.current_page < state.pagination.last_page) {
  258. state.loadStatus = 'more';
  259. } else {
  260. state.loadStatus = 'noMore';
  261. }
  262. }
  263. }
  264. // 加载更多
  265. function loadmore() {
  266. if (state.loadStatus !== 'noMore') {
  267. getList(
  268. state.currentSort,
  269. state.currentOrder,
  270. state.categoryId,
  271. state.keyword,
  272. state.pagination.current_page + 1,
  273. );
  274. }
  275. }
  276. onLoad((options) => {
  277. state.categoryId = options.categoryId;
  278. state.keyword = options.keyword;
  279. getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword);
  280. });
  281. // 上拉加载更多
  282. onReachBottom(() => {
  283. loadmore();
  284. });
  285. </script>
  286. <style lang="scss" scoped>
  287. .goods-list-box {
  288. width: 50%;
  289. box-sizing: border-box;
  290. .left-list {
  291. margin-right: 10rpx;
  292. margin-bottom: 20rpx;
  293. }
  294. .right-list {
  295. margin-left: 10rpx;
  296. margin-bottom: 20rpx;
  297. }
  298. }
  299. .goods-box {
  300. &:nth-last-of-type(1) {
  301. margin-bottom: 0 !important;
  302. }
  303. &:nth-child(2n) {
  304. margin-right: 0;
  305. }
  306. }
  307. .list-icon {
  308. width: 80rpx;
  309. .sicon-goods-card {
  310. font-size: 40rpx;
  311. }
  312. .sicon-goods-list {
  313. font-size: 40rpx;
  314. }
  315. }
  316. .goods-card {
  317. margin-left: 20rpx;
  318. }
  319. .list-filter-tabs {
  320. background-color: #fff;
  321. }
  322. .filter-list-box {
  323. padding: 28rpx 52rpx;
  324. .filter-item {
  325. font-size: 28rpx;
  326. font-weight: 500;
  327. color: #333333;
  328. line-height: normal;
  329. margin-bottom: 24rpx;
  330. &:nth-last-child(1) {
  331. margin-bottom: 0;
  332. }
  333. }
  334. .filter-item-active {
  335. color: var(--ui-BG-Main);
  336. }
  337. }
  338. .tab-item {
  339. height: 50px;
  340. position: relative;
  341. z-index: 11;
  342. .tab-title {
  343. font-size: 30rpx;
  344. }
  345. .cur-tab-title {
  346. font-weight: $font-weight-bold;
  347. }
  348. .tab-line {
  349. width: 60rpx;
  350. height: 6rpx;
  351. border-radius: 6rpx;
  352. position: absolute;
  353. left: 50%;
  354. transform: translateX(-50%);
  355. bottom: 10rpx;
  356. background-color: var(--ui-BG-Main);
  357. z-index: 12;
  358. }
  359. }
  360. </style>