cart.vue 13 KB

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