category.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <!-- 商品分类列表 -->
  2. <template>
  3. <s-layout title="分类" tabbar="/pages/index/category" :bgStyle="{ color: '#fff' }">
  4. <view class="s-category">
  5. <view class="three-level-wrap ss-flex ss-col-top" :style="[{ height: pageHeight + 'px' }]">
  6. <!-- 商品分类(左) -->
  7. <scroll-view class="side-menu-wrap" scroll-y :style="[{ height: pageHeight + 'px' }]">
  8. <view class="menu-item ss-flex" v-for="(item, index) in state.categoryList" :key="item.id"
  9. :class="[{ 'menu-item-active': index === state.activeMenu }]" @tap="onMenu(index)">
  10. <view class="menu-title ss-line-1">
  11. {{ item.name }}
  12. </view>
  13. </view>
  14. </scroll-view>
  15. <!-- 商品分类(右) -->
  16. <scroll-view class="goods-list-box" scroll-y :style="[{ height: pageHeight + 'px' }]"
  17. v-if="state.categoryList?.length">
  18. <image v-if="state.categoryList[state.activeMenu].picUrl" class="banner-img"
  19. :src="sheep.$url.cdn(state.categoryList[state.activeMenu].picUrl)" mode="widthFix" />
  20. <first-one v-if="state.style === 'first_one'" :pagination="state.pagination" />
  21. <first-two v-if="state.style === 'first_two'" :pagination="state.pagination" />
  22. <second-one v-if="state.style === 'second_one'" :data="state.categoryList"
  23. :activeMenu="state.activeMenu" />
  24. <uni-load-more v-if="
  25. (state.style === 'first_one' || state.style === 'first_two') &&
  26. state.pagination.total > 0
  27. " :status="state.loadStatus" :content-text="{
  28. contentdown: '点击查看更多',
  29. }" @tap="loadMore" />
  30. </scroll-view>
  31. </view>
  32. </view>
  33. </s-layout>
  34. </template>
  35. <script setup>
  36. import secondOne from './components/second-one.vue';
  37. import firstOne from './components/first-one.vue';
  38. import firstTwo from './components/first-two.vue';
  39. import sheep from '@/sheep';
  40. import CategoryApi from '@/sheep/api/product/category';
  41. import SpuApi from '@/sheep/api/product/spu';
  42. import {
  43. onLoad,
  44. onReachBottom
  45. } from '@dcloudio/uni-app';
  46. import {
  47. computed,
  48. reactive
  49. } from 'vue';
  50. import _ from 'lodash';
  51. import {
  52. handleTree
  53. } from '@/sheep/util';
  54. const state = reactive({
  55. style: 'second_one', // first_one(一级 - 样式一), first_two(二级 - 样式二), second_one(二级)
  56. categoryList: [], // 商品分类树
  57. activeMenu: 0, // 选中的一级菜单,在 categoryList 的下标
  58. pagination: {
  59. // 商品分页
  60. list: [], // 商品列表
  61. total: [], // 商品总数
  62. pageNo: 1,
  63. pageSize: 6,
  64. },
  65. loadStatus: '',
  66. });
  67. const {
  68. safeArea
  69. } = sheep.$platform.device;
  70. const pageHeight = computed(() => safeArea.height - 44 - 50);
  71. // 加载商品分类
  72. async function getList() {
  73. const {
  74. code,
  75. data
  76. } = await CategoryApi.getCategoryList();
  77. if (code !== 0) {
  78. return;
  79. }
  80. state.categoryList = handleTree(data);
  81. }
  82. // 选中菜单
  83. const onMenu = (val) => {
  84. state.activeMenu = val;
  85. if (state.style === 'first_one' || state.style === 'first_two') {
  86. state.pagination.pageNo = 1;
  87. state.pagination.list = [];
  88. state.pagination.total = 0;
  89. getGoodsList();
  90. }
  91. };
  92. // 加载商品列表
  93. async function getGoodsList() {
  94. // 加载列表
  95. state.loadStatus = 'loading';
  96. const res = await SpuApi.getSpuPage({
  97. categoryId: state.categoryList[state.activeMenu].id,
  98. pageNo: state.pagination.pageNo,
  99. pageSize: state.pagination.pageSize,
  100. });
  101. if (res.code !== 0) {
  102. return;
  103. }
  104. // 合并列表
  105. state.pagination.list = _.concat(state.pagination.list, res.data.list);
  106. state.pagination.total = res.data.total;
  107. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  108. }
  109. // 加载更多商品
  110. function loadMore() {
  111. if (state.loadStatus === 'noMore') {
  112. return;
  113. }
  114. state.pagination.pageNo++;
  115. getGoodsList();
  116. }
  117. onLoad(async () => {
  118. await getList();
  119. // 如果是 first 风格,需要加载商品分页
  120. if (state.style === 'first_one' || state.style === 'first_two') {
  121. onMenu(0);
  122. }
  123. });
  124. onReachBottom(() => {
  125. loadMore();
  126. });
  127. </script>
  128. <style lang="scss" scoped>
  129. .s-category {
  130. :deep() {
  131. .side-menu-wrap {
  132. width: 200rpx;
  133. height: 100%;
  134. padding-left: 12rpx;
  135. background-color: #f6f6f6;
  136. .menu-item {
  137. width: 100%;
  138. height: 88rpx;
  139. position: relative;
  140. transition: all linear 0.2s;
  141. .menu-title {
  142. line-height: 32rpx;
  143. font-size: 30rpx;
  144. font-weight: 400;
  145. color: #333;
  146. margin-left: 28rpx;
  147. position: relative;
  148. z-index: 0;
  149. // &::before {
  150. // content: '';
  151. // width: 64rpx;
  152. // height: 12rpx;
  153. // background: linear-gradient(90deg,
  154. // var(--ui-BG-Main-gradient),
  155. // var(--ui-BG-Main-light)) !important;
  156. // position: absolute;
  157. // left: -64rpx;
  158. // bottom: 0;
  159. // z-index: -1;
  160. // transition: all linear 0.2s;
  161. // }
  162. }
  163. &.menu-item-active {
  164. background-color: #fff;
  165. border-radius: 20rpx 0 0 20rpx;
  166. &::before {
  167. content: '';
  168. position: absolute;
  169. right: 0;
  170. bottom: -20rpx;
  171. width: 20rpx;
  172. height: 20rpx;
  173. background: radial-gradient(circle at 0 100%, transparent 20rpx, #fff 0);
  174. }
  175. &::after {
  176. content: '';
  177. position: absolute;
  178. top: -20rpx;
  179. right: 0;
  180. width: 20rpx;
  181. height: 20rpx;
  182. background: radial-gradient(circle at 0% 0%, transparent 20rpx, #fff 0);
  183. }
  184. .menu-title {
  185. font-weight: 600;
  186. color: var(--ui-BG-Main);
  187. &::before {
  188. left: 0;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. .goods-list-box {
  195. background-color: #fff;
  196. width: calc(100vw - 100px);
  197. padding: 10px;
  198. }
  199. .banner-img {
  200. width: calc(100vw - 130px);
  201. border-radius: 5px;
  202. margin-bottom: 20rpx;
  203. }
  204. }
  205. }
  206. </style>