shop-car1.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <template>
  2. <view class="shop-car1">
  3. <view v-if="list.length" class="list">
  4. <view class="shopcar-item" v-for="(item, index) in list" :key="index">
  5. <view class="name" @tap="chooseGood(!item.choosed, index)">
  6. <view class="left">
  7. <text v-if="item.choosed" class="cuIcon-roundcheckfill"></text>
  8. <text v-else class="cuIcon-round"></text>
  9. <view class="name ellipsis">{{ item.name }}</view>
  10. </view>
  11. <view class="right">
  12. <view class="right-box">
  13. <view style="margin-right: 40rpx;">
  14. <text class="one">数量:</text>
  15. <text class="two">{{item.goods_num}}</text>
  16. </view>
  17. <view class="">
  18. <text class="one">金额:</text>
  19. <text class="two">¥{{item.goods_money}}</text>
  20. </view>
  21. <!-- {{numChange}} -->
  22. </view>
  23. <text class="cuIcon-delete" @click.stop="deleteGood(index)"></text>
  24. </view>
  25. </view>
  26. <view class="sizeItem" v-for="(sizeItem, sizeIndex) in item.size" :key="sizeIndex"
  27. v-if="item.cart[sizeIndex]"
  28. @tap="chooseSize(!item.sizeChoosed[sizeIndex], index, sizeIndex,item.cart[sizeIndex])">
  29. <text v-if="item.sizeChoosed[sizeIndex]" class="cuIcon-roundcheckfill"></text>
  30. <text v-else class="cuIcon-round"></text>
  31. <image :src="item.main_img"></image>
  32. <view class="info">
  33. <text>尺码:{{ sizeItem }}</text>
  34. <text class="basecolor">¥{{ item.money }}/{{ item.unit }} </text>
  35. </view>
  36. <NumInput :value="item.cart[sizeIndex]" :args="[index, sizeIndex]" :num="item.step"
  37. @change="numChange" />
  38. </view>
  39. </view>
  40. <view class="gift_container" v-if="giftList.length > 0">
  41. <view class="fix_title">
  42. <text class="cuIcon-roundcheckfill"></text>
  43. <text class="title">赠品信息</text>
  44. </view>
  45. <div class="gift_list">
  46. <view class="gift_item" v-for="item in giftList" :key="item.id"
  47. v-show="Math.floor(giftNum / item.num) > 0">
  48. <template v-if="giftNum >= item.limit_num">
  49. <image :src="item.img" class="gift_img"></image>
  50. <view class="right">
  51. <view class="fixed">赠品名称:</view>
  52. <view class="bottom">
  53. <view class="name">{{item.name}}</view>
  54. <view class="num">数量:{{ Math.floor(giftNum / item.num) }}</view>
  55. </view>
  56. </view>
  57. </template>
  58. </view>
  59. </div>
  60. </view>
  61. </view>
  62. <view v-else class="none">
  63. <image src="../../static/icon/emptyshopcar.png"></image>
  64. <view class="big-btn" @tap="goToBuy">去下单</view>
  65. </view>
  66. <view class="buy">
  67. <view class="all" @tap="chooseAll(!choosedAll)">
  68. <text v-if="choosedAll" class="cuIcon-roundcheckfill"></text>
  69. <text v-else class="cuIcon-round"></text>
  70. <text>全选</text>
  71. </view>
  72. <!-- <view class="total">总计: <text>{{ '¥ ' + choosedPrice + '' }}</text></view> -->
  73. <!-- <view class="pay" @tap="confirm">结算({{ ' ' + choosedNum + ' ' }})</view> -->
  74. <view class="total">合计: <text>{{ '¥ ' + choosedPrice + '' }}</text></view>
  75. <view class="pay" @tap="confirm">下一步</view>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import {
  81. deepClone
  82. } from '@/common/util/index.js'
  83. import {
  84. _API_GoodList,
  85. GetGoodStatus
  86. } from '@/apis/good.js'
  87. import {
  88. _API_GetallGift,
  89. CheckGoodNum
  90. } from '@/apis/order.js'
  91. import NumInput from '@/components/public/num-input.vue'
  92. function sum(arr) {
  93. var len = arr.length;
  94. if (len == 0) {
  95. return 0;
  96. } else if (len == 1) {
  97. return arr[0];
  98. } else {
  99. return arr[0] + sum(arr.slice(1));
  100. }
  101. }
  102. export default {
  103. components: {
  104. NumInput
  105. },
  106. data() {
  107. return {
  108. giftList: [],
  109. isHelper: '',
  110. isLimit: false //下单数量是否限制 false不限制 true限制
  111. };
  112. },
  113. computed: {
  114. list() {
  115. return this.$store.state.cart.list
  116. },
  117. choosedAll() {
  118. return this.$store.getters['cart/choosedAll']
  119. },
  120. choosedNum() {
  121. return this.$store.getters['cart/choosedNum']
  122. },
  123. choosedPrice() {
  124. return this.$store.getters['cart/choosedPrice']
  125. },
  126. giftNum() {
  127. return this.$store.getters['cart/giftNum']
  128. },
  129. },
  130. // async onShow() {
  131. // uni.loading()
  132. // const { data: { list } } = await _API_GoodList()
  133. // for(let i = this.list.length - 1; i >= 0; i --) { // 遍历购物车
  134. // const inCartGood = list.find(listItem => listItem.attr_id == this.list[i].attr_id)
  135. // if (inCartGood) { // 如果购物车的商品存在于商品列表中
  136. // for(let j = this.list[i].size_id.length - 1; j >= 0; j --) {
  137. // const cartSize = this.list[i].size_id[j]
  138. // const inCartSize = inCartGood.size_id.find(e => e == cartSize)
  139. // console.log(inCartSize)
  140. // // // if (inCartSize) {
  141. // // // console.log(inCartSize)
  142. // // // } else {
  143. // // // console.log(inCartSize)
  144. // // // }
  145. // // console.log(inCartSize)
  146. // }
  147. // } else { // 不存直接删除
  148. // this.$store.commit('cart/DELETE', i)
  149. // }
  150. // }
  151. // },
  152. onLoad() {
  153. // 如果是助手登录隐藏tabbar
  154. this.isHelper = uni.getStorageSync('isHelper')
  155. if (this.isHelper) {
  156. uni.hideTabBar()
  157. } else {
  158. uni.showTabBar()
  159. }
  160. },
  161. onShow() {
  162. console.log(this.$store.state.cart.list)
  163. const cartinfo = this.$store.state.cart.list
  164. for (var i = 0; i < cartinfo.length; i++) {
  165. // cartinfo[i].goods_num= sum(cartinfo[i].cart)
  166. cartinfo[i].goods_money = sum(cartinfo[i].cart) * cartinfo[i].money
  167. let cart = cartinfo[i].cart
  168. let sizeChoosed = cartinfo[i].sizeChoosed
  169. let options = []
  170. for (var e = 0; e < cart.length; e++) {
  171. var a = {};
  172. a.num = cart[e];
  173. a.sizeChoosed = sizeChoosed[e];
  174. options.push(a);
  175. }
  176. let newArr = options.filter((item, key) => {
  177. return item.num != 0 && item.sizeChoosed != false
  178. })
  179. cartinfo[i].options = newArr
  180. cartinfo[i].goods_num = newArr.reduce((p, e) => p + e.num, 0);
  181. cartinfo[i].goods_money = cartinfo[i].goods_num * cartinfo[i].money
  182. }
  183. console.log(cartinfo)
  184. // if (this.$store.getters['cart/shopcarNum']) {
  185. // // uni.setTabBarBadge({ index: 1, text: this.$store.getters['cart/shopcarNum'] + '' })
  186. // } else {
  187. // uni.removeTabBarBadge({ index: 1 })
  188. // }
  189. this.getAllGift()
  190. // 客户下单限制测试
  191. // this.checkStatus()
  192. },
  193. methods: {
  194. // 客户下单数量限制是否开启
  195. // 客户下单限制测试
  196. // checkStatus() {
  197. // GetGoodStatus().then(res => {
  198. // if (res.code === 200) {
  199. // this.isLimit = res.data.status
  200. // } else {
  201. // uni.showToast({
  202. // title: '获取状态失败'
  203. // })
  204. // }
  205. // })
  206. // },
  207. getAllGift() {
  208. this.giftList = []
  209. _API_GetallGift().then(res => {
  210. if (res.code === 200) {
  211. this.giftList = res.data.list
  212. }
  213. })
  214. },
  215. numChange(value, index, sizeIndex) { // 购物车数量发生变化
  216. if (value) {
  217. const cartinfo = this.$store.state.cart.list
  218. cartinfo[index].cart[sizeIndex] = value
  219. cartinfo[index].goods_num = sum(cartinfo[index].cart)
  220. cartinfo[index].goods_money = sum(cartinfo[index].cart) * cartinfo[index].money
  221. this.$store.commit('cart/COUNTCHANGE', {
  222. value,
  223. index,
  224. sizeIndex,
  225. del: true
  226. })
  227. } else {
  228. uni.showModal({
  229. title: '提示',
  230. content: '确定删除这个尺寸?',
  231. success: res => {
  232. if (res.confirm) {
  233. const cartinfo = this.$store.state.cart.list
  234. cartinfo[index].cart[sizeIndex] = value
  235. cartinfo[index].goods_num = sum(cartinfo[index].cart)
  236. cartinfo[index].goods_money = sum(cartinfo[index].cart) * cartinfo[index].money
  237. this.$store.commit('cart/COUNTCHANGE', {
  238. value,
  239. index,
  240. sizeIndex,
  241. del: true
  242. })
  243. }
  244. }
  245. })
  246. }
  247. },
  248. chooseAll(to) { // 点击全选
  249. console.log(to)
  250. this.$store.commit('cart/CHOOSEDCHANGE', [to])
  251. if (to === true) {
  252. const cartinfo = this.$store.state.cart.list
  253. for (var i = 0; i < cartinfo.length; i++) {
  254. cartinfo[i].goods_num = sum(cartinfo[i].cart)
  255. cartinfo[i].goods_money = sum(cartinfo[i].cart) * cartinfo[i].money
  256. }
  257. } else {
  258. const cartinfo = this.$store.state.cart.list
  259. for (var i = 0; i < cartinfo.length; i++) {
  260. cartinfo[i].goods_num = 0
  261. cartinfo[i].goods_money = 0
  262. }
  263. }
  264. },
  265. chooseGood(to, index) { // 点击商品
  266. this.$store.commit('cart/CHOOSEDCHANGE', [to, index])
  267. console.log(to, index)
  268. if (to === true) {
  269. const cartinfo = this.$store.state.cart.list
  270. cartinfo[index].goods_num = sum(cartinfo[index].cart)
  271. cartinfo[index].goods_money = sum(cartinfo[index].cart) * cartinfo[index].money
  272. } else {
  273. const cartinfo = this.$store.state.cart.list
  274. cartinfo[index].goods_num = 0
  275. cartinfo[index].goods_money = 0
  276. }
  277. },
  278. chooseSize(to, index, sizeIndex, num) { // 点击尺寸
  279. console.log(to, index, sizeIndex, num)
  280. this.$store.commit('cart/CHOOSEDCHANGE', [to, index, sizeIndex])
  281. const cartinfo = this.$store.state.cart.list
  282. if (to === true) {
  283. cartinfo[index].goods_money = cartinfo[index].goods_money + cartinfo[index].money * num
  284. cartinfo[index].goods_num = cartinfo[index].goods_num + num
  285. // const cartinfo = this.$store.state.cart.list
  286. // cartinfo[index].goods_num= sum(cartinfo[index].cart)
  287. // cartinfo[index].goods_money = sum(cartinfo[index].cart) * cartinfo[index].money
  288. } else {
  289. cartinfo[index].goods_money = cartinfo[index].goods_money - cartinfo[index].money * num
  290. cartinfo[index].goods_num = cartinfo[index].goods_num - num
  291. // cartinfo[index].goods_money = -
  292. // const cartinfo = this.$store.state.cart.list
  293. // cartinfo[index].goods_num= 0
  294. // cartinfo[index].goods_money = 0
  295. }
  296. // for(var i=0;i<cartinfo.length;i++) {
  297. // // if (cartinfo[i].cart.index)
  298. // let num = sum(cartinfo[i].cart) - cartinfo[i].cart
  299. // // cartinfo[i].goods_num= sum(cartinfo[i].cart)
  300. // cartinfo[i].goods_money = sum(cartinfo[i].cart) * cartinfo[i].money
  301. // }
  302. },
  303. goToBuy() { // 点击去下单
  304. uni.navigateTo({
  305. url: '../place-order/place-order'
  306. })
  307. },
  308. deleteGood(index) { // 点击删除商品
  309. uni.showModal({
  310. title: '提示',
  311. content: '确定删除这件商品?',
  312. success: res => {
  313. if (res.confirm) {
  314. this.$store.commit('cart/DELETE', index)
  315. }
  316. }
  317. })
  318. },
  319. confirm() { // 点击结算
  320. let type = 0
  321. let total = 0
  322. let num = []
  323. let size_id = []
  324. //客户下单限制测试
  325. // let size6 = ''
  326. // let size3 = ''
  327. deepClone(this.list).forEach((goodItem, goodIndex) => {
  328. //客户下单限制测试
  329. // if (goodItem.name === '大卫博士青春版' || goodItem.name === '大卫博士高腰版') {
  330. // size3 = goodItem.cart.reduce((a, b) => a + b, 0)
  331. // }
  332. // if (goodItem.name === '大卫博士精装版') {
  333. // size6 = goodItem.cart.reduce((a, b) => a + b, 0)
  334. // }
  335. goodItem.cart.forEach((n, i) => {
  336. if (n && goodItem.sizeChoosed[i]) {
  337. type++
  338. total += n
  339. num.push(n)
  340. size_id.push(goodItem.size_id[i])
  341. }
  342. })
  343. })
  344. //客户下单限制测试
  345. // if (size3 % 3 != 0 && this.isLimit) {
  346. // uni.showModal({
  347. // content: '青春版和高腰版请按照3的倍数下单',
  348. // showCancel: false
  349. // })
  350. // return
  351. // }
  352. // if (size6 % 6 != 0 && this.isLimit) {
  353. // uni.showModal({
  354. // content: '精装版请按照6的倍数下单',
  355. // showCancel: false
  356. // })
  357. // return
  358. // }
  359. if (this.choosedNum) {
  360. CheckGoodNum({
  361. total,
  362. type,
  363. num,
  364. size_id
  365. }).then(res => {
  366. if (res.code == 200) {
  367. uni.navigateTo({
  368. url: '../confirm-order1/confirm-order1'
  369. })
  370. location.reload()
  371. } else {
  372. uni.showModal({
  373. showCancel: false,
  374. title: '提示',
  375. content: res.message
  376. })
  377. }
  378. })
  379. }
  380. }
  381. }
  382. }
  383. </script>
  384. <style lang="scss">
  385. .shop-car1 {
  386. height: 100vh;
  387. overflow: auto;
  388. background: $app-base-bg;
  389. border-bottom: calc(var(--window-bottom) + 100rpx) solid transparent;
  390. .gift_container {
  391. width: 100%;
  392. .fix_title {
  393. height: 90rpx;
  394. line-height: 90rpx;
  395. padding: 0 32rpx;
  396. display: flex;
  397. justify-content: flex-start;
  398. align-items: center;
  399. border-bottom: 1px solid $app-base-bg;
  400. .cuIcon-roundcheckfill {
  401. color: #F76454;
  402. font-size: 32rpx;
  403. }
  404. .title {
  405. margin-left: 30rpx;
  406. color: #181818;
  407. font-size: 32rpx;
  408. }
  409. }
  410. .gift_list {
  411. background-color: #FFFFFF;
  412. .gift_item {
  413. padding: 28rpx 30rpx;
  414. margin-bottom: 1px;
  415. background-color: #FFFFFF;
  416. display: flex;
  417. justify-content: space-between;
  418. align-items: center;
  419. .gift_img {
  420. display: block;
  421. width: 140rpx;
  422. height: 140rpx;
  423. }
  424. .right {
  425. width: calc(100% - 140rpx);
  426. height: 140rpx;
  427. padding: 0 50rpx 0 30rpx;
  428. display: flex;
  429. justify-content: space-between;
  430. flex-direction: column;
  431. .fixed {
  432. color: #181818;
  433. font-size: 32rpx;
  434. }
  435. .bottom {
  436. display: flex;
  437. justify-content: space-between;
  438. align-items: center;
  439. .name {
  440. color: #F76454;
  441. font-size: 32rpx;
  442. }
  443. .num {
  444. color: #181818;
  445. font-size: 28rpx;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. .shopcar-item {
  453. margin-bottom: 10rpx;
  454. .name {
  455. background: #FFFFFF;
  456. padding: 20rpx 0;
  457. .left {
  458. @include flex();
  459. justify-content: flex-start;
  460. >.name {
  461. color: #181818;
  462. font-size: 32rpx;
  463. font-weight: bold;
  464. }
  465. .cuIcon-round,
  466. .cuIcon-roundcheckfill {
  467. @include flex();
  468. width: 102rpx;
  469. height: 100%;
  470. font-size: 32rpx;
  471. &.cuIcon-roundcheckfill {
  472. color: $app-base-color;
  473. }
  474. }
  475. }
  476. .right {
  477. margin-top: 15rpx;
  478. padding-left: 92rpx;
  479. display: flex;
  480. justify-content: space-between;
  481. align-items: center;
  482. &-box {
  483. // width: 384rpx;
  484. line-height: 52rpx;
  485. padding: 0 20rpx;
  486. background-color: #FFF4F3;
  487. border-radius: 8rpx;
  488. display: flex;
  489. font-size: 32rpx;
  490. .one {
  491. color: #333;
  492. }
  493. .two {
  494. color: #F76454;
  495. font-weight: bold;
  496. }
  497. }
  498. }
  499. .cuIcon-delete {
  500. font-size: 40rpx;
  501. margin-right: 30rpx;
  502. }
  503. }
  504. .sizeItem {
  505. @include flex();
  506. height: 200rpx;
  507. margin-top: 4rpx;
  508. background: #FFFFFF;
  509. box-sizing: border-box;
  510. padding-right: 39rpx;
  511. .cuIcon-round,
  512. .cuIcon-roundcheckfill {
  513. @include flex();
  514. width: 102rpx;
  515. height: 100%;
  516. font-size: 32rpx;
  517. &.cuIcon-roundcheckfill {
  518. color: $app-base-color;
  519. }
  520. }
  521. image {
  522. width: 140rpx;
  523. height: 140rpx;
  524. border-radius: 6rpx;
  525. }
  526. .info {
  527. flex: 1;
  528. height: 100%;
  529. @include flex(column);
  530. align-items: flex-start;
  531. padding: 43rpx 29rpx;
  532. box-sizing: border-box;
  533. justify-content: space-between;
  534. }
  535. }
  536. }
  537. .none {
  538. @include flex(column);
  539. height: 100%;
  540. image {
  541. width: 305rpx;
  542. height: 417rpx;
  543. }
  544. .big-btn {
  545. width: 280rpx;
  546. height: 80rpx;
  547. margin-top: 123rpx;
  548. border-radius: 40rpx;
  549. }
  550. }
  551. .buy {
  552. @include flex();
  553. position: fixed;
  554. left: 0;
  555. right: 0;
  556. bottom: 0;
  557. z-index: 1;
  558. height: 98rpx;
  559. font-size: 32rpx;
  560. background: #FFFFFF;
  561. justify-content: space-between;
  562. box-shadow: 0rpx -1rpx 0rpx 0rpx rgba(178, 178, 178, 1);
  563. view {
  564. @include flex();
  565. height: 100%;
  566. &.all {
  567. font-size: 32rpx;
  568. text {
  569. margin-left: 30rpx;
  570. &.cuIcon-roundcheckfill {
  571. color: $app-base-color;
  572. }
  573. }
  574. }
  575. &.total {
  576. flex: 1;
  577. margin-right: 30rpx;
  578. justify-content: flex-end;
  579. text {
  580. color: $app-base-color;
  581. font-size: 40rpx;
  582. }
  583. }
  584. &.pay {
  585. width: 220rpx;
  586. color: #FFFFFF;
  587. background: $app-base-color;
  588. }
  589. }
  590. }
  591. }
  592. </style>