chooseAddress.vue 4.6 KB
<template>
	<view class="chooseHomeWrap">
		<view class="chooseHome">
			<view class="addressWrap">
				<view class="address">
					{{chAddressName}}
				</view>
			</view>
			<view class="changeAddress">
				<view class="addressItem" @click="changeChAddress(0)" v-if="chAddress.length >= 0">
					省级
					<view class="addressLine" v-if="chAddressIndex == 0"></view>
				</view>
				<view class="addressItem" @click="changeChAddress(1)" v-if="chAddress.length >= 1">

					<view class="addressLine" v-if="chAddressIndex == 1"></view>
				</view>
				<view class="addressItem" @click="changeChAddress(2)" v-if="chAddress.length >= 2">
					区县
					<view class="addressLine" v-if="chAddressIndex == 2"></view>
				</view>
				<view class="addressItem" @click="changeChAddress(3)" v-if="chAddress.length >= 3">

					<view class="addressLine" v-if="chAddressIndex == 3"></view>
				</view>
				<view class="addressItem" @click="changeChAddress(4)" v-if="chAddress.length >= 4">

					<view class="addressLine" v-if="chAddressIndex == 4"></view>
				</view>
			</view>
			<scroll-view scroll-y class="addressList">
				<view class="addressName" v-for="(item,index) in addressList" :key="index" @click="clickAddress(index)">
					{{item.Name}}
				</view>
			</scroll-view>
			<view class="bottomBtn">
				<view class="btn" @click="$emit('close')">
					取消
				</view>
				<view class="btn active" @click="confirm">
					确定
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"chooseAddress",
		data() {
			return {
				chAddressIndex:0,
				chAddress:[],
				chAddressName:'',
				addressList:[]
			};
		},
		mounted() {
			this.$request('/common/area').then((res)=>{
				this.addressList = res.data
			})
		},
		methods:{
			changeChAddress(index){
				let area_id = ''
				this.chAddressIndex = index
				if(index > 0){
					area_id = this.chAddress[index-1].id
					this.$request('/common/area',{area_id:area_id}).then((res)=>{
						this.addressList = res.data
					})
				}else{
					this.$request('/common/area').then((res)=>{
						this.addressList = res.data
					})
				}
				
				
			},
			clickAddress(index){
				if(this.chAddressIndex < this.chAddress.length - 1){
					for(let i = this.chAddress.length - 1; i > this.chAddressIndex; i--){
						this.chAddress.splice(i,1)
					}
				}
				let ID = this.addressList[index].ID
				this.chAddress[this.chAddressIndex] = {
					id:ID,
					name:this.addressList[index].Name,
				}
				this.chAddressName = this.chAddress.map((obj)=>{return obj.name}).join("");
				if(this.chAddressIndex < 4){
					this.addressList = []
					this.chAddressIndex++
					this.$request('/common/area',{area_id:ID}).then((res)=>{
						this.addressList = res.data
					})
				}
			},
			confirm(){
				if(this.chAddress.length < 3){
					uni.showToast({
						title:'请选择至区县',
						icon:'none'
					})
					return
				}
				this.$emit('confirm',{address:this.chAddressName,area_id:this.chAddress[this.chAddress.length-1].id})
			}
		}
	}
</script>

<style lang="scss">
	// 选择家乡弹框区域
	.chooseHomeWrap{
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		background: rgba(0,0,0,0.5);
		display: flex;
		align-items: flex-end;
		.chooseHome{
			width: 750rpx;
			height: 1200rpx;
			background: #fff;
			border-radius: 30rpx 30rpx 0 0;
			padding: 32rpx 0;
			.addressWrap{
				padding: 0 32rpx;
				.address{
					background: #f3f3f3;
					padding: 32rpx;
					height: 32rpx;
					border-radius: 30rpx;
					line-height: 32rpx;
					color: #9c9c9c;
					font-size: 28rpx;
				}
			}
			.changeAddress{
				height: 90rpx;
				border-bottom: 2rpx solid #ededed;
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #585858;
				.addressItem{
					width: 150rpx;
					line-height: 90rpx;
					text-align: center;
					position: relative;
					.addressLine{
						position: absolute;
						width: 36rpx;
						height: 8rpx;
						background: #35655f;
						border-radius: 18rpx;
						bottom: 10rpx;
						left: 57rpx;
					}
				}
			}
			.addressList{
				padding: 0 32rpx;
				height: calc(1200rpx - 64rpx - 92rpx - 100rpx);
				.addressName{
					height: 60rpx;
					line-height: 60rpx;
					font-size: 28rpx;
				}
			}
			.bottomBtn{
				height: 100rpx;
				padding: 0 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.btn{
					height: 80rpx;
					width: 333rpx;
					background: #9c9c9c;
					text-align: center;
					line-height: 80rpx;
					border-radius: 10rpx;
					color: #fff;
					font-size: 28rpx;
				}
				.btn.active{
					background: #35655f;
				}
			}
		}
	}
</style>