cart.vue 10 KB

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