cart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <template>
  2. <view class="content">
  3. <!-- 商品列表 -->
  4. <view class="goods-list">
  5. <view class="item" v-for="(item,index) in carts" :key="index">
  6. <label style="margin-right: 30upx;">
  7. <checkbox :value="item.checked" :checked="item.checked" @click="checkChange(item)" />
  8. </label>
  9. <image class="goods-img" :src="item.productAttrImage==null||item.productAttrImage==''?item.productImage:item.productAttrImage" mode="aspectFit"></image>
  10. <view class="info-box">
  11. <view>
  12. <view class="title-box">
  13. <!-- <view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view> -->
  14. <view class="title ellipsis">{{ item.productName }}</view>
  15. </view>
  16. <view class="intro ellipsis">{{item.productAttrName}}</view>
  17. </view>
  18. <view class="price-num">
  19. <view class="price">
  20. <text class="unit">¥</text>
  21. <text class="text">{{item.price}}</text>
  22. </view>
  23. <view class="num-box">
  24. <view class="img-box" @click="delNum(item)">
  25. <image v-if="item.cartNum <= 1" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/jian.png" mode=""></image>
  26. <image v-else src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/jian2.png" mode=""></image>
  27. </view>
  28. <!-- <input type="number" @change="changeNum($event,item)" :value="item.cartNum" /> -->
  29. <input type="number" @focus="saveOriginalNum(item)" @blur="changeNum($event,item)" v-model="item.cartNum" />
  30. <view class="img-box" @click="addNum(item)">
  31. <image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/add.png" mode=""></image>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view v-if="carts.length == 0" class="no-data-box">
  39. <image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/no_data.png" mode="aspectFit"></image>
  40. <view class="empty-title">暂无数据</view>
  41. </view>
  42. <!-- 猜你喜欢 -->
  43. <!-- <view class="like-product">
  44. <likeProduct ref="product" />
  45. </view> -->
  46. <!-- 底部按钮 -->
  47. <view class="btn-foot">
  48. <view class="left">
  49. <label>
  50. <checkbox :checked="checkAll" @click="handleCheckAll()" />
  51. </label>
  52. <text class="text">全选</text>
  53. <text class="text" @click="delCart()">删除</text>
  54. </view>
  55. <view class="right">
  56. <view class="total">
  57. <text class="label">合计:</text>
  58. <view class="price">
  59. <text class="unit">¥</text>
  60. <text class="num">{{totalMoney.toFixed(2)}}</text>
  61. </view>
  62. </view>
  63. <view class="btn" @click="submit">结算</view>
  64. </view>
  65. </view>
  66. <CustomToast ref="customToast">
  67. </CustomToast>
  68. </view>
  69. </template>
  70. <script>
  71. import {getCarts,cartNum,delCart,checkCartPurchaseLimit} from '@/api/product'
  72. import likeProduct from '@/components/likeProduct.vue'
  73. import {CustomToast} from '@/components/custom-toast.vue';
  74. export default {
  75. components: {
  76. likeProduct,CustomToast
  77. },
  78. data() {
  79. return {
  80. totalMoney:0.00,
  81. carts:[],
  82. checkAll:false,
  83. }
  84. },
  85. onLoad() {
  86. },
  87. onShow(){
  88. this.getCarts();
  89. },
  90. onReachBottom() {
  91. this.$refs.product.getGoodsProducts();
  92. },
  93. methods: {
  94. // 保存原始数量(在input获得焦点时)
  95. saveOriginalNum(item){
  96. // 在item对象上保存原始值(每次获得焦点时都保存)
  97. item._originalCartNum = item.cartNum;
  98. },
  99. delCart(){
  100. var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  101. return ele.id
  102. });
  103. if(selectCarts.length==0){
  104. uni.showToast({
  105. icon:'none',
  106. title: "请选择商品删除",
  107. });
  108. return;
  109. }
  110. let data = {ids:selectCarts};
  111. delCart(data).then(
  112. res => {
  113. if(res.code==200){
  114. // uni.showToast({
  115. // icon:'success',
  116. // title: "操作成功",
  117. // });
  118. this.getCarts()
  119. }else{
  120. uni.showToast({
  121. icon:'none',
  122. title: res.msg,
  123. });
  124. }
  125. },
  126. rej => {}
  127. );
  128. console.log(selectCarts)
  129. },
  130. computedMoney(){
  131. var money=0;
  132. var that=this;
  133. this.carts.forEach((item,index,arr)=>{
  134. if(item.checked){
  135. money+=item.price*item.cartNum;
  136. }
  137. })
  138. this.totalMoney=money;
  139. },
  140. handleCheckAll(){
  141. this.checkAll=!this.checkAll;
  142. var that=this;
  143. this.carts.forEach((item,index,arr)=>{
  144. item.checked=that.checkAll;
  145. })
  146. this.computedMoney();
  147. },
  148. checkChange(item){
  149. item.checked=!item.checked;
  150. this.checkAll = this.carts.every(item=>item.checked)
  151. this.computedMoney();
  152. },
  153. // changeNum(e,item) {
  154. // item.cartNum = e.detail.value.replace(/\D/g, '')
  155. // if (item.cartNum <= 1) {
  156. // uni.showToast({
  157. // title: "已经是底线啦!",
  158. // icon: "none",
  159. // duration: 2000
  160. // });
  161. // return;
  162. // }
  163. // if(item.cartNum < 1) {
  164. // item.cartNum = 1
  165. // }
  166. // if(item.cartNum>=item.stock){
  167. // item.cartNum=item.stock;
  168. // }
  169. // this.changeCartNum(item)
  170. // },
  171. changeCartNum(item, targetNum, originalNum){
  172. // targetNum: 目标数量
  173. // originalNum: 原始数量(用于失败时恢复)
  174. let data = {number:targetNum,id:item.id};
  175. cartNum(data).then(
  176. res => {
  177. if(res.code==200){
  178. // API成功,更新本地数量
  179. item.cartNum = targetNum;
  180. // 清除临时保存的原始值
  181. if(item._originalCartNum !== undefined){
  182. delete item._originalCartNum;
  183. }
  184. this.computedMoney();
  185. // uni.showToast({
  186. // icon:'none',
  187. // title: "操作成功",
  188. // });
  189. }else{
  190. // API失败,不更新数量(保持原值)
  191. // 确保显示的是原始值
  192. if(originalNum !== undefined){
  193. item.cartNum = originalNum;
  194. }
  195. // 清除临时保存的原始值
  196. if(item._originalCartNum !== undefined){
  197. delete item._originalCartNum;
  198. }
  199. this.$forceUpdate(); // 强制更新视图
  200. this.$refs.customToast.show({
  201. title: res.msg || '该商品限购,目前剩余可购买数量不足',
  202. duration: 2000
  203. });
  204. // uni.showToast({
  205. // icon:'none',
  206. // title: ,
  207. // });
  208. }
  209. },
  210. rej => {
  211. // 网络错误,不更新数量(保持原值)
  212. if(originalNum !== undefined){
  213. item.cartNum = originalNum;
  214. }
  215. // 清除临时保存的原始值
  216. if(item._originalCartNum !== undefined){
  217. delete item._originalCartNum;
  218. }
  219. this.$forceUpdate();
  220. uni.showToast({
  221. icon:'none',
  222. title: '网络错误,请重试',
  223. });
  224. }
  225. );
  226. },
  227. changeNum(e, item) {
  228. // 获取原始数量(在focus时保存的)
  229. let originalNum = item._originalCartNum !== undefined ? item._originalCartNum : item.cartNum;
  230. let value = e.detail.value.replace(/\D/g, '')
  231. // 转换为数字
  232. let num = parseInt(value)
  233. // 检查是否为有效数字
  234. if (isNaN(num) || value === '') {
  235. num = 1
  236. uni.showToast({
  237. title: "数量不能为0",
  238. icon: "none",
  239. duration: 2000
  240. });
  241. }
  242. // 限制最小值
  243. if (num < 1) {
  244. num = 1
  245. uni.showToast({
  246. title: "最小数量为1",
  247. icon: "none",
  248. duration: 2000
  249. });
  250. }
  251. // 限制最大值(库存)
  252. if (num > item.stock) {
  253. num = item.stock
  254. uni.showToast({
  255. title: "库存不足,已设为最大库存",
  256. icon: "none",
  257. duration: 2000
  258. });
  259. }
  260. // 如果目标数量与原始数量相同,不需要调用API,恢复显示
  261. if(num === originalNum){
  262. item.cartNum = originalNum;
  263. delete item._originalCartNum; // 清除临时保存的值
  264. return;
  265. }
  266. // 先恢复原值(因为v-model已经改变了显示)
  267. item.cartNum = originalNum;
  268. // 调用API,根据结果决定是否更新
  269. this.changeCartNum(item, num, originalNum)
  270. },
  271. getCarts(){
  272. getCarts().then(
  273. res => {
  274. if(res.code==200){
  275. this.carts=res.carts;
  276. this.carts.forEach((item,index,arr)=>{
  277. item.checked=false;
  278. })
  279. this.computedMoney();
  280. }else{
  281. uni.showToast({
  282. icon:'none',
  283. title: "请求失败",
  284. });
  285. }
  286. },
  287. rej => {}
  288. );
  289. },
  290. // 购物车减法
  291. delNum(item) {
  292. if (item.cartNum <= 1) {
  293. uni.showToast({
  294. title: "已经是底线啦!",
  295. icon: "none",
  296. duration: 2000
  297. });
  298. return;
  299. }
  300. // 保存原始数量
  301. let originalNum = item.cartNum;
  302. // 计算目标数量
  303. let targetNum = originalNum - 1;
  304. if(targetNum < 1) {
  305. targetNum = 1;
  306. }
  307. // 先不更新本地数量,直接调用API,根据结果决定是否更新
  308. this.changeCartNum(item, targetNum, originalNum)
  309. },
  310. // 购物车加法
  311. addNum(item) {
  312. console.log(item)
  313. // 保存原始数量
  314. let originalNum = item.cartNum;
  315. // 计算目标数量
  316. let targetNum = originalNum + 1;
  317. if(targetNum >= item.stock){
  318. targetNum = item.stock;
  319. }
  320. // 先不更新本地数量,直接调用API,根据结果决定是否更新
  321. this.changeCartNum(item, targetNum, originalNum)
  322. },
  323. // 结算
  324. async submit() {
  325. var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  326. return ele.id
  327. });
  328. if(selectCarts.length==0){
  329. uni.showToast({
  330. icon:'none',
  331. title: "请选择商品",
  332. });
  333. return;
  334. }
  335. // 获取选中的商品信息,用于限购校验
  336. var selectedProducts = this.carts.filter(ele => ele.checked==true).map(ele => {
  337. return {
  338. productId: ele.productId,
  339. num: ele.cartNum
  340. }
  341. });
  342. // 结算前先校验限购
  343. try {
  344. const res = await checkCartPurchaseLimit({ products: selectedProducts });
  345. if (res.code !== 200) {
  346. this.$refs.customToast.show({
  347. title: res.msg || '商品限购校验失败',
  348. duration: 2000
  349. });
  350. // uni.showToast({
  351. // icon: 'none',
  352. // title: res.msg || '商品限购校验失败'
  353. // });
  354. return;
  355. }
  356. } catch (error) {
  357. console.error('检查限购失败:', error);
  358. uni.showToast({
  359. icon: 'none',
  360. title: '检查限购失败,请稍后重试'
  361. });
  362. return;
  363. }
  364. // 限购检查通过,进行跳转
  365. uni.navigateTo({
  366. url: './confirmOrder?type=cart&cartIds='+selectCarts.toString()
  367. })
  368. },
  369. showProduct(item){
  370. uni.navigateTo({
  371. url: '../shopping/productDetails?productId='+item.productId
  372. })
  373. },
  374. }
  375. }
  376. </script>
  377. <style lang="scss">
  378. page {
  379. height: 100%;
  380. }
  381. .content{
  382. height: 100%;
  383. padding: 20upx;
  384. .goods-list{
  385. .item{
  386. box-sizing: border-box;
  387. height: 221upx;
  388. background: #FFFFFF;
  389. border-radius: 16upx;
  390. margin-bottom: 20upx;
  391. padding: 30upx;
  392. display: flex;
  393. align-items: center;
  394. &:last-child{
  395. margin-bottom: 0;
  396. }
  397. .goods-img{
  398. width: 160upx;
  399. height: 160upx;
  400. background: #FFFFFF;
  401. margin-right: 30upx;
  402. flex-shrink: 0;
  403. }
  404. .info-box{
  405. height: 160upx;
  406. display: flex;
  407. flex-direction: column;
  408. justify-content: space-between;
  409. width: calc(100% - 255upx);
  410. .title-box{
  411. width: 100%;
  412. display: flex;
  413. align-items: center;
  414. .tag{
  415. padding: 0 6upx;
  416. height: 30upx;
  417. line-height: 30upx;
  418. font-size: 22upx;
  419. font-family: PingFang SC;
  420. font-weight: bold;
  421. color: #FFFFFF;
  422. background: linear-gradient(90deg, #66b2ef 0%, #2BC7B9 100%);
  423. border-radius: 4upx;
  424. margin-right: 10upx;
  425. flex-shrink: 0;
  426. }
  427. .title{
  428. flex: 1;
  429. font-size: 28upx;
  430. font-family: PingFang SC;
  431. font-weight: 500;
  432. color: #111111;
  433. line-height: 1;
  434. }
  435. }
  436. .intro{
  437. font-size: 24upx;
  438. font-family: PingFang SC;
  439. font-weight: 500;
  440. color: #999999;
  441. margin-top: 22upx;
  442. line-height: 1;
  443. }
  444. .price-num{
  445. display: flex;
  446. align-items: center;
  447. justify-content: space-between;
  448. .price{
  449. display: flex;
  450. align-items: flex-end;
  451. .unit{
  452. font-size: 24upx;
  453. font-family: PingFang SC;
  454. font-weight: 500;
  455. color: #FF6633;
  456. line-height: 1.2;
  457. margin-right: 4upx;
  458. }
  459. .text{
  460. font-size: 32upx;
  461. font-family: PingFang SC;
  462. font-weight: bold;
  463. color: #FF6633;
  464. line-height: 1;
  465. }
  466. }
  467. .num-box{
  468. display: flex;
  469. align-items: center;
  470. .img-box{
  471. width: 60upx;
  472. height: 60upx;
  473. // border-radius: 4upx;
  474. border: 1px solid #dddddd;
  475. display: flex;
  476. align-items: center;
  477. justify-content: center;
  478. image{
  479. width: 25rpx;
  480. height: 25rpx;
  481. }
  482. }
  483. input{
  484. width: 60upx;
  485. height: 60upx;
  486. line-height: 60upx;
  487. font-size: 28upx;
  488. font-family: PingFang SC;
  489. font-weight: 500;
  490. color: #111111;
  491. // border-radius: 4upx;
  492. border-top: 1px solid #dddddd;
  493. border-bottom: 1px solid #dddddd;
  494. text-align: center;
  495. // margin: 0 16upx;
  496. }
  497. }
  498. }
  499. }
  500. }
  501. }
  502. .like-product{
  503. padding-bottom: 120upx;
  504. }
  505. .btn-foot{
  506. box-sizing: border-box;
  507. width: 100%;
  508. height: 121upx;
  509. background: #FFFFFF;
  510. padding: 16upx 30upx 16upx 60upx;
  511. display: flex;
  512. align-items: center;
  513. justify-content: space-between;
  514. position: fixed;
  515. left: 0;
  516. bottom: 0;
  517. z-index: 99;
  518. .left{
  519. display: flex;
  520. align-items: center;
  521. .text{
  522. margin-left: 14upx;
  523. font-size: 28upx;
  524. font-family: PingFang SC;
  525. font-weight: 500;
  526. color: #666666;
  527. line-height: 1;
  528. }
  529. }
  530. .right{
  531. display: flex;
  532. align-items: center;
  533. .total{
  534. display: flex;
  535. align-items: flex-end;
  536. margin-right: 36upx;
  537. .label{
  538. font-size: 26upx;
  539. font-family: PingFang SC;
  540. font-weight: 500;
  541. color: #999999;
  542. line-height: 1.5;
  543. }
  544. .price{
  545. display: flex;
  546. align-items: flex-end;
  547. .unit{
  548. font-size: 32upx;
  549. font-family: PingFang SC;
  550. font-weight: bold;
  551. color: #FF6633;
  552. line-height: 1.2;
  553. margin-right: 10upx;
  554. }
  555. .num{
  556. font-size: 30upx;
  557. font-family: PingFang SC;
  558. font-weight: bold;
  559. color: #FF6633;
  560. line-height: 1;
  561. }
  562. }
  563. }
  564. .btn{
  565. width: 200upx;
  566. height: 88upx;
  567. line-height: 88upx;
  568. text-align: center;
  569. font-size: 30upx;
  570. font-family: PingFang SC;
  571. font-weight: bold;
  572. color: #FFFFFF;
  573. background: #2BC7B9;
  574. border-radius: 44upx;
  575. }
  576. }
  577. }
  578. }
  579. </style>