index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. @scrolltolower="onreachBottom">
  22. <sticky-header>
  23. <!-- 头部 -->
  24. <view class="cart-head" v-if="total>0">
  25. <view class="cart-head-left">共<text>{{total}}</text>款商品</view>
  26. <view class="cart-head-right">
  27. <view class="cart-head-btton blue" @click="editAll">
  28. <text>{{editFlag?'完成':'编辑'}}</text>
  29. </view>
  30. </view>
  31. </view>
  32. </sticky-header>
  33. <productItem
  34. v-for="(item, index) in list"
  35. :key="index"
  36. :index="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. checked: _this.allChecked,
  177. cartSn: key.cartSn,
  178. price: key.price,
  179. qty: key.qty,
  180. productSn: key.productSn,
  181. productName: key.productEntity.productName,
  182. productImage: key.productEntity.images,
  183. productCode: key.productEntity.code,
  184. productOrigCode: key.productEntity.origCode,
  185. unit: key.productEntity.unit,
  186. status: key.status,
  187. dealerScopeFlag: key.dealerScopeFlag
  188. }
  189. }))
  190. }
  191. // 默认全选
  192. if(_this.pageNo == 1){
  193. _this.$nextTick(()=>{
  194. _this.chooseAll()
  195. })
  196. }
  197. // 判断是否最后一页
  198. const maxPages = Math.ceil(_this.total / _this.pageSize)
  199. if(this.pageNo >= maxPages){
  200. _this.status = 'nomore'
  201. _this.noDataText = '没有更多了'
  202. }else{
  203. _this.status = 'loadmore'
  204. _this.noDataText = '加载更多'
  205. }
  206. } else {
  207. _this.list = []
  208. _this.total = 0
  209. _this.status = 'loadmore'
  210. _this.noDataText = res.message
  211. }
  212. })
  213. },
  214. // scroll-view到底部加载更多
  215. onreachBottom() {
  216. // 判断是否最后一页
  217. const maxPages = Math.ceil(this.total / this.pageSize)
  218. if(this.pageNo >= maxPages){
  219. this.status = "nomore"
  220. this.noDataText = '没有更多了'
  221. }else{
  222. this.pageNo += 1
  223. this.getRow()
  224. }
  225. },
  226. // 根据cartSn查找
  227. getItemById(cartSn){
  228. const a = this.list.findIndex(k => k.find(s=> s.cartSn == cartSn))
  229. const b = a>=0 ? this.list[a].findIndex(s=> s.cartSn == cartSn) : -1
  230. return {p:a,s:b}
  231. },
  232. // 已选产品变更数量
  233. changeQty(val,cartSn){
  234. uni.showLoading({
  235. title: '加载中'
  236. })
  237. updateCart({
  238. cartSn: cartSn,
  239. qty: val
  240. }).then(res => {
  241. if(res.status == 200){
  242. const crow = this.getItemById(cartSn)
  243. if(crow.p>=0&&crow.s>=0){
  244. this.list[crow.p][crow.s].qty = val
  245. this.list[crow.p].splice()
  246. // 合计
  247. this.getChooseHeji()
  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. // 判断是否全选
  338. this.isAllChecked()
  339. }
  340. }
  341. },
  342. // 批量删除
  343. batchRemove(){
  344. if(this.totalNum==0){
  345. uni.showToast({
  346. title: '请选择产品',
  347. icon: 'none'
  348. })
  349. return false
  350. }
  351. const _this = this
  352. uni.showModal({
  353. title: '提示',
  354. content: '确定删除商品吗?',
  355. confirmText: '确定',
  356. success(res) {
  357. if(res.confirm){
  358. const snList = []
  359. _this.list.forEach(item => {
  360. item.filter(k=>k.checked).forEach(s=>{
  361. snList.push(s.cartSn)
  362. })
  363. })
  364. // 删除购物车
  365. _this.removeCart(snList,true,true)
  366. }
  367. }
  368. })
  369. },
  370. // 删除商品
  371. removeCart(snList,isBatch,isTip){
  372. const _this = this
  373. if(isTip){
  374. uni.showLoading({
  375. title: '删除中'
  376. })
  377. }
  378. deleteCart({cartSns:snList}).then(ret => {
  379. if(ret.status == 200){
  380. snList.forEach(sn => {
  381. _this.removeChoose(sn,isBatch)
  382. })
  383. // 合计,批量操作
  384. if(isBatch){
  385. _this.getChooseHeji()
  386. // 判断是否全选
  387. _this.isAllChecked()
  388. }
  389. if(isTip){
  390. uni.showToast({
  391. title: '删除成功',
  392. icon: 'none'
  393. })
  394. }
  395. }
  396. uni.hideLoading()
  397. })
  398. },
  399. // 去结算
  400. settlement(){
  401. if(this.totalAmount>0){
  402. this.submitOrder()
  403. }else{
  404. uni.showToast({
  405. title: '请选择产品',
  406. icon: 'none'
  407. })
  408. }
  409. },
  410. // 去结算
  411. submitOrder(){
  412. const _this = this
  413. // 获取已选商品
  414. const detailList = []
  415. const cartSn = []
  416. _this.list.forEach(key => {
  417. key.filter(item=>item.checked).forEach(item => {
  418. detailList.push({
  419. productSn:item.productSn,
  420. productCode:item.productCode,
  421. qty: item.qty,
  422. price:item.price,
  423. })
  424. cartSn.push({
  425. cartSn: item.cartSn,
  426. productSn:item.productSn
  427. })
  428. })
  429. })
  430. // 校验并提示信息
  431. purchaseCheck({detailList:detailList}).then(res => {
  432. if(res.status == 200){
  433. const successList = res.data.successList.map(item => {
  434. return {
  435. productSn:item.productSn,
  436. productCode: item.productCode,
  437. qty: item.qty,
  438. price:item.price
  439. }
  440. })
  441. const delSn = cartSn.filter(item => successList.find(k => k.productSn == item.productSn)).map(item => item.cartSn)
  442. const removeList = res.data.removeList.map(item => item.productCode)
  443. const selloutList = res.data.selloutList.map(item => item.productCode)
  444. console.log(successList,delSn)
  445. console.log(removeList,selloutList)
  446. // 有已下架或已售罄产品提示
  447. if(removeList.length || selloutList.length){
  448. uni.showModal({
  449. title: '提示',
  450. content: (removeList.length ? `产品${removeList.join(',')}已下架,`:'')+(selloutList.length?`产品${selloutList.join(',')}已售罄,`:'')+`不可采购。`+(successList.length?'是否继续采购其他产品?':''),
  451. showCancel: successList.length,
  452. confirmText: successList.length?'确定':'只知道了',
  453. success(ret) {
  454. if(ret.confirm && successList.length){
  455. _this.submitForm(successList,delSn)
  456. }
  457. }
  458. })
  459. }else{
  460. // 提交
  461. _this.submitForm(successList,delSn)
  462. }
  463. }else{
  464. uni.showToast({
  465. title: res.message,
  466. icon: 'none'
  467. })
  468. }
  469. })
  470. },
  471. // 确认提交
  472. submitForm(detailList,cartSn){
  473. uni.showLoading({
  474. title: '提交中...',
  475. mask: true
  476. })
  477. this.loading = true
  478. purchaseSave({
  479. detailList: detailList
  480. }).then(res => {
  481. this.loading = false
  482. uni.hideLoading()
  483. if(res.status == 200){
  484. // 删除已提交产品
  485. this.removeCart(cartSn,true,false)
  486. res.data.detailList = []
  487. this.$store.state.vuex_tempData = res.data
  488. uni.navigateTo({
  489. url: "/pagesB/procureOrder"
  490. })
  491. }else{
  492. uni.showToast({
  493. title: res.message,
  494. icon: 'none'
  495. })
  496. }
  497. })
  498. },
  499. // 计算总款数、合计、数量
  500. getChooseHeji(){
  501. this.totalAmount = 0
  502. this.totalNum = 0
  503. this.totalSel = 0
  504. this.list.forEach(key => {
  505. key.filter(item=>item.checked).forEach(item => {
  506. this.totalAmount += item.price * item.qty // 总金额
  507. this.totalNum += item.qty // 总数量
  508. this.totalSel += 1 // 已选数量
  509. })
  510. })
  511. },
  512. }
  513. }
  514. </script>
  515. <style lang="less">
  516. .pages{
  517. height: 100vh;
  518. display: flex;
  519. flex-direction: column;
  520. .scrollPage-header{
  521. z-index: 1;
  522. .header-title{
  523. display: flex;
  524. align-items: center;
  525. justify-content: space-between;
  526. padding: 0 10px;
  527. height: 44px;
  528. background-color: #FFFFFF;
  529. box-shadow: 0px 1px 0px 0px #E6E6E6;
  530. .header-title{
  531. font-size: 16px;
  532. color: #333333;
  533. max-width: 70%;
  534. }
  535. .header-left{
  536. display: flex;
  537. align-items: center;
  538. text{
  539. font-size: 14px;
  540. color: #333333;
  541. margin-left: 5px;
  542. }
  543. }
  544. .header-right{
  545. width: 50px;
  546. }
  547. }
  548. }
  549. .cart-body{
  550. position: relative;
  551. flex-grow: 1;
  552. display: flex;
  553. flex-direction: column;
  554. .cart-head{
  555. display: flex;
  556. justify-content: space-between;
  557. align-items: center;
  558. padding: 0 20rpx;
  559. background-color: #FFFFFF;
  560. height: 40px;
  561. .cart-head-left{
  562. display: flex;
  563. align-items: center;
  564. font-size: 28rpx;
  565. color: #333333;
  566. height: 100%;
  567. text{
  568. color: #d81e06;
  569. margin: 0 10rpx;
  570. }
  571. }
  572. .cart-head-right{
  573. display: flex;
  574. align-items: center;
  575. }
  576. .red{
  577. color: #d81e06;
  578. }
  579. .blue{
  580. color: #2196F3;
  581. }
  582. .cart-head-btton{
  583. height: 100%;
  584. font-size: 28rpx;
  585. margin-right: 10px;
  586. }
  587. }
  588. .cart-list{
  589. flex-grow: 1;
  590. background-color: #F5F5F5;
  591. width: 100%;
  592. .loading-bar{
  593. text-align: center;
  594. padding: 10px 0;
  595. color: #999999;
  596. }
  597. .empty-bar{
  598. display: flex;
  599. flex-direction: column;
  600. align-items: center;
  601. justify-content: center;
  602. padding: 20px 0;
  603. image{
  604. width: 100px;
  605. height: 100px;
  606. }
  607. view{
  608. font-size: 14px;
  609. color: #999999;
  610. margin-top: 10px;
  611. }
  612. }
  613. }
  614. .cart-footer{
  615. width: 100%;
  616. background-color: #FFFFFF;
  617. .cart-footer-box{
  618. width: 100%;
  619. height: 100rpx;
  620. display: flex;
  621. justify-content: space-between;
  622. align-items: center;
  623. }
  624. .cart-footer-left{
  625. display: flex;
  626. align-items: center;
  627. padding: 0 20rpx 0 36rpx;
  628. .cart-footer-left-all{
  629. display: flex;
  630. align-items: center;
  631. .cart-footer-left-all-text{
  632. margin-left: 10rpx;
  633. font-size: 28rpx;
  634. color: #333333;
  635. }
  636. }
  637. }
  638. .cart-footer-right{
  639. padding: 0 20rpx;
  640. display: flex;
  641. align-items: center;
  642. justify-content: space-between;
  643. .cart-footer-left-price{
  644. display: flex;
  645. align-items: center;
  646. .cart-footer-left-price-text{
  647. font-size: 28rpx;
  648. color: #333333;
  649. }
  650. .cart-footer-left-price-num{
  651. font-size: 28rpx;
  652. color: #d81e06;
  653. }
  654. }
  655. .red{
  656. color: #d81e06;
  657. }
  658. .cart-head-btton{
  659. height: 100%;
  660. font-size: 28rpx;
  661. color: #333333;
  662. }
  663. .cart-footer-right-button{
  664. display: flex;
  665. align-items: center;
  666. margin-left: 20rpx;
  667. text-align: right;
  668. .btns{
  669. border-radius: 100rpx;
  670. height: 36px;
  671. }
  672. }
  673. }
  674. }
  675. }
  676. }
  677. </style>