index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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">删除</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.every(item => item.checked && (item.status==1||item.dealerScopeFlag==1)))
  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. key.forEach(item => {
  299. if(item.status == 1 || item.dealerScopeFlag == 1){
  300. item.checked = this.allChecked
  301. }
  302. })
  303. })
  304. // 合计
  305. this.getChooseHeji()
  306. },
  307. //单个删除
  308. removeChoose(cartSn,isBatch){
  309. // 删除项
  310. const crow = this.getItemById(cartSn)
  311. if(crow.p>=0&&crow.s>=0){
  312. this.list[crow.p].splice(crow.s, 1);
  313. this.total = this.total - 1
  314. // 合计
  315. if(!isBatch){
  316. this.getChooseHeji()
  317. // 判断是否全选
  318. this.isAllChecked()
  319. }
  320. }
  321. },
  322. // 批量删除
  323. batchRemove(){
  324. if(this.totalNum==0){
  325. uni.showToast({
  326. title: '请选择产品',
  327. icon: 'none'
  328. })
  329. return false
  330. }
  331. const _this = this
  332. uni.showModal({
  333. title: '提示',
  334. content: '确定删除商品吗?',
  335. confirmText: '确定',
  336. success(res) {
  337. if(res.confirm){
  338. const snList = []
  339. _this.list.forEach(item => {
  340. item.filter(k=>k.checked).forEach(s=>{
  341. snList.push(s.cartSn)
  342. })
  343. })
  344. // 删除购物车
  345. _this.removeCart(snList,true)
  346. }
  347. }
  348. })
  349. },
  350. // 删除商品
  351. removeCart(snList,isBatch){
  352. const _this = this
  353. uni.showLoading({
  354. title: '删除中'
  355. })
  356. deleteCart({cartSns:snList}).then(ret => {
  357. if(ret.status == 200){
  358. uni.showToast({
  359. title: '删除成功',
  360. icon: 'none'
  361. })
  362. snList.forEach(sn => {
  363. _this.removeChoose(sn,isBatch)
  364. })
  365. // 合计,批量操作
  366. if(isBatch){
  367. // 如果全选
  368. if(_this.allChecked){
  369. _this.list = []
  370. _this.allChecked = false
  371. }
  372. _this.getChooseHeji()
  373. // 判断是否全选
  374. this.isAllChecked()
  375. }
  376. }
  377. uni.hideLoading()
  378. })
  379. },
  380. // 去结算
  381. settlement(){
  382. if(this.totalAmount>0){
  383. this.submitOrder()
  384. }else{
  385. uni.showToast({
  386. title: '请选择产品',
  387. icon: 'none'
  388. })
  389. }
  390. },
  391. // 去结算
  392. submitOrder(){
  393. const _this = this
  394. // 获取已选商品
  395. const detailList = []
  396. const cartSn = []
  397. const soldOutSn = []
  398. const sellOutSn = []
  399. this.list.forEach(key => {
  400. key.filter(item=>item.checked).forEach(item => {
  401. // 已下架产品提示
  402. if(item.status == 0){
  403. soldOutSn.push(item.productCode)
  404. }
  405. //售罄产品提示
  406. else if(item.dealerScopeFlag == 0){
  407. sellOutSn.push(item.productCode)
  408. }else{
  409. detailList.push({
  410. productSn:item.productSn,
  411. productCode:item.productCode,
  412. qty: item.qty,
  413. price:item.price,
  414. })
  415. cartSn.push(item.cartSn)
  416. }
  417. })
  418. })
  419. // 提示信息
  420. if(soldOutSn.length || sellOutSn.length){
  421. uni.showModal({
  422. title: '提示',
  423. content: (soldOutSn.length ? `产品${soldOutSn.join(',')}已下架,`:'')+(sellOutSn.length?`产品${sellOutSn.join(',')}已售罄,`:'')+`不可采购。是否继续采购其他产品?`,
  424. success(res) {
  425. if(res.confirm){
  426. _this.submitForm(detailList,cartSn)
  427. }
  428. }
  429. })
  430. return
  431. }
  432. // 提交
  433. this.submitForm(detailList,cartSn)
  434. },
  435. // 确认提交
  436. submitForm(){
  437. uni.showLoading({
  438. title: '提交中...',
  439. mask: true
  440. })
  441. this.loading = true
  442. purchaseSave({
  443. detailList: detailList
  444. }).then(res => {
  445. this.loading = false
  446. uni.hideLoading()
  447. if(res.status == 200){
  448. // 删除已提交产品
  449. this.removeCart(cartSn,true)
  450. res.data.detailList = []
  451. this.$store.state.vuex_tempData = res.data
  452. uni.navigateTo({
  453. url: "/pagesB/procureOrder"
  454. })
  455. }else{
  456. uni.showToast({
  457. title: res.message,
  458. icon: 'none'
  459. })
  460. }
  461. })
  462. },
  463. // 计算总款数、合计、数量
  464. getChooseHeji(){
  465. this.totalAmount = 0
  466. this.totalNum = 0
  467. this.list.forEach(key => {
  468. key.filter(item=>item.checked).forEach(item => {
  469. this.totalAmount += item.price * item.qty // 总金额
  470. this.totalNum += item.qty // 总数量
  471. })
  472. })
  473. },
  474. }
  475. }
  476. </script>
  477. <style lang="less">
  478. .pages{
  479. height: 100vh;
  480. display: flex;
  481. flex-direction: column;
  482. .scrollPage-header{
  483. z-index: 1;
  484. .header-title{
  485. display: flex;
  486. align-items: center;
  487. justify-content: space-between;
  488. padding: 0 10px;
  489. height: 44px;
  490. background-color: #FFFFFF;
  491. box-shadow: 0px 1px 0px 0px #E6E6E6;
  492. .header-title{
  493. font-size: 16px;
  494. color: #333333;
  495. max-width: 70%;
  496. }
  497. .header-left{
  498. display: flex;
  499. align-items: center;
  500. text{
  501. font-size: 14px;
  502. color: #333333;
  503. margin-left: 5px;
  504. }
  505. }
  506. .header-right{
  507. width: 50px;
  508. }
  509. }
  510. }
  511. .cart-body{
  512. position: relative;
  513. flex-grow: 1;
  514. display: flex;
  515. flex-direction: column;
  516. .cart-head{
  517. display: flex;
  518. justify-content: space-between;
  519. align-items: center;
  520. padding: 0 20rpx;
  521. background-color: #FFFFFF;
  522. height: 40px;
  523. .cart-head-left{
  524. display: flex;
  525. align-items: center;
  526. font-size: 28rpx;
  527. color: #333333;
  528. height: 100%;
  529. text{
  530. color: #d81e06;
  531. margin: 0 10rpx;
  532. }
  533. }
  534. .cart-head-right{
  535. display: flex;
  536. align-items: center;
  537. }
  538. .red{
  539. color: #d81e06;
  540. }
  541. .blue{
  542. color: #2196F3;
  543. }
  544. .cart-head-btton{
  545. height: 100%;
  546. font-size: 28rpx;
  547. margin-right: 10px;
  548. }
  549. }
  550. .cart-list{
  551. flex-grow: 1;
  552. background-color: #F5F5F5;
  553. width: 100%;
  554. .loading-bar{
  555. text-align: center;
  556. padding: 10px 0;
  557. color: #999999;
  558. }
  559. .empty-bar{
  560. display: flex;
  561. flex-direction: column;
  562. align-items: center;
  563. justify-content: center;
  564. padding: 20px 0;
  565. image{
  566. width: 100px;
  567. height: 100px;
  568. }
  569. view{
  570. font-size: 14px;
  571. color: #999999;
  572. margin-top: 10px;
  573. }
  574. }
  575. }
  576. .cart-footer{
  577. width: 100%;
  578. background-color: #FFFFFF;
  579. .cart-footer-box{
  580. width: 100%;
  581. height: 100rpx;
  582. display: flex;
  583. justify-content: space-between;
  584. align-items: center;
  585. }
  586. .cart-footer-left{
  587. display: flex;
  588. align-items: center;
  589. padding: 0 20rpx 0 36rpx;
  590. .cart-footer-left-all{
  591. display: flex;
  592. align-items: center;
  593. .cart-footer-left-all-text{
  594. margin-left: 10rpx;
  595. font-size: 28rpx;
  596. color: #333333;
  597. }
  598. }
  599. }
  600. .cart-footer-right{
  601. padding: 0 20rpx;
  602. display: flex;
  603. align-items: center;
  604. justify-content: space-between;
  605. .cart-footer-left-price{
  606. display: flex;
  607. align-items: center;
  608. .cart-footer-left-price-text{
  609. font-size: 28rpx;
  610. color: #333333;
  611. }
  612. .cart-footer-left-price-num{
  613. font-size: 28rpx;
  614. color: #d81e06;
  615. }
  616. }
  617. .red{
  618. color: #d81e06;
  619. }
  620. .cart-head-btton{
  621. height: 100%;
  622. font-size: 28rpx;
  623. color: #333333;
  624. }
  625. .cart-footer-right-button{
  626. display: flex;
  627. align-items: center;
  628. margin-left: 20rpx;
  629. text-align: right;
  630. .btns{
  631. border-radius: 100rpx;
  632. height: 36px;
  633. }
  634. }
  635. }
  636. }
  637. }
  638. }
  639. </style>