uniapp 学习笔记三十二 默认地址和配送地址联动选地址

uniapp 学习笔记三十二 默认地址和配送地址联动选地址

 order.vue

<template>
    <view>
        <view @click="handleAddress" class="padding flex align-center justify-between">
            <view class="">
                {{orderAddress.username}},{{orderAddress.phone}}
                <view class="">
                    {{orderAddress.city}}
                    {{orderAddress.region}}
                    {{orderAddress.detail}}
                </view>
            </view>
            <text class="cuIcon-right"></text>
        </view>
        <view class="padding">
            配送时间
        </view>
        <view class="padding flex justify-between">
            <view class="">
                选择配送日期
            </view>
            <view class="">
                选择配送时间
            </view>
        </view>
        <view class="flex padding" v-for="(item,index) in 1">
        	<view class="flex align-center">
        		<!-- <text :class="['iconfont','icon-youxiajiaogouxuan','margin-right',{'yellow':item.isCheck}]"></text> -->
        		<image class="poster margin-right" :src="item.img" mode=""></image>
        	</view>
        	<view class="flex justify-between info">
        		<view class="">
        			<!-- {{item.name}} -->蛋糕
        			<view class="margin-tb-xs">	Fraction	</view>
        			<!-- Y{{item.list[item.idx].price}} -->
        		</view>
        		<view class="flex flex-direction align-end">
        			<view class="edit margin-bottom-xs">
        				<text class="iconfont icon-bianjishuru"></text>
        			</view>
                    <!-- {{item.list[item.idx].spec}} -->
                    X 
                    <!-- {{item.num}} -->
        		</view>
        	</view>
        </view>
        <view class="fixed padding flex justify-between align-center">
            总计
            <view class="">
                199
            </view>
            <button class="cu-btn bg-brown">立即支付</button>
        </view>
    </view>
</template>

<script>
    import {mapGetters} from 'vuex'
    export default {
        data() {
            return {
                
            };
        },
        computed:{
            ...mapGetters({
                'orderAddress':'address/orderAddress'
            })
        },
        methods:{
            handleAddress(){
                uni.navigateTo({
                    url:'../address/address'
                })
            }
        }
    }
</script>

<style lang="scss">
.poster{
    width: 180upx;
    height: 180upx;
    background-color: #d8d8d8;
}
.info{
    width: 60%;
    .edit{
        width: 80upx;
        height: 80upx;
        text-align: center;
        line-height: 80upx;
        background-color: #e6e6e6;
        border-radius: 50%;
    }
}
.flex.align-end{
    min-width: 210upx;
}
.icon-youxiajiaogouxuan{
    color: #e7e7e7;
}
.cu-btn.lg{
    width: 50%;
}
.fixed {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    box-shadow: 0 0 10upx 2upx rgba(0, 0, 0, 0.2);
}
</style>

address.vue

<template>
    <view>
        <view 
            v-for="(item,index) in addressList" 
            class="margin-sm u-border padding"
            :class="{'default':item.isdefault}"
        >
            <view class="default-cont">默</view>
            <view class="flex justify-between">
                <view class="flex align-center">
                    <text 
                        class="iconfont icon-checkbox-xuanzhong margin-right"
                        :class="{'color-9898':checkedIdx==index||item.isdefault}"
                        @click="handleCheckAddress(index)"
                    ></text>
                    <view class="">
                       {{item.username}},{{item.phone}}
                        <view class="">
                            {{item.city}}
                            {{item.region}}
                            {{item.detail}}
                        </view>
                    </view>
                </view>
                <view class="edit margin-bottom-sm">
                    <text class="iconfont icon-bianjishuru"></text>
                </view>
            </view>
            <view class="flex justify-around align-center margin-top">
                <view v-if="item.isdefault" class="">
                    默认地址
                </view>
                <view v-else class="" @click="handleDefault(index)">
                    设为默认
                </view>
                <u-line length="15" direction="col"></u-line>
                <view class="">
                    删除地址
                </view>
            </view>
        </view>
        <view class="text-center">
            <button class="cu-btn bg-brown">新增地址</button>
        </view>
    </view>
</template>

