order_addcart.vue 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. <template>
  2. <view>
  3. <view class="shoppingCart copy-data">
  4. <view class="borRadius14 cartBox">
  5. <view
  6. v-if="cartList.valid.length > 0 || cartList.invalid.length > 0"
  7. class="pad20"
  8. >
  9. <view
  10. v-if="
  11. (cartList.valid.length === 0 && cartList.invalid.length === 0) ||
  12. cartList.valid.length > 0
  13. "
  14. class="nav acea-row row-between-wrapper"
  15. >
  16. <view
  17. >购物车数量
  18. <!-- <text class="num font-color">{{ cartCount }}</text></view> -->
  19. <text class="num font-color">{{ orderGoodsCount }}</text></view>
  20. <view
  21. v-if="cartList.valid.length > 0 || cartList.invalid.length > 0"
  22. class="administrate acea-row row-center-wrapper"
  23. @click="manage"
  24. >{{ footerswitch ? "管理" : "取消" }}
  25. </view>
  26. </view>
  27. <view class="list">
  28. <up-checkbox-group
  29. @change="checkboxChange"
  30. shape="circle"
  31. v-model="shopCheckbox"
  32. >
  33. <block v-for="(item, index) in cartList.valid" :key="index">
  34. <view class="item acea-row row-between-wrapper">
  35. <up-checkbox
  36. activeColor="#E93323"
  37. :name="item.id"
  38. :disabled="!item.attrStatus && footerswitch"
  39. :checked="item.checked"
  40. :customStyle="{ marginRight: '10px' }"
  41. ></up-checkbox>
  42. <navigator
  43. :render-link="false"
  44. :url="'/pages/goods_details/index?id=' + item.productId"
  45. hover-class="none"
  46. class="picTxt row-between-wrapper"
  47. >
  48. <view class="pictrue">
  49. <image :src="item.image"></image>
  50. </view>
  51. <view class="item-info">
  52. <view class="text">
  53. <view
  54. class="line1"
  55. :class="item.attrStatus ? '' : 'reColor'"
  56. >{{ item.storeName }}
  57. </view>
  58. <view class="attribute-box infor line1">
  59. <view class="attribute-suk" v-show="item.suk">属性:{{ item.suk }}</view>
  60. <view v-show="!item.attrStatus" class="invalid-count">数量:{{ item.cartNum }}</view>
  61. </view>
  62. <view class="labor-costs line1" v-if="item.attrStatus"
  63. >工费:{{ item.price }}/克</view
  64. >
  65. <view class="money" v-if="item.attrStatus"
  66. >¥{{ item.storePrice }}</view
  67. >
  68. <!-- <view class='money' v-if="item.attrStatus">¥{{item.truePrice}}</view> -->
  69. <view
  70. class="reElection acea-row row-between-wrapper"
  71. v-else
  72. >
  73. <view class="title">请重新选择商品规格</view>
  74. <view
  75. class="reBnt cart-color acea-row row-center-wrapper"
  76. @click.stop="reElection(item)"
  77. >重选
  78. </view>
  79. </view>
  80. </view>
  81. <view
  82. class="carnum acea-row row-center-wrapper"
  83. v-if="item.attrStatus"
  84. >
  85. <view
  86. class="reduce"
  87. :class="item.numSub ? 'on' : ''"
  88. @click.stop="subCart(index)"
  89. >-</view
  90. >
  91. <view class="num">{{ item.cartNum }}</view>
  92. <view
  93. class="plus"
  94. :class="item.numAdd ? 'on' : ''"
  95. @click.stop="addCart(index)"
  96. >+</view
  97. >
  98. </view>
  99. </view>
  100. </navigator>
  101. </view>
  102. </block>
  103. </up-checkbox-group>
  104. </view>
  105. <!-- cartList.valid.length===0 && cartList.invalid.length > 0 -->
  106. <view
  107. v-if="cartList.invalid.length > 0"
  108. class="invalidGoods borRadius14"
  109. :style="
  110. cartList.valid.length === 0 && cartList.invalid.length > 0
  111. ? 'position: relative;z-index: 111;top: -120rpx;'
  112. : 'position: static;'
  113. "
  114. >
  115. <view class="goodsNav acea-row row-between-wrapper">
  116. <view
  117. v-if="cartList.invalid.length > 1 || cartList.valid.length > 0"
  118. @click="goodsOpen"
  119. >
  120. <text
  121. class="iconfont"
  122. :class="
  123. goodsHidden == true ? 'icon-xiangxia' : 'icon-xiangshang'
  124. "
  125. ></text
  126. >失效商品
  127. </view>
  128. <view v-else> 失效商品 </view>
  129. <view class="del" @click="unsetCart"
  130. ><text class="iconfont icon-shanchu1"></text>清空</view
  131. >
  132. </view>
  133. <view class="goodsList" :hidden="goodsHidden">
  134. <block v-for="(item, index) in cartList.invalid" :key="index">
  135. <view class="item acea-row row-between-wrapper">
  136. <view class="invalid">失效</view>
  137. <view class="picTxt acea-row row-between-wrapper">
  138. <view class="pictrue">
  139. <image :src="item.image"></image>
  140. </view>
  141. <view class="text acea-row row-column-between">
  142. <view class="line1 name">{{ item.storeName }}</view>
  143. <view class="infor line1" v-if="item.suk"
  144. >属性:{{ item.suk }}</view
  145. >
  146. <view class="acea-row row-between-wrapper">
  147. <view class="end">该商品已失效</view>
  148. </view>
  149. </view>
  150. </view>
  151. </view>
  152. </block>
  153. </view>
  154. </view>
  155. <!-- <view class='loadingicon acea-row row-center-wrapper' v-if="cartList.valid.length&&!loadend">
  156. <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
  157. </view> -->
  158. <view
  159. class="loadingicon acea-row row-center-wrapper"
  160. v-if="cartList.invalid.length && loadend"
  161. >
  162. <text
  163. class="loading iconfont icon-jiazai"
  164. :hidden="loadingInvalid == false"
  165. ></text
  166. >{{ loadTitleInvalid }}
  167. </view>
  168. </view>
  169. <view
  170. class="noCart"
  171. v-if="
  172. cartList.valid.length == 0 &&
  173. cartList.invalid.length == 0 &&
  174. canShow
  175. "
  176. >
  177. <view class="pictrue">
  178. <image src="../../static/images/noCart.png"></image>
  179. </view>
  180. <recommend :hostProduct="hostProduct"></recommend>
  181. </view>
  182. </view>
  183. </view>
  184. <view
  185. class="footer acea-row row-between-wrapper"
  186. v-if="cartList.valid.length > 0"
  187. >
  188. <view>
  189. <up-checkbox-group shape="circle" @change="checkboxAllChange">
  190. <!-- <checkbox value="all" :checked="!!isAllSelect" /> -->
  191. <up-checkbox
  192. activeColor="#E93323"
  193. :checked="!!isAllSelect"
  194. ></up-checkbox>
  195. <text class="checkAll">全选({{ selectValue.length }})</text>
  196. </up-checkbox-group>
  197. </view>
  198. <view class="money acea-row row-middle" v-if="footerswitch == true">
  199. <text class="font-color">¥{{ selectCountPrice }}</text>
  200. <form @submit="subOrder" report-submit="true">
  201. <button class="placeOrder bg-color" formType="submit">
  202. 立即下单
  203. </button>
  204. </form>
  205. </view>
  206. <view class="button acea-row row-middle" v-else>
  207. <form @submit="subCollect" report-submit="true">
  208. <button class="bnt cart-color" formType="submit">收藏</button>
  209. </form>
  210. <form @submit="subDel" report-submit="true">
  211. <button class="bnt" formType="submit">删除</button>
  212. </form>
  213. </view>
  214. </view>
  215. <productWindow
  216. :attr="attr"
  217. :isShow="1"
  218. :iSplus="1"
  219. :iScart="1"
  220. :showPopup="showProductPopup"
  221. @closePopup="closeProductPopup"
  222. @submit="handleSubmit"
  223. @myevent="onMyEvent"
  224. @ChangeAttr="ChangeAttr"
  225. @ChangeCartNum="ChangeCartNum"
  226. @attrVal="attrVal"
  227. @iptCartNum="iptCartNum"
  228. id="product-window"
  229. >
  230. </productWindow>
  231. <!-- <view class="uni-p-b-96"></view> -->
  232. <!-- <view class="uni-p-b-98"></view> -->
  233. <!-- #ifdef MP -->
  234. <!-- <authorize :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
  235. <!-- #endif -->
  236. </view>
  237. </template>
  238. <script setup>
  239. import { ref, reactive, computed } from "vue";
  240. import { onLoad, onShow, onReachBottom } from "@dcloudio/uni-app";
  241. import { useAppStore } from "@/stores/app.js";
  242. import {
  243. getCartList,
  244. getCartCounts,
  245. changeCartNum,
  246. cartDel,
  247. getResetCart,
  248. } from "@/api/order.js";
  249. import { getProductHot, collectAll, getProductDetail } from "@/api/store.js";
  250. import { toLogin } from "@/libs/login.js";
  251. import recommend from "@/components/recommend";
  252. import productWindow from "@/components/productWindow";
  253. import util from "@/utils/util";
  254. import { getPreOrder } from "@/libs/order";
  255. import { useToast } from "@/hooks/useToast";
  256. const appStore = useAppStore();
  257. const { Toast } = useToast();
  258. const cartCount = ref(0);
  259. const goodsHidden = ref(false);
  260. const footerswitch = ref(true);
  261. const hostProduct = ref([]);
  262. const cartList = reactive({
  263. valid: [],
  264. invalid: [],
  265. });
  266. const isAllSelect = ref(false); //全选
  267. const selectValue = ref([]); //选中的数据
  268. const selectCountPrice = ref(0.0);
  269. const isShowAuth = ref(false); //是否隐藏授权
  270. const hotScroll = ref(false);
  271. const hotPage = ref(1);
  272. const hotLimit = ref(10);
  273. const loading = ref(false);
  274. const loadend = ref(false);
  275. const page = ref(1);
  276. const limit = ref(20);
  277. const loadingInvalid = ref(false);
  278. const loadendInvalid = ref(false);
  279. const loadTitleInvalid = ref("加载更多");
  280. const pageInvalid = ref(1);
  281. const limitInvalid = ref(20);
  282. const attr = reactive({
  283. cartAttr: false,
  284. productAttr: [],
  285. productSelect: {},
  286. });
  287. const productValue = ref([]);
  288. const productInfo = ref({});
  289. const attrValue = ref("");
  290. const attrTxt = ref("请选择");
  291. const cartId = ref(0);
  292. const product_id = ref(0);
  293. const canShow = ref(false);
  294. const shopCheckbox = ref([]);
  295. const showProductPopup = ref(false);
  296. const isLogin = appStore.isLogin;
  297. onLoad((options) => {
  298. if (!isLogin) {
  299. toLogin();
  300. }
  301. });
  302. onShow(() => {
  303. canShow.value = false;
  304. if (isLogin) {
  305. hotPage.value = 1;
  306. hostProduct.value = [];
  307. hotScroll.value = false;
  308. loadend.value = false;
  309. page.value = 1;
  310. cartList.valid = [];
  311. getCartListFn();
  312. loadendInvalid.value = false;
  313. pageInvalid.value = 1;
  314. cartList.invalid = [];
  315. getInvalidList();
  316. footerswitch.value = true;
  317. hotScroll.value = false;
  318. hotPage.value = 1;
  319. hotLimit.value = 10;
  320. cartList.valid = [];
  321. cartList.invalid = [];
  322. isAllSelect.value = false;
  323. selectValue.value = [];
  324. selectCountPrice.value = 0.0;
  325. cartCount.value = 0;
  326. isShowAuth.value = false;
  327. }
  328. });
  329. onReachBottom(() => {
  330. if (loadend.value) {
  331. getInvalidList();
  332. }
  333. if (
  334. cartList.valid.length == 0 &&
  335. cartList.invalid.length == 0 &&
  336. hotPage.value != 1
  337. ) {
  338. getHostProduct();
  339. }
  340. });
  341. const orderGoodsCount = computed(() => {
  342. // console.log('fsdfsdfsd', cartList, cartList)
  343. return cartList.valid.length + cartList.invalid.length;
  344. });
  345. const closeProductPopup = () => {
  346. showProductPopup.value = false;
  347. };
  348. function authColse(e) {
  349. isShowAuth.value = e;
  350. }
  351. async function handleSubmit() {
  352. try {
  353. const productSelect = productValue.value[attrValue.value];
  354. if (attr.productAttr.length && productSelect === undefined)
  355. return Toast({
  356. title: "产品库存不足,请选择其它",
  357. });
  358. const params = {
  359. id: cartId.value,
  360. productId: product_id.value,
  361. num: attr.productSelect.cart_num,
  362. unique:
  363. attr.productSelect !== undefined
  364. ? attr.productSelect.unique
  365. : productInfo.value.id,
  366. };
  367. await getResetCart(params);
  368. attr.cartAttr = false;
  369. Toast({
  370. title: "添加购物车成功",
  371. success: () => {
  372. loadend.value = false;
  373. page.value = 1;
  374. cartList.valid = [];
  375. getCartListFn();
  376. getCartNum();
  377. },
  378. });
  379. } catch (error) {
  380. Toast({
  381. title: error,
  382. });
  383. } finally {
  384. showProductPopup.value = false;
  385. }
  386. }
  387. function onMyEvent() {
  388. attr.cartAttr = false;
  389. }
  390. function reElection(item) {
  391. showProductPopup.value = true;
  392. getGoodsDetails(item);
  393. }
  394. function getGoodsDetails(item) {
  395. console.log("item", item);
  396. uni.showLoading({
  397. title: "加载中",
  398. mask: true,
  399. });
  400. cartId.value = item.id;
  401. product_id.value = item.productId;
  402. getProductDetail(item.productId)
  403. .then((res) => {
  404. uni.hideLoading();
  405. attr.cartAttr = true;
  406. productInfo.value = res.data.productInfo;
  407. attr.productAttr = res.data.productAttr.map((item) => ({
  408. ...item,
  409. attrValues: item.attrValues.split(","),
  410. }));
  411. productValue.value = res.data.productValue;
  412. DefaultSelect();
  413. })
  414. .catch((err) => {
  415. uni.hideLoading();
  416. });
  417. }
  418. function ChangeAttr(res) {
  419. const productSelect = productValue.value[res];
  420. if (productSelect && productSelect.stock > 0) {
  421. attr.productSelect = {
  422. ...attr.productSelect,
  423. image: productSelect.image,
  424. sales: productSelect.sales,
  425. weight: productSelect.weight,
  426. price: productSelect.price,
  427. storePrice: productSelect.storePrice,
  428. stock: productSelect.stock,
  429. unique: productSelect.id,
  430. cart_num: 1,
  431. };
  432. attrValue.value = res;
  433. attrTxt.value = "已选择";
  434. } else {
  435. attr.productSelect = {
  436. ...attr.productSelect,
  437. image: productInfo.value.image,
  438. weight: productSelect.weight,
  439. price: productInfo.value.price,
  440. storePrice: productSelect.storePrice,
  441. stock: 0,
  442. unique: productInfo.value.id,
  443. cart_num: 1,
  444. };
  445. attrValue.value = "";
  446. attrTxt.value = "请选择";
  447. }
  448. }
  449. function DefaultSelect() {
  450. const productAttr = attr.productAttr;
  451. console.log("productValue", productValue);
  452. let value = [];
  453. for (let key in productValue.value) {
  454. if (productValue.value[key].stock > 0) {
  455. value = attr.productAttr.length ? key.split(",") : [];
  456. break;
  457. }
  458. }
  459. for (let i = 0; i < productAttr.length; i++) {
  460. productAttr[i].index = value[i];
  461. }
  462. let productSelect = productValue.value[value.sort().join(",")];
  463. if (productSelect && productAttr.length) {
  464. attr.productSelect.storeName = productInfo.value.storeName;
  465. attr.productSelect.image = productSelect.image;
  466. attr.productSelect.price = productSelect.price;
  467. attr.productSelect.storePrice = productSelect.storePrice;
  468. attr.productSelect.stock = productSelect.stock;
  469. attr.productSelect.unique = productSelect.id;
  470. attr.productSelect.cart_num = 1;
  471. attrValue.value = value.sort().join(",");
  472. attrTxt.value = "已选择";
  473. } else if (!productSelect && productAttr.length) {
  474. attr.productSelect.storeName = productInfo.value.storeName;
  475. attr.productSelect.image = productInfo.value.image;
  476. attr.productSelect.price = productInfo.value.price;
  477. attr.productSelect.storePrice = productSelect.storePrice;
  478. attr.productSelect.stock = 0;
  479. attr.productSelect.unique = productInfo.value.id;
  480. attr.productSelect.cart_num = 0;
  481. attrValue.value = "";
  482. attrTxt.value = "请选择";
  483. } else if (!productSelect && !productAttr.length) {
  484. attr.productSelect.storeName = productInfo.value.storeName;
  485. attr.productSelect.image = productInfo.value.image;
  486. attr.productSelect.price = productInfo.value.price;
  487. attr.productSelect.storePrice = productSelect.storePrice;
  488. attr.productSelect.stock = productInfo.value.stock;
  489. attr.productSelect.unique = productInfo.value.id || "";
  490. attr.productSelect.cart_num = 1;
  491. attrValue.value = "";
  492. attrTxt.value = "请选择";
  493. }
  494. }
  495. function attrVal(val) {
  496. attr.productAttr[val.indexw].index =
  497. attr.productAttr[val.indexw].attrValues[val.indexn];
  498. }
  499. function ChangeCartNum(changeValue) {
  500. let productSelect = productValue.value[attrValue.value];
  501. if (productSelect === undefined && !attr.productAttr.length)
  502. productSelect = attr.productSelect;
  503. if (productSelect === undefined) return;
  504. let stock = productSelect.stock || 0;
  505. let num = attr.productSelect;
  506. if (changeValue) {
  507. num.cart_num++;
  508. if (num.cart_num > stock) {
  509. num.cart_num = stock ? stock : 1;
  510. }
  511. } else {
  512. num.cart_num--;
  513. if (num.cart_num < 1) {
  514. num.cart_num = 1;
  515. }
  516. }
  517. }
  518. function iptCartNum(e) {
  519. attr.productSelect.cart_num = e;
  520. }
  521. function subDel(event) {
  522. const selectValueArr = selectValue.value;
  523. if (selectValueArr.length > 0)
  524. cartDel(selectValueArr).then((res) => {
  525. loadend.value = false;
  526. page.value = 1;
  527. cartList.valid = [];
  528. getCartListFn();
  529. getCartNum();
  530. });
  531. else
  532. return Toast({
  533. title: "请选择产品",
  534. });
  535. }
  536. function getSelectValueProductId() {
  537. const validList = cartList.valid;
  538. const selectValueArr = selectValue.value;
  539. let productId = [];
  540. if (selectValueArr.length > 0) {
  541. for (let index in validList) {
  542. if (inArray(validList[index].id, selectValueArr)) {
  543. productId.push(validList[index].productId);
  544. }
  545. }
  546. }
  547. return productId;
  548. }
  549. function subCollect(event) {
  550. const selectValueArr = selectValue.value;
  551. if (selectValueArr.length > 0) {
  552. const selectValueProductId = getSelectValueProductId();
  553. collectAll(selectValueProductId)
  554. .then((res) => {
  555. return Toast({
  556. title: "收藏成功",
  557. icon: "success",
  558. });
  559. })
  560. .catch((err) => {
  561. return Toast({
  562. title: err,
  563. });
  564. });
  565. } else {
  566. return Toast({
  567. title: "请选择产品",
  568. });
  569. }
  570. }
  571. function subOrder(event) {
  572. const selectValueArr = selectValue.value;
  573. if (selectValueArr.length > 0) {
  574. getPreOrderFn();
  575. } else {
  576. return Toast({
  577. title: "请选择产品",
  578. });
  579. }
  580. }
  581. function getPreOrderFn() {
  582. const shoppingCartId = selectValue.value.map((item) => {
  583. return {
  584. shoppingCartId: Number(item),
  585. };
  586. });
  587. const params = {
  588. mallType: 0,
  589. preOrderType: "shoppingCart",
  590. orderDetails: shoppingCartId,
  591. };
  592. getPreOrder(params);
  593. }
  594. function checkboxAllChange(value) {
  595. if (value.length > 0) {
  596. setAllSelectValue(1);
  597. } else {
  598. setAllSelectValue(0);
  599. }
  600. }
  601. function setAllSelectValue(status) {
  602. const valid = cartList.valid;
  603. let selectValueArr = [];
  604. if (valid.length > 0) {
  605. let newValid = valid.map((item) => {
  606. if (status) {
  607. if (footerswitch.value) {
  608. if (item.attrStatus) {
  609. item.checked = true;
  610. selectValueArr.push(item.id);
  611. } else {
  612. item.checked = false;
  613. }
  614. } else {
  615. item.checked = true;
  616. selectValueArr.push(item.id);
  617. }
  618. isAllSelect.value = true;
  619. } else {
  620. item.checked = false;
  621. isAllSelect.value = false;
  622. }
  623. return item;
  624. });
  625. cartList.valid = newValid;
  626. selectValue.value = selectValueArr;
  627. switchSelect();
  628. }
  629. }
  630. function checkboxChange(value) {
  631. const valid = cartList.valid;
  632. let arr1 = [];
  633. let arr2 = [];
  634. let arr3 = [];
  635. let newValid = valid.map((item) => {
  636. if (inArray(item.id, value)) {
  637. if (footerswitch.value) {
  638. if (item.attrStatus) {
  639. item.checked = true;
  640. arr1.push(item);
  641. } else {
  642. item.checked = false;
  643. }
  644. } else {
  645. item.checked = true;
  646. arr1.push(item);
  647. }
  648. } else {
  649. item.checked = false;
  650. arr2.push(item);
  651. }
  652. return item;
  653. });
  654. if (footerswitch.value) {
  655. arr3 = arr2.filter((item) => !item.attrStatus);
  656. }
  657. cartList.valid = newValid;
  658. isAllSelect.value = newValid.length === arr1.length + arr3.length;
  659. selectValue.value = value;
  660. switchSelect();
  661. }
  662. function inArray(search, array) {
  663. for (let i in array) {
  664. if (array[i] == search) {
  665. return true;
  666. }
  667. }
  668. return false;
  669. }
  670. // 选中商品的价格
  671. function switchSelect() {
  672. const validList = cartList.valid;
  673. const selectValueArr = selectValue.value;
  674. let selectCount = 0.0;
  675. if (selectValueArr.length < 1) {
  676. selectCountPrice.value = selectCount;
  677. } else {
  678. for (let index in validList) {
  679. if (inArray(validList[index].id, selectValueArr)) {
  680. selectCount = util.$h.Add(
  681. selectCount,
  682. util.$h.Mul(validList[index].cartNum, validList[index].storePrice)
  683. );
  684. }
  685. }
  686. selectCountPrice.value = selectCount;
  687. }
  688. }
  689. function iptCartNumInput(index) {
  690. const item = cartList.valid[index];
  691. if (item.cartNum) {
  692. setCartNum(item.id, item.cartNum);
  693. }
  694. switchSelect();
  695. }
  696. function blurInput(index) {
  697. const item = cartList.valid[index];
  698. if (!item.cartNum) {
  699. item.cartNum = 1;
  700. }
  701. }
  702. function subCart(index) {
  703. let status = false;
  704. const item = cartList.valid[index];
  705. item.cartNum = Number(item.cartNum) - 1;
  706. if (item.cartNum < 1) status = true;
  707. if (item.cartNum <= 1) {
  708. item.cartNum = 1;
  709. item.numSub = true;
  710. } else {
  711. item.numSub = false;
  712. item.numAdd = false;
  713. }
  714. if (false == status) {
  715. setCartNum(item.id, item.cartNum, function (data) {
  716. cartList.valid[index] = item;
  717. switchSelect();
  718. getCartNum();
  719. });
  720. }
  721. }
  722. function addCart(index) {
  723. const item = cartList.valid[index];
  724. item.cartNum = Number(item.cartNum) + 1;
  725. if (item.cartNum >= item.stock) {
  726. item.cartNum = item.stock;
  727. item.numAdd = true;
  728. item.numSub = false;
  729. } else {
  730. item.numAdd = false;
  731. item.numSub = false;
  732. }
  733. setCartNum(item.id, item.cartNum, function (data) {
  734. cartList.valid[index] = item;
  735. switchSelect();
  736. getCartNum();
  737. });
  738. }
  739. function setCartNum(cartIdVal, cartNum, successCallback) {
  740. changeCartNum(cartIdVal, cartNum).then((res) => {
  741. successCallback && successCallback(res.data);
  742. });
  743. }
  744. function getCartNum() {
  745. getCartCounts(true, "sum").then((res) => {
  746. cartCount.value = res.data.count;
  747. });
  748. }
  749. function getCartData(data) {
  750. return new Promise((resolve, reject) => {
  751. getCartList(data)
  752. .then((res) => {
  753. resolve(res.data);
  754. })
  755. .catch(function (err) {
  756. loading.value = false;
  757. canShow.value = true;
  758. Toast({
  759. title: err,
  760. });
  761. });
  762. });
  763. }
  764. async function getCartListFn() {
  765. uni.showLoading({
  766. title: "加载中",
  767. mask: true,
  768. });
  769. let data = {
  770. page: page.value,
  771. limit: limit.value,
  772. isValid: true,
  773. };
  774. getCartCounts(true, "sum").then(async (c) => {
  775. // 获取购物车商品数量
  776. cartCount.value = c.data.count;
  777. // 当没有商品时,获取推荐商品
  778. if (c.data.count === 0) getHostProduct();
  779. for (let i = 0; i < Math.ceil(cartCount.value / limit.value); i++) {
  780. let cartListData = await getCartData(data);
  781. let valid = cartListData.list;
  782. let validList = [...cartList.valid, ...valid];
  783. let selectValueArr = [];
  784. if (validList.length > 0) {
  785. for (let index in validList) {
  786. // 购物车加减状态
  787. if (validList[index].cartNum == 1) {
  788. validList[index].numSub = true;
  789. } else {
  790. validList[index].numSub = false;
  791. }
  792. // 库存
  793. let stock = validList[index].stock ? validList[index].stock : 0;
  794. // 当购物车数量大于库存时,设置购物车数量为库存
  795. if (validList[index].cartNum == stock) {
  796. validList[index].numAdd = true;
  797. } else if (validList[index].cartNum == validList[index].stock) {
  798. validList[index].numAdd = true;
  799. } else {
  800. validList[index].numAdd = false;
  801. }
  802. // attrStatus 判断商品是否有效
  803. if (validList[index].attrStatus) {
  804. validList[index].checked = true;
  805. selectValueArr.push(validList[index].id);
  806. } else {
  807. validList[index].checked = false;
  808. }
  809. }
  810. }
  811. cartList.valid = validList;
  812. data.page += 1;
  813. selectValue.value = selectValueArr;
  814. let newArr = validList.filter((item) => item.attrStatus);
  815. // 全选状态
  816. isAllSelect.value =
  817. newArr.length == selectValueArr.length && newArr.length;
  818. switchSelect();
  819. }
  820. loading.value = false;
  821. canShow.value = true;
  822. uni.hideLoading();
  823. });
  824. }
  825. function getInvalidList() {
  826. if (loadendInvalid.value) return false;
  827. if (loadingInvalid.value) return false;
  828. let data = {
  829. page: pageInvalid.value,
  830. limit: limitInvalid.value,
  831. isValid: false,
  832. };
  833. getCartList(data)
  834. .then((res) => {
  835. let invalid = res.data.list,
  836. loadendInvalidFlag = invalid.length < limitInvalid.value;
  837. cartList.invalid = [...invalid, ...cartList.invalid];
  838. loadendInvalid.value = loadendInvalidFlag;
  839. loadTitleInvalid.value = loadendInvalidFlag
  840. ? "我也是有底线的"
  841. : "加载更多";
  842. pageInvalid.value = pageInvalid.value + 1;
  843. loadingInvalid.value = false;
  844. })
  845. .catch((res) => {
  846. loadingInvalid.value = false;
  847. loadTitleInvalid.value = "加载更多";
  848. });
  849. }
  850. function getHostProduct() {
  851. if (hotScroll.value) return;
  852. getProductHot(hotPage.value, hotLimit.value).then((res) => {
  853. hotPage.value++;
  854. hotScroll.value = res.data.list.length < hotLimit.value;
  855. hostProduct.value = hostProduct.value.concat(res.data.list);
  856. });
  857. }
  858. function goodsOpen() {
  859. goodsHidden.value = !goodsHidden.value;
  860. }
  861. function manage() {
  862. footerswitch.value = !footerswitch.value;
  863. let arr1 = [];
  864. let arr2 = [];
  865. let newValid = cartList.valid.map((item) => {
  866. if (footerswitch.value) {
  867. if (item.attrStatus) {
  868. if (item.checked) {
  869. arr1.push(item.id);
  870. }
  871. } else {
  872. item.checked = false;
  873. arr2.push(item);
  874. }
  875. } else {
  876. if (item.checked) {
  877. arr1.push(item.id);
  878. }
  879. }
  880. return item;
  881. });
  882. cartList.valid = newValid;
  883. if (footerswitch.value) {
  884. isAllSelect.value = newValid.length === arr1.length + arr2.length;
  885. } else {
  886. isAllSelect.value = newValid.length === arr1.length;
  887. }
  888. selectValue.value = arr1;
  889. switchSelect();
  890. }
  891. function unsetCart() {
  892. let ids = [];
  893. for (let i = 0, len = cartList.invalid.length; i < len; i++) {
  894. ids.push(cartList.invalid[i].id);
  895. }
  896. cartDel(ids)
  897. .then((res) => {
  898. Toast({
  899. title: "清除成功",
  900. });
  901. cartList.invalid = [];
  902. getHostProduct();
  903. })
  904. .catch((res) => {});
  905. }
  906. </script>
  907. <style scoped lang="scss">
  908. .cartBox {
  909. margin-top: 20rpx;
  910. }
  911. .shoppingCart {
  912. // .labelNav
  913. .labelNav {
  914. position: sticky;
  915. height: 178rpx;
  916. padding: 30rpx 30rpx 0 30rpx;
  917. font-size: 22rpx;
  918. color: #fff;
  919. position: fixed;
  920. left: 0;
  921. width: 100%;
  922. box-sizing: border-box;
  923. background-color: $theme-color;
  924. z-index: 5;
  925. top: 0;
  926. height: calc(178rpx + 44px + 45rpx);
  927. padding-top: calc(44px + 50rpx);
  928. .item {
  929. .iconfont {
  930. font-size: 25rpx;
  931. margin-right: 10rpx;
  932. }
  933. }
  934. }
  935. // .nav
  936. .nav {
  937. height: 90rpx;
  938. background-color: #fff;
  939. padding: 0 24rpx;
  940. box-sizing: border-box;
  941. font-size: 28rpx;
  942. color: #282828;
  943. border-top-left-radius: 14rpx;
  944. border-top-right-radius: 14rpx;
  945. .num {
  946. margin-left: 12rpx;
  947. }
  948. .administrate {
  949. font-size: 28rpx;
  950. color: #333;
  951. }
  952. }
  953. // .noCart
  954. .noCart {
  955. background-color: #fff;
  956. padding-top: 0.1rpx;
  957. .pictrue {
  958. width: 414rpx;
  959. height: 336rpx;
  960. margin: 78rpx auto 56rpx auto;
  961. image {
  962. width: 100%;
  963. height: 100%;
  964. }
  965. }
  966. }
  967. // .list
  968. .list {
  969. width: 100%;
  970. overflow: hidden;
  971. border-bottom-left-radius: 14rpx;
  972. border-bottom-right-radius: 14rpx;
  973. .item {
  974. width: 100%;
  975. padding: 24rpx;
  976. background-color: #fff;
  977. display: flex;
  978. .picTxt {
  979. flex: 1;
  980. position: relative;
  981. display: flex;
  982. .pictrue {
  983. width: 160rpx;
  984. height: 160rpx;
  985. margin-right: 15rpx;
  986. image {
  987. width: 100%;
  988. height: 100%;
  989. border-radius: 6rpx;
  990. }
  991. }
  992. .item-info,
  993. .text {
  994. width: 396rpx;
  995. font-size: 28rpx;
  996. color: #282828;
  997. .reColor {
  998. color: #999;
  999. }
  1000. .reElection {
  1001. margin-top: 20rpx;
  1002. .title {
  1003. font-size: 24rpx;
  1004. }
  1005. .reBnt {
  1006. width: 120rpx;
  1007. height: 46rpx;
  1008. border-radius: 23rpx;
  1009. font-size: 26rpx;
  1010. }
  1011. }
  1012. .attribute-box {
  1013. display: flex;
  1014. justify-content: space-between;
  1015. .attribute-suk {
  1016. }
  1017. .invalid-count {
  1018. text-align: right;
  1019. }
  1020. }
  1021. .infor {
  1022. font-size: 24rpx;
  1023. color: #999;
  1024. margin-top: 16rpx;
  1025. }
  1026. .labor-costs {
  1027. font-size: 24rpx;
  1028. color: #999;
  1029. }
  1030. .money {
  1031. font-size: 32rpx;
  1032. color: #e93323;
  1033. margin-top: 28rpx;
  1034. font-weight: 600;
  1035. }
  1036. }
  1037. .carnum {
  1038. height: 47rpx;
  1039. position: absolute;
  1040. bottom: 7rpx;
  1041. right: 0;
  1042. view {
  1043. border: 1rpx solid #a4a4a4;
  1044. width: 66rpx;
  1045. text-align: center;
  1046. height: 100%;
  1047. line-height: 44rpx;
  1048. font-size: 28rpx;
  1049. color: #a4a4a4;
  1050. }
  1051. .reduce {
  1052. border-right: 0;
  1053. border-radius: 22rpx 0 0 22rpx;
  1054. font-size: 34rpx;
  1055. line-height: 40rpx;
  1056. &.on {
  1057. border-color: #e3e3e3;
  1058. color: #dedede;
  1059. }
  1060. }
  1061. .plus {
  1062. border-left: 0;
  1063. border-radius: 0 22rpx 22rpx 0;
  1064. font-size: 34rpx;
  1065. line-height: 40rpx;
  1066. }
  1067. .num {
  1068. color: #282828;
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. // .invalidGoods
  1075. .invalidGoods {
  1076. background-color: #fff;
  1077. margin-top: 30rpx;
  1078. .goodsNav {
  1079. width: 100%;
  1080. height: 90rpx;
  1081. padding: 0 24rpx;
  1082. box-sizing: border-box;
  1083. font-size: 28rpx;
  1084. color: #333;
  1085. .iconfont {
  1086. color: #424242;
  1087. font-size: 28rpx;
  1088. margin-right: 17rpx;
  1089. }
  1090. .del {
  1091. font-size: 26rpx;
  1092. color: #333;
  1093. .icon-shanchu1 {
  1094. color: #333;
  1095. font-size: 33rpx;
  1096. vertical-align: -2rpx;
  1097. margin-right: 8rpx;
  1098. }
  1099. }
  1100. }
  1101. .goodsList {
  1102. .item {
  1103. padding: 24rpx;
  1104. .invalid {
  1105. font-size: 22rpx;
  1106. color: #ccc;
  1107. height: 36rpx;
  1108. border-radius: 3rpx;
  1109. text-align: center;
  1110. line-height: 36rpx;
  1111. }
  1112. .picTxt {
  1113. width: 576rpx;
  1114. .pictrue {
  1115. width: 160rpx;
  1116. height: 160rpx;
  1117. image {
  1118. width: 100%;
  1119. height: 100%;
  1120. border-radius: 6rpx;
  1121. }
  1122. }
  1123. .text {
  1124. width: 396rpx;
  1125. font-size: 28rpx;
  1126. color: #999;
  1127. height: 140rpx;
  1128. .name {
  1129. width: 100%;
  1130. }
  1131. .infor {
  1132. font-size: 24rpx;
  1133. }
  1134. .end {
  1135. font-size: 26rpx;
  1136. color: #bbb;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. }
  1144. // footer
  1145. .footer {
  1146. z-index: 9;
  1147. width: 100%;
  1148. height: 100rpx;
  1149. background-color: #fff;
  1150. position: fixed;
  1151. padding: 0 24rpx;
  1152. box-sizing: border-box;
  1153. border-top: 1rpx solid #eee;
  1154. bottom: 0;
  1155. .checkAll {
  1156. font-size: 28rpx;
  1157. color: #282828;
  1158. margin-left: 14rpx;
  1159. }
  1160. .money {
  1161. font-size: 30rpx;
  1162. .font-color {
  1163. font-weight: 600;
  1164. }
  1165. }
  1166. .placeOrder {
  1167. color: #fff;
  1168. font-size: 30rpx;
  1169. width: 226rpx;
  1170. height: 70rpx;
  1171. border-radius: 50rpx;
  1172. text-align: center;
  1173. line-height: 70rpx;
  1174. margin-left: 22rpx;
  1175. }
  1176. .button {
  1177. .bnt {
  1178. font-size: 28rpx;
  1179. color: #999;
  1180. border-radius: 50rpx;
  1181. border: 1px solid #999;
  1182. width: 160rpx;
  1183. height: 60rpx;
  1184. text-align: center;
  1185. line-height: 60rpx;
  1186. }
  1187. form ~ form {
  1188. margin-left: 17rpx;
  1189. }
  1190. }
  1191. }
  1192. // 其它样式
  1193. .invalidClas {
  1194. position: relative;
  1195. z-index: 111;
  1196. top: -120rpx;
  1197. }
  1198. .invalidClasNO {
  1199. position: static;
  1200. margin-top: 15px;
  1201. }
  1202. .uni-p-b-96 {
  1203. height: 96rpx;
  1204. }
  1205. // .my_nav {
  1206. // top: calc(44px + 88rpx + 50rpx) !important;
  1207. // }
  1208. // .my_nav_top {
  1209. // margin-top: calc(44px + 88rpx + 30rpx + 105rpx) !important;
  1210. // }
  1211. </style>