category.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <!-- 商品分类列表 -->
  2. <template>
  3. <su-navbar :title="state.shopName" statusBar :color="color" :tools="tools" :opacityBgUi="opacityBgUi"
  4. @search="(e) => emits('search', e)" :defaultSearch="defaultSearch" />
  5. <view class="s-category">
  6. <view class="three-level-wrap ss-flex ss-col-top" :style="[{ height: pageHeight + 'px' }]">
  7. <!-- 商品分类(左) -->
  8. <scroll-view class="side-menu-wrap" scroll-y :style="[{ height: pageHeight + 'px' }]">
  9. <view class="menu-item ss-flex" v-for="(item, index) in state.categoryList" :key="item.id"
  10. :class="[{ 'menu-item-active': index === state.activeMenu }]" @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 class="goods-list-box" scroll-y :style="[{ height: pageHeight + 'px' }]"
  18. v-if="state.categoryList?.length">
  19. <image v-if="state.categoryList[state.activeMenu].picUrl" class="banner-img"
  20. :src="sheep.$url.cdn(state.categoryList[state.activeMenu].picUrl)" mode="widthFix" />
  21. <first-one v-if="state.style === 'first_one'" :pagination="state.pagination" :merchantId="state.merchantId" :shopId="state.shopId"/>
  22. <first-two v-if="state.style === 'first_two'" :pagination="state.pagination" :merchantId="state.merchantId" :shopId="state.shopId"/>
  23. <second-one v-if="state.style === 'second_one'" :data="state.categoryList" :merchantId="state.merchantId " :shopId="state.shopId" :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-tabbar path="/pages/shop/category" :tabbar="tabbar" />
  34. <s-menu-tools />
  35. </template>
  36. <script setup>
  37. import secondOne from '@/pages/index/components/second-one.vue';
  38. import firstOne from '@/pages/index/components/first-one.vue';
  39. import firstTwo from '@/pages/index/components/first-two.vue';
  40. import sheep from '@/sheep';
  41. import CategoryApi from '@/sheep/api/product/category';
  42. import SpuApi from '@/sheep/api/product/spu';
  43. import {
  44. onLoad,
  45. onReachBottom
  46. } from '@dcloudio/uni-app';
  47. import {
  48. ref,
  49. computed,
  50. reactive
  51. } from 'vue';
  52. import _ from 'lodash';
  53. import {
  54. handleTree
  55. } from '@/sheep/util';
  56. import {
  57. t
  58. } from '@/locale'
  59. const tabbar = ref({
  60. "theme": "red",
  61. "style": {
  62. "bgType": "color",
  63. "bgColor": "#ffffff",
  64. "color": "#3c3c3c",
  65. "activeColor": "#1fa380"
  66. },
  67. "items": [{
  68. "text": t('common.shop_index'),
  69. "url": "/pages/shop/index",
  70. "iconUrl": sheep.$url.static('/static/shopIndex/shopIndex.svg'),
  71. "activeIconUrl": sheep.$url.static('/static/shopIndex/shopIndex-active.svg')
  72. },
  73. {
  74. "text": t('common.shop_category'),
  75. "url": "/pages/shop/category",
  76. "iconUrl": sheep.$url.static('/static/shopIndex/category.svg'),
  77. "activeIconUrl": sheep.$url.static('/static/shopIndex/category-active.svg')
  78. },
  79. {
  80. "text": t('common.all_product'),
  81. "url": "/pages/shop/allproduct",
  82. "iconUrl": sheep.$url.static('/static/shopIndex/allProduct.svg'),
  83. "activeIconUrl": sheep.$url.static('/static/shopIndex/allProduct-active.svg')
  84. },
  85. ]
  86. });
  87. const state = reactive({
  88. style: 'second_one', // first_one(一级 - 样式一), first_two(二级 - 样式二), second_one(二级)
  89. categoryList: [], // 商品分类树
  90. activeMenu: 0, // 选中的一级菜单,在 categoryList 的下标
  91. shopId: 0, // 商家id
  92. merchantId: 0, // 商家id
  93. shopName: '',
  94. pagination: {
  95. // 商品分页
  96. list: [], // 商品列表
  97. total: [], // 商品总数
  98. pageNo: 1,
  99. pageSize: 6,
  100. },
  101. loadStatus: '',
  102. });
  103. const {
  104. safeArea
  105. } = sheep.$platform.device;
  106. const pageHeight = computed(() => safeArea.height - 44 - 50);
  107. // 加载商品分类
  108. async function getList() {
  109. const {
  110. code,
  111. data
  112. } = await CategoryApi.getCategoryList({
  113. merchantId: state.merchantId,
  114. shopId: state.shopId
  115. });
  116. if (code !== 0) {
  117. return;
  118. }
  119. state.categoryList = handleTree(data);
  120. }
  121. // 选中菜单
  122. const onMenu = (val) => {
  123. state.activeMenu = val;
  124. if (state.style === 'first_one' || state.style === 'first_two') {
  125. state.pagination.pageNo = 1;
  126. state.pagination.list = [];
  127. state.pagination.total = 0;
  128. getGoodsList();
  129. }
  130. };
  131. // 加载商品列表
  132. async function getGoodsList() {
  133. // 加载列表
  134. state.loadStatus = 'loading';
  135. const res = await SpuApi.getSpuPage({
  136. categoryId: state.categoryList[state.activeMenu].id,
  137. pageNo: state.pagination.pageNo,
  138. pageSize: state.pagination.pageSize,
  139. });
  140. if (res.code !== 0) {
  141. return;
  142. }
  143. // 合并列表
  144. state.pagination.list = _.concat(state.pagination.list, res.data.list);
  145. state.pagination.total = res.data.total;
  146. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  147. }
  148. // 加载更多商品
  149. function loadMore() {
  150. if (state.loadStatus === 'noMore') {
  151. return;
  152. }
  153. state.pagination.pageNo++;
  154. getGoodsList();
  155. }
  156. onLoad(async (options) => {
  157. state.merchantId = options.merchantId;
  158. state.shopId = options.shopId;
  159. state.shopName = options.shopName;
  160. tabbar.value.items = tabbar.value.items.map(item => {
  161. // 为每个 URL 添加参数
  162. item.url =
  163. `${item.url}?shopId=${state.shopId}&shopName=${encodeURIComponent(state.shopName)}&merchantId=${state.merchantId}`;
  164. return item;
  165. });
  166. await getList();
  167. // 如果是 first 风格,需要加载商品分页
  168. if (state.style === 'first_one' || state.style === 'first_two') {
  169. onMenu(0);
  170. }
  171. });
  172. onReachBottom(() => {
  173. loadMore();
  174. });
  175. </script>
  176. <style lang="scss" scoped>
  177. .s-category {
  178. :deep() {
  179. .side-menu-wrap {
  180. width: 200rpx;
  181. height: 100%;
  182. padding-left: 12rpx;
  183. background-color: #f6f6f6;
  184. .menu-item {
  185. width: 100%;
  186. height: 88rpx;
  187. position: relative;
  188. transition: all linear 0.2s;
  189. .menu-title {
  190. line-height: 32rpx;
  191. font-size: 30rpx;
  192. font-weight: 400;
  193. color: #333;
  194. margin-left: 28rpx;
  195. position: relative;
  196. z-index: 0;
  197. // &::before {
  198. // content: '';
  199. // width: 64rpx;
  200. // height: 12rpx;
  201. // background: linear-gradient(90deg,
  202. // var(--ui-BG-Main-gradient),
  203. // var(--ui-BG-Main-light)) !important;
  204. // position: absolute;
  205. // left: -64rpx;
  206. // bottom: 0;
  207. // z-index: -1;
  208. // transition: all linear 0.2s;
  209. // }
  210. }
  211. &.menu-item-active {
  212. background-color: #fff;
  213. border-radius: 20rpx 0 0 20rpx;
  214. &::before {
  215. content: '';
  216. position: absolute;
  217. right: 0;
  218. bottom: -20rpx;
  219. width: 20rpx;
  220. height: 20rpx;
  221. background: radial-gradient(circle at 0 100%, transparent 20rpx, #fff 0);
  222. }
  223. &::after {
  224. content: '';
  225. position: absolute;
  226. top: -20rpx;
  227. right: 0;
  228. width: 20rpx;
  229. height: 20rpx;
  230. background: radial-gradient(circle at 0% 0%, transparent 20rpx, #fff 0);
  231. }
  232. .menu-title {
  233. font-weight: 600;
  234. color: var(--ui-BG-Main);
  235. &::before {
  236. left: 0;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. .goods-list-box {
  243. background-color: #fff;
  244. width: calc(100vw - 100px);
  245. padding: 10px;
  246. }
  247. .banner-img {
  248. width: calc(100vw - 130px);
  249. border-radius: 5px;
  250. margin-bottom: 20rpx;
  251. }
  252. }
  253. }
  254. </style>