cart.vue 14 KB

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