index.vue 15 KB

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