index.vue 15 KB

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