category.vue 6.8 KB

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