category.vue 6.2 KB

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