123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818 |
- <template>
- <view class="pages">
- <!-- head -->
- <sklineHeader title="购物车"></sklineHeader>
- <view class="cart-body">
- <!-- 内容 -->
- <scroll-view
- scroll-y
- class="cart-list"
- type="custom"
- :bounces="false"
- @scrolltolower="onreachBottom">
- <sticky-header>
- <!-- 头部 -->
- <view class="cart-head" v-if="total>0">
- <view class="cart-head-left">共<text>{{total}}</text>款商品</view>
- <view class="cart-head-right">
- <view class="cart-head-btton blue" @click="editAll">
- <text>{{editFlag?'完成':'编辑'}}</text>
- </view>
- </view>
- </view>
- </sticky-header>
- <productItem
- v-for="(item, index) in list"
- :key="index"
- :list="item"
- @changeQty="changeQty"
- @check="chooseItem"
- @editQty="editQty"
- @remove="removeCart">
- </productItem>
- <!-- loading -->
- <view class="loading-bar" v-if="total>0 && status!='loading'">{{noDataText}}</view>
- <view class="empty-bar" v-if="total==0 && status!='loading'" @click="goBack">
- <image mode="aspectFit" :src="empty.imgUrl"></image>
- <view>{{empty.tip}}</view>
- </view>
- </scroll-view>
- <!-- 底部 -->
- <view class="cart-footer" v-if="total>0">
- <view class="cart-footer-box">
- <view class="cart-footer-left">
- <view class="cart-footer-left-all" @click="chooseAll">
- <view class="cart-footer-left-all-icon">
- <uni-icons v-if="allDisabled&&!editFlag" type="circle-filled" size="24" color="#ccc"></uni-icons>
- <uni-icons v-else :type="allChecked?'checkbox-filled':'circle'" size="24" :color="allChecked?'#dd0000':'#666'"></uni-icons>
- </view>
- <view class="cart-footer-left-all-text">
- <text>全选</text>
- </view>
- </view>
- </view>
- <view class="cart-footer-right">
- <view class="cart-footer-left-price" v-if="!editFlag && totalAmount">
- <view class="flex align_center">
- <view class="cart-footer-left-price-text">
- <text>合计:</text>
- </view>
- <view class="cart-footer-left-price-num">
- ¥<text>{{Number(totalAmount).toFixed(2).toString().split('.')[0]}}</text>.{{Number(totalAmount).toFixed(2).toString().split('.')[1]}}
- </view>
- </view>
- <view class="flex align_center">
- <view class="cart-footer-left-price-text1">
- <text>优惠:¥{{totalDiscount}}</text>
- <text>代金券:¥{{totalCoupon}}</text>
- </view>
- </view>
- </view>
- <view v-else></view>
- <view class="cart-footer-right-button">
- <button class="btns" :loading="loading" v-if="!editFlag" type="warn" size="mini" @click="settlement">去结算({{totalNum}})</button>
- <button v-else class="btns" type="warn" plain size="mini" @click="batchRemove">删除({{totalSel}})</button>
- </view>
- </view>
- </view>
- <view :style="{height:safeAreaBottom+'px'}"></view>
- </view>
- </view>
- <!-- 修改数量弹框 -->
- <uni-popup ref="showPopu" type="center" :maskClick="false">
- <view class="popu-content">
- <view class="popu-close" @click="closePopu">
- <uni-icons size="26" type="closeempty"></uni-icons>
- </view>
- <view class="popu-content-title flex align_center justify_center"><text>修改商品数量</text></view>
- <view class="popu-content-num flex align_center justify_center">
- <uni-number-box v-model="curEditRow.qty" :size="28" :input-width="280" :input-height="70" :min="1" :max="999"></uni-number-box>
- </view>
- <view class="popu-content-btn">
- <view class="popu-content-btn-item cancelBtn" @click="closePopu">取消</view>
- <view class="popu-content-btn-item okbtn" @click="changeSingQty">确定</view>
- </view>
- </view>
- </uni-popup>
- </view>
- </template>
- <script>
- import {
- mapState,
- mapMutations,
- } from 'vuex'
- import productItem from '@/pagesB/cart/productitem.vue'
- import sklineHeader from '../../components/skline/sklineHeader.vue'
- import { getCartList, updateCart, deleteCart } from '@/api/cart.js'
- import { purchaseSave, purchaseCheck } from '@/api/purchase.js'
- export default {
- components:{
- productItem,
- sklineHeader
- },
- onLoad() {
- // 计算区域高度
- this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
- this.screenWidth = uni.getSystemInfoSync().windowWidth
- this.screenHeight = uni.getSystemInfoSync().windowHeight
- this.safeAreaBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
- // 获取列表数据
- this.pageInit()
- },
- data() {
- return {
- loading: false,
- status: 'loading',
- noDataText: '暂无产品',
- empty: {
- tip: '购物车空空的,去挑选商品吧',
- imgUrl: '/static/kongCart.png'
- },
- // 查询条件
- pageNo: 1, // 页数
- pageSize: 10000, // 每页数
- list: [], // 列表数据
- total: 0, // 总款数
- totalAmount: 0, // 总金额
- totalDiscount:0, // 总优惠
- totalCoupon: 0, // 总代金券
- totalNum: 0, // 总数量
- totalSel: 0, // 已选数量
- editFlag: false, // 是否编辑状态
- allChecked: false, // 是否全选
- screenWidth: 325, // 屏幕宽度
- screenHeight: 0, // 屏幕高度
- statusBarHeight: 44, // 状态栏高度
- safeAreaBottom: 0, // 底部安全区域高度
- curEditRow: null
- }
- },
- computed: {
- ...mapState(['hasLogin']),
- userInfo(){
- return this.$store.state.vuex_userInfo
- },
- allDisabled(){
- let ret = false
- if(!this.editFlag){
- this.list.forEach(key => {
- ret = key.every(item => !item.status || item.status==0 || item.dealerScopeFlag==0)
- })
- }
- return ret
- }
- },
- methods: {
- goBack(){
- uni.navigateBack()
- },
- pageInit(){
- this.pageNo = 1
- this.list = []
- this.getRow()
- },
- // 关闭弹框
- closePopu(){
- this.$refs.showPopu.close()
- this.curEditRow = null
- },
- // 打开编辑数量弹框
- editQty(item){
- this.curEditRow = JSON.parse(JSON.stringify(item))
- this.$refs.showPopu.open()
- },
- // 确定修改
- changeSingQty(){
- this.changeQty(this.curEditRow.qty,this.curEditRow.cartSn)
- },
- // 查询列表
- getRow (pageNo) {
- let _this = this
- if (pageNo) {
- this.pageNo = pageNo
- }
- let params = {
- pageNo: this.pageNo,
- pageSize: this.pageSize,
- queryWord: this.queryWord
- }
- this.status = "loading"
- getCartList(params).then(res => {
- if (res.status == 200) {
- _this.total = res.data.count || 0
- const list = res.data.list
- for(let i=0;i<list.length;i=i+10){
- _this.list.push(list.slice(i,(i+10)>list.length?list.length:(i+10)).map(key=>{
- const a = key.shopPromoProduct
- key.orginPrice = key.price
- if(a){
- key.promoType = a.promoType
- key.resultValue = a.resultValue
- key.conditionValue = a.conditionValue
- key.discountType = a.discountType
- key.promoProductSn = a.promoProductSn
- key.promoSn = a.promoSn
- // 特价
- if(key.promoType=='PROMO_PROD'){
- // 直降
- if(key.discountType == 'STRAIGHT_DOWN'){
- key.price = key.price - key.resultValue
- }
- // 折扣
- if(key.discountType == 'DISCOUNT'){
- key.price = Number(key.price * key.resultValue).toFixed(2)
- }
- }
- }
- delete key.shopPromoProduct
- return {
- id: key.id,
- edit: false,
- checked: _this.allChecked,
- cartSn: key.cartSn,
- price: key.price,
- orginPrice: key.orginPrice,
- qty: key.qty,
- productSn: key.productSn,
- productName: key.productEntity.productName,
- productImage: key.productEntity.images,
- productCode: key.productEntity.code,
- productOrigCode: key.productEntity.origCode,
- unit: key.productEntity.unit,
- status: key.status,
- dealerScopeFlag: key.dealerScopeFlag,
- resultValue: key.resultValue,
- conditionValue: key.conditionValue,
- discountType: key.discountType,
- promoType: key.promoType,
- promoProductSn: key.promoProductSn,
- promoSn: key.promoSn,
- giftQty: 0
- }
- }))
- }
-
- // 默认全选
- if(_this.pageNo == 1){
- _this.$nextTick(()=>{
- _this.chooseAll()
- })
- }
- // 判断是否最后一页
- const maxPages = Math.ceil(_this.total / _this.pageSize)
- if(this.pageNo >= maxPages){
- _this.status = 'nomore'
- _this.noDataText = '没有更多了'
- }else{
- _this.status = 'loadmore'
- _this.noDataText = '加载更多'
- }
- } else {
- _this.list = []
- _this.total = 0
- _this.status = 'loadmore'
- _this.noDataText = res.message
- }
- })
- },
- // scroll-view到底部加载更多
- onreachBottom() {
- // 判断是否最后一页
- const maxPages = Math.ceil(this.total / this.pageSize)
- if(this.pageNo >= maxPages){
- this.status = "nomore"
- this.noDataText = '没有更多了'
- }else{
- this.pageNo += 1
- this.getRow()
- }
- },
- // 根据cartSn查找
- getItemById(cartSn){
- const a = this.list.findIndex(k => k.find(s=> s.cartSn == cartSn))
- const b = a>=0 ? this.list[a].findIndex(s=> s.cartSn == cartSn) : -1
- return {p:a,s:b}
- },
- // 已选产品变更数量
- changeQty(val,cartSn){
- uni.showLoading({
- title: '加载中'
- })
- updateCart({
- cartSn: cartSn,
- qty: val
- }).then(res => {
- if(res.status == 200){
- const crow = this.getItemById(cartSn)
- if(crow.p>=0&&crow.s>=0){
- this.list[crow.p][crow.s].qty = val
- this.list[crow.p].splice()
- // 合计
- this.getChooseHeji()
- uni.$emit('refashCart')
- }
- this.closePopu()
- }else{
- uni.showToast({
- title: res.message,
- icon: 'none'
- })
- }
- uni.hideLoading()
- })
- },
- // 单选择
- chooseItem(cartSn){
- const crow = this.getItemById(cartSn)
- if(crow.p>=0&&crow.s>=0){
- this.list[crow.p][crow.s].checked = !this.list[crow.p][crow.s].checked
- // 合计
- this.getChooseHeji()
- // 判断是否全选
- this.isAllChecked()
- }
- },
- // 判断是否全选
- isAllChecked(){
- if(this.editFlag){
- this.allChecked = this.list.every(key => key.every(item => item.checked))
- }else{
- this.allChecked = this.list.every(key => key.filter(item => item.status==1&&item.dealerScopeFlag==1 ).every(item => item.checked))
- }
- },
- // 编辑
- editAll(){
- this.editFlag = !this.editFlag
- this.list.forEach(key => {
- key.forEach(item => {
- item.edit = this.editFlag
- if(!item.status||item.status==0||item.dealerScopeFlag==0){
- item.checked = false
- }
- })
- })
- // 合计
- this.getChooseHeji()
- // 判断是否全选
- this.isAllChecked()
- },
- // 清空购物车
- clearAll(){
- const _this = this
- uni.showModal({
- title: '提示',
- content: '确定清空购物车?',
- confirmText: '确定',
- success(res) {
- if(res.confirm){
- uni.showLoading({
- title: '正在清空...',
- mask: true
- })
-
- }
- }
- })
- },
- // 全选
- chooseAll(){
- if(this.allDisabled){
- return
- }
- this.allChecked = !this.allChecked
- this.list.forEach(key => {
- // 非编辑模式
- if(!this.editFlag){
- key.filter(item=>item.status == 1 && item.dealerScopeFlag == 1).forEach(item => {
- item.checked = this.allChecked
- })
- }else{
- // 编辑模式
- key.forEach(item => {
- item.checked = this.allChecked
- })
- }
- })
- // 合计
- this.getChooseHeji()
- },
- //单个删除
- removeChoose(cartSn,isBatch){
- // 删除项
- const crow = this.getItemById(cartSn)
- if(crow.p>=0&&crow.s>=0){
- this.list[crow.p].splice(crow.s, 1);
- this.total = this.total - 1
- // 合计
- if(!isBatch){
- this.getChooseHeji()
- uni.$emit('refashCart')
- // 判断是否全选
- this.isAllChecked()
- }
- }
- },
- // 批量删除
- batchRemove(){
- if(this.totalNum==0){
- uni.showToast({
- title: '请选择产品',
- icon: 'none'
- })
- return false
- }
- const _this = this
- uni.showModal({
- title: '提示',
- content: '确定删除商品吗?',
- confirmText: '确定',
- success(res) {
- if(res.confirm){
- const snList = []
- _this.list.forEach(item => {
- item.filter(k=>k.checked).forEach(s=>{
- snList.push(s.cartSn)
- })
- })
-
- // 删除购物车
- _this.removeCart(snList,true,true)
- }
- }
- })
- },
- // 删除商品
- removeCart(snList,isBatch,isTip){
- const _this = this
- if(isTip){
- uni.showLoading({
- title: '删除中'
- })
- }
- deleteCart({cartSns:snList}).then(ret => {
- if(ret.status == 200){
- snList.forEach(sn => {
- _this.removeChoose(sn,isBatch)
- })
- // 合计,批量操作
- if(isBatch){
- _this.getChooseHeji()
- uni.$emit('refashCart')
- // 判断是否全选
- _this.isAllChecked()
- }
- if(isTip){
- uni.showToast({
- title: '删除成功',
- icon: 'none'
- })
- }
- }
- uni.hideLoading()
- })
- },
- // 去结算
- settlement(){
- if(this.totalAmount>0){
- this.submitOrder()
- }else{
- uni.showToast({
- title: '请选择产品',
- icon: 'none'
- })
- }
- },
- // 去结算
- submitOrder(){
- const _this = this
- // 获取已选商品
- const detailList = []
- const cartSn = []
- _this.list.forEach(key => {
- key.filter(item=>item.checked).forEach(item => {
- detailList.push({
- productSn:item.productSn,
- productCode:item.productCode,
- qty: item.qty,
- price:item.orginPrice,
- })
- cartSn.push({
- cartSn: item.cartSn,
- productSn:item.productSn
- })
- })
- })
-
- // 校验并提示信息
- purchaseCheck({detailList:detailList}).then(res => {
- if(res.status == 200){
- const successList = res.data.successList.map(item => {
- return {
- productSn:item.productSn,
- productCode: item.productCode,
- qty: item.qty,
- price:item.price
- }
- })
- const delSn = cartSn.filter(item => successList.find(k => k.productSn == item.productSn)).map(item => item.cartSn)
- const removeList = res.data.removeList.map(item => item.productCode)
- const selloutList = res.data.selloutList.map(item => item.productCode)
- console.log(successList,delSn)
- console.log(removeList,selloutList)
- // 有已下架或已售罄产品提示
- if(removeList.length || selloutList.length){
- uni.showModal({
- title: '提示',
- content: (removeList.length ? `产品${removeList.join(',')}已下架,`:'')+(selloutList.length?`产品${selloutList.join(',')}已售罄,`:'')+`不可采购。`+(successList.length?'是否继续采购其他产品?':''),
- showCancel: successList.length,
- confirmText: successList.length?'确定':'知道了',
- success(ret) {
- if(ret.confirm && successList.length){
- _this.submitForm(successList,delSn)
- }
- }
- })
- }else{
- // 提交
- _this.submitForm(successList,delSn)
- }
- }else{
- uni.showToast({
- title: res.message,
- icon: 'none'
- })
- }
- })
- },
- // 确认提交
- submitForm(detailList,cartSn){
- uni.showLoading({
- title: '提交中...',
- mask: true
- })
- this.loading = true
- purchaseSave({
- detailList: detailList
- }).then(res => {
- this.loading = false
- uni.hideLoading()
- if(res.status == 200){
- // 删除已提交产品
- this.removeCart(cartSn,true,false)
- res.data.detailList = []
- this.$store.state.vuex_tempData = res.data
- uni.navigateTo({
- url: "/pagesB/procureOrder"
- })
- }else{
- uni.showToast({
- title: res.message,
- icon: 'none'
- })
- }
- })
- },
- // 计算总款数、合计、数量
- getChooseHeji(){
- this.totalAmount = 0
- this.totalDiscount = 0
- this.totalCoupon = 0
- this.totalNum = 0
- this.totalSel = 0
- this.list.forEach(key => {
- key.filter(item=>item.checked).forEach(item => {
- this.totalAmount += item.price * item.qty // 总金额
- this.totalNum += item.qty // 总数量
- this.totalSel += 1 // 已选数量
- // 如果是返券类型
- if(item.promoType=='BUY_PROD_GIVE_VALID'){
- this.totalCoupon += item.resultValue * item.qty
- }
- // 特价
- if(item.promoType=='PROMO_PROD'){
- // 直降
- if(item.discountType == 'STRAIGHT_DOWN'){
- this.totalDiscount += item.resultValue * item.qty
- }
- // 折扣
- if(item.discountType == 'DISCOUNT'){
- this.totalDiscount += Number(item.price * (1-item.resultValue)) * item.qty
- }
- }
- // 满赠
- item.giftQty = item.promoType=='BUY_PROD_GIVE_PROD' ? Math.floor(item.qty / item.conditionValue)*item.resultValue : 0
- })
- })
- },
- }
- }
- </script>
- <style lang="less">
- .pages{
- height: 100vh;
- display: flex;
- flex-direction: column;
- .popu-content{
- background-color: #FFFFFF;
- border-radius: 10px;
- width: 300px;
- display: flex;
- flex-direction: column;
- align-items: center;
- padding: 10px 15px;
- .popu-close{
- padding: 10px;
- position: absolute;
- right: 0;
- top: 0;
- }
- .popu-content-title{
- padding: 10px;
- font-size: 16px;
- }
- .popu-content-num{
- font-size: 14px;
- color: #999999;
- padding: 10px 0;
- > text{
- margin-right: 10px;
- font-size: 10px;
- }
- }
- .popu-content-btn{
- width: 100%;
- display: flex;
- align-items: center;
- justify-content: space-around;
- margin: 20px 0 10px;
- .popu-content-btn-item{
- width: 40%;
- height: 36px;
- font-size: 14px;
- display: flex;
- align-items: center;
- justify-content: center;
- border-radius: 100px;
- }
- .cancelBtn{
- background-color: #f8f8f8;
- color: #666;
- }
- .okbtn{
- background-color: #f70000;
- color: #FFFFFF;
- }
- }
- }
- .cart-body{
- position: relative;
- flex-grow: 1;
- display: flex;
- flex-direction: column;
- .cart-head{
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 0 20rpx;
- background-color: #FFFFFF;
- height: 40px;
- .cart-head-left{
- display: flex;
- align-items: center;
- font-size: 28rpx;
- color: #333333;
- height: 100%;
- text{
- color: #d81e06;
- margin: 0 10rpx;
- }
- }
- .cart-head-right{
- display: flex;
- align-items: center;
- }
- .red{
- color: #d81e06;
- }
- .blue{
- color: #2196F3;
- }
- .cart-head-btton{
- height: 100%;
- font-size: 28rpx;
- margin-right: 10px;
- }
- }
- .cart-list{
- flex-grow: 1;
- background-color: #F5F5F5;
- width: 100%;
- .loading-bar{
- text-align: center;
- padding: 10px 0;
- color: #999999;
- }
- .empty-bar{
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 20px 0;
- margin-top: 20vh;
- image{
- width: 100px;
- height: 100px;
- }
- view{
- font-size: 14px;
- color: #999999;
- margin-top: 10px;
- }
- }
- }
- .cart-footer{
- width: 100%;
- background-color: #FFFFFF;
- .cart-footer-box{
- width: 100%;
- height: 100rpx;
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- .cart-footer-left{
- display: flex;
- align-items: center;
- padding: 0 20rpx 0 36rpx;
- .cart-footer-left-all{
- display: flex;
- align-items: center;
- .cart-footer-left-all-text{
- margin-left: 10rpx;
- font-size: 28rpx;
- color: #333333;
- }
- }
- }
- .cart-footer-right{
- padding: 0 20rpx;
- display: flex;
- align-items: center;
- justify-content: space-between;
- flex-grow:1;
- .cart-footer-left-price{
- flex-grow:1;
- > view{
- display: flex;
- align-items: baseline;
- justify-content: flex-end;
- }
- .cart-footer-left-price-text{
- font-size: 24rpx;
- color: #333333;
- }
- .cart-footer-left-price-text1{
- font-size: 20rpx;
- color: #666;
- display: flex;
- align-items: center;
- margin-top: 2px;
- text{
- margin-right: 5px;
- }
- }
- .cart-footer-left-price-num{
- display: flex;
- align-items: baseline;
- font-size: 24rpx;
- color: #d81e06;
- text{
- font-size: 32rpx;
- }
- }
- }
- .red{
- color: #d81e06;
- }
- .cart-head-btton{
- height: 100%;
- font-size: 28rpx;
- color: #333333;
- }
- .cart-footer-right-button{
- display: flex;
- align-items: center;
- margin-left: 20rpx;
- text-align: right;
- .btns{
- border-radius: 100rpx;
- height: 36px;
- }
- }
- }
- }
- }
- }
- </style>
|