index.vue 18 KB

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