cart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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" @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. </view>
  67. </template>
  68. <script>
  69. import {getCarts,cartNum,delCart} from '@/api/product'
  70. import likeProduct from '@/components/likeProduct.vue'
  71. export default {
  72. components: {
  73. likeProduct
  74. },
  75. data() {
  76. return {
  77. totalMoney:0.00,
  78. carts:[],
  79. checkAll:false,
  80. }
  81. },
  82. onLoad() {
  83. this.getCarts();
  84. },
  85. onReachBottom() {
  86. this.$refs.product.getGoodsProducts();
  87. },
  88. methods: {
  89. delCart(){
  90. var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  91. return ele.id
  92. });
  93. if(selectCarts.length==0){
  94. uni.showToast({
  95. icon:'none',
  96. title: "请选择商品删除",
  97. });
  98. return;
  99. }
  100. let data = {ids:selectCarts};
  101. delCart(data).then(
  102. res => {
  103. if(res.code==200){
  104. uni.showToast({
  105. icon:'success',
  106. title: "操作成功",
  107. });
  108. this.getCarts()
  109. }else{
  110. uni.showToast({
  111. icon:'none',
  112. title: res.msg,
  113. });
  114. }
  115. },
  116. rej => {}
  117. );
  118. console.log(selectCarts)
  119. },
  120. computedMoney(){
  121. var money=0;
  122. var that=this;
  123. this.carts.forEach((item,index,arr)=>{
  124. if(item.checked){
  125. money+=item.price*item.cartNum;
  126. }
  127. })
  128. this.totalMoney=money;
  129. },
  130. handleCheckAll(){
  131. this.checkAll=!this.checkAll;
  132. var that=this;
  133. this.carts.forEach((item,index,arr)=>{
  134. item.checked=that.checkAll;
  135. })
  136. this.computedMoney();
  137. },
  138. checkChange(item){
  139. item.checked=!item.checked;
  140. this.checkAll = this.carts.every(item=>item.checked)
  141. this.computedMoney();
  142. },
  143. // changeNum(e,item) {
  144. // item.cartNum = e.detail.value.replace(/\D/g, '')
  145. // if (item.cartNum <= 1) {
  146. // uni.showToast({
  147. // title: "已经是底线啦!",
  148. // icon: "none",
  149. // duration: 2000
  150. // });
  151. // return;
  152. // }
  153. // if(item.cartNum < 1) {
  154. // item.cartNum = 1
  155. // }
  156. // if(item.cartNum>=item.stock){
  157. // item.cartNum=item.stock;
  158. // }
  159. // this.changeCartNum(item)
  160. // },
  161. changeCartNum(item){
  162. let data = {number:item.cartNum,id:item.id};
  163. cartNum(data).then(
  164. res => {
  165. if(res.code==200){
  166. uni.showToast({
  167. icon:'none',
  168. title: "操作成功",
  169. });
  170. this.computedMoney();
  171. }else{
  172. uni.showToast({
  173. icon:'none',
  174. title: res.msg,
  175. });
  176. }
  177. },
  178. rej => {}
  179. );
  180. },
  181. changeNum(e, item) {
  182. let value = e.detail.value.replace(/\D/g, '')
  183. // 转换为数字
  184. let num = parseInt(value)
  185. // 检查是否为有效数字
  186. if (isNaN(num) || value === '') {
  187. num = 1
  188. uni.showToast({
  189. title: "数量不能为空",
  190. icon: "none",
  191. duration: 2000
  192. });
  193. }
  194. // 限制最小值
  195. if (num < 1) {
  196. num = 1
  197. uni.showToast({
  198. title: "最小数量为1",
  199. icon: "none",
  200. duration: 2000
  201. });
  202. }
  203. // 限制最大值(库存)
  204. if (num > item.stock) {
  205. num = item.stock
  206. uni.showToast({
  207. title: "库存不足,已设为最大库存",
  208. icon: "none",
  209. duration: 2000
  210. });
  211. }
  212. item.cartNum = num
  213. this.changeCartNum(item)
  214. },
  215. getCarts(){
  216. getCarts().then(
  217. res => {
  218. if(res.code==200){
  219. this.carts=res.carts;
  220. this.carts.forEach((item,index,arr)=>{
  221. item.checked=false;
  222. })
  223. this.computedMoney();
  224. }else{
  225. uni.showToast({
  226. icon:'none',
  227. title: "请求失败",
  228. });
  229. }
  230. },
  231. rej => {}
  232. );
  233. },
  234. // 购物车减法
  235. delNum(item) {
  236. if (item.cartNum <= 1) {
  237. uni.showToast({
  238. title: "已经是底线啦!",
  239. icon: "none",
  240. duration: 2000
  241. });
  242. return;
  243. }
  244. item.cartNum --
  245. if(item.cartNum < 1) {
  246. item.cartNum = 1
  247. }
  248. this.changeCartNum(item)
  249. },
  250. // 购物车加法
  251. addNum(item) {
  252. console.log(item)
  253. item.cartNum++
  254. if(item.cartNum>=item.stock){
  255. item.cartNum=item.stock;
  256. }
  257. this.changeCartNum(item)
  258. },
  259. // 结算
  260. submit() {
  261. var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  262. return ele.id
  263. });
  264. if(selectCarts.length==0){
  265. uni.showToast({
  266. icon:'none',
  267. title: "请选择商品",
  268. });
  269. return;
  270. }
  271. uni.navigateTo({
  272. url: './confirmOrder?type=cart&cartIds='+selectCarts.toString()
  273. })
  274. },
  275. showProduct(item){
  276. uni.navigateTo({
  277. url: '../shopping/productDetails?productId='+item.productId
  278. })
  279. },
  280. }
  281. }
  282. </script>
  283. <style lang="scss">
  284. page {
  285. height: 100%;
  286. }
  287. .content{
  288. height: 100%;
  289. padding: 20upx;
  290. .goods-list{
  291. .item{
  292. box-sizing: border-box;
  293. height: 221upx;
  294. background: #FFFFFF;
  295. border-radius: 16upx;
  296. margin-bottom: 20upx;
  297. padding: 30upx;
  298. display: flex;
  299. align-items: center;
  300. &:last-child{
  301. margin-bottom: 0;
  302. }
  303. .goods-img{
  304. width: 160upx;
  305. height: 160upx;
  306. background: #FFFFFF;
  307. margin-right: 30upx;
  308. flex-shrink: 0;
  309. }
  310. .info-box{
  311. height: 160upx;
  312. display: flex;
  313. flex-direction: column;
  314. justify-content: space-between;
  315. width: calc(100% - 255upx);
  316. .title-box{
  317. width: 100%;
  318. display: flex;
  319. align-items: center;
  320. .tag{
  321. padding: 0 6upx;
  322. height: 30upx;
  323. line-height: 30upx;
  324. font-size: 22upx;
  325. font-family: PingFang SC;
  326. font-weight: bold;
  327. color: #FFFFFF;
  328. background: linear-gradient(90deg, #66b2ef 0%, #2BC7B9 100%);
  329. border-radius: 4upx;
  330. margin-right: 10upx;
  331. flex-shrink: 0;
  332. }
  333. .title{
  334. flex: 1;
  335. font-size: 28upx;
  336. font-family: PingFang SC;
  337. font-weight: 500;
  338. color: #111111;
  339. line-height: 1;
  340. }
  341. }
  342. .intro{
  343. font-size: 24upx;
  344. font-family: PingFang SC;
  345. font-weight: 500;
  346. color: #999999;
  347. margin-top: 22upx;
  348. line-height: 1;
  349. }
  350. .price-num{
  351. display: flex;
  352. align-items: center;
  353. justify-content: space-between;
  354. .price{
  355. display: flex;
  356. align-items: flex-end;
  357. .unit{
  358. font-size: 24upx;
  359. font-family: PingFang SC;
  360. font-weight: 500;
  361. color: #FF6633;
  362. line-height: 1.2;
  363. margin-right: 4upx;
  364. }
  365. .text{
  366. font-size: 32upx;
  367. font-family: PingFang SC;
  368. font-weight: bold;
  369. color: #FF6633;
  370. line-height: 1;
  371. }
  372. }
  373. .num-box{
  374. display: flex;
  375. align-items: center;
  376. .img-box{
  377. width: 60upx;
  378. height: 60upx;
  379. // border-radius: 4upx;
  380. border: 1px solid #dddddd;
  381. display: flex;
  382. align-items: center;
  383. justify-content: center;
  384. image{
  385. width: 25rpx;
  386. height: 25rpx;
  387. }
  388. }
  389. input{
  390. width: 60upx;
  391. height: 60upx;
  392. line-height: 60upx;
  393. font-size: 28upx;
  394. font-family: PingFang SC;
  395. font-weight: 500;
  396. color: #111111;
  397. // border-radius: 4upx;
  398. border-top: 1px solid #dddddd;
  399. border-bottom: 1px solid #dddddd;
  400. text-align: center;
  401. // margin: 0 16upx;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. .like-product{
  409. padding-bottom: 120upx;
  410. }
  411. .btn-foot{
  412. box-sizing: border-box;
  413. width: 100%;
  414. height: 121upx;
  415. background: #FFFFFF;
  416. padding: 16upx 30upx 16upx 60upx;
  417. display: flex;
  418. align-items: center;
  419. justify-content: space-between;
  420. position: fixed;
  421. left: 0;
  422. bottom: 0;
  423. z-index: 99;
  424. .left{
  425. display: flex;
  426. align-items: center;
  427. .text{
  428. margin-left: 14upx;
  429. font-size: 28upx;
  430. font-family: PingFang SC;
  431. font-weight: 500;
  432. color: #666666;
  433. line-height: 1;
  434. }
  435. }
  436. .right{
  437. display: flex;
  438. align-items: center;
  439. .total{
  440. display: flex;
  441. align-items: flex-end;
  442. margin-right: 36upx;
  443. .label{
  444. font-size: 26upx;
  445. font-family: PingFang SC;
  446. font-weight: 500;
  447. color: #999999;
  448. line-height: 1.5;
  449. }
  450. .price{
  451. display: flex;
  452. align-items: flex-end;
  453. .unit{
  454. font-size: 32upx;
  455. font-family: PingFang SC;
  456. font-weight: bold;
  457. color: #FF6633;
  458. line-height: 1.2;
  459. margin-right: 10upx;
  460. }
  461. .num{
  462. font-size: 30upx;
  463. font-family: PingFang SC;
  464. font-weight: bold;
  465. color: #FF6633;
  466. line-height: 1;
  467. }
  468. }
  469. }
  470. .btn{
  471. width: 200upx;
  472. height: 88upx;
  473. line-height: 88upx;
  474. text-align: center;
  475. font-size: 30upx;
  476. font-family: PingFang SC;
  477. font-weight: bold;
  478. color: #FFFFFF;
  479. background: #2BC7B9;
  480. border-radius: 44upx;
  481. }
  482. }
  483. }
  484. }
  485. </style>