cart.vue 10 KB

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