<script>
    import {mapState,mapMutations} from 'vuex'
    export default {
        data() {
            return {
                
            };
        },
        computed: {
            ...mapState({
                addressList:state=>state.address.addressList, 
                checkedIdx:state=>state.address.checkedIdx 
            })
        },
        methods:{
            ...mapMutations({
                'handleDefault':'address/addressDefaultMut',
                'handleCheckAddress':'address/addressCheckMut'
            })
        }
    }
</script>

<style lang="scss">
.edit{
    width: 80upx;
    height: 80upx;
    text-align: center;
    line-height: 80upx;
    background-color: #e6e6e6;
    border-radius: 50%;
}
.cu-btn.lg{
    width: 50%;
}
.fixed {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    box-shadow: 0 0 10upx 2upx rgba(0, 0, 0, 0.2);
}
.icon-checkbox-xuanzhong{
	width: 50upx;
	height: 50upx;
    font-size: 50upx;
	text-align: center;
	line-height: 50upx;
    color: #e6e6e6;
	background-color: #e6e6e6;
	border-radius: 20%;
}
.default{
    position: relative;
    overflow: hidden;
    .default-cont{
        padding: 20upx 10upx 10upx;
        width: 100upx;
        background-color: #fae456;
        font-size: 12upx;
        text-align: center;
        position: absolute;
        top: -20upx;
        right: -40upx;
        transform: rotate(45deg);
        display: block;
    }
}
.default-cont{
    display: none;
}
</style>

address.js

export default{
    namespaced:true,
    state(){
        return{
            checkedIdx:-1, //记录用户勾选的地址
            addressList:[
                {
                    username:'曹国舅',
                    phone:'13010101010',
                    city:'北京',
                    region:'朝阳区',
                    detail:'朝阳路周家井',
                    isdefault:true
                },{
                    username:'何仙姑',
                    phone:'13010101010',
                    city:'北京',
                    region:'海淀区',
                    detail:'中关村二街',
                    isdefault:false
                },{
                    username:'吕洞宾',
                    phone:'13010101010',
                    city:'北京',
                    region:'门头沟',
                    detail:'黑山大街',
                    isdefault:false
                }
            ]
        }
    },
    getters:{
        orderAddress(state){ // 订单中的地址
            // 根据 checkedIdx 与 isdefault 共同得到一个地址对象
            let {checkedIdx,addressList} = state
            if (checkedIdx!=-1) {
                return addressList[checkedIdx]
            }
            let len = addressList.length
            for(let i=0;i<len;i++){
                if(addressList[i].isdefault){
                    return addressList[i]
                }
            }
        }
    },
    mutations:{
        addressDefaultMut(state,idx){ //设为默认
            state.addressList.forEach((item,i)=>{
                if(i==idx){
                    item.isdefault = true
                }else{
                    item.isdefault = false
                }
            })
        },
        addressCheckMut(state,idx){
            //选取地址
            state.checkedIdx = idx
            //点选返回
            uni.navigateBack({
                delta:1
            })
        }
    }
}

 

uniapp中,可以使用Vuex来管理地址信息。在一个名为address.js的文件中,定义了一个address的模块,该模块具有namespaced属性,并且包含了state、getters和mutations等属性。state中包含了一个addressList数组,其中存储了用户的地址信息,包括用户名、电话、城市、区域、详细地址和是否默认地址等。getters中定义了一个orderAddress方法,用于获取订单中的地址,根据checkedIdx和isdefault共同确定一个地址对象。mutations中定义了addressDefaultMut和addressCheckMut方法,用于设置默认地址地址。 在页面中,可以使用mapState和mapMutations来获取和修改地址信息。在data中定义了一个addressList数组,通过computed和mapState将addressList映射到页面中。通过methods和mapMutations将getLocalAddressList方法映射到页面中,用于获取本地地址信息。同时,还可以使用toAddSite方法新建地址和updateAddress方法修改地址。 通过上述方法,可以实现uniapp中的地址管理功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [uni-app微信公众号(4)——地址管理页面](https://blog.csdn.net/qq_40601005/article/details/121080638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [uniapp 学习笔记三十二 默认地址配送地址联动地址](https://blog.csdn.net/gixome/article/details/126813958)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值