| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <template>
- <!-- 编辑模式:输入框 -->
- <view v-if="mode === 'edit'" class="location-picker-wrapper">
- <view class="input-wrapper" @click="openPicker">
- <input
- class="location-input"
- :value="displayLabel"
- :placeholder="placeholder"
- readonly
- disabled
- />
- <view class="suffix-icons">
- <view
- v-if="innerValue && displayLabel"
- @click.stop="clearAddress"
- class="clear-btn"
- >
- <text class="clear-icon">×</text>
- </view>
- <text class="arrow-icon">></text>
- </view>
- </view>
-
- <!-- 自定义弹窗选择器 -->
- <view v-if="showPicker" class="picker-modal" @click="closePicker">
- <view class="picker-content" @click.stop>
- <view class="picker-header">
- <text class="picker-cancel" @click="closePicker">取消</text>
- <text class="picker-title">{{ popupTitle }}</text>
- <text class="picker-confirm" @click="confirmSelection">确定</text>
- </view>
- <view class="picker-body">
- <picker-view
- :value="pickerValue"
- @change="onPickerChange"
- class="picker-view"
- >
- <picker-view-column>
- <view class="picker-item" v-for="(item, index) in provinceList" :key="index">
- {{ item.text }}
- </view>
- </picker-view-column>
- <picker-view-column>
- <view class="picker-item" v-for="(item, index) in cityList" :key="index">
- {{ item.text }}
- </view>
- </picker-view-column>
- <picker-view-column>
- <view class="picker-item" v-for="(item, index) in districtList" :key="index">
- {{ item.text }}
- </view>
- </picker-view-column>
- </picker-view>
- </view>
- </view>
- </view>
- </view>
- <!-- 展示模式:纯文本 -->
- <text v-else class="location-text">
- {{ displayLabel || "无" }}
- </text>
- </template>
- <script setup>
- import { ref, watch, computed } from 'vue'
- import cityRows from '@/utils/data.json'
- // Props definition
- const props = defineProps({
- mode: {
- type: String,
- default: "edit" // edit / view
- },
- modelValue: {
- type: [String, Number],
- default: ""
- },
- label: {
- type: String,
- default: "所在地"
- },
- placeholder: {
- type: String,
- default: "请选择省市区"
- },
- popupTitle: {
- type: String,
- default: "请选择省市区"
- },
- required: {
- type: Boolean,
- default: false
- }
- })
- // Emits definition
- const emit = defineEmits(['update:modelValue', 'clear'])
- // Reactive data
- const innerValue = ref(props.modelValue)
- const displayLabel = ref('')
- const showPicker = ref(false)
- const pickerValue = ref([0, 0, 0]) // picker-view 的索引值
- const tempSelection = ref({ province: null, city: null, district: null }) // 临时选择
- // 省市区数据
- const localData = ref([])
- const provinceList = ref([])
- const cityList = ref([])
- const districtList = ref([])
- // 计算属性:根据当前选择更新城市和区县列表
- const updateCityList = () => {
- const provinceIndex = pickerValue.value[0]
- if (provinceList.value[provinceIndex] && provinceList.value[provinceIndex].children) {
- cityList.value = provinceList.value[provinceIndex].children
- } else {
- cityList.value = []
- }
- // 重置城市索引
- if (pickerValue.value[1] >= cityList.value.length) {
- pickerValue.value[1] = 0
- }
- }
- const updateDistrictList = () => {
- const cityIndex = pickerValue.value[1]
- if (cityList.value[cityIndex] && cityList.value[cityIndex].children) {
- districtList.value = cityList.value[cityIndex].children
- } else {
- districtList.value = []
- }
- // 重置区县索引
- if (pickerValue.value[2] >= districtList.value.length) {
- pickerValue.value[2] = 0
- }
- }
- // Watchers
- watch(() => props.modelValue, (newVal) => {
- console.log('props.modelValue 变化:', newVal)
- innerValue.value = newVal
- const label = getLocationLabel(newVal)
- displayLabel.value = label
- console.log('更新后的 displayLabel:', label)
- })
- watch(innerValue, (newVal) => {
- console.log('innerValue 变化:', newVal)
- emit("update:modelValue", newVal)
- })
- // Methods
- /** 打开选择器 */
- const openPicker = () => {
- console.log('打开地区选择器')
- showPicker.value = true
-
- // 如果有已选值,定位到对应位置
- if (innerValue.value) {
- locateSelection(innerValue.value)
- }
- }
- /** 关闭选择器 */
- const closePicker = () => {
- showPicker.value = false
- }
- /** picker-view 变化事件 */
- const onPickerChange = (e) => {
- const val = e.detail.value
- const oldValue = [...pickerValue.value]
- pickerValue.value = val
-
- // 如果省份改变,更新城市列表
- if (oldValue[0] !== val[0]) {
- updateCityList()
- updateDistrictList()
- }
- // 如果城市改变,更新区县列表
- else if (oldValue[1] !== val[1]) {
- updateDistrictList()
- }
-
- console.log('picker 值变化:', val)
- }
- /** 确认选择 */
- const confirmSelection = () => {
- const provinceIndex = pickerValue.value[0]
- const cityIndex = pickerValue.value[1]
- const districtIndex = pickerValue.value[2]
-
- const province = provinceList.value[provinceIndex]
- const city = cityList.value[cityIndex]
- const district = districtList.value[districtIndex]
-
- if (province && city && district) {
- // 更新值为区县的 code
- innerValue.value = district.value
-
- // 构建显示文本
- displayLabel.value = `${province.text} - ${city.text} - ${district.text}`
-
- console.log('确认选择:', displayLabel.value, '值:', innerValue.value)
-
- emit("update:modelValue", innerValue.value)
- }
-
- closePicker()
- }
- /** 清空地址 */
- const clearAddress = () => {
- console.log('清空地址')
- innerValue.value = ""
- displayLabel.value = ""
- pickerValue.value = [0, 0, 0]
- emit("update:modelValue", "")
- emit("clear")
- }
- /** 定位到已选择的值 */
- const locateSelection = (value) => {
- if (!value) return
-
- // 在数据中查找对应的省市区
- for (let i = 0; i < provinceList.value.length; i++) {
- const province = provinceList.value[i]
- if (province.children) {
- for (let j = 0; j < province.children.length; j++) {
- const city = province.children[j]
- if (city.children) {
- for (let k = 0; k < city.children.length; k++) {
- const district = city.children[k]
- if (district.value == value) {
- pickerValue.value = [i, j, k]
- updateCityList()
- updateDistrictList()
- console.log('定位到:', i, j, k)
- return
- }
- }
- }
- }
- }
- }
- }
- /** 回显文字(递归找路径) */
- const getLocationLabel = (value) => {
- console.log('getLocationLabel 被调用, value:', value)
- if (!value) return ""
-
- let label = ""
-
- const traverse = (nodes, currentPath = []) => {
- for (const node of nodes) {
- const newPath = [...currentPath, node.text]
-
- if (node.value == value) {
- label = newPath.join(' - ')
- console.log('找到匹配节点:', node, '路径:', label)
- return true
- }
-
- if (node.children && node.children.length > 0) {
- if (traverse(node.children, newPath)) {
- return true
- }
- }
- }
- return false
- }
-
- traverse(localData.value)
- console.log('最终返回的 label:', label)
- return label
- }
- /** 生成树数据 */
- const get_city_tree = () => {
- let res = []
- if (cityRows.length) {
- res = handleTree(cityRows)
- }
- return res
- }
- /** 递归组装树 */
- const handleTree = (data, parent_code = null) => {
- let res = []
- let keys = {
- id: "code",
- pid: "parent_code",
- children: "children",
- text: "name",
- value: "code"
- }
- for (let item of data) {
- if (parent_code === null) {
- // 顶级
- if (!item.hasOwnProperty(keys.pid) || item[keys.pid] == parent_code) {
- let node = {
- text: item[keys.text],
- value: item[keys.value],
- children: handleTree(data, item[keys.id])
- }
- res.push(node)
- }
- } else {
- // 子级
- if (item.hasOwnProperty(keys.pid) && item[keys.pid] == parent_code) {
- let node = {
- text: item[keys.text],
- value: item[keys.value],
- children: handleTree(data, item[keys.id])
- }
- res.push(node)
- }
- }
- }
- return res
- }
- // Initialize data
- localData.value = get_city_tree()
- provinceList.value = localData.value
- console.log('初始化 localData, 数据长度:', localData.value.length)
- // 初始化城市和区县列表
- if (provinceList.value.length > 0) {
- updateCityList()
- updateDistrictList()
- }
- // 初始化显示标签
- if (props.modelValue) {
- displayLabel.value = getLocationLabel(props.modelValue)
- console.log('初始化 displayLabel:', displayLabel.value)
- }
- </script>
- <style scoped>
- .location-picker-wrapper {
- width: 100%;
- }
- .input-wrapper {
- position: relative;
- display: flex;
- align-items: center;
- width: 100%;
- height: 44rpx;
- border-bottom: 1rpx solid #f0f0f0;
- padding: 12rpx 0;
- }
- .location-input {
- flex: 1;
- font-size: 28rpx;
- color: #333;
- height: 44rpx;
- line-height: 44rpx;
- }
- .location-input:disabled {
- background-color: transparent;
- color: #333;
- }
- .suffix-icons {
- display: flex;
- align-items: center;
- gap: 8rpx;
- }
- .clear-btn {
- display: flex;
- align-items: center;
- justify-content: center;
- width: 32rpx;
- height: 32rpx;
- }
- .clear-icon {
- font-size: 32rpx;
- color: #999;
- line-height: 1;
- }
- .arrow-icon {
- font-size: 24rpx;
- color: #999;
- }
- .location-text {
- font-size: 28rpx;
- color: #333;
- }
- /* 弹窗样式 */
- .picker-modal {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background-color: rgba(0, 0, 0, 0.5);
- z-index: 9999;
- display: flex;
- align-items: flex-end;
- }
- .picker-content {
- width: 100%;
- background-color: #fff;
- border-radius: 24rpx 24rpx 0 0;
- overflow: hidden;
- }
- .picker-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 24rpx 32rpx;
- border-bottom: 1rpx solid #f0f0f0;
- }
- .picker-cancel,
- .picker-confirm {
- font-size: 28rpx;
- color: #007aff;
- padding: 8rpx 16rpx;
- }
- .picker-title {
- font-size: 32rpx;
- font-weight: bold;
- color: #333;
- }
- .picker-body {
- height: 500rpx;
- }
- .picker-view {
- width: 100%;
- height: 100%;
- }
- .picker-item {
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 28rpx;
- color: #333;
- height: 80rpx;
- line-height: 80rpx;
- }
- </style>
|