shoppingCart.vue 15 KB

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