index.vue 17 KB

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