cart.vue 12 KB

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