cart.vue 9.8 KB

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