Cart.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <template>
  2. <div class="cart" @click="couponAvailable = false">
  3. <BaseHeader></BaseHeader>
  4. <!-- LOGO 搜索 -->
  5. <div class="width_1200 logo">
  6. <div>
  7. <router-link to="/">
  8. <img style="width: 100px;height: 65px" :src="logoImg" alt="南粤商城" title="南粤商城" />
  9. </router-link>
  10. <div>
  11. 购物车(<span>{{ goodsTotal }}</span>)
  12. </div>
  13. </div>
  14. <Search :showTag="false" :showLogo="false"></Search>
  15. </div>
  16. <Divider />
  17. <!-- 购物车主体 -->
  18. <div class="cart-content width_1200 center">
  19. <div class="available-area">
  20. <div class="cart-steps">
  21. <span :class="stepIndex == 0 ? 'active' : ''">1.我的购物车</span>
  22. <Icon :class="stepIndex == 0 ? 'active-arrow' : ''" custom="icomoon icon-next"></Icon>
  23. <span :class="stepIndex == 1 ? 'active' : ''">2.填写订单信息</span>
  24. <Icon :class="stepIndex == 1 ? 'active-arrow' : ''" custom="icomoon icon-next"></Icon>
  25. <span :class="stepIndex == 2 ? 'active' : ''">3.成功提交订单</span>
  26. </div>
  27. </div>
  28. <!-- 购物车商品列表 -->
  29. <div class="cart-goods">
  30. <div class="cart-goods-title">
  31. <div class="width_60">
  32. <Checkbox v-model="allChecked" @on-change="changeChecked(allChecked, 'all')">全选</Checkbox>
  33. </div>
  34. <div class="goods-title">商品</div>
  35. <div class="width_150">单价(元)</div>
  36. <div class="width_100">数量</div>
  37. <div class="width_150">小计</div>
  38. <div class="width_100">操作</div>
  39. </div>
  40. <div v-if="cartList.length === 0" class="cart-empty">
  41. <p>购物车空空如也</p>
  42. <router-link to="/">去选购&gt;</router-link>
  43. </div>
  44. <div v-else class="cart-goods-items" v-for="(shop, index) in cartList" :key="index">
  45. <div class="shop-name">
  46. <div>
  47. <Checkbox v-model="shop.checked" @on-change="changeChecked(shop.checked, 'shop', shop.storeId)"></Checkbox>
  48. <span class="go-shop-page" @click="goShopPage(shop.storeId)">{{shop.storeName}}</span>
  49. </div>
  50. <span class="shop-coupon" v-if="shop.couponList.length" :class="couponAvailable === index ? 'shop-coupon-show' : ''" @click.stop="showCoupon(shop.id, index)">
  51. <!-- 优惠券模态框 -->
  52. <div v-if="couponAvailable === index">
  53. <div class="coupon-item" v-for="(item, index) in shop.couponList" :key="index">
  54. <span v-if="item.couponType === 'PRICE'">¥{{ item.price }}</span>
  55. <span v-if="item.couponType === 'DISCOUNT'">{{ item.couponDiscount }}折</span>
  56. <span>满{{item.consumeThreshold}}元可用</span>
  57. <Button class="coupon-btn" size="small" type="primary" @click="receiveShopCoupon(item)" :disabled="item.disabled">{{ item.disabled ? "已领取" : "领取" }}</Button>
  58. </div>
  59. </div>
  60. </span>
  61. <div class="promotion-notice">{{shop.promotionNotice}}</div>
  62. </div>
  63. <template v-for="(goods, goodsIndex) in shop.skuList">
  64. <div class="goods-item" :key="goodsIndex">
  65. <div class="width_60">
  66. <Checkbox v-model="goods.checked" @on-change="changeChecked(goods.checked, 'goods', goods.goodsSku.id)"></Checkbox>
  67. </div>
  68. <div class="goods-title" @click="goGoodsDetail(goods.goodsSku.id, goods.goodsSku.goodsId)">
  69. <img :src="
  70. goods.goodsSku.thumbnail || '../assets/images/goodsDetail/item-detail-1.jpg'
  71. " />
  72. <div>
  73. <p>{{ goods.goodsSku.goodsName }}</p>
  74. <template v-for="(promotion, promotionIndex) in goods.promotions">
  75. <div class="promotion" :key="promotionIndex" v-if="promotion.promotionType === 'SECKILL'">
  76. <span>秒杀</span>
  77. <promotion :time="promotion.endTime" type="cart"></promotion>
  78. </div>
  79. </template>
  80. </div>
  81. </div>
  82. <div class="width_150">
  83. {{ goods.goodsSku.price | unitPrice("¥") }}
  84. </div>
  85. <div class="width_100">
  86. <InputNumber :min="1" size="small" v-model="goods.num" @on-change="changeNum(goods.num, goods.goodsSku.id)"></InputNumber>
  87. <div class="fontsize_12">{{goods.goodsSku.quantity > 0 ? '有货' : '无货'}}</div>
  88. </div>
  89. <div class="width_150">
  90. {{ goods.num * goods.goodsSku.price | unitPrice("¥") }}
  91. </div>
  92. <div class="width_100">
  93. <span class="handle-btn" v-if="!goods.errorMessage" @click="delGoods(goods.goodsSku.id)">删除</span>
  94. <span class="handle-btn" v-if="!goods.errorMessage" @click="collectGoods(goods.goodsSku.id)">收藏</span>
  95. </div>
  96. <div class="error-goods" v-if="goods.errorMessage">
  97. <div>{{goods.errorMessage}}</div>
  98. <Button type="primary" @click="delGoods(goods.goodsSku.id)">删除</Button>
  99. </div>
  100. </div>
  101. </template>
  102. </div>
  103. <!-- 底部支付栏 -->
  104. <div class="cart-goods-footer">
  105. <div>
  106. <div class="width_60">
  107. <Checkbox v-model="allChecked" @on-change="changeChecked(allChecked, 'all')">全选</Checkbox>
  108. </div>
  109. <div class="width_100 handle-btn" @click="delGoods">删除选中商品</div>
  110. <!-- <div class="width_100 handle-btn" @click="collectGoods">移到我的收藏</div> -->
  111. <div class="width_100 handle-btn" @click="clearCart">清空购物车</div>
  112. </div>
  113. <div>
  114. <div class="selected-count">
  115. 已选择<span>{{ checkedNum }}</span>件商品
  116. </div>
  117. <div class="ml_20 save-price">
  118. 已节省<span>{{ priceDetailDTO.discountPrice | unitPrice("¥") }}</span>
  119. </div>
  120. <div class="ml_20 total-price">
  121. 总价(不含运费):<div>{{ priceDetailDTO.billPrice | unitPrice("¥") }}</div>
  122. </div>
  123. <div class="pay ml_20" @click="pay">去结算</div>
  124. </div>
  125. </div>
  126. </div>
  127. <Spin size="large" fix v-if="loading"></Spin>
  128. </div>
  129. <!-- 猜你喜欢 -->
  130. <!-- <div class="like">
  131. <div class="likeGoods">
  132. <ShowLikeGoods />
  133. </div>
  134. </div> -->
  135. <BaseFooter></BaseFooter>
  136. </div>
  137. </template>
  138. <script>
  139. import Promotion from "@/components/goodsDetail/Promotion";
  140. import Search from "@/components/Search";
  141. import ShowLikeGoods from "@/components/like";
  142. import * as APICart from "@/api/cart";
  143. import * as APIMember from "@/api/member";
  144. import { getLogo } from "@/api/common.js";
  145. export default {
  146. name: "Cart",
  147. beforeRouteEnter(to, from, next) {
  148. window.scrollTo(0, 0);
  149. next();
  150. },
  151. components: {
  152. Search,
  153. ShowLikeGoods,
  154. Promotion,
  155. },
  156. data() {
  157. return {
  158. logoImg: "", // logo图
  159. couponAvailable: false, // 展示优惠券
  160. stepIndex: 0, // 当前处于哪一步,购物车==0,填写订单信息==1,成功提交订单==2
  161. goodsTotal: 1, // 商品数量
  162. checkedNum: 0, // 选中数量
  163. allChecked: false, // 全选
  164. loading: false, // 加载状态
  165. cartList: [], // 购物车列表
  166. couponList: [], // 优惠券列表
  167. priceDetailDTO: {}, // 价格明细
  168. skuList: [], // sku列表
  169. };
  170. },
  171. computed: {},
  172. methods: {
  173. // 跳转商品详情
  174. goGoodsDetail(skuId, goodsId) {
  175. let routeUrl = this.$router.resolve({
  176. path: "/goodsDetail",
  177. query: { skuId, goodsId },
  178. });
  179. window.open(routeUrl.href, "_blank");
  180. },
  181. // 跳转店铺首页
  182. goShopPage(id) {
  183. let routeUrl = this.$router.resolve({
  184. path: "/Merchant",
  185. query: { id },
  186. });
  187. window.open(routeUrl.href, "_blank");
  188. },
  189. // 收藏商品
  190. collectGoods(id) {
  191. this.$Modal.confirm({
  192. title: "收藏",
  193. content: "<p>商品收藏后可在个人中心我的收藏查看</p>",
  194. onOk: () => {
  195. APIMember.collectGoods("GOODS", id).then((res) => {
  196. if (res.success) {
  197. this.$Message.success("收藏商品成功");
  198. this.getCartList();
  199. }
  200. });
  201. },
  202. onCancel: () => {},
  203. });
  204. },
  205. // 删除商品
  206. delGoods(id) {
  207. const idArr = [];
  208. if (!id) {
  209. const list = this.cartList;
  210. list.forEach((shop) => {
  211. shop.skuList.forEach((goods) => {
  212. idArr.push(goods.goodsSku.id);
  213. });
  214. });
  215. } else {
  216. idArr.push(id);
  217. }
  218. this.$Modal.confirm({
  219. title: "删除",
  220. content: "<p>确定要删除该商品吗?</p>",
  221. onOk: () => {
  222. APICart.delCartGoods({ skuIds: idArr.toString() }).then((res) => {
  223. if (res.success) {
  224. this.$Message.success("删除成功");
  225. this.getCartList();
  226. } else {
  227. this.$Message.error(res.message);
  228. }
  229. });
  230. },
  231. });
  232. },
  233. clearCart() {
  234. // 清空购物车
  235. this.$Modal.confirm({
  236. title: "提示",
  237. content: "<p>确定要清空购物车吗?清空后不可恢复</p>",
  238. onOk: () => {
  239. APICart.clearCart().then((res) => {
  240. if (res.success) {
  241. this.$Message.success("清空购物车成功");
  242. this.getCartList();
  243. } else {
  244. this.$Message.error(res.message);
  245. }
  246. });
  247. },
  248. });
  249. },
  250. // 跳转支付页面
  251. pay() {
  252. if (this.checkedNum) {
  253. this.$router.push({ path: "/pay", query: { way: "CART" } });
  254. } else {
  255. this.$Message.warning("请至少选择一件商品");
  256. }
  257. },
  258. // 展示优惠券
  259. showCoupon(storeId, index) {
  260. this.couponAvailable = index;
  261. },
  262. changeNum(val, id) {
  263. // 设置购买数量
  264. console.log(val, id);
  265. APICart.setCartGoodsNum({ skuId: id, num: val }).then((res) => {
  266. console.log(res);
  267. if (res.success) {
  268. this.getCartList();
  269. }
  270. });
  271. },
  272. async changeChecked(status, type, id) {
  273. // 设置商品选中状态
  274. const check = status ? 1 : 0;
  275. if (type === "all") {
  276. // 全选
  277. await APICart.setCheckedAll({ checked: check });
  278. } else if (type === "shop") {
  279. // 选中店铺所有商品
  280. await APICart.setCheckedSeller({ checked: check, storeId: id });
  281. } else {
  282. // 单个商品
  283. await APICart.setCheckedGoods({ checked: check, skuId: id });
  284. }
  285. this.getCartList();
  286. },
  287. async receiveShopCoupon(item) {
  288. // 领取优惠券
  289. let res = await APIMember.receiveCoupon(item.id);
  290. if (res.success) {
  291. this.$set(item, "disabled", true);
  292. this.$Message.success("领取成功");
  293. } else {
  294. this.$Message.error(res.message);
  295. }
  296. },
  297. async getCartList() {
  298. // 购物车列表
  299. this.loading = true;
  300. try {
  301. let res = await APICart.cartGoodsAll();
  302. this.loading = false;
  303. if (res.success) {
  304. this.cartList = res.result.cartList;
  305. this.priceDetailDTO = res.result.priceDetailDTO;
  306. this.skuList = res.result.skuList;
  307. this.checkedNum = 0;
  308. let allChecked = true;
  309. for (let k = 0; k < this.cartList.length; k++) {
  310. let shop = this.cartList[k];
  311. let list = await APIMember.couponList({ storeId: shop.storeId });
  312. shop.couponList.push(...list.result.records);
  313. }
  314. for (let i = 0; i < this.skuList.length; i++) {
  315. if (this.skuList[i].checked) {
  316. this.checkedNum += this.skuList[i].num;
  317. } else {
  318. allChecked = false;
  319. }
  320. }
  321. this.$forceUpdate();
  322. this.allChecked = allChecked;
  323. }
  324. } catch (error) {
  325. this.loading = false;
  326. }
  327. },
  328. },
  329. mounted() {
  330. this.getCartList();
  331. APICart.cartCount().then((res) => {
  332. // 购物车商品数量
  333. if (res.success) this.goodsTotal = res.result;
  334. });
  335. if (!this.Cookies.getItem("logo")) {
  336. getLogo().then((res) => {
  337. if (res.success) {
  338. let logoObj = JSON.parse(res.result.settingValue);
  339. this.Cookies.setItem("logo", logoObj.buyerSideLogo);
  340. }
  341. });
  342. } else {
  343. this.logoImg = this.Cookies.getItem("logo");
  344. }
  345. },
  346. };
  347. </script>
  348. <style scoped lang="scss">
  349. /** logo 搜索 start **/
  350. .logo {
  351. height: 40px;
  352. display: flex;
  353. justify-content: space-between;
  354. align-items: center;
  355. margin: 20px auto 0;
  356. div:nth-child(1) {
  357. display: flex;
  358. justify-content: space-between;
  359. align-items: center;
  360. img {
  361. width: 150px;
  362. height: auto;
  363. cursor: pointer;
  364. }
  365. div:nth-child(2) {
  366. width: 200px;
  367. color: #999;
  368. font-size: 16px;
  369. margin: 0 20px;
  370. span {
  371. color: $theme_color;
  372. }
  373. }
  374. }
  375. }
  376. .cart-content {
  377. margin: 0 auto;
  378. width: 1200px;
  379. position: relative;
  380. }
  381. /** logo end */
  382. /** step步骤条 */
  383. .cart-steps {
  384. height: 30px;
  385. display: flex;
  386. align-items: center;
  387. margin-bottom: 15px;
  388. span {
  389. @include content_color($light_content_color);
  390. height: 30px;
  391. text-align: center;
  392. line-height: 30px;
  393. display: inline-block;
  394. padding: 0 15px;
  395. }
  396. .ivu-icon {
  397. @include content_color($light_content_color);
  398. font-size: 20px;
  399. margin: 0 15px;
  400. }
  401. .active {
  402. border-radius: 50px;
  403. background-color: #ff8f23;
  404. color: #fff;
  405. }
  406. .active-arrow {
  407. color: #ff8f23;
  408. }
  409. }
  410. /** 步骤条和配送区域总体 */
  411. .available-area {
  412. display: flex;
  413. align-items: center;
  414. justify-content: space-between;
  415. margin-bottom: 15px;
  416. }
  417. /** 商品列表 */
  418. .cart-goods {
  419. &-title {
  420. height: 50px;
  421. @include background_color($light_white_background_color);
  422. @include title_color($title_color);
  423. display: flex;
  424. align-items: center;
  425. padding: 0 20px;
  426. div {
  427. text-align: center;
  428. }
  429. .goods-title {
  430. flex: 1;
  431. }
  432. }
  433. .cart-empty {
  434. width: 100%;
  435. text-align: center;
  436. height: 300px;
  437. padding-top: 100px;
  438. }
  439. &-items {
  440. .shop-name {
  441. height: 50px;
  442. display: flex;
  443. align-items: center;
  444. padding: 0 20px;
  445. position: relative;
  446. @include title_color($light_title_color);
  447. > * {
  448. width: 50%;
  449. }
  450. .go-shop-page:hover {
  451. color: $theme_color;
  452. cursor: pointer;
  453. }
  454. .customer-service {
  455. margin-left: 5px;
  456. color: #fcc217;
  457. cursor: pointer;
  458. &:hover {
  459. color: $theme_color;
  460. }
  461. }
  462. /** 优惠券 */
  463. .shop-coupon {
  464. width: 80px;
  465. height: 24px;
  466. position: relative;
  467. background: url(../assets/images/cart-coupon-icons02.png) 0 0 no-repeat;
  468. > div {
  469. position: absolute;
  470. top: 35px;
  471. left: 0;
  472. width: 300px;
  473. height: 300px;
  474. background-color: #fff;
  475. border: 1px solid $theme_color;
  476. // border-radius: 3px;
  477. z-index: 1;
  478. padding: 10px 20px;
  479. &::before {
  480. content: "";
  481. display: block;
  482. background: url(../assets/images/cart-coupon-icons02.png) 0 -58px no-repeat;
  483. width: 80px;
  484. height: 12px;
  485. position: absolute;
  486. top: -12px;
  487. left: 0;
  488. }
  489. .coupon-item {
  490. margin-bottom: 10px;
  491. span:nth-child(1) {
  492. border: 1px solid #e33937;
  493. display: inline-block;
  494. padding: 3px 10px;
  495. color: $theme_color;
  496. border-radius: 3px;
  497. }
  498. span:nth-child(2) {
  499. font-size: 12px;
  500. margin-left: 5px;
  501. color: #999;
  502. }
  503. .coupon-btn {
  504. height: 26px;
  505. float: right;
  506. font-size: 12px;
  507. }
  508. &::after {
  509. display: block;
  510. content: "";
  511. clear: right;
  512. }
  513. }
  514. }
  515. }
  516. .promotion-notice {
  517. text-align: right;
  518. font-size: 12px;
  519. }
  520. .shop-coupon-show {
  521. background-position-y: -34px;
  522. }
  523. }
  524. .goods-item {
  525. position: relative;
  526. @extend .cart-goods-title;
  527. @include background_color($light_white_background_color);
  528. padding: 10px 20px;
  529. height: auto;
  530. > div:nth-child(1) {
  531. padding-left: 15px;
  532. width: 30px;
  533. }
  534. > div:nth-child(2) {
  535. cursor: pointer;
  536. display: flex;
  537. box-sizing: border-box;
  538. padding-left: 20px;
  539. position: relative;
  540. img {
  541. width: 70px;
  542. height: 70px;
  543. }
  544. > div > p {
  545. @include content_color($light_content_color);
  546. font-size: 13px;
  547. text-align: left;
  548. margin-left: 10px;
  549. &:hover {
  550. color: $theme_color;
  551. }
  552. }
  553. }
  554. > div:nth-child(5) {
  555. font-weight: bold;
  556. }
  557. .num-input {
  558. width: 60px;
  559. border: 1px solid #999;
  560. border-radius: 5px;
  561. padding: 0 5px;
  562. &:focus {
  563. outline-color: $theme_color;
  564. }
  565. }
  566. }
  567. .error-goods {
  568. position: absolute;
  569. width: 100%;
  570. height: 100%;
  571. margin-left: -20px;
  572. background-color: rgba($color: #999, $alpha: 0.5);
  573. z-index: 10;
  574. display: flex;
  575. align-items: center;
  576. justify-content: space-between;
  577. padding-left: 150px;
  578. color: #000;
  579. padding-right: 30px;
  580. }
  581. }
  582. &-footer {
  583. @extend .cart-goods-title;
  584. position: sticky;
  585. bottom: 0;
  586. border-top: 1px solid #ddd;
  587. margin-top: 10px;
  588. padding: 0 0 0 20px;
  589. line-height: 50px;
  590. justify-content: space-between;
  591. > div {
  592. display: flex;
  593. }
  594. .selected-count {
  595. span {
  596. color: $theme_color;
  597. }
  598. }
  599. .save-price span {
  600. color: #000;
  601. }
  602. .total-price div {
  603. color: $theme_color;
  604. font-size: 20px;
  605. }
  606. .pay {
  607. background-color: $theme_color;
  608. width: 150px;
  609. font-size: 20px;
  610. color: #fff;
  611. height: 100%;
  612. line-height: 50px;
  613. cursor: pointer;
  614. }
  615. }
  616. .handle-btn {
  617. font-size: 12px;
  618. color: $handle-btn-color;
  619. cursor: pointer;
  620. &:hover {
  621. color: $theme_color;
  622. }
  623. }
  624. }
  625. .like {
  626. width: 1200px;
  627. margin: 10px auto;
  628. // padding: 20px 0;
  629. @include white_background_color();
  630. }
  631. .likeGoods,
  632. .shop-nav-container {
  633. width: 1200px;
  634. margin: 0 auto;
  635. }
  636. .ivu-divider {
  637. background: $theme_color;
  638. height: 2px;
  639. }
  640. .width_150 {
  641. width: 150px;
  642. }
  643. .width_60 {
  644. width: 60px;
  645. }
  646. .promotion {
  647. display: flex;
  648. margin-top: 5px;
  649. margin-left: 5px;
  650. > span {
  651. border: 1px solid $theme_color;
  652. color: $theme_color;
  653. font-size: 12px;
  654. border-radius: 2px;
  655. padding: 0 2px;
  656. }
  657. > p {
  658. font-size: 12px;
  659. margin-left: 10px;
  660. color: #999;
  661. }
  662. }
  663. .cart-goods-footer > div{
  664. display: flex;
  665. align-items: center;
  666. overflow: hidden;
  667. }
  668. .total-price{
  669. display: flex;
  670. align-items: center;
  671. }
  672. </style>
  673. <style>
  674. .ivu-input-number-input {
  675. text-align: center;
  676. }
  677. </style>