shoppingCart.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <template>
  2. <view class="container">
  3. <!-- 有数据 -->
  4. <uv-sticky v-if="shopList.length > 0">
  5. <view class="topStatus u-flex-center-sb u-plr30">
  6. <text class="u-1A1A1A u-font30">共{{ totalNum }}件商品</text>
  7. <text class="u-1A1A1A u-font30" @click="editorBtn">{{
  8. editorStatus ? "完成" : "编辑"
  9. }}</text>
  10. </view>
  11. </uv-sticky>
  12. <view v-if="shopList.length > 0">
  13. <view class="carbox">
  14. <view
  15. class="shopList u-mt20"
  16. v-for="(item, index) in shopList"
  17. :key="item.businessId"
  18. >
  19. <view class="u-flex-center-sb">
  20. <view class="u-flex-center">
  21. <view class="check u-mr10" @click="shopChecked(index)">
  22. <uv-icon
  23. name="checkmark-circle-fill"
  24. color="#eb5153"
  25. size="36rpx"
  26. v-if="item.flag"
  27. ></uv-icon>
  28. <view class="radios" v-else></view>
  29. </view>
  30. <text class="iconfont u-bold u-ml10" style="color: #eb5153"
  31. >&#xe71b;</text
  32. >
  33. <text class="u-1A1A1A u-font30 u-ml15 u-text1 name-width">{{
  34. item.businessName
  35. }}</text>
  36. <text class="u-CCC right-icon iconfont">&#xe6c7;</text>
  37. </view>
  38. </view>
  39. <view
  40. class="goodsItem u-border-one-one u-flex-center"
  41. v-for="children in item.userShoppingCartList"
  42. :key="children.id"
  43. >
  44. <view
  45. class="check u-mr10"
  46. @click.stop="goodsChecked(index, children.id)"
  47. >
  48. <uv-icon
  49. name="checkmark-circle-fill"
  50. color="#eb5153"
  51. size="36rpx"
  52. v-if="children.flag"
  53. ></uv-icon>
  54. <view class="radios" v-else></view>
  55. </view>
  56. <view class="u-goods200 u-ml15 positionRetave">
  57. <image
  58. :src="`${children.productCover}?x-oss-process=style/w_150`"
  59. class="u-goods200"
  60. mode="scaleToFill"
  61. >
  62. </image>
  63. </view>
  64. <view class="u-flex1 right-box rightText u-ml30">
  65. <view class="u-text2 u-1A1A1A u-font28">
  66. <text>{{ children.productTitle }}</text>
  67. </view>
  68. <view class="u-flex-column-start">
  69. <text class="u-font22 u-999" v-if="children.skuSetName"
  70. >规格: {{ children.skuSetName }}</text
  71. >
  72. <view class="u-flex-center-sb botPrice">
  73. <view class="u-FF0000 u-font32 u-flex1">
  74. <rich-text
  75. :nodes="$mUtil.priceBigSmall(children.salePrice)"
  76. ></rich-text>
  77. </view>
  78. <view class="rightAddRes u-flex-center">
  79. <text
  80. class="btnTag"
  81. @click.stop="resNum(children, children.id)"
  82. >-</text
  83. >
  84. <text class="u-flex1 centertext u-font28 u-999">{{
  85. children.num
  86. }}</text>
  87. <text
  88. class="btnTag"
  89. @click.stop="addNum(children, children.id)"
  90. >+</text
  91. >
  92. </view>
  93. </view>
  94. </view>
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. <!-- 底部 -->
  100. <view v-if="ispay" class="botfixed u-flex-center-sb">
  101. <view class="leftAll u-flex-center" @click="allChecked">
  102. <view class="check u-mr10">
  103. <!-- <text class="iconfont iconbox u-font32 u-FF0000" v-if="checkAggrement">&#xe646;</text> -->
  104. <uv-icon
  105. name="checkmark-circle-fill"
  106. color="#eb5153"
  107. size="36rpx"
  108. v-if="checkAggrement"
  109. ></uv-icon>
  110. <view class="radios" v-else></view>
  111. </view>
  112. <text class="u-ml5 u-font24 u-999">{{
  113. checkAggrement ? "反选" : "全选"
  114. }}</text>
  115. </view>
  116. <view class="rightP u-flex-center">
  117. <view class="u-flex-center u-mr10" v-if="!editorStatus">
  118. <text class="u-font30 u-333 u-bold">合计:</text>
  119. <view class="u-FF0000 u-bold">
  120. <rich-text :nodes="$mUtil.priceBigSmall(totalPrice)"></rich-text>
  121. </view>
  122. </view>
  123. <view
  124. class="u-btn-two btnWidth u-ml30"
  125. v-if="!editorStatus"
  126. @click="surePay"
  127. >去结算({{ checkNum }})</view
  128. >
  129. <view class="u-btn-two btnWidth u-ml30" v-else @click="delCart"
  130. >删除</view
  131. >
  132. </view>
  133. </view>
  134. </view>
  135. <!-- 没有数据 -->
  136. <view
  137. class="nogoods u-mt30 u-flex-column-center"
  138. v-if="shopList.length == 0"
  139. >
  140. <image
  141. src="/static/image/common/noGoods.png"
  142. mode=""
  143. class="noImg"
  144. ></image>
  145. <text class="u-font26 u-999">您的购物车空空如也~快快去逛逛吧!~</text>
  146. <view class="btnview" @click="gotoshop">
  147. <button class="u-DCCDA4 shop-btn u-font30">
  148. {{ isLogin ? "去逛逛" : "去登录" }}
  149. </button>
  150. </view>
  151. </view>
  152. <!-- 猜你喜欢商品 -->
  153. <CartGuessYouLike />
  154. <!--页面加载动画-->
  155. <ldLoading isFullScreen :active="loading"></ldLoading>
  156. </view>
  157. </template>
  158. <script setup>
  159. import { ref } from "vue";
  160. import { onShow, onLoad } from "@dcloudio/uni-app";
  161. import {
  162. userShoppingCartList_Api,
  163. userShoppingCartAdd_Api,
  164. userShoppingCartDel_Api,
  165. } from "@/api/shop";
  166. import CartGuessYouLike from "./components/CartGuessYouLike.vue";
  167. import utils from "@/util/index.js";
  168. const totalPrice = ref(0);
  169. const totalNum = ref(0);
  170. const checkNum = ref(0);
  171. const checkAggrement = ref(false);
  172. const loading = ref(false);
  173. const shopList = ref([]);
  174. const arrLenid = ref([]);
  175. const editorStatus = ref(false);
  176. const isLogin = ref(false);
  177. const ispay = ref(true);
  178. const gotoshop = () => {
  179. if (!utils.isLoginTo(true)) {
  180. uni.switchTab({
  181. url: "/pages/tabtar/home",
  182. });
  183. }
  184. };
  185. const shopChecked = (index) => {
  186. shopList.value[index].flag = !shopList.value[index].flag;
  187. shopList.value[index].userShoppingCartList.forEach((j) => {
  188. if (shopList.value[index].flag) {
  189. j.flag = true;
  190. } else {
  191. j.flag = false;
  192. }
  193. });
  194. checkTotalNum();
  195. };
  196. const goodsChecked = (index, id) => {
  197. shopList.value[index].userShoppingCartList.forEach((j) => {
  198. if (j.id == id) {
  199. j.flag = !j.flag;
  200. }
  201. });
  202. shopHeightLaign(index);
  203. checkTotalNum();
  204. };
  205. const allChecked = () => {
  206. checkAggrement.value = !checkAggrement.value;
  207. shopList.value.forEach((k) => {
  208. k.flag = checkAggrement.value;
  209. k.userShoppingCartList.forEach((j) => {
  210. j.flag = checkAggrement.value;
  211. });
  212. });
  213. checkTotalNum();
  214. };
  215. const setGoodsNum = (item, type) => {
  216. // console.log("setGoodsNum", item, type);
  217. let obj = {
  218. isSet: true,
  219. productId: item.productId,
  220. skuHashCode: item.skuHashCode,
  221. skuSetName: item.skuSetName,
  222. num: item.num || 1,
  223. // effectiveStatus: 1,
  224. businessId: item.businessId,
  225. };
  226. // return;
  227. // console.log("obj", obj);
  228. userShoppingCartAdd_Api(obj)
  229. .then((res) => {
  230. if (res && res.code == 200) {
  231. checkTotalNum();
  232. }
  233. })
  234. .catch((err) => {
  235. item.num = type == "add" ? item.num - 1 : item.num + 1;
  236. });
  237. };
  238. const resNum = (item, id) => {
  239. let show = false;
  240. shopList.value.forEach((k) => {
  241. k.userShoppingCartList.forEach((j) => {
  242. if (j.id == id) {
  243. if (j.num > 1) {
  244. j.num--;
  245. } else {
  246. // uni.$uv.toast("不能再减了~");
  247. show = true;
  248. userShoppingCartDel_Api(item.id).then((res) => {
  249. if (res && res.code == 200) {
  250. uni.$uv.toast("删除成功");
  251. rest();
  252. cartList();
  253. }
  254. });
  255. }
  256. }
  257. });
  258. });
  259. if (show) return;
  260. setGoodsNum(item, "subtract");
  261. };
  262. const addNum = (item, id) => {
  263. shopList.value.forEach((k) => {
  264. k.userShoppingCartList.forEach((j) => {
  265. if (j.id == id) {
  266. if (j.num >= j.limit_buy_num && j.limit_buy_num > 0) {
  267. uni.$uv.toast("商品库存只有" + j.limit_buy_num + "件");
  268. } else {
  269. j.num++;
  270. }
  271. }
  272. });
  273. });
  274. setGoodsNum(item, "add");
  275. };
  276. const editorBtn = () => {
  277. console.log("editorBtn");
  278. editorStatus.value = !editorStatus.value;
  279. };
  280. const checkTotalNum = () => {
  281. checkNum.value = 0;
  282. let arrLenids = [];
  283. let total = 0;
  284. shopList.value.forEach((k) => {
  285. k.userShoppingCartList.forEach((j) => {
  286. if (j.flag) {
  287. arrLenids.push(j.id);
  288. total += j.salePrice * j.num;
  289. }
  290. });
  291. });
  292. totalPrice.value = total.toFixed(2);
  293. arrLenid.value = arrLenids;
  294. checkNum.value = arrLenids.length;
  295. if (checkNum.value == totalNum.value) {
  296. checkAggrement.value = true;
  297. } else {
  298. checkAggrement.value = false;
  299. }
  300. };
  301. const shopHeightLaign = (index) => {
  302. let arr = [];
  303. shopList.value[index].userShoppingCartList.forEach((j) => {
  304. arr.push(j.flag);
  305. });
  306. if (arr.indexOf(false) == -1) {
  307. shopList.value[index].flag = true;
  308. } else {
  309. shopList.value[index].flag = false;
  310. }
  311. };
  312. const getIsHaveOnline = () => {
  313. return new Promise((resolve, reject) => {
  314. let arr = [];
  315. for (let item of shopList.value) {
  316. for (let el of item.userShoppingCartList) {
  317. for (let i of arrLenid.value) {
  318. if (i == el.id) {
  319. arr.push(el.businessId);
  320. }
  321. }
  322. }
  323. }
  324. let value = arr.every((item) => {
  325. return item == arr[0];
  326. });
  327. resolve(value);
  328. });
  329. };
  330. const surePay = async () => {
  331. if (arrLenid.value.length > 0) {
  332. let isBoolean = await getIsHaveOnline();
  333. if (!isBoolean) return uni.$uv.toast("不支持多店铺商品一起下单");
  334. let flagKeys = [];
  335. shopList.value.map((res, i) => {
  336. res.userShoppingCartList.map((keys, j) => {
  337. if (keys.flag) {
  338. if (!keys.skuValid) {
  339. flagKeys.push("false");
  340. } else {
  341. flagKeys.push("true");
  342. }
  343. }
  344. });
  345. });
  346. if (flagKeys.indexOf("false") == -1) {
  347. psotJson();
  348. } else {
  349. uni.$uv.toast("已失效的商品不能结算");
  350. }
  351. } else {
  352. uni.$uv.toast("请选择需要购买的商品");
  353. }
  354. };
  355. const delCart = () => {
  356. if (arrLenid.value.length > 0) {
  357. uni.showModal({
  358. title: "提示",
  359. content: "确定删除选中的商品?",
  360. success: function (e) {
  361. if (e.confirm) {
  362. userShoppingCartDel_Api(arrLenid.value.toString()).then((res) => {
  363. if (res && res.code == 200) {
  364. uni.$uv.toast("删除成功");
  365. rest();
  366. cartList();
  367. }
  368. });
  369. }
  370. },
  371. });
  372. } else {
  373. uni.$uv.toast("请选择需要删除的商品");
  374. }
  375. };
  376. const rest = () => {
  377. arrLenid.value = [];
  378. totalPrice.value = 0;
  379. checkNum.value = 0;
  380. checkAggrement.value = false;
  381. };
  382. const cartList = () => {
  383. loading.value = true;
  384. userShoppingCartList_Api()
  385. .then((res) => {
  386. loading.value = false;
  387. if (res && res.code == 200) {
  388. if (res.data.length > 0) {
  389. let nums = 0;
  390. let shopLists = [...shopList.value];
  391. let list = [];
  392. res.data.forEach((k) => {
  393. k.flag = false;
  394. nums += k.userShoppingCartList.length;
  395. let objs =
  396. shopLists.find((item) => item.businessId == k.businessId) || {};
  397. k.userShoppingCartList.forEach((j) => {
  398. let obj = objs.userShoppingCartList
  399. ? objs.userShoppingCartList.find((item) => item.id == j.id) ||
  400. {}
  401. : {};
  402. if (obj.flag) {
  403. j.flag = true;
  404. } else {
  405. j.flag = false;
  406. }
  407. });
  408. if (k.userShoppingCartList.length) {
  409. list.push(k);
  410. }
  411. });
  412. totalNum.value = nums;
  413. shopList.value = [...list];
  414. checkAggrement.value = false;
  415. } else {
  416. shopList.value = [];
  417. }
  418. }
  419. })
  420. .catch((err) => {
  421. loading.value = false;
  422. });
  423. };
  424. const psotJson = () => {
  425. let parendArr = [];
  426. // let childArr = [[], [], [], [], [], [], [], [], []];
  427. let dataJson = {};
  428. let businessId = "";
  429. let num = 0;
  430. shopList.value.map((res, i) => {
  431. res.userShoppingCartList.map((keys, j) => {
  432. if (keys.flag) {
  433. if (businessId == "") {
  434. businessId = res.businessId;
  435. } else {
  436. if (businessId != res.businessId) {
  437. num++;
  438. }
  439. }
  440. parendArr.push({
  441. userShoppingCardId: keys.id,
  442. skuHashCode: keys.skuHashCode,
  443. productNum: keys.num,
  444. productId: keys.productId,
  445. });
  446. }
  447. });
  448. });
  449. if (num) {
  450. uni.$uv.toast("不支持多店铺商品一起下单");
  451. return;
  452. }
  453. dataJson = {
  454. shippingMethod: 0, //0物流,10自提
  455. createOrderDetailBos: parendArr,
  456. marketingType: 0, //0无活动,1秒杀,2拼团
  457. orderType: 0,
  458. channelType: 5,
  459. exchange: false,
  460. userUsePoint: true,
  461. businessId: businessId,
  462. };
  463. // console.log(dataJson);
  464. // return;
  465. uni.setStorageSync("dataJson", dataJson);
  466. uni.navigateTo({
  467. url: "/pages/surePay/surePay",
  468. });
  469. };
  470. onShow(() => {
  471. let token = uni.getStorageSync("apiToken");
  472. if (token) {
  473. cartList();
  474. isLogin.value = true;
  475. } else {
  476. isLogin.value = false;
  477. }
  478. });
  479. </script>
  480. <style lang="scss">
  481. page {
  482. background-color: #f5f5f5;
  483. }
  484. // 宽度相关
  485. .u-goods200 {
  486. width: 200rpx;
  487. height: 200rpx;
  488. }
  489. // 原有样式
  490. .shop-btn {
  491. width: 300rpx;
  492. height: 86rpx;
  493. background: #f5f5f5;
  494. border: 1px solid #fbbd1c;
  495. border-radius: 43rpx;
  496. font-size: 30rpx;
  497. color: #fbbd1c;
  498. text-align: center;
  499. line-height: 86rpx;
  500. margin-top: 30rpx;
  501. }
  502. .right-icon {
  503. font-size: 25rpx;
  504. }
  505. .border {
  506. border-bottom: 3rpx solid #e7e7e7;
  507. }
  508. .right-box {
  509. display: flex;
  510. flex-direction: column;
  511. // align-items: flex-start;
  512. justify-content: space-between;
  513. }
  514. .name-width {
  515. max-width: 420rpx;
  516. }
  517. .positionRetave {
  518. position: relative;
  519. .positinAble {
  520. position: absolute;
  521. width: 200rpx;
  522. height: 40rpx;
  523. text-align: center;
  524. background-color: rgba($color: #000000, $alpha: 0.5);
  525. color: #fff;
  526. font-size: 26rpx;
  527. line-height: 40rpx;
  528. bottom: 0;
  529. left: 0;
  530. }
  531. }
  532. .tipsStatus {
  533. display: inline-block;
  534. // width: 102rpx;
  535. // height: 28rpx;
  536. padding: 0 10rpx;
  537. background: #ff0000;
  538. border-radius: 10rpx 0px 10rpx 0px;
  539. line-height: 28rpx;
  540. text-align: center;
  541. margin-right: 10rpx;
  542. color: #fff;
  543. font-size: 20rpx;
  544. }
  545. .thirdTag {
  546. display: inline-block;
  547. background: #30b885;
  548. border-radius: 10rpx;
  549. line-height: 28rpx;
  550. text-align: center;
  551. margin-right: 10rpx;
  552. padding: 6rpx 10rpx;
  553. color: #fff;
  554. font-size: 20rpx;
  555. }
  556. .check {
  557. position: relative;
  558. width: 32rpx;
  559. height: 32rpx;
  560. .radios {
  561. width: 30rpx;
  562. height: 30rpx;
  563. display: block;
  564. border: 1px solid #ccc;
  565. border-radius: 50%;
  566. position: absolute;
  567. left: 0;
  568. top: 0;
  569. z-index: 1;
  570. }
  571. .iconbox {
  572. position: relative;
  573. z-index: 2;
  574. }
  575. }
  576. .topStatus {
  577. height: 80rpx;
  578. background-color: #fff;
  579. // position: fixed;
  580. // z-index: 9;
  581. width: 100%;
  582. box-sizing: border-box;
  583. }
  584. .goodsItem {
  585. padding: 60rpx 0;
  586. border-bottom: 3rpx solid #f7f7f7;
  587. }
  588. .goodsItem:last-child {
  589. border-bottom: none;
  590. }
  591. .noImg {
  592. margin-top: 10vh;
  593. width: 229rpx;
  594. height: 243rpx;
  595. }
  596. .btnview {
  597. // width: 300rpx;
  598. // margin: 30rpx auto 0;
  599. }
  600. .carbox {
  601. padding: 20rpx 0 160rpx 0;
  602. .shopList {
  603. background-color: #fff;
  604. padding: 30rpx 30rpx 0;
  605. }
  606. }
  607. .rightText {
  608. justify-content: space-between;
  609. // height: 200rpx;
  610. }
  611. .bottext,
  612. .botPrice {
  613. width: 100%;
  614. }
  615. .bottext {
  616. margin-top: 36rpx;
  617. }
  618. .rightAddRes {
  619. width: 172rpx;
  620. height: 50rpx;
  621. border-radius: 8rpx;
  622. border: 1px solid #e5e5e5;
  623. .centertext {
  624. text-align: center;
  625. }
  626. .btnTag {
  627. width: 50rpx;
  628. height: 50rpx;
  629. line-height: 50rpx;
  630. text-align: center;
  631. background-color: #dedede;
  632. font-size: 46rpx;
  633. color: #888;
  634. }
  635. }
  636. .btnWidth {
  637. width: 208rpx;
  638. }
  639. .botfixed {
  640. position: fixed;
  641. background-color: #fff;
  642. width: 100%;
  643. height: 138rpx;
  644. bottom: 0;
  645. left: 0;
  646. z-index: 9;
  647. padding: 0 30rpx;
  648. box-sizing: border-box;
  649. }
  650. </style>