cart.vue 11 KB

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