index.vue 17 KB

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