index.vue 16 KB

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