index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. }
  195. delete key.shopPromoProduct
  196. return {
  197. id: key.id,
  198. edit: false,
  199. checked: _this.allChecked,
  200. cartSn: key.cartSn,
  201. price: key.price,
  202. orginPrice: key.orginPrice,
  203. qty: key.qty,
  204. productSn: key.productSn,
  205. productName: key.productEntity.productName,
  206. productImage: key.productEntity.images,
  207. productCode: key.productEntity.code,
  208. productOrigCode: key.productEntity.origCode,
  209. unit: key.productEntity.unit,
  210. status: key.status,
  211. dealerScopeFlag: key.dealerScopeFlag,
  212. resultValue: key.resultValue,
  213. conditionValue: key.conditionValue,
  214. discountType: key.discountType,
  215. promoType: key.promoType,
  216. promoProductSn: key.promoProductSn,
  217. promoSn: key.promoSn,
  218. }
  219. }))
  220. }
  221. // 默认全选
  222. if(_this.pageNo == 1){
  223. _this.$nextTick(()=>{
  224. _this.chooseAll()
  225. })
  226. }
  227. // 判断是否最后一页
  228. const maxPages = Math.ceil(_this.total / _this.pageSize)
  229. if(this.pageNo >= maxPages){
  230. _this.status = 'nomore'
  231. _this.noDataText = '没有更多了'
  232. }else{
  233. _this.status = 'loadmore'
  234. _this.noDataText = '加载更多'
  235. }
  236. } else {
  237. _this.list = []
  238. _this.total = 0
  239. _this.status = 'loadmore'
  240. _this.noDataText = res.message
  241. }
  242. })
  243. },
  244. // scroll-view到底部加载更多
  245. onreachBottom() {
  246. // 判断是否最后一页
  247. const maxPages = Math.ceil(this.total / this.pageSize)
  248. if(this.pageNo >= maxPages){
  249. this.status = "nomore"
  250. this.noDataText = '没有更多了'
  251. }else{
  252. this.pageNo += 1
  253. this.getRow()
  254. }
  255. },
  256. // 根据cartSn查找
  257. getItemById(cartSn){
  258. const a = this.list.findIndex(k => k.find(s=> s.cartSn == cartSn))
  259. const b = a>=0 ? this.list[a].findIndex(s=> s.cartSn == cartSn) : -1
  260. return {p:a,s:b}
  261. },
  262. // 已选产品变更数量
  263. changeQty(val,cartSn){
  264. uni.showLoading({
  265. title: '加载中'
  266. })
  267. updateCart({
  268. cartSn: cartSn,
  269. qty: val
  270. }).then(res => {
  271. if(res.status == 200){
  272. const crow = this.getItemById(cartSn)
  273. if(crow.p>=0&&crow.s>=0){
  274. this.list[crow.p][crow.s].qty = val
  275. this.list[crow.p].splice()
  276. // 合计
  277. this.getChooseHeji()
  278. uni.$emit('refashCart')
  279. }
  280. }
  281. uni.hideLoading()
  282. })
  283. },
  284. // 单选择
  285. chooseItem(cartSn){
  286. const crow = this.getItemById(cartSn)
  287. if(crow.p>=0&&crow.s>=0){
  288. this.list[crow.p][crow.s].checked = !this.list[crow.p][crow.s].checked
  289. // 合计
  290. this.getChooseHeji()
  291. // 判断是否全选
  292. this.isAllChecked()
  293. }
  294. },
  295. // 判断是否全选
  296. isAllChecked(){
  297. if(this.editFlag){
  298. this.allChecked = this.list.every(key => key.every(item => item.checked))
  299. }else{
  300. this.allChecked = this.list.every(key => key.filter(item => item.status==1&&item.dealerScopeFlag==1 ).every(item => item.checked))
  301. }
  302. },
  303. // 编辑
  304. editAll(){
  305. this.editFlag = !this.editFlag
  306. this.list.forEach(key => {
  307. key.forEach(item => {
  308. item.edit = this.editFlag
  309. if(item.status==0||item.dealerScopeFlag==0){
  310. item.checked = false
  311. }
  312. })
  313. })
  314. // 合计
  315. this.getChooseHeji()
  316. // 判断是否全选
  317. this.isAllChecked()
  318. },
  319. // 清空购物车
  320. clearAll(){
  321. const _this = this
  322. uni.showModal({
  323. title: '提示',
  324. content: '确定清空购物车?',
  325. confirmText: '确定',
  326. success(res) {
  327. if(res.confirm){
  328. uni.showLoading({
  329. title: '正在清空...',
  330. mask: true
  331. })
  332. }
  333. }
  334. })
  335. },
  336. // 全选
  337. chooseAll(){
  338. if(this.allDisabled){
  339. return
  340. }
  341. this.allChecked = !this.allChecked
  342. this.list.forEach(key => {
  343. // 非编辑模式
  344. if(!this.editFlag){
  345. key.filter(item=>item.status == 1 && item.dealerScopeFlag == 1).forEach(item => {
  346. item.checked = this.allChecked
  347. })
  348. }else{
  349. // 编辑模式
  350. key.forEach(item => {
  351. item.checked = this.allChecked
  352. })
  353. }
  354. })
  355. // 合计
  356. this.getChooseHeji()
  357. },
  358. //单个删除
  359. removeChoose(cartSn,isBatch){
  360. // 删除项
  361. const crow = this.getItemById(cartSn)
  362. if(crow.p>=0&&crow.s>=0){
  363. this.list[crow.p].splice(crow.s, 1);
  364. this.total = this.total - 1
  365. // 合计
  366. if(!isBatch){
  367. this.getChooseHeji()
  368. uni.$emit('refashCart')
  369. // 判断是否全选
  370. this.isAllChecked()
  371. }
  372. }
  373. },
  374. // 批量删除
  375. batchRemove(){
  376. if(this.totalNum==0){
  377. uni.showToast({
  378. title: '请选择产品',
  379. icon: 'none'
  380. })
  381. return false
  382. }
  383. const _this = this
  384. uni.showModal({
  385. title: '提示',
  386. content: '确定删除商品吗?',
  387. confirmText: '确定',
  388. success(res) {
  389. if(res.confirm){
  390. const snList = []
  391. _this.list.forEach(item => {
  392. item.filter(k=>k.checked).forEach(s=>{
  393. snList.push(s.cartSn)
  394. })
  395. })
  396. // 删除购物车
  397. _this.removeCart(snList,true,true)
  398. }
  399. }
  400. })
  401. },
  402. // 删除商品
  403. removeCart(snList,isBatch,isTip){
  404. const _this = this
  405. if(isTip){
  406. uni.showLoading({
  407. title: '删除中'
  408. })
  409. }
  410. deleteCart({cartSns:snList}).then(ret => {
  411. if(ret.status == 200){
  412. snList.forEach(sn => {
  413. _this.removeChoose(sn,isBatch)
  414. })
  415. // 合计,批量操作
  416. if(isBatch){
  417. _this.getChooseHeji()
  418. uni.$emit('refashCart')
  419. // 判断是否全选
  420. _this.isAllChecked()
  421. }
  422. if(isTip){
  423. uni.showToast({
  424. title: '删除成功',
  425. icon: 'none'
  426. })
  427. }
  428. }
  429. uni.hideLoading()
  430. })
  431. },
  432. // 去结算
  433. settlement(){
  434. if(this.totalAmount>0){
  435. this.submitOrder()
  436. }else{
  437. uni.showToast({
  438. title: '请选择产品',
  439. icon: 'none'
  440. })
  441. }
  442. },
  443. // 去结算
  444. submitOrder(){
  445. const _this = this
  446. // 获取已选商品
  447. const detailList = []
  448. const cartSn = []
  449. _this.list.forEach(key => {
  450. key.filter(item=>item.checked).forEach(item => {
  451. detailList.push({
  452. productSn:item.productSn,
  453. productCode:item.productCode,
  454. qty: item.qty,
  455. price:item.price,
  456. })
  457. cartSn.push({
  458. cartSn: item.cartSn,
  459. productSn:item.productSn
  460. })
  461. })
  462. })
  463. // 校验并提示信息
  464. purchaseCheck({detailList:detailList}).then(res => {
  465. if(res.status == 200){
  466. const successList = res.data.successList.map(item => {
  467. return {
  468. productSn:item.productSn,
  469. productCode: item.productCode,
  470. qty: item.qty,
  471. price:item.price
  472. }
  473. })
  474. const delSn = cartSn.filter(item => successList.find(k => k.productSn == item.productSn)).map(item => item.cartSn)
  475. const removeList = res.data.removeList.map(item => item.productCode)
  476. const selloutList = res.data.selloutList.map(item => item.productCode)
  477. console.log(successList,delSn)
  478. console.log(removeList,selloutList)
  479. // 有已下架或已售罄产品提示
  480. if(removeList.length || selloutList.length){
  481. uni.showModal({
  482. title: '提示',
  483. content: (removeList.length ? `产品${removeList.join(',')}已下架,`:'')+(selloutList.length?`产品${selloutList.join(',')}已售罄,`:'')+`不可采购。`+(successList.length?'是否继续采购其他产品?':''),
  484. showCancel: successList.length,
  485. confirmText: successList.length?'确定':'知道了',
  486. success(ret) {
  487. if(ret.confirm && successList.length){
  488. _this.submitForm(successList,delSn)
  489. }
  490. }
  491. })
  492. }else{
  493. // 提交
  494. _this.submitForm(successList,delSn)
  495. }
  496. }else{
  497. uni.showToast({
  498. title: res.message,
  499. icon: 'none'
  500. })
  501. }
  502. })
  503. },
  504. // 确认提交
  505. submitForm(detailList,cartSn){
  506. uni.showLoading({
  507. title: '提交中...',
  508. mask: true
  509. })
  510. this.loading = true
  511. purchaseSave({
  512. detailList: detailList
  513. }).then(res => {
  514. this.loading = false
  515. uni.hideLoading()
  516. if(res.status == 200){
  517. // 删除已提交产品
  518. this.removeCart(cartSn,true,false)
  519. res.data.detailList = []
  520. this.$store.state.vuex_tempData = res.data
  521. uni.navigateTo({
  522. url: "/pagesB/procureOrder"
  523. })
  524. }else{
  525. uni.showToast({
  526. title: res.message,
  527. icon: 'none'
  528. })
  529. }
  530. })
  531. },
  532. // 计算总款数、合计、数量
  533. getChooseHeji(){
  534. this.totalAmount = 0
  535. this.totalDiscount = 0
  536. this.totalCoupon = 0
  537. this.totalNum = 0
  538. this.totalSel = 0
  539. this.list.forEach(key => {
  540. key.filter(item=>item.checked).forEach(item => {
  541. this.totalAmount += item.price * item.qty // 总金额
  542. this.totalNum += item.qty // 总数量
  543. this.totalSel += 1 // 已选数量
  544. // 如果是返券类型
  545. if(item.promoType=='BUY_PROD_GIVE_VALID'){
  546. this.totalCoupon += item.resultValue
  547. }
  548. // 特价
  549. if(item.promoType=='PROMO_PROD'){
  550. // 直降
  551. if(item.discountType == 'STRAIGHT_DOWN'){
  552. this.totalDiscount += item.resultValue
  553. }
  554. // 折扣
  555. if(item.discountType == 'DISCOUNT'){
  556. this.totalDiscount += Number(item.price * (1-item.resultValue))
  557. }
  558. }
  559. })
  560. })
  561. },
  562. }
  563. }
  564. </script>
  565. <style lang="less">
  566. .pages{
  567. height: 100vh;
  568. display: flex;
  569. flex-direction: column;
  570. .cart-body{
  571. position: relative;
  572. flex-grow: 1;
  573. display: flex;
  574. flex-direction: column;
  575. .cart-head{
  576. display: flex;
  577. justify-content: space-between;
  578. align-items: center;
  579. padding: 0 20rpx;
  580. background-color: #FFFFFF;
  581. height: 40px;
  582. .cart-head-left{
  583. display: flex;
  584. align-items: center;
  585. font-size: 28rpx;
  586. color: #333333;
  587. height: 100%;
  588. text{
  589. color: #d81e06;
  590. margin: 0 10rpx;
  591. }
  592. }
  593. .cart-head-right{
  594. display: flex;
  595. align-items: center;
  596. }
  597. .red{
  598. color: #d81e06;
  599. }
  600. .blue{
  601. color: #2196F3;
  602. }
  603. .cart-head-btton{
  604. height: 100%;
  605. font-size: 28rpx;
  606. margin-right: 10px;
  607. }
  608. }
  609. .cart-list{
  610. flex-grow: 1;
  611. background-color: #F5F5F5;
  612. width: 100%;
  613. .loading-bar{
  614. text-align: center;
  615. padding: 10px 0;
  616. color: #999999;
  617. }
  618. .empty-bar{
  619. display: flex;
  620. flex-direction: column;
  621. align-items: center;
  622. justify-content: center;
  623. padding: 20px 0;
  624. margin-top: 20vh;
  625. image{
  626. width: 100px;
  627. height: 100px;
  628. }
  629. view{
  630. font-size: 14px;
  631. color: #999999;
  632. margin-top: 10px;
  633. }
  634. }
  635. }
  636. .cart-footer{
  637. width: 100%;
  638. background-color: #FFFFFF;
  639. .cart-footer-box{
  640. width: 100%;
  641. height: 100rpx;
  642. display: flex;
  643. justify-content: space-between;
  644. align-items: center;
  645. }
  646. .cart-footer-left{
  647. display: flex;
  648. align-items: center;
  649. padding: 0 20rpx 0 36rpx;
  650. .cart-footer-left-all{
  651. display: flex;
  652. align-items: center;
  653. .cart-footer-left-all-text{
  654. margin-left: 10rpx;
  655. font-size: 28rpx;
  656. color: #333333;
  657. }
  658. }
  659. }
  660. .cart-footer-right{
  661. padding: 0 20rpx;
  662. display: flex;
  663. align-items: center;
  664. justify-content: space-between;
  665. flex-grow:1;
  666. .cart-footer-left-price{
  667. flex-grow:1;
  668. > view{
  669. display: flex;
  670. align-items: baseline;
  671. justify-content: flex-end;
  672. }
  673. .cart-footer-left-price-text{
  674. font-size: 24rpx;
  675. color: #333333;
  676. }
  677. .cart-footer-left-price-text1{
  678. font-size: 20rpx;
  679. color: #666;
  680. display: flex;
  681. align-items: center;
  682. margin-top: 2px;
  683. text{
  684. margin-right: 5px;
  685. }
  686. }
  687. .cart-footer-left-price-num{
  688. display: flex;
  689. align-items: baseline;
  690. font-size: 24rpx;
  691. color: #d81e06;
  692. text{
  693. font-size: 32rpx;
  694. }
  695. }
  696. }
  697. .red{
  698. color: #d81e06;
  699. }
  700. .cart-head-btton{
  701. height: 100%;
  702. font-size: 28rpx;
  703. color: #333333;
  704. }
  705. .cart-footer-right-button{
  706. display: flex;
  707. align-items: center;
  708. margin-left: 20rpx;
  709. text-align: right;
  710. .btns{
  711. border-radius: 100rpx;
  712. height: 36px;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. }
  719. </style>