category.vue 7.4 KB

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