index.vue 18 KB

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