list.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 pagination = {
  124. data: [],
  125. current_page: 1,
  126. total: 1,
  127. last_page: 1,
  128. };
  129. const state = reactive({
  130. pagination: {
  131. data: [],
  132. current_page: 1,
  133. total: 1,
  134. last_page: 1,
  135. },
  136. currentSort: 'weigh',
  137. currentOrder: 'desc',
  138. currentTab: 0,
  139. filterParams: {},
  140. curFilter: 0,
  141. showFilter: false,
  142. iconStatus: false,
  143. categoryId: 0,
  144. tabList: [
  145. {
  146. name: '综合推荐',
  147. value: 'weigh',
  148. list: [
  149. {
  150. label: '综合推荐',
  151. sort: 'weigh',
  152. order: 'desc',
  153. },
  154. {
  155. label: '价格升序',
  156. sort: 'price',
  157. order: 'asc',
  158. },
  159. {
  160. label: '价格降序',
  161. sort: 'price',
  162. order: 'desc',
  163. },
  164. ],
  165. },
  166. {
  167. name: '销量',
  168. value: 'total_sales',
  169. },
  170. {
  171. name: '新品优先',
  172. value: 'create_time',
  173. },
  174. ],
  175. loadStatus: '',
  176. keyword: '',
  177. leftGoodsList: [],
  178. rightGoodsList: [],
  179. });
  180. // 加载瀑布流
  181. let count = 0;
  182. let leftHeight = 0;
  183. let rightHeight = 0;
  184. function mountMasonry(height = 0, where = 'left') {
  185. if (!state.pagination.data[count]) return;
  186. if (where === 'left') {
  187. leftHeight += height;
  188. } else {
  189. rightHeight += height;
  190. }
  191. if (leftHeight <= rightHeight) {
  192. state.leftGoodsList.push(state.pagination.data[count]);
  193. } else {
  194. state.rightGoodsList.push(state.pagination.data[count]);
  195. }
  196. count++;
  197. }
  198. function emptyList() {
  199. state.pagination = pagination
  200. state.leftGoodsList = [];
  201. state.rightGoodsList = [];
  202. count = 0;
  203. leftHeight = 0;
  204. rightHeight = 0;
  205. }
  206. //搜索
  207. function onSearch(e) {
  208. state.keyword = e;
  209. emptyList();
  210. getList(state.currentSort, state.currentOrder, state.categoryId, e);
  211. }
  212. // 点击
  213. function onTabsChange(e) {
  214. if (state.tabList[e.index].list) {
  215. state.currentTab = e.index;
  216. state.showFilter = !state.showFilter;
  217. return;
  218. } else {
  219. state.showFilter = false;
  220. }
  221. if (e.index === state.currentTab) {
  222. return;
  223. } else {
  224. state.currentTab = e.index;
  225. }
  226. emptyList();
  227. getList(e.value, state.currentOrder, state.categoryId, state.keyword);
  228. }
  229. // 点击筛选项
  230. const onFilterItem = (val) => {
  231. if (
  232. state.currentSort === state.tabList[0].list[val].sort &&
  233. state.currentOrder === state.tabList[0].list[val].order
  234. ) {
  235. state.showFilter = false;
  236. return;
  237. }
  238. state.curFilter = val;
  239. state.tabList[0].name = state.tabList[0].list[val].label;
  240. state.currentSort = state.tabList[0].list[val].sort;
  241. state.currentOrder = state.tabList[0].list[val].order;
  242. emptyList();
  243. getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword);
  244. state.showFilter = false;
  245. };
  246. async function getList(Sort, Order, categoryId, keyword, page = 1, list_rows = 6) {
  247. state.loadStatus = 'loading';
  248. const res = await sheep.$api.goods.list({
  249. sort: Sort,
  250. order: Order,
  251. category_id: !keyword ? categoryId : '',
  252. list_rows,
  253. keyword: keyword,
  254. page,
  255. });
  256. if (res.error === 0) {
  257. let couponList = _.concat(state.pagination.data, res.data.data);
  258. state.pagination = {
  259. ...res.data,
  260. data: couponList,
  261. };
  262. mountMasonry();
  263. if (state.pagination.current_page < state.pagination.last_page) {
  264. state.loadStatus = 'more';
  265. } else {
  266. state.loadStatus = 'noMore';
  267. }
  268. }
  269. }
  270. // 加载更多
  271. function loadmore() {
  272. if (state.loadStatus !== 'noMore') {
  273. getList(
  274. state.currentSort,
  275. state.currentOrder,
  276. state.categoryId,
  277. state.keyword,
  278. state.pagination.current_page + 1,
  279. );
  280. }
  281. }
  282. onLoad((options) => {
  283. state.categoryId = options.categoryId;
  284. state.keyword = options.keyword;
  285. getList(state.currentSort, state.currentOrder, state.categoryId, state.keyword);
  286. });
  287. // 上拉加载更多
  288. onReachBottom(() => {
  289. loadmore();
  290. });
  291. </script>
  292. <style lang="scss" scoped>
  293. .goods-list-box {
  294. width: 50%;
  295. box-sizing: border-box;
  296. .left-list {
  297. margin-right: 10rpx;
  298. margin-bottom: 20rpx;
  299. }
  300. .right-list {
  301. margin-left: 10rpx;
  302. margin-bottom: 20rpx;
  303. }
  304. }
  305. .goods-box {
  306. &:nth-last-of-type(1) {
  307. margin-bottom: 0 !important;
  308. }
  309. &:nth-child(2n) {
  310. margin-right: 0;
  311. }
  312. }
  313. .list-icon {
  314. width: 80rpx;
  315. .sicon-goods-card {
  316. font-size: 40rpx;
  317. }
  318. .sicon-goods-list {
  319. font-size: 40rpx;
  320. }
  321. }
  322. .goods-card {
  323. margin-left: 20rpx;
  324. }
  325. .list-filter-tabs {
  326. background-color: #fff;
  327. }
  328. .filter-list-box {
  329. padding: 28rpx 52rpx;
  330. .filter-item {
  331. font-size: 28rpx;
  332. font-weight: 500;
  333. color: #333333;
  334. line-height: normal;
  335. margin-bottom: 24rpx;
  336. &:nth-last-child(1) {
  337. margin-bottom: 0;
  338. }
  339. }
  340. .filter-item-active {
  341. color: var(--ui-BG-Main);
  342. }
  343. }
  344. .tab-item {
  345. height: 50px;
  346. position: relative;
  347. z-index: 11;
  348. .tab-title {
  349. font-size: 30rpx;
  350. }
  351. .cur-tab-title {
  352. font-weight: $font-weight-bold;
  353. }
  354. .tab-line {
  355. width: 60rpx;
  356. height: 6rpx;
  357. border-radius: 6rpx;
  358. position: absolute;
  359. left: 50%;
  360. transform: translateX(-50%);
  361. bottom: 10rpx;
  362. background-color: var(--ui-BG-Main);
  363. z-index: 12;
  364. }
  365. }
  366. </style>