cart.vue 13 KB

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