index.vue 18 KB

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