cart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. <view class="choose" @click.stop="checkChange(item,index)">
  7. <image src="https://hdtobs.obs.cn-north-4.myhuaweicloud.com/fs/20250729/1753758788583.png" v-show="item.checked"></image>
  8. <image src="https://hdtobs.obs.cn-north-4.myhuaweicloud.com/fs/20250729/1753758821601.png" v-show="!item.checked"></image>
  9. </view>
  10. <image class="goods-img" :src="item.imgUrl" mode="aspectFit" @click="showProduct(item)"></image>
  11. <view class="info-box" @click="showProduct(item)">
  12. <view>
  13. <view class="title-box">
  14. <view class="title ellipsis">{{ item.goodsName }}</view>
  15. </view>
  16. </view>
  17. <view class="price-num">
  18. <view class="price">
  19. <text class="text" >{{item.newIntegral}}</text>
  20. <text class="unit">积分</text>
  21. <text class="unit">+</text>
  22. <text class="text" >{{item.newCash.toFixed(2)}}</text>
  23. <text class="unit">元</text>
  24. </view>
  25. <view class="num-box" @click.stop>
  26. <u-number-box v-model="item.cartNum" buttonSize="48rpx" integer :step="1" :min="1" :max="100000" @change="changeCartNum($event,item)"></u-number-box>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <view v-if="carts.length == 0" class="no-data-box">
  33. <image src="https://hdtobs.obs.cn-north-4.myhuaweicloud.com/hdtappimgs/cf4a86b913a04341bb44e34bb4d37aa2.png" mode="aspectFit"></image>
  34. <view class="empty-title">购物车为空</view>
  35. <view class="empty-cart-btn x-f" @click="goPage">去逛逛</view>
  36. </view>
  37. <!-- 猜你喜欢 -->
  38. <!-- <view class="like-product">
  39. <likeProduct ref="product" />
  40. </view> -->
  41. <!-- 底部按钮 -->
  42. <view class="btn-foot">
  43. <view class="left">
  44. <view class="choose" @click="handleCheckAll()">
  45. <image src="https://hdtobs.obs.cn-north-4.myhuaweicloud.com/fs/20250729/1753758788583.png" v-show="checkAll"></image>
  46. <image src="https://hdtobs.obs.cn-north-4.myhuaweicloud.com/fs/20250729/1753758821601.png" v-show="!checkAll"></image>
  47. </view>
  48. <text class="text">全选</text>
  49. <text class="text" @click="delCart()">删除</text>
  50. </view>
  51. <view class="right">
  52. <view>
  53. <view class="total">
  54. <text class="label">合计:</text>
  55. <view class="price">
  56. <text class="unit">¥</text>
  57. <text class="num">{{totalMoney.toFixed(2)}}</text>
  58. </view>
  59. </view>
  60. <view class="total">
  61. <text class="label">所需积分:</text>
  62. <view class="price">
  63. <text class="num">{{totalIntegral}}</text>
  64. </view>
  65. </view>
  66. </view>
  67. <view class="btn" @click="submit">结算</view>
  68. </view>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import { getCarts,addOrUpdateCart,delCart } from '@/api/integral.js'
  74. // import likeProduct from '@/components/likeProduct.vue'
  75. export default {
  76. // components: {
  77. // likeProduct
  78. // },
  79. data() {
  80. return {
  81. totalMoney:0.00,
  82. totalIntegral: 0,
  83. carts:[],
  84. checkAll:false,
  85. }
  86. },
  87. onLoad() {
  88. // this.getCarts();
  89. },
  90. onShow() {
  91. this.getCarts();
  92. },
  93. onReachBottom() {
  94. // this.$refs.product.getGoodsProducts();
  95. },
  96. methods: {
  97. delCart(){
  98. var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  99. return ele.id
  100. });
  101. if(selectCarts.length==0){
  102. uni.showToast({
  103. icon:'none',
  104. title: "请选择商品删除",
  105. });
  106. return;
  107. }
  108. let data = {ids:selectCarts};
  109. delCart(selectCarts).then(
  110. res => {
  111. if(res.code==200){
  112. uni.showToast({
  113. icon:'success',
  114. title: "操作成功",
  115. });
  116. this.getCarts()
  117. }else{
  118. uni.showToast({
  119. icon:'none',
  120. title: res.msg,
  121. });
  122. }
  123. },
  124. rej => {}
  125. );
  126. },
  127. computedMoney(){
  128. let totalIntegral = 0;
  129. let totalMoney = 0;
  130. let arry = this.carts.filter(item=>item.checked)
  131. arry.forEach(item => {
  132. totalIntegral += item.newIntegral * item.cartNum;
  133. totalMoney += item.newCash * item.cartNum;
  134. });
  135. this.totalIntegral = totalIntegral;
  136. this.totalMoney = totalMoney;
  137. },
  138. handleCheckAll(){
  139. this.checkAll=!this.checkAll;
  140. var that=this;
  141. this.carts.forEach((item,index,arr)=>{
  142. item.checked=that.checkAll;
  143. })
  144. this.computedMoney();
  145. },
  146. checkChange(item,index){
  147. item.checked=!item.checked;
  148. this.checkAll = this.carts.length > 0 &&this.carts.every(item=>item.checked)
  149. this.computedMoney();
  150. },
  151. changeNum(e,item) {
  152. item.cartNum = e.detail.value.replace(/\D/g, '')
  153. if (item.cartNum <= 1) {
  154. uni.showToast({
  155. title: "已经是底线啦!",
  156. icon: "none",
  157. duration: 2000
  158. });
  159. return;
  160. }
  161. if(item.cartNum < 1) {
  162. item.cartNum = 1
  163. }
  164. if(item.cartNum>=item.stock){
  165. item.cartNum=item.stock;
  166. }
  167. this.changeCartNum(item)
  168. },
  169. getCarts(){
  170. getCarts().then(
  171. res => {
  172. if(res.code==200){
  173. this.carts=res.carts;
  174. this.carts.forEach((item,index,arr)=>{
  175. item.checked=false;
  176. })
  177. this.checkAll = this.carts.length > 0 &&this.carts.every(item=>item.checked)
  178. this.computedMoney();
  179. }else{
  180. uni.showToast({
  181. icon:'none',
  182. title: "请求失败",
  183. });
  184. }
  185. },
  186. rej => {}
  187. );
  188. },
  189. changeCartNum(e, item) {
  190. // 保存原始数量和索引
  191. const originalNum = item.cartNum;
  192. const itemIndex = this.carts.findIndex(cart => cart.id === item.id);
  193. // 先更新本地UI(立即响应)
  194. item.cartNum = e.value;
  195. let data = { cartNum: e.value, goodsId: item.goodsId, isCart: 1 };
  196. addOrUpdateCart(data).then(
  197. res => {
  198. if (res.code == 200) {
  199. this.computedMoney();
  200. } else {
  201. // 库存不足等错误情况
  202. // 恢复原始数量
  203. this.carts[itemIndex].cartNum = originalNum;
  204. // 如果需要,可以更新库存信息
  205. if (res.data && res.data.stock) {
  206. this.carts[itemIndex].stock = res.data.stock;
  207. }
  208. // 强制刷新
  209. this.$forceUpdate();
  210. // 计算总额
  211. this.computedMoney();
  212. uni.showToast({
  213. icon: 'none',
  214. title: res.msg,
  215. duration: 2000
  216. });
  217. }
  218. },
  219. rej => {
  220. // 网络错误
  221. this.carts[itemIndex].cartNum = originalNum;
  222. this.$forceUpdate();
  223. this.computedMoney();
  224. uni.showToast({
  225. icon: 'none',
  226. title: '网络错误,请重试',
  227. duration: 2000
  228. });
  229. }
  230. );
  231. },
  232. // 结算
  233. submit() {
  234. var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  235. return ele.id
  236. });
  237. if(selectCarts.length==0){
  238. uni.showToast({
  239. icon:'none',
  240. title: "请选择商品",
  241. });
  242. return;
  243. }
  244. uni.navigateTo({
  245. url: '/pages_user/user/confirmIntegralOrder?type=cart&cartIds='+selectCarts.toString()
  246. })
  247. },
  248. showProduct(item){
  249. console.log("这个item")
  250. uni.navigateTo({
  251. url: '/pages_user/user/integralGoodsDetails?goodsId='+item.goodsId
  252. })
  253. },
  254. goPage() {
  255. uni.navigateTo({
  256. url: '/pages_user/integralGoodsList'
  257. })
  258. }
  259. }
  260. }
  261. </script>
  262. <style lang="scss">
  263. page {
  264. height: 100%;
  265. }
  266. .content{
  267. height: 100%;
  268. padding: 20upx;
  269. .goods-list{
  270. .item{
  271. box-sizing: border-box;
  272. background: #FFFFFF;
  273. border-radius: 16upx;
  274. margin-bottom: 20upx;
  275. padding: 30upx;
  276. display: flex;
  277. align-items: center;
  278. &:last-child{
  279. margin-bottom: 0;
  280. }
  281. .goods-img{
  282. width: 160upx;
  283. height: 160upx;
  284. background: #FFFFFF;
  285. margin-right: 30upx;
  286. flex-shrink: 0;
  287. }
  288. .info-box{
  289. display: flex;
  290. flex-direction: column;
  291. justify-content: space-between;
  292. width: calc(100% - 255upx);
  293. .title-box{
  294. width: 100%;
  295. display: flex;
  296. align-items: center;
  297. .title{
  298. flex: 1;
  299. font-size: 28upx;
  300. font-family: PingFang SC;
  301. font-weight: 500;
  302. color: #111111;
  303. line-height: 1;
  304. }
  305. }
  306. .price-num{
  307. margin-top: 24rpx;
  308. .price{
  309. margin-bottom: 24rpx;
  310. display: flex;
  311. align-items: flex-end;
  312. .unit{
  313. font-size: 24upx;
  314. font-family: PingFang SC;
  315. font-weight: 500;
  316. color: #2BC7B9;
  317. line-height: 1.2;
  318. margin-right: 4upx;
  319. }
  320. .text{
  321. font-size: 32upx;
  322. font-family: PingFang SC;
  323. font-weight: bold;
  324. color: #2BC7B9;
  325. line-height: 1;
  326. }
  327. }
  328. .num-box{
  329. display: flex;
  330. align-items: center;
  331. justify-content: flex-end;
  332. .img-box{
  333. width: 60upx;
  334. height: 60upx;
  335. // border-radius: 4upx;
  336. border: 1px solid #dddddd;
  337. display: flex;
  338. align-items: center;
  339. justify-content: center;
  340. image{
  341. width: 25rpx;
  342. height: 25rpx;
  343. }
  344. }
  345. input{
  346. width: 60upx;
  347. height: 60upx;
  348. line-height: 60upx;
  349. font-size: 28upx;
  350. font-family: PingFang SC;
  351. font-weight: 500;
  352. color: #111111;
  353. // border-radius: 4upx;
  354. border-top: 1px solid #dddddd;
  355. border-bottom: 1px solid #dddddd;
  356. text-align: center;
  357. // margin: 0 16upx;
  358. }
  359. }
  360. }
  361. }
  362. }
  363. }
  364. .like-product{
  365. padding-bottom: 120upx;
  366. }
  367. .btn-foot{
  368. box-sizing: border-box;
  369. width: 100%;
  370. height: 121upx;
  371. background: #FFFFFF;
  372. padding: 16upx 30upx 16upx 30upx;
  373. display: flex;
  374. align-items: center;
  375. justify-content: space-between;
  376. position: fixed;
  377. left: 0;
  378. bottom: 0;
  379. z-index: 99;
  380. .left{
  381. display: flex;
  382. align-items: center;
  383. .text{
  384. margin-left: 14upx;
  385. font-size: 28upx;
  386. font-family: PingFang SC;
  387. font-weight: 500;
  388. color: #666666;
  389. line-height: 1;
  390. }
  391. }
  392. .right{
  393. display: flex;
  394. align-items: center;
  395. .total{
  396. display: flex;
  397. align-items: flex-end;
  398. margin-right: 36upx;
  399. .label{
  400. font-size: 26upx;
  401. font-family: PingFang SC;
  402. font-weight: 500;
  403. color: #999999;
  404. line-height: 1.5;
  405. }
  406. .price{
  407. display: flex;
  408. align-items: flex-end;
  409. .unit{
  410. font-size: 24upx;
  411. font-family: PingFang SC;
  412. font-weight: bold;
  413. color: #2BC7B9;
  414. line-height: 1.2;
  415. margin-right: 10upx;
  416. }
  417. .num{
  418. font-size: 32rpx;
  419. font-family: PingFang SC;
  420. font-weight: bold;
  421. color: #2BC7B9;
  422. line-height: 1;
  423. }
  424. }
  425. }
  426. .btn{
  427. width: 200upx;
  428. height: 88upx;
  429. line-height: 88upx;
  430. text-align: center;
  431. font-size: 30upx;
  432. font-family: PingFang SC;
  433. font-weight: bold;
  434. color: #FFFFFF;
  435. background: #2BC7B9;
  436. border-radius: 44upx;
  437. }
  438. }
  439. }
  440. }
  441. .choose {
  442. margin-right: 20rpx;
  443. image{
  444. width: 36rpx;
  445. height: 36rpx;
  446. flex-shrink: 0;
  447. }
  448. }
  449. .empty-cart-btn {
  450. border: 1rpx solid #2BC7B9;
  451. color: #2BC7B9;
  452. padding: 10rpx 30rpx;
  453. border-radius: 40rpx;
  454. margin-bottom: 20vh;
  455. margin-top: 50rpx;
  456. display: inline-block;
  457. }
  458. </style>