省市区匹配

main
lion 1 year ago
parent ebdec37b81
commit db906bf4ba

@ -3,7 +3,9 @@
*
*/
import moment from 'moment';
import { lang } from 'moment';
import {
lang
} from 'moment';
const base64ToFile = (dataurl, filename = 'file') => {
let arr = dataurl.split(',')
@ -65,8 +67,8 @@ const isMobile = (phoneNumber) => {
}
// 分享
const shareInfo = {
title:"蟹太太",
imageUrl:"/static/share.jpg"
title: "蟹太太",
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
const lat1 = parseFloat(lat)
const lng1 = parseFloat(lng)
@ -174,6 +176,40 @@ const isEmptyObject = (obj) => {
return Object.keys(obj).length === 0;
}
const parseAddress = (address) => {
// 正则表达式匹配省市区和直辖市
// 直辖市
const regex = /^(.*?省|.*?市|.*?自治区|.*?特别行政区)(.*?市|.*?自治州|.*?盟|.*?县|.*?区|.*?旗)?/;
// 省市区
const regex1 = /^(.*?省|.*?市|.*?自治区|.*?特别行政区)(.*?市|.*?自治州|.*?盟)(.*?区|.*?县|.*?市|.*?旗)/;
// 初始化返回数组
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 {
base64ToFile,
toast,
@ -185,5 +221,6 @@ export {
getDistance,
toMapAPP,
shareInfo,
isEmptyObject
isEmptyObject,
parseAddress
}

@ -54,7 +54,8 @@
import {
toast,
isMobile,
isNull
isNull,
parseAddress
} from '@/common/util.js'
import auiPicker from '@/components/aui-picker.vue';
export default {
@ -193,6 +194,27 @@
console.log("res", res)
// _this.form.area = res.address
_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) {
console.log("fail", res)
@ -247,9 +269,10 @@
font-size: 32rpx;
display: flex;
align-items: center;
margin-top:35rpx;
>view{
margin:0 20rpx;
margin-top: 35rpx;
>view {
margin: 0 20rpx;
}
}

@ -212,7 +212,8 @@
import {
toast,
isMobile,
isNull
isNull,
parseAddress
} from '@/common/util.js'
import solarLunar from '@/node_modules/solarlunar';
import auiPicker from '@/components/aui-picker.vue';
@ -477,6 +478,7 @@
changeArea(e) {
// return
if (e) {
console.log("e",e)
let data = e.data
let _this = this
_this.form.area = ''
@ -486,16 +488,6 @@
this.form.province_id = data[0] ? data[0].id : ''
this.form.city_id = data[1] ? data[1].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) {
@ -516,6 +508,28 @@
console.log("res", res)
// _this.form.area = res.address
_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) {
console.log("fail", res)

Loading…
Cancel
Save