省市区匹配

main
lion 1 year ago
parent ebdec37b81
commit db906bf4ba

@ -3,7 +3,9 @@
* *
*/ */
import moment from 'moment'; import moment from 'moment';
import { lang } from 'moment'; import {
lang
} from 'moment';
const base64ToFile = (dataurl, filename = 'file') => { const base64ToFile = (dataurl, filename = 'file') => {
let arr = dataurl.split(',') let arr = dataurl.split(',')
@ -65,8 +67,8 @@ const isMobile = (phoneNumber) => {
} }
// 分享 // 分享
const shareInfo = { const shareInfo = {
title:"蟹太太", title: "蟹太太",
imageUrl:"/static/share.jpg" imageUrl: "/static/share.jpg"
} }
// 计算距离 // 计算距离
@ -93,7 +95,7 @@ const getDistance = (la1, lo1, la2, lo2) => { // 当前的纬度,当前的经
} }
// 打开导航 // 打开导航
const toMapAPP = (lat,lng,name) => { const toMapAPP = (lat, lng, name) => {
let that = this let that = this
const lat1 = parseFloat(lat) const lat1 = parseFloat(lat)
const lng1 = parseFloat(lng) const lng1 = parseFloat(lng)
@ -165,15 +167,49 @@ const toMapAPP = (lat,lng,name) => {
} }
// 检查是否空对象 // 检查是否空对象
const isEmptyObject = (obj) => { const isEmptyObject = (obj) => {
// 检查传入参数是否为一个对象 // 检查传入参数是否为一个对象
if (typeof obj !== 'object' || obj === null) { if (typeof obj !== 'object' || obj === null) {
throw new Error('Input must be an object'); throw new Error('Input must be an object');
} }
// 使用Object.keys()方法获取对象的属性键数组并检查其长度是否为0
return Object.keys(obj).length === 0;
}
const parseAddress = (address) => {
// 正则表达式匹配省市区和直辖市
// 直辖市
const regex = /^(.*?省|.*?市|.*?自治区|.*?特别行政区)(.*?市|.*?自治州|.*?盟|.*?县|.*?区|.*?旗)?/;
// 省市区
const regex1 = /^(.*?省|.*?市|.*?自治区|.*?特别行政区)(.*?市|.*?自治州|.*?盟)(.*?区|.*?县|.*?市|.*?旗)/;
// 使用Object.keys()方法获取对象的属性键数组并检查其长度是否为0
return Object.keys(obj).length === 0; // 初始化返回数组
let regionArray = [];
let match = null
// 检查是否是直辖市
const directCities = ['北京市', '天津市', '上海市', '重庆市'];
if(directCities.some(city => address.includes(city))){
match = address.match(regex);
}else{
match = address.match(regex1);
}
console.log("address",address,match)
if (match) {
if (directCities.includes(match[1])) {
// 直辖市没有省这一级,直接将市名和区名放入数组
regionArray = [match[1], '市辖区', match[2]];
} else {
// 非直辖市,包含省市区
regionArray = [match[1], match[2], match[3]];
}
}
return regionArray;
} }
export { export {
base64ToFile, base64ToFile,
toast, toast,
@ -185,5 +221,6 @@ export {
getDistance, getDistance,
toMapAPP, toMapAPP,
shareInfo, shareInfo,
isEmptyObject isEmptyObject,
parseAddress
} }

@ -54,7 +54,8 @@
import { import {
toast, toast,
isMobile, isMobile,
isNull isNull,
parseAddress
} from '@/common/util.js' } from '@/common/util.js'
import auiPicker from '@/components/aui-picker.vue'; import auiPicker from '@/components/aui-picker.vue';
export default { export default {
@ -193,6 +194,27 @@
console.log("res", res) console.log("res", res)
// _this.form.area = res.address // _this.form.area = res.address
_this.form.address = res.name _this.form.address = res.name
//
let pca = parseAddress(res.address)
if (pca.length > 0) {
_this.form.area = pca.join('')
_this.$u.api.matchRegion({
province: pca[0],
city: pca[1],
district: pca[2]
}).then(res => {
_this.form.province_id = res.province_id ? res
.province_id.id : ''
_this.form.city_id = res.city_id ? res.city_id.id : ''
_this.form.district_id = res.district_id ? res
.district_id.id : ''
})
} else {
uni.showToast({
title: '未获取到地址,重新获取',
icon: 'none'
})
}
}, },
fail(res) { fail(res) {
console.log("fail", res) console.log("fail", res)
@ -247,9 +269,10 @@
font-size: 32rpx; font-size: 32rpx;
display: flex; display: flex;
align-items: center; align-items: center;
margin-top:35rpx; margin-top: 35rpx;
>view{
margin:0 20rpx; >view {
margin: 0 20rpx;
} }
} }

@ -212,7 +212,8 @@
import { import {
toast, toast,
isMobile, isMobile,
isNull isNull,
parseAddress
} from '@/common/util.js' } from '@/common/util.js'
import solarLunar from '@/node_modules/solarlunar'; import solarLunar from '@/node_modules/solarlunar';
import auiPicker from '@/components/aui-picker.vue'; import auiPicker from '@/components/aui-picker.vue';
@ -477,6 +478,7 @@
changeArea(e) { changeArea(e) {
// return // return
if (e) { if (e) {
console.log("e",e)
let data = e.data let data = e.data
let _this = this let _this = this
_this.form.area = '' _this.form.area = ''
@ -486,16 +488,6 @@
this.form.province_id = data[0] ? data[0].id : '' this.form.province_id = data[0] ? data[0].id : ''
this.form.city_id = data[1] ? data[1].id : '' this.form.city_id = data[1] ? data[1].id : ''
this.form.district_id = data[2] ? data[2].id : '' this.form.district_id = data[2] ? data[2].id : ''
// this.form.area = e.province.label + e.city.label + e.area.label
// this.$u.api.matchRegion({
// province: e.province.label,
// city: e.city.label,
// district: e.area.label
// }).then(res => {
// this.form.province_id = res.province_id ? res.province_id.id : ''
// this.form.city_id = res.city_id ? res.city_id.id : ''
// this.form.district_id = res.district_id ? res.district_id.id : ''
// })
} }
}, },
changeIsMySelf(e) { changeIsMySelf(e) {
@ -516,6 +508,28 @@
console.log("res", res) console.log("res", res)
// _this.form.area = res.address // _this.form.area = res.address
_this.form.address = res.name _this.form.address = res.name
//
let pca = parseAddress(res.address)
if(pca.length>0){
_this.form.area = pca.join('')
_this.$u.api.matchRegion({
province: pca[0],
city: pca[1],
district: pca[2]
}).then(res => {
_this.form.province_id = res.province_id ? res.province_id.id : ''
_this.form.city_id = res.city_id ? res.city_id.id : ''
_this.form.district_id = res.district_id ? res.district_id.id : ''
})
}else{
uni.showToast({
title:'未获取到地址,重新获取',
icon:'none'
})
}
}, },
fail(res) { fail(res) {
console.log("fail", res) console.log("fail", res)

Loading…
Cancel
Save