index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. uni.$emit('refashCart')
  250. }
  251. }
  252. uni.hideLoading()
  253. })
  254. },
  255. // 单选择
  256. chooseItem(cartSn){
  257. const crow = this.getItemById(cartSn)
  258. if(crow.p>=0&&crow.s>=0){
  259. this.list[crow.p][crow.s].checked = !this.list[crow.p][crow.s].checked
  260. // 合计
  261. this.getChooseHeji()
  262. // 判断是否全选
  263. this.isAllChecked()
  264. }
  265. },
  266. // 判断是否全选
  267. isAllChecked(){
  268. if(this.editFlag){
  269. this.allChecked = this.list.every(key => key.every(item => item.checked))
  270. }else{
  271. this.allChecked = this.list.every(key => key.filter(item => item.status==1&&item.dealerScopeFlag==1 ).every(item => item.checked))
  272. }
  273. },
  274. // 编辑
  275. editAll(){
  276. this.editFlag = !this.editFlag
  277. this.list.forEach(key => {
  278. key.forEach(item => {
  279. item.edit = this.editFlag
  280. if(item.status==0||item.dealerScopeFlag==0){
  281. item.checked = false
  282. }
  283. })
  284. })
  285. // 合计
  286. this.getChooseHeji()
  287. // 判断是否全选
  288. this.isAllChecked()
  289. },
  290. // 清空购物车
  291. clearAll(){
  292. const _this = this
  293. uni.showModal({
  294. title: '提示',
  295. content: '确定清空购物车?',
  296. confirmText: '确定',
  297. success(res) {
  298. if(res.confirm){
  299. uni.showLoading({
  300. title: '正在清空...',
  301. mask: true
  302. })
  303. }
  304. }
  305. })
  306. },
  307. // 全选
  308. chooseAll(){
  309. if(this.allDisabled){
  310. return
  311. }
  312. this.allChecked = !this.allChecked
  313. this.list.forEach(key => {
  314. // 非编辑模式
  315. if(!this.editFlag){
  316. key.filter(item=>item.status == 1 && item.dealerScopeFlag == 1).forEach(item => {
  317. item.checked = this.allChecked
  318. })
  319. }else{
  320. // 编辑模式
  321. key.forEach(item => {
  322. item.checked = this.allChecked
  323. })
  324. }
  325. })
  326. // 合计
  327. this.getChooseHeji()
  328. },
  329. //单个删除
  330. removeChoose(cartSn,isBatch){
  331. // 删除项
  332. const crow = this.getItemById(cartSn)
  333. if(crow.p>=0&&crow.s>=0){
  334. this.list[crow.p].splice(crow.s, 1);
  335. this.total = this.total - 1
  336. // 合计
  337. if(!isBatch){
  338. this.getChooseHeji()
  339. // 判断是否全选
  340. this.isAllChecked()
  341. }
  342. }
  343. },
  344. // 批量删除
  345. batchRemove(){
  346. if(this.totalNum==0){
  347. uni.showToast({
  348. title: '请选择产品',
  349. icon: 'none'
  350. })
  351. return false
  352. }
  353. const _this = this
  354. uni.showModal({
  355. title: '提示',
  356. content: '确定删除商品吗?',
  357. confirmText: '确定',
  358. success(res) {
  359. if(res.confirm){
  360. const snList = []
  361. _this.list.forEach(item => {
  362. item.filter(k=>k.checked).forEach(s=>{
  363. snList.push(s.cartSn)
  364. })
  365. })
  366. // 删除购物车
  367. _this.removeCart(snList,true,true)
  368. }
  369. }
  370. })
  371. },
  372. // 删除商品
  373. removeCart(snList,isBatch,isTip){
  374. const _this = this
  375. if(isTip){
  376. uni.showLoading({
  377. title: '删除中'
  378. })
  379. }
  380. deleteCart({cartSns:snList}).then(ret => {
  381. if(ret.status == 200){
  382. snList.forEach(sn => {
  383. _this.removeChoose(sn,isBatch)
  384. })
  385. // 合计,批量操作
  386. if(isBatch){
  387. _this.getChooseHeji()
  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. .scrollPage-header{
  523. z-index: 1;
  524. .header-title{
  525. display: flex;
  526. align-items: center;
  527. justify-content: space-between;
  528. padding: 0 10px;
  529. height: 44px;
  530. background-color: #FFFFFF;
  531. box-shadow: 0px 1px 0px 0px #E6E6E6;
  532. .header-title{
  533. font-size: 16px;
  534. color: #333333;
  535. max-width: 70%;
  536. }
  537. .header-left{
  538. display: flex;
  539. align-items: center;
  540. text{
  541. font-size: 14px;
  542. color: #333333;
  543. margin-left: 5px;
  544. }
  545. }
  546. .header-right{
  547. width: 50px;
  548. }
  549. }
  550. }
  551. .cart-body{
  552. position: relative;
  553. flex-grow: 1;
  554. display: flex;
  555. flex-direction: column;
  556. .cart-head{
  557. display: flex;
  558. justify-content: space-between;
  559. align-items: center;
  560. padding: 0 20rpx;
  561. background-color: #FFFFFF;
  562. height: 40px;
  563. .cart-head-left{
  564. display: flex;
  565. align-items: center;
  566. font-size: 28rpx;
  567. color: #333333;
  568. height: 100%;
  569. text{
  570. color: #d81e06;
  571. margin: 0 10rpx;
  572. }
  573. }
  574. .cart-head-right{
  575. display: flex;
  576. align-items: center;
  577. }
  578. .red{
  579. color: #d81e06;
  580. }
  581. .blue{
  582. color: #2196F3;
  583. }
  584. .cart-head-btton{
  585. height: 100%;
  586. font-size: 28rpx;
  587. margin-right: 10px;
  588. }
  589. }
  590. .cart-list{
  591. flex-grow: 1;
  592. background-color: #F5F5F5;
  593. width: 100%;
  594. .loading-bar{
  595. text-align: center;
  596. padding: 10px 0;
  597. color: #999999;
  598. }
  599. .empty-bar{
  600. display: flex;
  601. flex-direction: column;
  602. align-items: center;
  603. justify-content: center;
  604. padding: 20px 0;
  605. image{
  606. width: 100px;
  607. height: 100px;
  608. }
  609. view{
  610. font-size: 14px;
  611. color: #999999;
  612. margin-top: 10px;
  613. }
  614. }
  615. }
  616. .cart-footer{
  617. width: 100%;
  618. background-color: #FFFFFF;
  619. .cart-footer-box{
  620. width: 100%;
  621. height: 100rpx;
  622. display: flex;
  623. justify-content: space-between;
  624. align-items: center;
  625. }
  626. .cart-footer-left{
  627. display: flex;
  628. align-items: center;
  629. padding: 0 20rpx 0 36rpx;
  630. .cart-footer-left-all{
  631. display: flex;
  632. align-items: center;
  633. .cart-footer-left-all-text{
  634. margin-left: 10rpx;
  635. font-size: 28rpx;
  636. color: #333333;
  637. }
  638. }
  639. }
  640. .cart-footer-right{
  641. padding: 0 20rpx;
  642. display: flex;
  643. align-items: center;
  644. justify-content: space-between;
  645. .cart-footer-left-price{
  646. display: flex;
  647. align-items: center;
  648. .cart-footer-left-price-text{
  649. font-size: 28rpx;
  650. color: #333333;
  651. }
  652. .cart-footer-left-price-num{
  653. font-size: 28rpx;
  654. color: #d81e06;
  655. }
  656. }
  657. .red{
  658. color: #d81e06;
  659. }
  660. .cart-head-btton{
  661. height: 100%;
  662. font-size: 28rpx;
  663. color: #333333;
  664. }
  665. .cart-footer-right-button{
  666. display: flex;
  667. align-items: center;
  668. margin-left: 20rpx;
  669. text-align: right;
  670. .btns{
  671. border-radius: 100rpx;
  672. height: 36px;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. }
  679. </style>