diff --git a/common/util.js b/common/util.js index 3860b02..cb61f45 100644 --- a/common/util.js +++ b/common/util.js @@ -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(',') @@ -62,12 +64,12 @@ const isNull = (p) => { const isMobile = (phoneNumber) => { const regex = /^1[3-9]\d{9}$/; return regex.test(phoneNumber); -} -// 分享 -const shareInfo = { - title:"蟹太太", - imageUrl:"/static/share.jpg" -} +} +// 分享 +const shareInfo = { + title: "蟹太太", + imageUrl: "/static/share.jpg" +} // 计算距离 const getDistance = (la1, lo1, la2, lo2) => { // 当前的纬度,当前的经度,接口拿到的纬度,接口拿到的经度 @@ -91,11 +93,11 @@ const getDistance = (la1, lo1, la2, lo2) => { // 当前的纬度,当前的经 return distance.toFixed(2) + '公里' } } - + // 打开导航 -const toMapAPP = (lat,lng,name) => { - let that = this - const lat1 = parseFloat(lat) +const toMapAPP = (lat, lng, name) => { + let that = this + const lat1 = parseFloat(lat) const lng1 = parseFloat(lng) // 获取用户是否开启 授权获取当前的地理位置、速度的权限。 uni.getSetting({ @@ -150,8 +152,8 @@ const toMapAPP = (lat,lng,name) => { console.log('有授权'); // 有权限则直接获取 uni.openLocation({ - latitude: lat1, //到达的纬度 - longitude: lng1, //到达的经度 + latitude: lat1, //到达的纬度 + longitude: lng1, //到达的经度 name: name, //到达的名字 scale: 12, success() { @@ -162,18 +164,52 @@ const toMapAPP = (lat,lng,name) => { } }) -} -// 检查是否空对象 -const isEmptyObject = (obj) => { - // 检查传入参数是否为一个对象 - if (typeof obj !== 'object' || obj === null) { - throw new Error('Input must be an object'); - } +} +// 检查是否空对象 +const isEmptyObject = (obj) => { + // 检查传入参数是否为一个对象 + if (typeof obj !== 'object' || obj === null) { + 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 { base64ToFile, toast, @@ -182,8 +218,9 @@ export { isOnlyChinese, isNull, isMobile, - getDistance, - toMapAPP, - shareInfo, - isEmptyObject + getDistance, + toMapAPP, + shareInfo, + isEmptyObject, + parseAddress } \ No newline at end of file diff --git a/packages/my/addaddress.vue b/packages/my/addaddress.vue index 1464a60..2b9701c 100644 --- a/packages/my/addaddress.vue +++ b/packages/my/addaddress.vue @@ -19,10 +19,10 @@ --> - - - - 定位 + + + + 定位 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) @@ -246,10 +268,11 @@ margin-bottom: 20rpx; font-size: 32rpx; display: flex; - align-items: center; - margin-top:35rpx; - >view{ - margin:0 20rpx; + align-items: center; + margin-top: 35rpx; + + >view { + margin: 0 20rpx; } } diff --git a/packages/order/order.vue b/packages/order/order.vue index 566c23a..aa64016 100644 --- a/packages/order/order.vue +++ b/packages/order/order.vue @@ -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'; @@ -476,7 +477,8 @@ }, changeArea(e) { // return - if (e) { + 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) { @@ -515,7 +507,29 @@ success(res) { console.log("res", res) // _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) { console.log("fail", res)