cart.vue 10 KB

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