日照项目的居民端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

218 lines
5.5 KiB

import { config } from "../config/config";
import { hexMD5 } from "./md5";
import qqMapWx from "./qqmap-wx-jssdk.js";
const qqMapSdk = new qqMapWx({ key: "XMLBZ-5TRWF-TNBJF-J3MDK-YTEZF-2VBI5" });
// 获取位置授权
function getAuth() {
return new Promise(reslove => {
wx.getSetting({
success(res) {
console.log(res.authSetting);
if (!res.authSetting["scope.userLocation"]) {
wx.showModal({
title: "请求授权当前位置",
content: "需要获取您的地理位置,请确认授权",
success(res) {
if (res.cancel) {
reslove({
data: "",
msg: "用户取消授权"
});
} else if (res.confirm) {
wx.openSetting({
success(dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
reslove({
data: "",
msg: "success"
});
} else {
reslove({
data: "",
msg: "用户未授权"
});
}
},
fail(err) {
reslove({
data: "",
msg: err.errMsg
});
}
});
}
},
fail(err) {
reslove({
data: "",
msg: err.errMsg
});
}
});
} else {
reslove({
data: "",
msg: "success"
});
}
},
fail(err) {
reslove({
data: "",
msg: err.errMsg
});
}
});
});
}
// 获取坐标所在位置数据
export function getLocalData(latitude: number, longitude: number) {
return new Promise(reslove => {
let sign = hexMD5(
"/ws/geocoder/v1?key=" +
config.qqMapKey +
"&location=" +
latitude +
"," +
longitude +
config.qqMapSKey
);
wx.request({
url:
"https://apis.map.qq.com/ws/geocoder/v1?key=" +
config.qqMapKey +
"&location=" +
latitude +
"," +
longitude +
"&sig=" +
sign,
method: "GET",
header: {
"Content-Type": "application/json; charset=UTF-8"
},
success(response) {
console.log(response);
const {
data: { result }
} = response;
reslove({
data: {
adcode: result.ad_info.adcode,
nation: result.address_component.nation,
province: result.address_component.province,
city: result.address_component.city,
district: result.address_component.district,
street: result.address_component.street,
street_number: result.address_component.street_number,
address: result.address,
longitude,
latitude
},
msg: "success"
});
},
fail(err) {
reslove({
data: "",
msg: err.errMsg
});
}
});
});
}
// 获取坐标所在位置数据
export function getPosByAddress(address: string) {
return new Promise((reslove) => {
qqMapSdk.geocoder({
address,
sig: config.qqMapSKey,
success(response) {
console.log(response);
const { result } = response;
reslove({
data: {
address,
longitude: result.location.lng,
latitude: result.location.lat,
},
msg: "success",
});
},
fail(err) {
reslove({
data: "",
msg: err.errMsg,
});
},
});
});
}
// 获取坐标
export function getLocation() {
return new Promise(reslove => {
wx.getLocation({
type: "gcj02",
success(res) {
let lat = res.latitude;
let lng = res.longitude;
const { latitude, longitude } = res;
reslove({
msg: "success",
data: {
latitude,
longitude
}
});
},
fail(err) {
reslove({
data: "",
msg: err.errMsg
});
}
});
});
}
// isQuiet为true时不打扰用户,如未获取授权,不请求
export default (isQuiet = true) => {
return new Promise(async reslove => {
const retLocation = await getLocation();
let retData;
if (retLocation.msg === "success") {
retData = await getLocalData(
retLocation.data.latitude,
retLocation.data.longitude
);
} else {
if (isQuiet) {
retData = retLocation;
} else {
const retAuth = await getAuth();
if (retAuth.msg === "success") {
const retLocation2 = await getLocation();
if (retLocation2.msg === "success") {
retData = await getLocalData(
retLocation2.data.latitude,
retLocation2.data.longitude
);
} else {
retData = retLocation2;
}
} else {
retData = retAuth;
}
}
}
return reslove(retData);
});
};