category.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <!-- 商品分类列表 -->
  2. <template>
  3. <s-layout title="分类" tabbar="/pages/index/category" :bgStyle="{ color: '#fff' }">
  4. <view style="background-color: #fff;display: flex;justify-content: flex-end;">
  5. <view style="background-color:#fff;width: 200rpx;">
  6. <uni-search-bar v-if="!navbar" class="ss-flex-1" radius="40" placeholder="请输入" cancelButton="none"
  7. clearButton="none" @confirm="onSearch" v-model="state.searchVal" />
  8. </view>
  9. </view>
  10. <view class="s-category">
  11. <view class="three-level-wrap ss-flex ss-col-top" :style="[{ height: pageHeight + 'px' }]">
  12. <!-- 商品分类(左) -->
  13. <scroll-view class="side-menu-wrap" scroll-y :style="[{ height: pageHeight + 'px' }]">
  14. <view class="menu-item ss-flex" v-for="(item, index) in state.categoryList" :key="item.id"
  15. :class="[{ 'menu-item-active': index === state.activeMenu }]" @tap="onMenu(index)">
  16. <view class="menu-title ss-line-1">
  17. {{ item.name }}
  18. </view>
  19. </view>
  20. </scroll-view>
  21. <!-- 商品分类(右) -->
  22. <scroll-view class="goods-list-box" scroll-y :style="[{ height: pageHeight + 'px' }]"
  23. v-if="state.pagination.list?.length">
  24. <view v-if=" state.pagination.total > 0" class="goods-list ss-m-t-20">
  25. <view class="ss-p-l-20 ss-p-r-20 ss-m-b-20" v-for="(item,index) in state.pagination.list"
  26. :key="item.id">
  27. <s-goods-column class="" size="lg" :data="item" :index="index" :topRadius="10" :bottomRadius="10"
  28. @click="sheep.$router.go('/pages/goods/index', { id: item.id })" />
  29. </view>
  30. </view>
  31. <!-- <image
  32. v-if="state.categoryList[state.activeMenu].picUrl"
  33. class="banner-img"
  34. :src="sheep.$url.cdn(state.categoryList[state.activeMenu].picUrl)"
  35. mode="widthFix"
  36. />
  37. <first-one v-if="state.style === 'first_one'" :pagination="state.pagination" />
  38. <first-two v-if="state.style === 'first_two'" :pagination="state.pagination" />
  39. <second-one
  40. v-if="state.style === 'second_one'"
  41. :data="state.categoryList"
  42. :activeMenu="state.activeMenu"
  43. />
  44. <uni-load-more
  45. v-if="
  46. (state.style === 'first_one' || state.style === 'first_two') &&
  47. state.pagination.total > 0
  48. "
  49. :status="state.loadStatus"
  50. :content-text="{
  51. contentdown: '点击查看更多',
  52. }"
  53. @tap="loadMore"
  54. /> -->
  55. </scroll-view>
  56. </view>
  57. <!-- v-if="state.list.length > 0" -->
  58. </view>
  59. <su-fixed bottom :val="48" placeholder :isInset="false">
  60. <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom" @click="goCartList">
  61. <view class="footer-left ss-flex ss-col-center">
  62. <!-- <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll">
  63. <radio :checked="state.isAllSelected" color="var(--ui-BG-Main)"
  64. style="transform: scale(0.8)" @tap.stop="onSelectAll" />
  65. <view class="ss-m-l-8"> 全选 </view>
  66. </label> -->
  67. <text>合计:</text>
  68. <view class="text-price price-text">
  69. {{ state.totalPriceSelected?fen2yuan(state.totalPriceSelected):0 }}
  70. </view>
  71. </view>
  72. <view class="footer-right">
  73. <button v-if="state.editMode" class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
  74. @tap="onDelete">
  75. 删除
  76. </button>
  77. <button v-else class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main" @tap.stop="onConfirm">
  78. 去结算
  79. {{ state.selectedIds?.length ? `(${state.selectedIds.length})` : '' }}
  80. </button>
  81. </view>
  82. </view>
  83. </su-fixed>
  84. </s-layout>
  85. </template>
  86. <script setup>
  87. import secondOne from './components/second-one.vue';
  88. import firstOne from './components/first-one.vue';
  89. import firstTwo from './components/first-two.vue';
  90. import hxNumberBox from './components/uni-number-box';
  91. import sGoodsColumn from './components/s-goods-column.vue';
  92. import sheep from '@/sheep';
  93. import CategoryApi from '@/sheep/api/product/category';
  94. import SpuApi from '@/sheep/api/product/spu';
  95. import {
  96. onLoad,
  97. onReachBottom
  98. } from '@dcloudio/uni-app';
  99. import {
  100. computed,
  101. reactive
  102. } from 'vue';
  103. import _ from 'lodash';
  104. import {
  105. handleTree
  106. } from '@/sheep/util';
  107. import {
  108. fen2yuan
  109. } from '../../sheep/hooks/useGoods';
  110. // uni.hideTabBar();
  111. const cart = sheep.$store('cart');
  112. const state = reactive({
  113. style: 'second_one', // first_one(一级 - 样式一), first_two(二级 - 样式二), second_one(二级)
  114. categoryList: [], // 商品分类树
  115. activeMenu: 0, // 选中的一级菜单,在 categoryList 的下标
  116. pagination: {
  117. // 商品分页
  118. list: [], // 商品列表
  119. total: [], // 商品总数
  120. pageNo: 1,
  121. pageSize: 6,
  122. },
  123. loadStatus: '',
  124. categoryId: 0,
  125. keyword: '',
  126. list: computed(() => cart.list),
  127. selectedIds: computed(() => cart.selectedIds),
  128. totalPriceSelected: computed(() => cart.totalPriceSelected),
  129. });
  130. const {
  131. safeArea
  132. } = sheep.$platform.device;
  133. const pageHeight = computed(() => safeArea.height - 44 - 50);
  134. // 加载商品分类
  135. async function getList() {
  136. const {
  137. code,
  138. data
  139. } = await CategoryApi.getCategoryList();
  140. if (code !== 0) {
  141. return;
  142. }
  143. state.categoryList = handleTree(data);
  144. console.log(state.categoryList[state.activeMenu], 'state.categoryList[state.activeMenu]')
  145. const res = await SpuApi.getSpuPage({
  146. categoryId: state.categoryList[state.activeMenu].id,
  147. pageNo: state.pagination.pageNo,
  148. pageSize: state.pagination.pageSize,
  149. });
  150. state.pagination.list = _.concat(state.pagination.list, res.data.list)
  151. state.pagination.total = res.data.total;
  152. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  153. }
  154. // 选中菜单
  155. const onMenu = (val) => {
  156. console.log(val, 'valllll')
  157. state.activeMenu = val;
  158. console.log(state.style, 'state.style')
  159. // if (state.style === 'first_one' || state.style === 'first_two') {
  160. // state.pagination.pageNo = 1;
  161. // state.pagination.list = [];
  162. // state.pagination.total = 0;
  163. getGoodsList();
  164. // }
  165. };
  166. // 结算
  167. function onConfirm() {
  168. let items = []
  169. let goods_list = [];
  170. state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
  171. state.selectedList.map((item) => {
  172. // 此处前端做出修改
  173. items.push({
  174. skuId: item.sku.id,
  175. count: item.count,
  176. cartId: item.id,
  177. })
  178. goods_list.push({
  179. // goods_id: item.goods_id,
  180. goods_id: item.spu.id,
  181. // goods_num: item.goods_num,
  182. goods_num: item.count,
  183. // 商品价格id真没有
  184. // goods_sku_price_id: item.goods_sku_price_id,
  185. });
  186. });
  187. // return;
  188. if (goods_list.length === 0) {
  189. sheep.$helper.toast('请选择商品');
  190. return;
  191. }
  192. sheep.$router.go('/pages/order/confirm', {
  193. data: JSON.stringify({
  194. // order_type: 'goods',
  195. // goods_list,
  196. items,
  197. // from: 'cart',
  198. deliveryType: 1,
  199. pointStatus: false,
  200. }),
  201. });
  202. }
  203. // 加载商品列表
  204. async function getGoodsList() {
  205. // 加载列表
  206. state.loadStatus = 'loading';
  207. const res = await SpuApi.getSpuPage({
  208. categoryId: state.categoryList[state.activeMenu].id,
  209. pageNo: state.pagination.pageNo,
  210. pageSize: state.pagination.pageSize,
  211. });
  212. if (res.code !== 0) {
  213. return;
  214. }
  215. // 合并列表
  216. // state.pagination.list = _.concat(state.pagination.list, res.data.list);
  217. state.pagination.list = res.data.list
  218. state.pagination.total = res.data.total;
  219. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  220. }
  221. function goCartList(){
  222. uni.navigateTo({
  223. url:'/pages/index/cart'
  224. });
  225. }
  226. // 加载更多商品
  227. function loadMore() {
  228. if (state.loadStatus === 'noMore') {
  229. return;
  230. }
  231. state.pagination.pageNo++;
  232. getGoodsList();
  233. }
  234. onLoad(async () => {
  235. await getList();
  236. // const id=state.categoryList[state.activeMenu].id
  237. // state.categoryId = id;
  238. // state.keyword = options.keyword;
  239. console.log(state.categoryList[state.activeMenu], 'state.categoryList[state.activeMenu]')
  240. // 如果是 first 风格,需要加载商品分页
  241. if (state.style === 'first_one' || state.style === 'first_two') {
  242. onMenu(0);
  243. }
  244. });
  245. onReachBottom(() => {
  246. loadMore();
  247. });
  248. </script>
  249. <style lang="scss" scoped>
  250. .s-category {
  251. :deep() {
  252. .side-menu-wrap {
  253. width: 200rpx;
  254. height: 100%;
  255. padding-left: 12rpx;
  256. background-color: #f6f6f6;
  257. .menu-item {
  258. width: 100%;
  259. height: 88rpx;
  260. position: relative;
  261. transition: all linear 0.2s;
  262. .menu-title {
  263. line-height: 32rpx;
  264. font-size: 30rpx;
  265. font-weight: 400;
  266. color: #333;
  267. margin-left: 28rpx;
  268. position: relative;
  269. z-index: 0;
  270. &::before {
  271. content: '';
  272. width: 64rpx;
  273. height: 12rpx;
  274. background: linear-gradient(90deg,
  275. var(--ui-BG-Main-gradient),
  276. var(--ui-BG-Main-light)) !important;
  277. position: absolute;
  278. left: -64rpx;
  279. bottom: 0;
  280. z-index: -1;
  281. transition: all linear 0.2s;
  282. }
  283. }
  284. &.menu-item-active {
  285. background-color: #fff;
  286. border-radius: 20rpx 0 0 20rpx;
  287. &::before {
  288. content: '';
  289. position: absolute;
  290. right: 0;
  291. bottom: -20rpx;
  292. width: 20rpx;
  293. height: 20rpx;
  294. background: radial-gradient(circle at 0 100%, transparent 20rpx, #fff 0);
  295. }
  296. &::after {
  297. content: '';
  298. position: absolute;
  299. top: -20rpx;
  300. right: 0;
  301. width: 20rpx;
  302. height: 20rpx;
  303. background: radial-gradient(circle at 0% 0%, transparent 20rpx, #fff 0);
  304. }
  305. .menu-title {
  306. font-weight: 600;
  307. &::before {
  308. left: 0;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. .goods-list-box {
  315. background-color: #fff;
  316. width: calc(100vw - 100px);
  317. padding: 10px;
  318. }
  319. .banner-img {
  320. width: calc(100vw - 130px);
  321. border-radius: 5px;
  322. margin-bottom: 20rpx;
  323. }
  324. }
  325. }
  326. .cart-footer {
  327. height: 100rpx;
  328. color: #fff;
  329. background-color: #313131;
  330. border-radius: 49rpx;
  331. .pay-btn {
  332. width: 180rpx;
  333. height: 70rpx;
  334. font-size: 28rpx;
  335. line-height: 28rpx;
  336. font-weight: 500;
  337. border-radius: 40rpx;
  338. }
  339. }
  340. </style>