cart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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)" />
  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)" />
  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">{{utils.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="../../static/images/jian.png" mode=""></image>
  33. <image v-else src="../../static/images/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="../../static/images/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="../../static/images/no_data.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()" />
  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'
  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. onLoad() {
  90. // this.getCarts();
  91. },
  92. onShow() {
  93. this.getCarts();
  94. },
  95. onReachBottom() {
  96. this.$refs.product.getGoodsProducts();
  97. },
  98. methods: {
  99. delCart(){
  100. var selectCarts = this.carts.flatMap(item => item.list.filter(listItem => listItem.checked === true)).map(el => el.id);
  101. if(selectCarts.length==0){
  102. uni.showToast({
  103. icon:'none',
  104. title: "请选择商品删除",
  105. });
  106. return;
  107. }
  108. let data = {ids:selectCarts};
  109. delCart(data).then(
  110. res => {
  111. if(res.code==200){
  112. uni.showToast({
  113. icon:'success',
  114. title: "操作成功",
  115. });
  116. this.getCarts()
  117. }else{
  118. uni.showToast({
  119. icon:'none',
  120. title: res.msg,
  121. });
  122. }
  123. },
  124. rej => {}
  125. );
  126. console.log(selectCarts)
  127. },
  128. computedMoney(){
  129. var money=0;
  130. var that=this;
  131. this.carts.forEach((item,index,arr)=>{
  132. item.list.forEach(it => {
  133. if(it.checked){
  134. money+=it.price*it.cartNum;
  135. }
  136. });
  137. })
  138. console.log(money);
  139. this.totalMoney=money;
  140. },
  141. handleCheckAll(){
  142. this.checkAll=!this.checkAll;
  143. var that=this;
  144. this.carts.forEach((item,index,arr)=>{
  145. item.checked=that.checkAll;
  146. item.list.forEach(it => {
  147. it.checked=that.checkAll;
  148. });
  149. })
  150. this.computedMoney();
  151. },
  152. checkShopChange(item) {
  153. item.checked = !item.checked;
  154. item.list.forEach(it => {
  155. it.checked= item.checked;
  156. });
  157. this.computedMoney();
  158. },
  159. checkChange(item,shop){
  160. item.checked=!item.checked;
  161. shop.checked = shop.list.every(it => it.checked == true);
  162. this.computedMoney();
  163. },
  164. changeNum(e,item) {
  165. item.cartNum = e.detail.value.replace(/\D/g, '')
  166. if (item.cartNum <= 1) {
  167. uni.showToast({
  168. title: "已经是底线啦!",
  169. icon: "none",
  170. duration: 2000
  171. });
  172. return;
  173. }
  174. if(item.cartNum < 1) {
  175. item.cartNum = 1
  176. }
  177. if(item.cartNum>=item.stock){
  178. item.cartNum=item.stock;
  179. }
  180. this.changeCartNum(item)
  181. },
  182. changeCartNum(item){
  183. let data = {number:item.cartNum,id:item.id};
  184. cartNum(data).then(
  185. res => {
  186. if(res.code==200){
  187. uni.showToast({
  188. icon:'none',
  189. title: "操作成功",
  190. });
  191. this.computedMoney();
  192. }else{
  193. uni.showToast({
  194. icon:'none',
  195. title: res.msg,
  196. });
  197. }
  198. },
  199. rej => {}
  200. );
  201. },
  202. getCarts(){
  203. getCarts().then(
  204. res => {
  205. if(res.code==200){
  206. this.carts=res.carts;
  207. this.carts.forEach(item => {
  208. item.checked = false;
  209. item.list.forEach(it => {
  210. it.checked = false;
  211. });
  212. });
  213. this.computedMoney();
  214. }else{
  215. uni.showToast({
  216. icon:'none',
  217. title: "请求失败",
  218. });
  219. }
  220. },
  221. rej => {}
  222. );
  223. },
  224. // 购物车减法
  225. delNum(item) {
  226. if (item.cartNum <= 1) {
  227. uni.showToast({
  228. title: "已经是底线啦!",
  229. icon: "none",
  230. duration: 2000
  231. });
  232. return;
  233. }
  234. item.cartNum --
  235. if(item.cartNum < 1) {
  236. item.cartNum = 1
  237. }
  238. this.changeCartNum(item)
  239. },
  240. // 购物车加法
  241. addNum(item) {
  242. console.log(item)
  243. item.cartNum++
  244. if(item.cartNum>=item.stock){
  245. item.cartNum=item.stock;
  246. }
  247. this.changeCartNum(item)
  248. },
  249. // 结算
  250. submit() {
  251. let selectCarts = this.carts
  252. .filter(item => item.list.some(listItem => listItem.checked === true))
  253. .map(item => ({
  254. storeId: item.list[0].storeId || "",
  255. data: {
  256. type: "cart",
  257. cartIds: item.list.filter(it=>it.checked == true).map(it => it.id).join(","),
  258. }
  259. }));
  260. if(selectCarts.length==0){
  261. uni.showToast({
  262. icon:'none',
  263. title: "请选择商品",
  264. });
  265. return;
  266. }
  267. uni.navigateTo({
  268. url: './confirmOrder?type=cart&confirmParam='+ encodeURIComponent(JSON.stringify(selectCarts))
  269. })
  270. },
  271. showProduct(item){
  272. uni.navigateTo({
  273. url: '../shopping/productDetails?productId='+item.productId
  274. })
  275. },
  276. }
  277. }
  278. </script>
  279. <style lang="scss">
  280. page {
  281. height: 100%;
  282. }
  283. .content{
  284. height: 100%;
  285. padding: 20upx;
  286. .shopbox {
  287. background: #FFFFFF;
  288. border-radius: 16rpx;
  289. margin-bottom: 20rpx;
  290. }
  291. .shopbox-name {
  292. padding: 30rpx 30rpx 0 30rpx;
  293. font-family: PingFang SC, PingFang SC;
  294. font-weight: 400;
  295. font-size: 30rpx;
  296. color: #111;
  297. overflow: hidden;
  298. white-space: nowrap;
  299. text-overflow: ellipsis;
  300. }
  301. .goods-list{
  302. .item{
  303. box-sizing: border-box;
  304. height: 221upx;
  305. background: #FFFFFF;
  306. border-radius: 16upx;
  307. margin-bottom: 20upx;
  308. padding: 30upx;
  309. display: flex;
  310. align-items: center;
  311. &:last-child{
  312. margin-bottom: 0;
  313. }
  314. .goods-img{
  315. width: 160upx;
  316. height: 160upx;
  317. background: #FFFFFF;
  318. margin-right: 30upx;
  319. flex-shrink: 0;
  320. }
  321. .info-box{
  322. height: 160upx;
  323. display: flex;
  324. flex-direction: column;
  325. justify-content: space-between;
  326. width: calc(100% - 255upx);
  327. .title-box{
  328. width: 100%;
  329. display: flex;
  330. align-items: center;
  331. .tag{
  332. padding: 0 6upx;
  333. height: 30upx;
  334. line-height: 30upx;
  335. font-size: 22upx;
  336. font-family: PingFang SC;
  337. font-weight: bold;
  338. color: #FFFFFF;
  339. background: linear-gradient(90deg, #66b2ef 0%, #0bb3f2 100%);
  340. border-radius: 4upx;
  341. margin-right: 10upx;
  342. flex-shrink: 0;
  343. }
  344. .title{
  345. flex: 1;
  346. font-size: 28upx;
  347. font-family: PingFang SC;
  348. font-weight: 500;
  349. color: #111111;
  350. line-height: 1;
  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>