cart.vue 12 KB

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