cart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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)" />
  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)" />
  15. </label>
  16. <image class="goods-img" :src="item.productAttrImage==null||item.productAttrImage==''?item.productImage:item.productAttrImage" mode="aspectFit"></image>
  17. <view class="info-box">
  18. <view>
  19. <view class="title-box">
  20. <view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view>
  21. <view class="title ellipsis">{{ 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">
  31. <view class="img-box" @click="delNum(item)">
  32. <image v-if="item.cartNum <= 1" src="../../static/images/jian.png" mode=""></image>
  33. <image v-else src="../../static/images/jian2.png" mode=""></image>
  34. </view>
  35. <input type="text" @change="changeNum($event,item)" :value="item.cartNum" />
  36. <view class="img-box" @click="addNum(item)">
  37. <image src="../../static/images/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="../../static/images/no_data.png" mode="aspectFit"></image>
  47. <view class="empty-title">暂无数据</view>
  48. </view>
  49. <!-- 底部按钮 -->
  50. <view class="btn-foot">
  51. <view class="left">
  52. <label>
  53. <checkbox :checked="checkAll" @click="handleCheckAll()" />
  54. </label>
  55. <text class="text">全选</text>
  56. <text class="text" @click="delCart()">删除</text>
  57. </view>
  58. <view class="right">
  59. <view class="total">
  60. <text class="label">合计:</text>
  61. <view class="price">
  62. <text class="unit">¥</text>
  63. <text class="num">{{totalMoney.toFixed(2)}}</text>
  64. </view>
  65. </view>
  66. <view class="btn" @click="submit">制单</view>
  67. </view>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import {getCarts,cartNum,delCart} from '@/api/product'
  73. import {createSalesOrder} from '@/api/companyOrder.js'
  74. export default {
  75. data() {
  76. return {
  77. totalMoney:0.00,
  78. carts:[],
  79. checkAll:false,
  80. }
  81. },
  82. onLoad() {
  83. this.getCarts();
  84. },
  85. methods: {
  86. changeNum(e,item) {
  87. item.cartNum = e.detail.value.replace(/\D/g, '')
  88. if (item.cartNum <= 1) {
  89. uni.showToast({
  90. title: "已经是底线啦!",
  91. icon: "none",
  92. duration: 2000
  93. });
  94. return;
  95. }
  96. if(item.cartNum < 1) {
  97. item.cartNum = 1
  98. }
  99. if(item.cartNum>=item.stock){
  100. item.cartNum=item.stock;
  101. }
  102. this.changeCartNum(item)
  103. },
  104. delCart(){
  105. var selectCarts = this.carts.flatMap(item => item.list.filter(listItem => listItem.checked === true)).map(el => el.id);
  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. },
  132. computedMoney(){
  133. var money=0;
  134. var that=this;
  135. this.carts.forEach((item,index,arr)=>{
  136. item.list.forEach(it => {
  137. if(it.checked){
  138. money+=it.price*it.cartNum;
  139. }
  140. });
  141. })
  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. item.list.forEach(it => {
  150. it.checked=that.checkAll;
  151. });
  152. })
  153. this.computedMoney();
  154. },
  155. checkShopChange(item) {
  156. item.checked = !item.checked;
  157. item.list.forEach(it => {
  158. it.checked= item.checked;
  159. });
  160. this.computedMoney();
  161. },
  162. checkChange(item,shop){
  163. item.checked=!item.checked;
  164. shop.checked = shop.list.every(it => it.checked == true);
  165. this.checkAll = this.carts.every(item=>item.checked)
  166. this.computedMoney();
  167. },
  168. changeCartNum(item){
  169. let data = {number:item.cartNum,id:item.id};
  170. cartNum(data).then(
  171. res => {
  172. if(res.code==200){
  173. uni.showToast({
  174. icon:'none',
  175. title: "操作成功",
  176. });
  177. this.computedMoney();
  178. }else{
  179. uni.showToast({
  180. icon:'none',
  181. title: res.msg,
  182. });
  183. }
  184. },
  185. rej => {}
  186. );
  187. },
  188. getCarts(){
  189. getCarts().then(
  190. res => {
  191. if(res.code==200){
  192. this.carts=res.carts;
  193. this.carts.forEach(item => {
  194. item.checked = false;
  195. item.list.forEach(it => {
  196. it.checked = false;
  197. });
  198. });
  199. this.computedMoney();
  200. }else{
  201. uni.showToast({
  202. icon:'none',
  203. title: "请求失败",
  204. });
  205. }
  206. },
  207. rej => {}
  208. );
  209. },
  210. // 购物车减法
  211. delNum(item) {
  212. if (item.cartNum <= 1) {
  213. uni.showToast({
  214. title: "已经是底线啦!",
  215. icon: "none",
  216. duration: 2000
  217. });
  218. return;
  219. }
  220. item.cartNum --
  221. if(item.cartNum < 1) {
  222. item.cartNum = 1
  223. }
  224. this.changeCartNum(item)
  225. },
  226. // 购物车加法
  227. addNum(item) {
  228. item.cartNum++
  229. if(item.cartNum>=item.stock){
  230. item.cartNum=item.stock;
  231. }
  232. this.changeCartNum(item)
  233. },
  234. // 结算
  235. submit() {
  236. // var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  237. // return ele.id
  238. // });
  239. let selectCarts = this.carts
  240. .filter(item => item.list.some(listItem => listItem.checked === true))
  241. .map(item => ({
  242. storeId: item.list[0].storeId || "",
  243. cartIds: item.list.filter(it=>it.checked == true).map(it => it.id).join(","),
  244. }));
  245. if(selectCarts.length==0){
  246. uni.showToast({
  247. icon:'none',
  248. title: "请选择商品",
  249. });
  250. return;
  251. }
  252. var data={token:uni.getStorageSync('CompanyUserToken'),data:selectCarts}
  253. createSalesOrder(data).then(
  254. res => {
  255. if(res.code==200){
  256. uni.navigateTo({
  257. url: './confirmCompanyOrder?orderKey='+res.orderKey
  258. })
  259. }else{
  260. uni.showToast({
  261. icon:'none',
  262. title: "请求失败",
  263. });
  264. }
  265. },
  266. rej => {}
  267. );
  268. },
  269. }
  270. }
  271. </script>
  272. <style lang="scss">
  273. page {
  274. height: 100%;
  275. }
  276. .content{
  277. // height: 100%;
  278. padding: 20upx;
  279. padding-bottom: 140upx;
  280. .shopbox {
  281. background: #FFFFFF;
  282. border-radius: 16rpx;
  283. margin-bottom: 20rpx;
  284. }
  285. .shopbox-name {
  286. padding: 30rpx 30rpx 0 30rpx;
  287. font-family: PingFang SC, PingFang SC;
  288. font-weight: 400;
  289. font-size: 30rpx;
  290. color: #111;
  291. overflow: hidden;
  292. white-space: nowrap;
  293. text-overflow: ellipsis;
  294. }
  295. .goods-list{
  296. // padding-bottom: 120upx;
  297. .item{
  298. box-sizing: border-box;
  299. height: 221upx;
  300. background: #FFFFFF;
  301. border-radius: 16upx;
  302. margin-bottom: 20upx;
  303. padding: 30upx;
  304. display: flex;
  305. align-items: center;
  306. &:last-child{
  307. margin-bottom: 0;
  308. }
  309. .goods-img{
  310. width: 160upx;
  311. height: 160upx;
  312. background: #FFFFFF;
  313. margin-right: 30upx;
  314. flex-shrink: 0;
  315. }
  316. .info-box{
  317. height: 160upx;
  318. display: flex;
  319. flex-direction: column;
  320. justify-content: space-between;
  321. width: calc(100% - 255upx);
  322. .title-box{
  323. width: 100%;
  324. display: flex;
  325. align-items: center;
  326. .tag{
  327. padding: 0 6upx;
  328. height: 30upx;
  329. line-height: 30upx;
  330. font-size: 22upx;
  331. font-family: PingFang SC;
  332. font-weight: bold;
  333. color: #FFFFFF;
  334. background: linear-gradient(90deg, #66b2ef 0%, #0bb3f2 100%);
  335. border-radius: 4upx;
  336. margin-right: 10upx;
  337. flex-shrink: 0;
  338. }
  339. .title{
  340. flex: 1;
  341. font-size: 28upx;
  342. font-family: PingFang SC;
  343. font-weight: 500;
  344. color: #111111;
  345. line-height: 1;
  346. }
  347. }
  348. .intro{
  349. font-size: 24upx;
  350. font-family: PingFang SC;
  351. font-weight: 500;
  352. color: #999999;
  353. margin-top: 22upx;
  354. line-height: 1;
  355. }
  356. .price-num{
  357. display: flex;
  358. align-items: center;
  359. justify-content: space-between;
  360. .price{
  361. display: flex;
  362. align-items: flex-end;
  363. .unit{
  364. font-size: 24upx;
  365. font-family: PingFang SC;
  366. font-weight: 500;
  367. color: #FF6633;
  368. line-height: 1.2;
  369. margin-right: 4upx;
  370. }
  371. .text{
  372. font-size: 32upx;
  373. font-family: PingFang SC;
  374. font-weight: bold;
  375. color: #FF6633;
  376. line-height: 1;
  377. }
  378. }
  379. .num-box{
  380. display: flex;
  381. align-items: center;
  382. .img-box{
  383. width: 60upx;
  384. height: 60upx;
  385. // border-radius: 4upx;
  386. border: 1px solid #dddddd;
  387. display: flex;
  388. align-items: center;
  389. justify-content: center;
  390. image{
  391. width: 25rpx;
  392. height: 25rpx;
  393. }
  394. }
  395. input{
  396. width: 60upx;
  397. height: 60upx;
  398. line-height: 60upx;
  399. font-size: 28upx;
  400. font-family: PingFang SC;
  401. font-weight: 500;
  402. color: #111111;
  403. // border-radius: 4upx;
  404. border-top: 1px solid #dddddd;
  405. border-bottom: 1px solid #dddddd;
  406. text-align: center;
  407. // margin: 0 16upx;
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. .btn-foot{
  415. box-sizing: border-box;
  416. width: 100%;
  417. height: 121upx;
  418. background: #FFFFFF;
  419. padding: 16upx 30upx 16upx 60upx;
  420. display: flex;
  421. align-items: center;
  422. justify-content: space-between;
  423. position: fixed;
  424. left: 0;
  425. bottom: 0;
  426. z-index: 99;
  427. .left{
  428. display: flex;
  429. align-items: center;
  430. .text{
  431. margin-left: 14upx;
  432. font-size: 28upx;
  433. font-family: PingFang SC;
  434. font-weight: 500;
  435. color: #666666;
  436. line-height: 1;
  437. }
  438. }
  439. .right{
  440. display: flex;
  441. align-items: center;
  442. .total{
  443. display: flex;
  444. align-items: flex-end;
  445. margin-right: 36upx;
  446. .label{
  447. font-size: 26upx;
  448. font-family: PingFang SC;
  449. font-weight: 500;
  450. color: #999999;
  451. line-height: 1.5;
  452. }
  453. .price{
  454. display: flex;
  455. align-items: flex-end;
  456. .unit{
  457. font-size: 32upx;
  458. font-family: PingFang SC;
  459. font-weight: bold;
  460. color: #FF6633;
  461. line-height: 1.2;
  462. margin-right: 10upx;
  463. }
  464. .num{
  465. font-size: 30upx;
  466. font-family: PingFang SC;
  467. font-weight: bold;
  468. color: #FF6633;
  469. line-height: 1;
  470. }
  471. }
  472. }
  473. .btn{
  474. width: 200upx;
  475. height: 88upx;
  476. line-height: 88upx;
  477. text-align: center;
  478. font-size: 30upx;
  479. font-family: PingFang SC;
  480. font-weight: bold;
  481. color: #FFFFFF;
  482. background: #0bb3f2;
  483. border-radius: 44upx;
  484. }
  485. }
  486. }
  487. }
  488. </style>