文件
yjyf/yjyf-app/components/RegionPickerDrawer.vue

446 行
10 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="drawer-mask" v-if="visible" @tap="close">
<view class="drawer-sheet" @tap.stop>
<view class="drawer-head">
<text class="dh-title">选择所在省市区</text>
<view class="dh-right">
<view class="mode-switch clickable-hover" @tap="manualMode = !manualMode">
<text class="ms-txt">{{ manualMode ? '返回列表选择' : '手动输入区县' }}</text>
</view>
<view class="dh-close clickable-hover" @tap="close">
<image class="dh-close-img" src="/static/icons/close.svg" mode="aspectFit" />
</view>
</view>
</view>
<!-- 手动编辑模式:允许自由输入全国任意省/市/区县 -->
<view class="manual-body" v-if="manualMode">
<view class="m-field">
<text class="m-label">省份 / 直辖市</text>
<input class="m-input" v-model="selProvince" placeholder="请输入省份或直辖市" />
</view>
<view class="m-field">
<text class="m-label">城市 / 州盟</text>
<input class="m-input" v-model="selCity" placeholder="请输入城市或州盟" />
</view>
<view class="m-field">
<text class="m-label">区 / 县 / 县级市</text>
<input class="m-input" v-model="selDistrict" placeholder="请输入区、县或县级市" />
</view>
<view class="confirm-btn clickable-hover" @tap="confirmSelection">
<text class="cb-txt">确认地区</text>
</view>
</view>
<!-- 三级级联选择模式 -->
<template v-else>
<view class="step-tabs">
<view class="s-tab" :class="{ active: activeTab === 0 }" @tap="activeTab = 0">
<text class="st-txt">{{ selProvince || '请选择省' }}</text>
</view>
<view class="s-tab" :class="{ active: activeTab === 1 }" v-if="selProvince" @tap="activeTab = 1">
<text class="st-txt">{{ selCity || '请选择市' }}</text>
</view>
<view class="s-tab" :class="{ active: activeTab === 2 }" v-if="selCity" @tap="activeTab = 2">
<text class="st-txt">{{ selDistrict || '请选择区县' }}</text>
</view>
</view>
<scroll-view scroll-y class="region-scroll">
<view class="state-box" v-if="loadingAreas">
<text class="state-txt">正在加载行政区划...</text>
</view>
<view class="state-box" v-else-if="loadError">
<text class="state-txt">{{ loadError }}</text>
<view class="retry-btn clickable-hover" @tap="loadAreaTree(true)">
<text class="retry-txt">重新加载</text>
</view>
</view>
<view class="region-list" v-else-if="activeTab === 0">
<view
v-for="p in regionTree"
:key="p.name"
class="r-item clickable-hover"
:class="{ selected: selProvince === p.name }"
@tap="pickProvince(p)"
>
<text class="ri-txt">{{ p.name }}</text>
<image v-if="selProvince === p.name" class="ri-check" src="/static/icons/check-orange.svg" mode="aspectFit" />
</view>
</view>
<view class="region-list" v-else-if="activeTab === 1">
<view
v-for="c in currentCities"
:key="c.name"
class="r-item clickable-hover"
:class="{ selected: selCity === c.name }"
@tap="pickCity(c)"
>
<text class="ri-txt">{{ c.name }}</text>
<image v-if="selCity === c.name" class="ri-check" src="/static/icons/check-orange.svg" mode="aspectFit" />
</view>
</view>
<view class="region-list" v-else>
<view
v-for="d in currentDistricts"
:key="d"
class="r-item clickable-hover"
:class="{ selected: selDistrict === d }"
@tap="pickDistrict(d)"
>
<text class="ri-txt">{{ d }}</text>
<image v-if="selDistrict === d" class="ri-check" src="/static/icons/check-orange.svg" mode="aspectFit" />
</view>
</view>
</scroll-view>
</template>
</view>
</view>
</template>
<script setup>
import { ref, computed, watch } from 'vue';
import api from '@/utils/api.js';
const props = defineProps({
visible: { type: Boolean, default: false },
province: { type: String, default: '' },
city: { type: String, default: '' },
district: { type: String, default: '' }
});
const emit = defineEmits(['update:visible', 'confirm']);
let cachedRegionTree = null;
const activeTab = ref(0);
const manualMode = ref(false);
const selProvince = ref('');
const selCity = ref('');
const selDistrict = ref('');
const regionTree = ref(cachedRegionTree ? cachedRegionTree : []);
const loadingAreas = ref(false);
const loadError = ref('');
const currentCities = computed(() => {
const hit = regionTree.value.find(p => p.name === selProvince.value);
return hit && Array.isArray(hit.cities) ? hit.cities : [];
});
const currentDistricts = computed(() => {
const hitCity = currentCities.value.find(c => c.name === selCity.value);
return hitCity && Array.isArray(hitCity.districts) ? hitCity.districts : [];
});
function syncActiveTab() {
const hasP = selProvince.value && regionTree.value.some(p => p.name === selProvince.value);
if (!selProvince.value || !hasP) {
activeTab.value = 0;
} else if (!selCity.value || !currentCities.value.some(c => c.name === selCity.value)) {
activeTab.value = 1;
} else {
activeTab.value = 2;
}
}
async function loadAreaTree(force = false) {
if (!force && cachedRegionTree && cachedRegionTree.length > 0) {
regionTree.value = cachedRegionTree;
loadError.value = '';
syncActiveTab();
return;
}
if (loadingAreas.value) return;
loadingAreas.value = true;
loadError.value = '';
try {
const res = await api.areas({ mode: 'tree' });
const tree = Array.isArray(res && res.tree)
? res.tree
: (Array.isArray(res && res.list) ? res.list : (Array.isArray(res) ? res : []));
if (tree.length === 0) {
regionTree.value = [];
loadError.value = '暂无行政区划数据,请稍后重试';
return;
}
cachedRegionTree = tree;
regionTree.value = tree;
syncActiveTab();
} catch (e) {
regionTree.value = [];
loadError.value = (e && (e.message || e.msg)) || '行政区划加载失败,请重试';
} finally {
loadingAreas.value = false;
}
}
watch(
() => props.visible,
(val) => {
if (val) {
selProvince.value = props.province || '';
selCity.value = props.city || '';
selDistrict.value = props.district || '';
manualMode.value = false;
syncActiveTab();
if (regionTree.value.length === 0) {
loadAreaTree(false);
}
}
},
{ immediate: true }
);
function close() {
emit('update:visible', false);
}
function pickProvince(p) {
selProvince.value = p.name;
const firstCity = p.cities && p.cities[0] ? p.cities[0] : null;
selCity.value = firstCity ? firstCity.name : '';
selDistrict.value = (firstCity && firstCity.districts && firstCity.districts[0]) ? firstCity.districts[0] : '';
activeTab.value = 1;
}
function pickCity(c) {
selCity.value = c.name;
selDistrict.value = (c.districts && c.districts[0]) ? c.districts[0] : '';
activeTab.value = 2;
}
function pickDistrict(d) {
selDistrict.value = d;
confirmSelection();
}
function confirmSelection() {
const p = (selProvince.value || '').trim();
const c = (selCity.value || '').trim();
const d = (selDistrict.value || '').trim();
if (!p || !c || !d) {
uni.showToast({ title: '请完整选择或填写省市区', icon: 'none' });
return;
}
emit('confirm', { province: p, city: c, district: d });
close();
}
</script>
<style scoped>
.drawer-mask {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.55);
z-index: 1050;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.drawer-sheet {
background: #FFFFFF;
border-top-left-radius: 24rpx;
border-top-right-radius: 24rpx;
padding: 30rpx 28rpx calc(32rpx + env(safe-area-inset-bottom));
max-height: 76vh;
display: flex;
flex-direction: column;
box-sizing: border-box;
}
.drawer-head {
display: flex;
align-items: center;
justify-content: space-between;
padding-bottom: 20rpx;
border-bottom: 1rpx solid #F0F0F2;
}
.dh-title {
font-size: 32rpx;
font-weight: 700;
color: #1F1F1F;
}
.dh-right {
display: flex;
align-items: center;
gap: 16rpx;
}
.mode-switch {
height: 50rpx;
padding: 0 18rpx;
border-radius: 25rpx;
background: #FFF7F0;
display: inline-flex;
align-items: center;
justify-content: center;
}
.ms-txt {
font-size: 22rpx;
color: #F0710D;
font-weight: 600;
}
.dh-close {
width: 48rpx;
height: 48rpx;
display: flex;
align-items: center;
justify-content: flex-end;
}
.dh-close-img {
width: 28rpx;
height: 28rpx;
opacity: 0.6;
}
.step-tabs {
display: flex;
align-items: center;
gap: 36rpx;
padding: 20rpx 4rpx;
border-bottom: 1rpx solid #F0F0F2;
}
.s-tab {
position: relative;
padding-bottom: 12rpx;
}
.st-txt {
font-size: 28rpx;
color: #444444;
}
.s-tab.active .st-txt {
color: #F0710D;
font-weight: 700;
}
.s-tab.active::after {
content: '';
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 4rpx;
border-radius: 2rpx;
background: #F0710D;
}
.region-scroll {
height: 52vh;
}
.region-list {
padding: 10rpx 0;
}
.r-item {
height: 84rpx;
padding: 0 8rpx;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1rpx solid #F8F8FA;
}
.ri-txt {
font-size: 28rpx;
color: #1F1F1F;
}
.r-item.selected .ri-txt {
color: #F0710D;
font-weight: 600;
}
.ri-check {
width: 28rpx;
height: 28rpx;
}
.manual-body {
padding: 24rpx 0 8rpx;
display: flex;
flex-direction: column;
gap: 20rpx;
}
.m-field {
display: flex;
align-items: center;
min-height: 88rpx;
border-bottom: 1rpx solid #F0F0F2;
}
.m-label {
width: 210rpx;
font-size: 28rpx;
color: #444444;
flex-shrink: 0;
}
.m-input {
flex: 1;
height: 80rpx;
font-size: 28rpx;
color: #1F1F1F;
}
.confirm-btn {
margin-top: 20rpx;
height: 84rpx;
border-radius: 42rpx;
background: linear-gradient(135deg, #FFA93F 0%, #F0710D 100%);
display: flex;
align-items: center;
justify-content: center;
}
.cb-txt {
font-size: 30rpx;
font-weight: 600;
color: #FFFFFF;
}
.state-box {
padding: 120rpx 32rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 24rpx;
}
.state-txt {
font-size: 26rpx;
color: #888888;
text-align: center;
}
.retry-btn {
padding: 14rpx 36rpx;
border-radius: 32rpx;
border: 1rpx solid #F0710D;
background: #FFF7F0;
}
.retry-txt {
font-size: 26rpx;
color: #F0710D;
font-weight: 500;
}
.clickable-hover:active {
opacity: 0.85;
}
</style>