cart.vue 10 KB

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