cart.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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="https://bjczwh.oss-cn-beijing.aliyuncs.com/app/shop/images/jian.png" mode=""></image>
  26. <image v-else src="https://bjczwh.oss-cn-beijing.aliyuncs.com/app/shop/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="https://bjczwh.oss-cn-beijing.aliyuncs.com/app/shop/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. if(item.cartNum>=item.purchaseLimit&&item.purchaseLimit!=0){
  205. uni.showToast({
  206. icon:'none',
  207. title: `本商品限购${item.purchaseLimit}件,您已购买${item.purchaseLimit}件,本次最多可购买${item.purchaseLimit}件`,
  208. });
  209. return
  210. }
  211. if(item.cartNum>=item.stock){
  212. uni.showToast({
  213. icon:'none',
  214. title: `本商品库存${item.stock}件,您已购买${item.stock}件,本次最多可购买${item.stock}件`,
  215. });
  216. return
  217. }
  218. item.cartNum++
  219. if(item.cartNum>=item.stock){
  220. item.cartNum=item.stock;
  221. }
  222. this.changeCartNum(item)
  223. },
  224. // 结算
  225. submit() {
  226. var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
  227. return ele.id
  228. });
  229. if(selectCarts.length==0){
  230. uni.showToast({
  231. icon:'none',
  232. title: "请选择商品",
  233. });
  234. return;
  235. }
  236. var data={token:uni.getStorageSync('CompanyUserToken'),cateIds:selectCarts.toString()}
  237. createSalesOrder(data).then(
  238. res => {
  239. if(res.code==200){
  240. uni.navigateTo({
  241. url: './confirmCompanyOrder?orderKey='+res.orderKey
  242. })
  243. }else{
  244. uni.showToast({
  245. icon:'none',
  246. title: "请求失败",
  247. });
  248. }
  249. },
  250. rej => {}
  251. );
  252. },
  253. }
  254. }
  255. </script>
  256. <style lang="scss">
  257. page {
  258. height: 100%;
  259. }
  260. .content{
  261. height: 100%;
  262. padding: 20upx;
  263. .goods-list{
  264. padding-bottom: 120upx;
  265. .item{
  266. box-sizing: border-box;
  267. height: 221upx;
  268. background: #FFFFFF;
  269. border-radius: 16upx;
  270. margin-bottom: 20upx;
  271. padding: 30upx;
  272. display: flex;
  273. align-items: center;
  274. &:last-child{
  275. margin-bottom: 0;
  276. }
  277. .goods-img{
  278. width: 160upx;
  279. height: 160upx;
  280. background: #FFFFFF;
  281. margin-right: 30upx;
  282. flex-shrink: 0;
  283. }
  284. .info-box{
  285. height: 160upx;
  286. display: flex;
  287. flex-direction: column;
  288. justify-content: space-between;
  289. width: calc(100% - 255upx);
  290. .title-box{
  291. width: 100%;
  292. display: flex;
  293. align-items: center;
  294. .tag{
  295. padding: 0 6upx;
  296. height: 30upx;
  297. line-height: 30upx;
  298. font-size: 22upx;
  299. font-family: PingFang SC;
  300. font-weight: bold;
  301. color: #FFFFFF;
  302. background: linear-gradient(90deg, #66b2ef 0%, #2BC7B9 100%);
  303. border-radius: 4upx;
  304. margin-right: 10upx;
  305. flex-shrink: 0;
  306. }
  307. .title{
  308. flex: 1;
  309. font-size: 28upx;
  310. font-family: PingFang SC;
  311. font-weight: 500;
  312. color: #111111;
  313. line-height: 1;
  314. }
  315. }
  316. .intro{
  317. font-size: 24upx;
  318. font-family: PingFang SC;
  319. font-weight: 500;
  320. color: #999999;
  321. margin-top: 22upx;
  322. line-height: 1;
  323. }
  324. .price-num{
  325. display: flex;
  326. align-items: center;
  327. justify-content: space-between;
  328. .price{
  329. display: flex;
  330. align-items: flex-end;
  331. .unit{
  332. font-size: 24upx;
  333. font-family: PingFang SC;
  334. font-weight: 500;
  335. color: #FF6633;
  336. line-height: 1.2;
  337. margin-right: 4upx;
  338. }
  339. .text{
  340. font-size: 32upx;
  341. font-family: PingFang SC;
  342. font-weight: bold;
  343. color: #FF6633;
  344. line-height: 1;
  345. }
  346. }
  347. .num-box{
  348. display: flex;
  349. align-items: center;
  350. .img-box{
  351. width: 60upx;
  352. height: 60upx;
  353. // border-radius: 4upx;
  354. border: 1px solid #dddddd;
  355. display: flex;
  356. align-items: center;
  357. justify-content: center;
  358. image{
  359. width: 25rpx;
  360. height: 25rpx;
  361. }
  362. }
  363. input{
  364. width: 60upx;
  365. height: 60upx;
  366. line-height: 60upx;
  367. font-size: 28upx;
  368. font-family: PingFang SC;
  369. font-weight: 500;
  370. color: #111111;
  371. // border-radius: 4upx;
  372. border-top: 1px solid #dddddd;
  373. border-bottom: 1px solid #dddddd;
  374. text-align: center;
  375. // margin: 0 16upx;
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. .btn-foot{
  383. box-sizing: border-box;
  384. width: 100%;
  385. height: 121upx;
  386. background: #FFFFFF;
  387. padding: 16upx 30upx 16upx 60upx;
  388. display: flex;
  389. align-items: center;
  390. justify-content: space-between;
  391. position: fixed;
  392. left: 0;
  393. bottom: 0;
  394. z-index: 99;
  395. .left{
  396. display: flex;
  397. align-items: center;
  398. .text{
  399. margin-left: 14upx;
  400. font-size: 28upx;
  401. font-family: PingFang SC;
  402. font-weight: 500;
  403. color: #666666;
  404. line-height: 1;
  405. }
  406. }
  407. .right{
  408. display: flex;
  409. align-items: center;
  410. .total{
  411. display: flex;
  412. align-items: flex-end;
  413. margin-right: 36upx;
  414. .label{
  415. font-size: 26upx;
  416. font-family: PingFang SC;
  417. font-weight: 500;
  418. color: #999999;
  419. line-height: 1.5;
  420. }
  421. .price{
  422. display: flex;
  423. align-items: flex-end;
  424. .unit{
  425. font-size: 32upx;
  426. font-family: PingFang SC;
  427. font-weight: bold;
  428. color: #FF6633;
  429. line-height: 1.2;
  430. margin-right: 10upx;
  431. }
  432. .num{
  433. font-size: 30upx;
  434. font-family: PingFang SC;
  435. font-weight: bold;
  436. color: #FF6633;
  437. line-height: 1;
  438. }
  439. }
  440. }
  441. .btn{
  442. width: 200upx;
  443. height: 88upx;
  444. line-height: 88upx;
  445. text-align: center;
  446. font-size: 30upx;
  447. font-family: PingFang SC;
  448. font-weight: bold;
  449. color: #FFFFFF;
  450. background: #2BC7B9;
  451. border-radius: 44upx;
  452. }
  453. }
  454. }
  455. }
  456. </style>