takeout-catgory.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <template>
  2. <view class="catgory-wrap">
  3. <view class="u-flex wrapper-box">
  4. <!-- 左侧分类列表 -->
  5. <scroll-view enable-flex scroll-y scroll-with-animation class="u-tab-view menu-scroll-view"
  6. :scroll-top="scrollLeftTop">
  7. <view v-for="(item, index) in tabbarList" :key="index" class="u-tab-item u-ellipsis-1"
  8. :class="[currentTab == index ? 'u-tab-item-active' : '']" :data-current="index"
  9. @tap.stop="swichMenu(index)">
  10. <text class="menu-name">{{ item.name }}</text>
  11. </view>
  12. <view class="hack-tabbar"></view>
  13. </scroll-view>
  14. <!-- 右侧商品列表 -->
  15. <view class="right-wrap ">
  16. <scroll-view scroll-y class="right-box menu-scroll-view" @scrolltolower="loadMore"
  17. v-if="tabbarList.length">
  18. <view class="u-flex u-row-center" v-if="tabbarList[currentTab].image">
  19. <image class="category-img" :src="tabbarList[currentTab].image" mode="aspectFill"></image>
  20. </view>
  21. <view class="category-title">{{ tabbarList[currentTab].name }}</view>
  22. <view class="sh-big-card-wrap u-m-20 " v-for="(item, index) in goodsList" :key="item.id">
  23. <view class="big-goods-card" @tap="jump('/pages/goods/detail', { id: item.id })">
  24. <view class="img-wrap u-m-b-20">
  25. <image class="goods-img" :src="item.image" mode="aspectFill"></image>
  26. <image class="goods-tag" v-if="item.is_hot && !item.activity"
  27. :src="$IMG_URL + '/goods/goods_hot_tag.png'" mode=""></image>
  28. </view>
  29. <view class="goods-title u-p-x-20 u-m-b-10 u-ellipsis-2">
  30. <view v-if="item.activity_type" class=" sm cu-tag radius title-tag u-m-r-10"
  31. :style="{ backgroundColor: typeMap[item.activity_type].tagBg, color: '#fff' }">
  32. {{ typeMap[item.activity_type].text }}
  33. </view>
  34. {{ item.title }}
  35. </view>
  36. <view class="goods-subtitle u-p-x-20 u-m-b-10 u-ellipsis-1">{{ item.subtitle }}</view>
  37. <!-- 满减,满折 -->
  38. <view class="u-p-x-20" v-if="item.activity_discounts_tags.length">
  39. <view class="tag-box u-m-b-10 u-m-r-10"
  40. v-for="(tag, index1) in item.activity_discounts_tags" :key="index1">{{ tag }}</view>
  41. </view>
  42. <view class="goods-progress u-p-x-20 u-flex u-m-b-10">
  43. <view style="width:230rpx;">
  44. <u-line-progress height="16" :show-percent="false" :percent="Number(item.percent)"
  45. active-color="#e6b873"></u-line-progress>
  46. </view>
  47. <view class="googs-send-num u-m-l-20">已售{{ item.sales }}件</view>
  48. </view>
  49. <view class="u-flex u-row-between u-p-x-20">
  50. <view class="price-box u-flex font-OPPOSANS">
  51. <view class="price">{{ item.price }}</view>
  52. <view class="origin-price u-m-l-10">¥{{ item.original_price }}</view>
  53. </view>
  54. <view class="cart-box">
  55. <!-- 单规格 -->
  56. <view class="" v-if="!item.activity">
  57. <view v-if="!item.is_sku">
  58. <button v-if="!isCart(item.id)"
  59. :style="item.activity_type ? typeMap[item.activity_type].btnBg : ''"
  60. @tap.stop="addCart(item.sku_price[0])" class="u-reset-button cart-btn">
  61. 加入购物车
  62. </button>
  63. <view class="num-step" @tap.stop v-else>
  64. <u-number-box :value="checkCart[item.id].num" :long-press="false"
  65. :min="0" :max="maxStep" :step="1" :index="index"
  66. @min="onMin(item.id)" @plus="plus($event, item.sku_price[0])"
  67. @change="onChangeNum($event, item.sku_price[0])"></u-number-box>
  68. </view>
  69. </view>
  70. <!-- 多规格 -->
  71. <button class="u-reset-button item-btn sel-sku "
  72. :style="item.activity_type ? typeMap[item.activity_type].btnBg : ''"
  73. @tap.stop="selSku(item)" v-else>
  74. 选规格
  75. </button>
  76. </view>
  77. <button class="u-reset-button item-btn sel-sku"
  78. :style="item.activity_type ? typeMap[item.activity_type].btnBg : ''"
  79. v-else>立即抢购</button>
  80. </view>
  81. </view>
  82. </view>
  83. </view>
  84. <!-- 缺省页 -->
  85. <shopro-empty v-if="isEmpty" marginTop="200rpx" :image="$IMG_URL + '/imgs/empty/empty_goods.png'"
  86. tipText="暂无该商品,还有更多好货等着你噢~"></shopro-empty>
  87. <!-- 加载更多 -->
  88. <u-loadmore v-if="goodsList.length" height="80rpx" :status="loadStatus" icon-type="flower"
  89. color="#ccc" />
  90. <view class="hack-tabbar"></view>
  91. </scroll-view>
  92. </view>
  93. </view>
  94. <!-- 规格弹窗 -->
  95. <shopro-sku v-if="showSku && goodsInfo.id" v-model="showSku" :goodsInfo="goodsInfo" :buyType="'cart'">
  96. </shopro-sku>
  97. </view>
  98. </template>
  99. <script>
  100. import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
  101. export default {
  102. components: {},
  103. data() {
  104. return {
  105. showSku: false, //是否显示规格弹窗
  106. isEmpty: false,
  107. currentTab: 0,
  108. categoryID: 0, //分类id
  109. scrollLeftTop: 0, //左边滚动距离
  110. menuHeight: 0, // 左边菜单的高度
  111. menuItemHeight: 0, // 左边菜单item的高度
  112. tabbarList: [], //左侧分类列表
  113. goodsList: [], //商品列表
  114. goodsInfo: {}, //点击商品详情
  115. numberDisabled: false, //购物车计数器
  116. loadStatus: 'loadmore', //loadmore-加载前的状态,loading-加载中的状态,nomore-没有更多的状态
  117. currentPage: 1,
  118. lastPage: 1,
  119. maxStep: 999,
  120. typeMap: {
  121. seckill: {
  122. text: '秒杀',
  123. tagBg: '#FF5854',
  124. goodsBg: '/imgs/tag/seckill_x_bg.png',
  125. btnText: '去抢购',
  126. btnBg: 'background: linear-gradient(90deg, #D01325, #ED3C30);'
  127. },
  128. groupon: {
  129. text: '拼团',
  130. tagBg: '#FE832A',
  131. goodsBg: '/imgs/tag/groupon_x_bg.png',
  132. btnText: '马上拼',
  133. btnBg: 'background: linear-gradient(90deg, #FF6600 0%, #FE832A 100%);'
  134. }
  135. }
  136. };
  137. },
  138. props: {
  139. categoryStyleId: {
  140. type: Number,
  141. default: 0
  142. }
  143. },
  144. computed: {
  145. ...mapGetters(['totalCount', 'isSel', 'cartNum', 'cartList', 'isLogin']),
  146. // 购物车检测
  147. checkCart() {
  148. let obj = {};
  149. this.cartList.forEach(item => {
  150. obj[item.goods_id] = {
  151. num: item.goods_num,
  152. cartOrderId: item.id
  153. };
  154. });
  155. return obj;
  156. }
  157. },
  158. async created() {
  159. console.log('%c当前分类:快速购买', 'color:green;background:yellow');
  160. await this.getCategory();
  161. let res = await this.getGoodsList();
  162. },
  163. methods: {
  164. ...mapActions(['getCartList', 'changeCartList', 'addCartGoods']),
  165. // 跳转详情
  166. toGoodDetail(id) {
  167. this.$Router.push({ path: '/pages/goods/detail', query: { id: id } });
  168. },
  169. // 百分比
  170. getProgress(sales, stock) {
  171. let unit = '';
  172. if (stock + sales > 0) {
  173. let num = (sales / (sales + stock)) * 100;
  174. unit = num.toFixed(2) + '%';
  175. } else {
  176. unit = '0%';
  177. }
  178. return unit;
  179. },
  180. // 加载更多
  181. loadMore() {
  182. if (this.currentPage < this.lastPage) {
  183. this.currentPage += 1;
  184. this.getGoodsList();
  185. }
  186. },
  187. // 获取分类
  188. async getCategory() {
  189. const tabbarData = await this.$http('category.detail', {
  190. id: this.categoryStyleId
  191. });
  192. this.tabbarList = tabbarData.data.children;
  193. this.categoryID = this.tabbarList[0]?.id;
  194. },
  195. // 获取分类商品
  196. getGoodsList() {
  197. let that = this;
  198. that.loadStatus = 'loading';
  199. that.$http(
  200. 'goods.lists', {
  201. category_id: that.categoryID,
  202. page: that.currentPage
  203. },
  204. '加载中...'
  205. )
  206. .then(res => {
  207. if (res.code === 1) {
  208. that.goodsList = [...that.goodsList, ...res.data.data];
  209. that.goodsList.map(item => {
  210. item.percent = item.stock + item.sales > 0 ? ((item.sales / (item.sales + item
  211. .stock)) * 100).toFixed(2) : 0;
  212. });
  213. that.isEmpty = !that.goodsList.length;
  214. that.lastPage = res.data.last_page;
  215. that.loadStatus = that.currentPage < res.data.last_page ? 'loadmore' : 'nomore';
  216. }
  217. })
  218. .then(() => {
  219. that.isLogin && that.getCartList();
  220. });
  221. },
  222. // 点击左边的栏目切换
  223. async swichMenu(index) {
  224. if (index == this.currentTab) return;
  225. this.currentTab = index;
  226. this.categoryID = this.tabbarList[index].id;
  227. this.currentPage = 1;
  228. this.lastPage = 1;
  229. this.goodsList = [];
  230. await this.getGoodsList();
  231. // 如果为0,意味着尚未初始化
  232. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  233. await this.getElRect('menu-scroll-view', 'menuHeight');
  234. await this.getElRect('u-tab-item', 'menuItemHeight');
  235. }
  236. // 将菜单菜单活动item垂直居中
  237. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  238. },
  239. // 获取一个目标元素的高度
  240. getElRect(elClass, dataVal) {
  241. new Promise((resolve, reject) => {
  242. const query = uni.createSelectorQuery().in(this);
  243. query
  244. .select('.' + elClass)
  245. .fields({ size: true }, res => {
  246. // 如果节点尚未生成,res值为null,循环调用执行
  247. if (!res) {
  248. setTimeout(() => {
  249. this.getElRect(elClass);
  250. }, 10);
  251. return;
  252. }
  253. this[dataVal] = res.height;
  254. })
  255. .exec();
  256. });
  257. },
  258. // 加入购物车
  259. addCart(goods) {
  260. let obj = {
  261. goods_id: goods.goods_id,
  262. goods_num: 1,
  263. sku_price_id: goods.id,
  264. goods_price: goods.price
  265. };
  266. let confirmGoodsList = {
  267. list: [obj],
  268. from: 'goods'
  269. };
  270. this.addCartGoods(confirmGoodsList).then(res => {
  271. if (res.code === 1) {
  272. this.$u.toast(res.msg);
  273. }
  274. });
  275. },
  276. // 检测商品在购物车中的下标
  277. checkGoodsIndex(id) {
  278. let cIndex = 0;
  279. this.cartList.forEach((item, index) => {
  280. if (id == item.goods_id) {
  281. cIndex = index;
  282. }
  283. });
  284. return cIndex;
  285. },
  286. // 到达最小值
  287. onMin(goodsId) {
  288. const that = this;
  289. let cartGoodId = 0;
  290. cartGoodId = this.cartList.filter(item => item.goods_id === goodsId)[0].id;
  291. uni.showModal({
  292. title: '删除提示',
  293. confirmColor: '#f0c785',
  294. content: `是否确认从购物车中删除此商品?`,
  295. success: res => {
  296. res.confirm && this.changeCartList({ ids: [cartGoodId], art: 'delete' });
  297. }
  298. });
  299. },
  300. // 增加
  301. plus(e, sku) {
  302. if (e.value >= sku.stock) {
  303. this.maxStep = sku.stock > 999 ? 999 : sku.stock;
  304. this.$u.toast('库存不足');
  305. return;
  306. }
  307. if (this.detail.activity_type === 'seckill' || this.detail.activity_type === 'groupon') {
  308. let rules = this.detail.activity.rules;
  309. if (rules.limit_buy != 0 && e.value >= rules.limit_buy) {
  310. this.maxStep = rules.limit_buy;
  311. this.$u.toast('本次活动最多购买' + rules.limit_buy + '件');
  312. return;
  313. }
  314. }
  315. },
  316. // 更改商品数
  317. // 更改商品数
  318. async onChangeNum(e, sku) {
  319. let gIndex = this.checkGoodsIndex(sku.goods_id);
  320. if (e.value != this.checkCart[sku.goods_id].num) {
  321. uni.showLoading({
  322. mask: true
  323. });
  324. this.$set(this.cartList[gIndex], 'goods_num', +e.value);
  325. await this.changeCartList({
  326. ids: [this.checkCart[sku.goods_id].cartOrderId],
  327. goodsNum: +e.value,
  328. art: 'change'
  329. });
  330. await uni.hideLoading();
  331. }
  332. },
  333. // 检测是否为购物车商品
  334. isCart(id) {
  335. let goodsId = id + '';
  336. return Object.keys(this.checkCart).includes(goodsId);
  337. },
  338. // 添加购物车
  339. async selSku(info) {
  340. this.goodsInfo = {};
  341. this.getGoodsDetail(info.id);
  342. this.showSku = true;
  343. },
  344. // 商品详情
  345. getGoodsDetail(id) {
  346. let that = this;
  347. that.$http('goods.detail', {
  348. id: id
  349. }).then(res => {
  350. if (res.code === 1) {
  351. that.goodsInfo = res.data;
  352. }
  353. });
  354. },
  355. // 路由跳转
  356. jump(path, parmas) {
  357. this.$Router.push({
  358. path: path,
  359. query: parmas
  360. });
  361. }
  362. }
  363. };
  364. </script>
  365. <style lang="scss" scoped>
  366. .hack-tabbar {
  367. height: calc(100rpx + env(safe-area-inset-bottom) / 2);
  368. width: 100%;
  369. }
  370. // 最外层结构包裹
  371. .catgory-wrap {
  372. height: calc(100vh);
  373. /* #ifdef H5 */
  374. height: calc(100vh - var(--window-top));
  375. /* #endif */
  376. display: flex;
  377. flex-direction: column;
  378. }
  379. .wrapper-box {
  380. flex: 1;
  381. display: flex;
  382. overflow: hidden;
  383. // 左侧menu
  384. .u-tab-view {
  385. width: 200rpx;
  386. height: 100%;
  387. .u-tab-item {
  388. height: 84rpx;
  389. background: #f6f6f6;
  390. width: 200rpx;
  391. padding-left: 16rpx;
  392. font-size: 26rpx;
  393. color: #444;
  394. font-weight: 400;
  395. line-height: 84rpx;
  396. }
  397. .u-tab-item-active {
  398. position: relative;
  399. color: #000;
  400. font-size: 30rpx;
  401. font-weight: bold;
  402. background: #fff;
  403. }
  404. .u-tab-item-active::before {
  405. content: '';
  406. position: absolute;
  407. border-left: 4px solid #e6b873;
  408. height: 32rpx;
  409. left: 0;
  410. top: 26rpx;
  411. }
  412. }
  413. }
  414. // 右侧商品滚动
  415. .right-wrap {
  416. background-color: #fff;
  417. height: 100%;
  418. width: 100%;
  419. .category-img {
  420. width: 505rpx;
  421. height: 150rpx;
  422. background: #ccc;
  423. margin: 20rpx auto;
  424. border-radius: 10rpx;
  425. }
  426. .category-title {
  427. font-size: 30rpx;
  428. font-weight: bold;
  429. color: #000;
  430. margin: 20rpx;
  431. }
  432. .right-box {
  433. height: 100%;
  434. }
  435. }
  436. .big-goods-card {
  437. background-color: #fff;
  438. border-radius: 20rpx;
  439. width: 100%;
  440. box-shadow: 0 0 8rpx 2rpx rgba(199, 199, 199, 0.3);
  441. padding-bottom: 20rpx;
  442. .img-wrap {
  443. position: relative;
  444. overflow: hidden;
  445. height: 256rpx;
  446. width: 100%;
  447. .goods-img {
  448. width: 100%;
  449. height: 256rpx;
  450. border-radius: 20rpx 20rpx 0 0;
  451. background: #f5f5f5;
  452. }
  453. .goods-tag {
  454. position: absolute;
  455. top: 16rpx;
  456. left: 16rpx;
  457. width: 88rpx;
  458. height: 88rpx;
  459. }
  460. }
  461. .goods-title {
  462. font-size: 30rpx;
  463. font-weight: bold;
  464. color: #333333;
  465. line-height: 36rpx;
  466. padding-top: 6rpx;
  467. .title-tag {
  468. transform: scale(0.9);
  469. position: relative;
  470. top: -6rpx;
  471. }
  472. }
  473. .goods-subtitle {
  474. font-size: 24rpx;
  475. font-weight: 400;
  476. color: #999999;
  477. line-height: 36rpx;
  478. width: 480rpx;
  479. }
  480. .tag-box {
  481. border: 1rpx solid #ff0000;
  482. display: inline-block;
  483. font-size: 20rpx;
  484. line-height: 30rpx;
  485. padding: 0 10rpx;
  486. color: #ff0000;
  487. border-radius: 8rpx;
  488. }
  489. .goods-progress {
  490. .cu-progress {
  491. width: 200rpx;
  492. height: 20rpx;
  493. background: rgba(#ff6361, 0.2);
  494. overflow: visible;
  495. position: relative;
  496. .progress--ing {
  497. background: #ff6361;
  498. border-radius: 20rpx;
  499. }
  500. .round-tag {
  501. position: absolute;
  502. bottom: 0;
  503. width: 20rpx;
  504. height: 28rpx;
  505. margin-left: -10rpx;
  506. .round-tag-img {
  507. width: 20rpx;
  508. height: 28rpx;
  509. }
  510. }
  511. }
  512. .googs-send-num {
  513. font-size: 24rpx;
  514. font-weight: 400;
  515. color: #bebebe;
  516. }
  517. }
  518. .price-box {
  519. vertical-align: text-bottom;
  520. .price {
  521. font-size: 36rpx;
  522. color: #ff0000;
  523. font-weight: 600;
  524. &::before {
  525. content: '¥';
  526. font-size: 28rpx;
  527. }
  528. vertical-align: text-bottom;
  529. }
  530. .origin-price {
  531. vertical-align: text-bottom;
  532. font-size: 26rpx;
  533. font-weight: 400;
  534. text-decoration: line-through;
  535. color: #c4c4c4;
  536. }
  537. }
  538. .cart-box {
  539. .cart-btn,
  540. .sel-sku {
  541. width: 172rpx;
  542. line-height: 58rpx;
  543. background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
  544. box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  545. border-radius: 30rpx;
  546. padding: 0;
  547. font-size: 26rpx;
  548. font-weight: 500;
  549. color: #ffffff;
  550. }
  551. }
  552. }
  553. </style>