index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <template>
  2. <view class="pages">
  3. <!-- head -->
  4. <sklineHeader title="购物车"></sklineHeader>
  5. <view class="cart-body">
  6. <!-- 内容 -->
  7. <scroll-view
  8. scroll-y
  9. class="cart-list"
  10. type="custom"
  11. :bounces="false"
  12. @scrolltolower="onreachBottom">
  13. <sticky-header>
  14. <!-- 头部 -->
  15. <view class="cart-head" v-if="total>0">
  16. <view class="cart-head-left">共<text>{{total}}</text>款商品</view>
  17. <view class="cart-head-right">
  18. <view class="cart-head-btton blue" @click="editAll">
  19. <text>{{editFlag?'完成':'编辑'}}</text>
  20. </view>
  21. </view>
  22. </view>
  23. </sticky-header>
  24. <productItem
  25. v-for="(item, index) in list"
  26. :key="index"
  27. :list="item"
  28. @changeQty="changeQty"
  29. @check="chooseItem"
  30. @remove="removeCart">
  31. </productItem>
  32. <!-- loading -->
  33. <view class="loading-bar" v-if="total>0 && status!='loading'">{{noDataText}}</view>
  34. <view class="empty-bar" v-if="total==0 && status!='loading'" @click="goBack">
  35. <image mode="aspectFit" :src="empty.imgUrl"></image>
  36. <view>{{empty.tip}}</view>
  37. </view>
  38. </scroll-view>
  39. <!-- 底部 -->
  40. <view class="cart-footer" v-if="total>0">
  41. <view class="cart-footer-box">
  42. <view class="cart-footer-left">
  43. <view class="cart-footer-left-all" @click="chooseAll">
  44. <view class="cart-footer-left-all-icon">
  45. <uni-icons v-if="allDisabled&&!editFlag" type="circle-filled" size="24" color="#ccc"></uni-icons>
  46. <uni-icons v-else :type="allChecked?'checkbox-filled':'circle'" size="24" :color="allChecked?'#dd0000':'#666'"></uni-icons>
  47. </view>
  48. <view class="cart-footer-left-all-text">
  49. <text>全选</text>
  50. </view>
  51. </view>
  52. </view>
  53. <view class="cart-footer-right">
  54. <view class="cart-footer-left-price" v-if="!editFlag && totalAmount">
  55. <view class="flex align_center">
  56. <view class="cart-footer-left-price-text">
  57. <text>合计:</text>
  58. </view>
  59. <view class="cart-footer-left-price-num">
  60. ¥<text>{{Number(totalAmount).toFixed(2).toString().split('.')[0]}}</text>.{{Number(totalAmount).toFixed(2).toString().split('.')[1]}}
  61. </view>
  62. </view>
  63. <view class="flex align_center">
  64. <view class="cart-footer-left-price-text1">
  65. <text>优惠:¥{{totalDiscount}}</text>
  66. <text>代金券:¥{{totalCoupon}}</text>
  67. </view>
  68. </view>
  69. </view>
  70. <view v-else></view>
  71. <view class="cart-footer-right-button">
  72. <button class="btns" :loading="loading" v-if="!editFlag" type="warn" size="mini" @click="settlement">去结算({{totalNum}})</button>
  73. <button v-else class="btns" type="warn" plain size="mini" @click="batchRemove">删除({{totalSel}})</button>
  74. </view>
  75. </view>
  76. </view>
  77. <view :style="{height:safeAreaBottom+'px'}"></view>
  78. </view>
  79. </view>
  80. </view>
  81. </template>
  82. <script>
  83. import {
  84. mapState,
  85. mapMutations,
  86. } from 'vuex'
  87. import productItem from '@/pagesB/cart/productitem.vue'
  88. import sklineHeader from '../../components/skline/sklineHeader.vue'
  89. import { getCartList, updateCart, deleteCart } from '@/api/cart.js'
  90. import { purchaseSave, purchaseCheck } from '@/api/purchase.js'
  91. export default {
  92. components:{
  93. productItem,
  94. sklineHeader
  95. },
  96. onLoad() {
  97. // 计算区域高度
  98. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
  99. this.screenWidth = uni.getSystemInfoSync().windowWidth
  100. this.screenHeight = uni.getSystemInfoSync().windowHeight
  101. this.safeAreaBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
  102. // 获取列表数据
  103. this.pageInit()
  104. },
  105. data() {
  106. return {
  107. loading: false,
  108. status: 'loading',
  109. noDataText: '暂无产品',
  110. empty: {
  111. tip: '购物车空空的,去挑选商品吧',
  112. imgUrl: '/static/kongCart.png'
  113. },
  114. // 查询条件
  115. pageNo: 1, // 页数
  116. pageSize: 10000, // 每页数
  117. list: [], // 列表数据
  118. total: 0, // 总款数
  119. totalAmount: 0, // 总金额
  120. totalDiscount:0, // 总优惠
  121. totalCoupon: 0, // 总代金券
  122. totalNum: 0, // 总数量
  123. totalSel: 0, // 已选数量
  124. editFlag: false, // 是否编辑状态
  125. allChecked: false, // 是否全选
  126. screenWidth: 325, // 屏幕宽度
  127. screenHeight: 0, // 屏幕高度
  128. statusBarHeight: 44, // 状态栏高度
  129. safeAreaBottom: 0, // 底部安全区域高度
  130. }
  131. },
  132. computed: {
  133. ...mapState(['hasLogin']),
  134. userInfo(){
  135. return this.$store.state.vuex_userInfo
  136. },
  137. allDisabled(){
  138. let ret = false
  139. if(!this.editFlag){
  140. this.list.forEach(key => {
  141. ret = key.every(item => item.status==0||item.dealerScopeFlag==0)
  142. })
  143. }
  144. return ret
  145. }
  146. },
  147. methods: {
  148. goBack(){
  149. uni.navigateBack()
  150. },
  151. pageInit(){
  152. this.pageNo = 1
  153. this.list = []
  154. this.getRow()
  155. },
  156. // 查询列表
  157. getRow (pageNo) {
  158. let _this = this
  159. if (pageNo) {
  160. this.pageNo = pageNo
  161. }
  162. let params = {
  163. pageNo: this.pageNo,
  164. pageSize: this.pageSize,
  165. queryWord: this.queryWord
  166. }
  167. this.status = "loading"
  168. getCartList(params).then(res => {
  169. if (res.status == 200) {
  170. _this.total = res.data.count || 0
  171. const list = res.data.list
  172. for(let i=0;i<list.length;i=i+10){
  173. _this.list.push(list.slice(i,(i+10)>list.length?list.length:(i+10)).map(key=>{
  174. const a = key.shopPromoProduct
  175. if(a){
  176. key.promoType = a.promoType
  177. key.resultValue = a.resultValue
  178. key.conditionValue = a.conditionValue
  179. key.discountType = a.discountType
  180. key.promoProductSn = a.promoProductSn
  181. key.promoSn = a.promoSn
  182. // 特价
  183. if(key.promoType=='PROMO_PROD'){
  184. key.orginPrice = key.price
  185. // 直降
  186. if(key.discountType == 'STRAIGHT_DOWN'){
  187. key.price = key.price - key.resultValue
  188. }
  189. // 折扣
  190. if(key.discountType == 'DISCOUNT'){
  191. key.price = Number(key.price * key.resultValue).toFixed(2)
  192. }
  193. }
  194. item.giftQty = key.promoType=='BUY_PROD_GIVE_PROD' ? Math.floor(key.qty / key.conditionValue)*key.resultValue : 0
  195. }
  196. delete key.shopPromoProduct
  197. return {
  198. id: key.id,
  199. edit: false,
  200. checked: _this.allChecked,
  201. cartSn: key.cartSn,
  202. price: key.price,
  203. orginPrice: key.orginPrice,
  204. qty: key.qty,
  205. productSn: key.productSn,
  206. productName: key.productEntity.productName,
  207. productImage: key.productEntity.images,
  208. productCode: key.productEntity.code,
  209. productOrigCode: key.productEntity.origCode,
  210. unit: key.productEntity.unit,
  211. status: key.status,
  212. dealerScopeFlag: key.dealerScopeFlag,
  213. resultValue: key.resultValue,
  214. conditionValue: key.conditionValue,
  215. discountType: key.discountType,
  216. promoType: key.promoType,
  217. promoProductSn: key.promoProductSn,
  218. promoSn: key.promoSn,
  219. giftQty: key.giftQty
  220. }
  221. }))
  222. }
  223. // 默认全选
  224. if(_this.pageNo == 1){
  225. _this.$nextTick(()=>{
  226. _this.chooseAll()
  227. })
  228. }
  229. // 判断是否最后一页
  230. const maxPages = Math.ceil(_this.total / _this.pageSize)
  231. if(this.pageNo >= maxPages){
  232. _this.status = 'nomore'
  233. _this.noDataText = '没有更多了'
  234. }else{
  235. _this.status = 'loadmore'
  236. _this.noDataText = '加载更多'
  237. }
  238. } else {
  239. _this.list = []
  240. _this.total = 0
  241. _this.status = 'loadmore'
  242. _this.noDataText = res.message
  243. }
  244. })
  245. },
  246. // scroll-view到底部加载更多
  247. onreachBottom() {
  248. // 判断是否最后一页
  249. const maxPages = Math.ceil(this.total / this.pageSize)
  250. if(this.pageNo >= maxPages){
  251. this.status = "nomore"
  252. this.noDataText = '没有更多了'
  253. }else{
  254. this.pageNo += 1
  255. this.getRow()
  256. }
  257. },
  258. // 根据cartSn查找
  259. getItemById(cartSn){
  260. const a = this.list.findIndex(k => k.find(s=> s.cartSn == cartSn))
  261. const b = a>=0 ? this.list[a].findIndex(s=> s.cartSn == cartSn) : -1
  262. return {p:a,s:b}
  263. },
  264. // 已选产品变更数量
  265. changeQty(val,cartSn){
  266. uni.showLoading({
  267. title: '加载中'
  268. })
  269. updateCart({
  270. cartSn: cartSn,
  271. qty: val
  272. }).then(res => {
  273. if(res.status == 200){
  274. const crow = this.getItemById(cartSn)
  275. if(crow.p>=0&&crow.s>=0){
  276. this.list[crow.p][crow.s].qty = val
  277. this.list[crow.p].splice()
  278. // 合计
  279. this.getChooseHeji()
  280. uni.$emit('refashCart')
  281. }
  282. }
  283. uni.hideLoading()
  284. })
  285. },
  286. // 单选择
  287. chooseItem(cartSn){
  288. const crow = this.getItemById(cartSn)
  289. if(crow.p>=0&&crow.s>=0){
  290. this.list[crow.p][crow.s].checked = !this.list[crow.p][crow.s].checked
  291. // 合计
  292. this.getChooseHeji()
  293. // 判断是否全选
  294. this.isAllChecked()
  295. }
  296. },
  297. // 判断是否全选
  298. isAllChecked(){
  299. if(this.editFlag){
  300. this.allChecked = this.list.every(key => key.every(item => item.checked))
  301. }else{
  302. this.allChecked = this.list.every(key => key.filter(item => item.status==1&&item.dealerScopeFlag==1 ).every(item => item.checked))
  303. }
  304. },
  305. // 编辑
  306. editAll(){
  307. this.editFlag = !this.editFlag
  308. this.list.forEach(key => {
  309. key.forEach(item => {
  310. item.edit = this.editFlag
  311. if(item.status==0||item.dealerScopeFlag==0){
  312. item.checked = false
  313. }
  314. })
  315. })
  316. // 合计
  317. this.getChooseHeji()
  318. // 判断是否全选
  319. this.isAllChecked()
  320. },
  321. // 清空购物车
  322. clearAll(){
  323. const _this = this
  324. uni.showModal({
  325. title: '提示',
  326. content: '确定清空购物车?',
  327. confirmText: '确定',
  328. success(res) {
  329. if(res.confirm){
  330. uni.showLoading({
  331. title: '正在清空...',
  332. mask: true
  333. })
  334. }
  335. }
  336. })
  337. },
  338. // 全选
  339. chooseAll(){
  340. if(this.allDisabled){
  341. return
  342. }
  343. this.allChecked = !this.allChecked
  344. this.list.forEach(key => {
  345. // 非编辑模式
  346. if(!this.editFlag){
  347. key.filter(item=>item.status == 1 && item.dealerScopeFlag == 1).forEach(item => {
  348. item.checked = this.allChecked
  349. })
  350. }else{
  351. // 编辑模式
  352. key.forEach(item => {
  353. item.checked = this.allChecked
  354. })
  355. }
  356. })
  357. // 合计
  358. this.getChooseHeji()
  359. },
  360. //单个删除
  361. removeChoose(cartSn,isBatch){
  362. // 删除项
  363. const crow = this.getItemById(cartSn)
  364. if(crow.p>=0&&crow.s>=0){
  365. this.list[crow.p].splice(crow.s, 1);
  366. this.total = this.total - 1
  367. // 合计
  368. if(!isBatch){
  369. this.getChooseHeji()
  370. uni.$emit('refashCart')
  371. // 判断是否全选
  372. this.isAllChecked()
  373. }
  374. }
  375. },
  376. // 批量删除
  377. batchRemove(){
  378. if(this.totalNum==0){
  379. uni.showToast({
  380. title: '请选择产品',
  381. icon: 'none'
  382. })
  383. return false
  384. }
  385. const _this = this
  386. uni.showModal({
  387. title: '提示',
  388. content: '确定删除商品吗?',
  389. confirmText: '确定',
  390. success(res) {
  391. if(res.confirm){
  392. const snList = []
  393. _this.list.forEach(item => {
  394. item.filter(k=>k.checked).forEach(s=>{
  395. snList.push(s.cartSn)
  396. })
  397. })
  398. // 删除购物车
  399. _this.removeCart(snList,true,true)
  400. }
  401. }
  402. })
  403. },
  404. // 删除商品
  405. removeCart(snList,isBatch,isTip){
  406. const _this = this
  407. if(isTip){
  408. uni.showLoading({
  409. title: '删除中'
  410. })
  411. }
  412. deleteCart({cartSns:snList}).then(ret => {
  413. if(ret.status == 200){
  414. snList.forEach(sn => {
  415. _this.removeChoose(sn,isBatch)
  416. })
  417. // 合计,批量操作
  418. if(isBatch){
  419. _this.getChooseHeji()
  420. uni.$emit('refashCart')
  421. // 判断是否全选
  422. _this.isAllChecked()
  423. }
  424. if(isTip){
  425. uni.showToast({
  426. title: '删除成功',
  427. icon: 'none'
  428. })
  429. }
  430. }
  431. uni.hideLoading()
  432. })
  433. },
  434. // 去结算
  435. settlement(){
  436. if(this.totalAmount>0){
  437. this.submitOrder()
  438. }else{
  439. uni.showToast({
  440. title: '请选择产品',
  441. icon: 'none'
  442. })
  443. }
  444. },
  445. // 去结算
  446. submitOrder(){
  447. const _this = this
  448. // 获取已选商品
  449. const detailList = []
  450. const cartSn = []
  451. _this.list.forEach(key => {
  452. key.filter(item=>item.checked).forEach(item => {
  453. detailList.push({
  454. productSn:item.productSn,
  455. productCode:item.productCode,
  456. qty: item.qty,
  457. price:item.price,
  458. })
  459. cartSn.push({
  460. cartSn: item.cartSn,
  461. productSn:item.productSn
  462. })
  463. })
  464. })
  465. // 校验并提示信息
  466. purchaseCheck({detailList:detailList}).then(res => {
  467. if(res.status == 200){
  468. const successList = res.data.successList.map(item => {
  469. return {
  470. productSn:item.productSn,
  471. productCode: item.productCode,
  472. qty: item.qty,
  473. price:item.price
  474. }
  475. })
  476. const delSn = cartSn.filter(item => successList.find(k => k.productSn == item.productSn)).map(item => item.cartSn)
  477. const removeList = res.data.removeList.map(item => item.productCode)
  478. const selloutList = res.data.selloutList.map(item => item.productCode)
  479. console.log(successList,delSn)
  480. console.log(removeList,selloutList)
  481. // 有已下架或已售罄产品提示
  482. if(removeList.length || selloutList.length){
  483. uni.showModal({
  484. title: '提示',
  485. content: (removeList.length ? `产品${removeList.join(',')}已下架,`:'')+(selloutList.length?`产品${selloutList.join(',')}已售罄,`:'')+`不可采购。`+(successList.length?'是否继续采购其他产品?':''),
  486. showCancel: successList.length,
  487. confirmText: successList.length?'确定':'知道了',
  488. success(ret) {
  489. if(ret.confirm && successList.length){
  490. _this.submitForm(successList,delSn)
  491. }
  492. }
  493. })
  494. }else{
  495. // 提交
  496. _this.submitForm(successList,delSn)
  497. }
  498. }else{
  499. uni.showToast({
  500. title: res.message,
  501. icon: 'none'
  502. })
  503. }
  504. })
  505. },
  506. // 确认提交
  507. submitForm(detailList,cartSn){
  508. uni.showLoading({
  509. title: '提交中...',
  510. mask: true
  511. })
  512. this.loading = true
  513. purchaseSave({
  514. detailList: detailList
  515. }).then(res => {
  516. this.loading = false
  517. uni.hideLoading()
  518. if(res.status == 200){
  519. // 删除已提交产品
  520. this.removeCart(cartSn,true,false)
  521. res.data.detailList = []
  522. this.$store.state.vuex_tempData = res.data
  523. uni.navigateTo({
  524. url: "/pagesB/procureOrder"
  525. })
  526. }else{
  527. uni.showToast({
  528. title: res.message,
  529. icon: 'none'
  530. })
  531. }
  532. })
  533. },
  534. // 计算总款数、合计、数量
  535. getChooseHeji(){
  536. this.totalAmount = 0
  537. this.totalDiscount = 0
  538. this.totalCoupon = 0
  539. this.totalNum = 0
  540. this.totalSel = 0
  541. this.list.forEach(key => {
  542. key.filter(item=>item.checked).forEach(item => {
  543. this.totalAmount += item.price * item.qty // 总金额
  544. this.totalNum += item.qty // 总数量
  545. this.totalSel += 1 // 已选数量
  546. // 如果是返券类型
  547. if(item.promoType=='BUY_PROD_GIVE_VALID'){
  548. this.totalCoupon += item.resultValue
  549. }
  550. // 特价
  551. if(item.promoType=='PROMO_PROD'){
  552. // 直降
  553. if(item.discountType == 'STRAIGHT_DOWN'){
  554. this.totalDiscount += item.resultValue
  555. }
  556. // 折扣
  557. if(item.discountType == 'DISCOUNT'){
  558. this.totalDiscount += Number(item.price * (1-item.resultValue))
  559. }
  560. }
  561. })
  562. })
  563. },
  564. }
  565. }
  566. </script>
  567. <style lang="less">
  568. .pages{
  569. height: 100vh;
  570. display: flex;
  571. flex-direction: column;
  572. .cart-body{
  573. position: relative;
  574. flex-grow: 1;
  575. display: flex;
  576. flex-direction: column;
  577. .cart-head{
  578. display: flex;
  579. justify-content: space-between;
  580. align-items: center;
  581. padding: 0 20rpx;
  582. background-color: #FFFFFF;
  583. height: 40px;
  584. .cart-head-left{
  585. display: flex;
  586. align-items: center;
  587. font-size: 28rpx;
  588. color: #333333;
  589. height: 100%;
  590. text{
  591. color: #d81e06;
  592. margin: 0 10rpx;
  593. }
  594. }
  595. .cart-head-right{
  596. display: flex;
  597. align-items: center;
  598. }
  599. .red{
  600. color: #d81e06;
  601. }
  602. .blue{
  603. color: #2196F3;
  604. }
  605. .cart-head-btton{
  606. height: 100%;
  607. font-size: 28rpx;
  608. margin-right: 10px;
  609. }
  610. }
  611. .cart-list{
  612. flex-grow: 1;
  613. background-color: #F5F5F5;
  614. width: 100%;
  615. .loading-bar{
  616. text-align: center;
  617. padding: 10px 0;
  618. color: #999999;
  619. }
  620. .empty-bar{
  621. display: flex;
  622. flex-direction: column;
  623. align-items: center;
  624. justify-content: center;
  625. padding: 20px 0;
  626. margin-top: 20vh;
  627. image{
  628. width: 100px;
  629. height: 100px;
  630. }
  631. view{
  632. font-size: 14px;
  633. color: #999999;
  634. margin-top: 10px;
  635. }
  636. }
  637. }
  638. .cart-footer{
  639. width: 100%;
  640. background-color: #FFFFFF;
  641. .cart-footer-box{
  642. width: 100%;
  643. height: 100rpx;
  644. display: flex;
  645. justify-content: space-between;
  646. align-items: center;
  647. }
  648. .cart-footer-left{
  649. display: flex;
  650. align-items: center;
  651. padding: 0 20rpx 0 36rpx;
  652. .cart-footer-left-all{
  653. display: flex;
  654. align-items: center;
  655. .cart-footer-left-all-text{
  656. margin-left: 10rpx;
  657. font-size: 28rpx;
  658. color: #333333;
  659. }
  660. }
  661. }
  662. .cart-footer-right{
  663. padding: 0 20rpx;
  664. display: flex;
  665. align-items: center;
  666. justify-content: space-between;
  667. flex-grow:1;
  668. .cart-footer-left-price{
  669. flex-grow:1;
  670. > view{
  671. display: flex;
  672. align-items: baseline;
  673. justify-content: flex-end;
  674. }
  675. .cart-footer-left-price-text{
  676. font-size: 24rpx;
  677. color: #333333;
  678. }
  679. .cart-footer-left-price-text1{
  680. font-size: 20rpx;
  681. color: #666;
  682. display: flex;
  683. align-items: center;
  684. margin-top: 2px;
  685. text{
  686. margin-right: 5px;
  687. }
  688. }
  689. .cart-footer-left-price-num{
  690. display: flex;
  691. align-items: baseline;
  692. font-size: 24rpx;
  693. color: #d81e06;
  694. text{
  695. font-size: 32rpx;
  696. }
  697. }
  698. }
  699. .red{
  700. color: #d81e06;
  701. }
  702. .cart-head-btton{
  703. height: 100%;
  704. font-size: 28rpx;
  705. color: #333333;
  706. }
  707. .cart-footer-right-button{
  708. display: flex;
  709. align-items: center;
  710. margin-left: 20rpx;
  711. text-align: right;
  712. .btns{
  713. border-radius: 100rpx;
  714. height: 36px;
  715. }
  716. }
  717. }
  718. }
  719. }
  720. }
  721. </style>