484 lines
20 KiB
JavaScript
484 lines
20 KiB
JavaScript
import api from './api'
|
|
// 封一些公共方法
|
|
|
|
// 拿到用户位置的方法 如果有记录就直接取缓存 没有就重新请求之后输出
|
|
export async function handleGetUserPointInfo() {
|
|
// 先看看之后有没有值
|
|
let seatInfo = uni.getStorageSync("seatInfo")
|
|
// 如果有值 就直接取缓存里面的值 输出
|
|
if (seatInfo) {
|
|
let res = seatInfo
|
|
return res
|
|
} else {
|
|
// 如果没有值 那么就去调用 拿到值之后 输出
|
|
let loginType = uni.getStorageSync("loginType");
|
|
let res = {}
|
|
if (loginType === 'min') {
|
|
res = await handleGetPosition()
|
|
} else if (loginType === 'android') {
|
|
res = await handleGetAndroidPosition()
|
|
} else {
|
|
res = await handleGetIOSPosition()
|
|
}
|
|
uni.setStorageSync("seatInfo", res)
|
|
return res
|
|
}
|
|
}
|
|
|
|
// 三个平台拿到当前位置的方法 反正最后输出经纬度的数据就好
|
|
async function handleGetPosition() {
|
|
return new Promise(async (resolve, reject) => {
|
|
const res = await uni.getSetting();
|
|
const times = uni.getStorageSync("howTimes");
|
|
|
|
// 没有权限时
|
|
if (!res.authSetting["scope.userLocation"] && !res.authSetting["scope.userFuzzyLocation"] && times === 2) {
|
|
uni.showModal({
|
|
title: "注意",
|
|
content: "拒绝授权位置服务会导致小程序功能无法正常使用,建议授权位置信息",
|
|
confirmText: "去设置",
|
|
success: async function (modalRes) {
|
|
if (modalRes.confirm) {
|
|
const settingRes = await uni.openSetting();
|
|
if (settingRes.authSetting["scope.userLocation"]) {
|
|
try {
|
|
const locationRes = await uni.getLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
isHighAccuracy: true,
|
|
});
|
|
const seatInfo = {
|
|
latitude: locationRes.latitude,
|
|
longitude: locationRes.longitude,
|
|
};
|
|
resolve(seatInfo);
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
}
|
|
} else {
|
|
reject(new Error("用户拒绝授权"));
|
|
}
|
|
},
|
|
});
|
|
} else {
|
|
// 有权限时,直接获取位置
|
|
try {
|
|
const locationRes = await uni.getLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
isHighAccuracy: true,
|
|
});
|
|
const seatInfo = {
|
|
latitude: locationRes.latitude,
|
|
longitude: locationRes.longitude,
|
|
};
|
|
resolve(seatInfo);
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// 安卓系统获取当前位置
|
|
export async function handleGetAndroidPosition() {
|
|
return new Promise(async (resolve, reject) => {
|
|
const context = plus.android.runtimeMainActivity();
|
|
const PackageManager = plus.android.importClass("android.content.pm.PackageManager");
|
|
const hasPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION") === PackageManager.PERMISSION_GRANTED;
|
|
|
|
const times = uni.getStorageSync("howTimes");
|
|
console.log('hasPermission', hasPermission);
|
|
|
|
// 判断是第二次进入 且 没有权限
|
|
if (!hasPermission && times === 2) {
|
|
uni.showModal({
|
|
title: "注意",
|
|
content: "拒绝授权位置服务会导致小程序功能无法正常使用,建议授权位置信息",
|
|
confirmText: "去设置",
|
|
success: async function (res) {
|
|
console.log('res', res);
|
|
if (res.confirm) {
|
|
try {
|
|
const locationRes = await uni.getLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
isHighAccuracy: true,
|
|
});
|
|
const seatInfo = {
|
|
latitude: locationRes.latitude,
|
|
longitude: locationRes.longitude,
|
|
};
|
|
resolve(seatInfo);
|
|
} catch (err) {
|
|
// reject(err);
|
|
resolve(false);
|
|
}
|
|
// const main = plus.android.runtimeMainActivity();
|
|
// const Intent = plus.android.importClass(
|
|
// "android.content.Intent"
|
|
// );
|
|
// const Settings = plus.android.importClass(
|
|
// "android.provider.Settings"
|
|
// );
|
|
// const intent = new Intent(
|
|
// Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
|
// );
|
|
// const uri = plus.android.invoke(
|
|
// "android.net.Uri",
|
|
// "parse",
|
|
// "package:" + main.getPackageName()
|
|
// );
|
|
// intent.setData(uri);
|
|
// main.startActivity(intent);
|
|
// uni.switchTab({
|
|
// url: "/pages/home/index"
|
|
// })
|
|
}
|
|
},
|
|
});
|
|
} else {
|
|
try {
|
|
const locationRes = await uni.getLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
isHighAccuracy: true,
|
|
});
|
|
const seatInfo = {
|
|
latitude: locationRes.latitude,
|
|
longitude: locationRes.longitude,
|
|
};
|
|
resolve(seatInfo);
|
|
} catch (err) {
|
|
// reject(err);
|
|
resolve(false);
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// ios 系统获取当前位置的方法
|
|
export async function handleGetIOSPosition() {
|
|
return new Promise(async (resolve, reject) => {
|
|
const locationRes = await uni.getLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
isHighAccuracy: true,
|
|
});
|
|
const seatInfo = {
|
|
latitude: locationRes.latitude,
|
|
longitude: locationRes.longitude,
|
|
};
|
|
resolve(seatInfo);
|
|
})
|
|
}
|
|
|
|
// 封装一下方法 拿到最近的服务区 三个服务区
|
|
// 输出的值为三个服务区的详情信息的数组 这个方法会帮忙存一下 最近的三个服务区的名称数组 和 最近一个服务区的数据
|
|
export async function handleGetNearThreeService() {
|
|
let currentService = uni.getStorageSync("currentService");
|
|
let nearList = uni.getStorageSync("nearThreeList");
|
|
let allService = uni.getStorageSync("allServiceList");
|
|
let nearService = uni.getStorageSync("allServiceList");
|
|
|
|
if (currentService && nearList && allService) {
|
|
return {
|
|
nearThreeList: nearList,// 最近的三个服务区的名称数组
|
|
allServiceList: allService,// 全部服务区的数据
|
|
currentService: currentService,// 最近的服务区数据
|
|
nearService: nearService
|
|
}
|
|
} else {
|
|
let seatInfo = await handleGetUserPointInfo()
|
|
console.log('seatInfo', seatInfo);
|
|
if (seatInfo) {
|
|
seatInfo = seatInfo
|
|
}
|
|
|
|
let req = {
|
|
Province_Code: "340000",
|
|
longitude: seatInfo.longitude,
|
|
latitude: seatInfo.latitude,
|
|
ShowService: true,
|
|
};
|
|
const res = await api.$get(
|
|
"/CommercialApi/BaseInfo/GetServerpartList",
|
|
req
|
|
);
|
|
let data = res.Result_Data.List;
|
|
// 最近的前三个的服务区 名称
|
|
let nearThreeList = [];
|
|
// 先存一下所有服务区的数据
|
|
let allServiceList = []
|
|
if (data && data.length > 0) {
|
|
data.forEach((item, index) => {
|
|
if (index <= 2) {
|
|
nearThreeList.push(item.SERVERPART_NAME)
|
|
}
|
|
allServiceList.push({
|
|
...item,
|
|
showName: item.SERVERPART_NAME,
|
|
ProvinceCode: "340000",
|
|
ServerPart_Id: item.SERVERPART_ID,
|
|
ServerPart_Name: item.SERVERPART_NAME,
|
|
})
|
|
});
|
|
}
|
|
|
|
uni.setStorageSync("currentService", data[0]); // 最近的服务区数据
|
|
uni.setStorageSync("nearService", data[0]); // 最近的服务区数据
|
|
uni.setStorageSync("nearThreeList", nearThreeList); // 最近的三个服务区
|
|
uni.setStorageSync("allServiceList", allServiceList); // 最近的三个服务区
|
|
|
|
return {
|
|
nearThreeList: nearThreeList,// 最近的三个服务区的名称数组
|
|
allServiceList: allServiceList,// 全部服务区的数据
|
|
currentService: data[0],// 最近的服务区数据
|
|
nearService: data[0]
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 判断小程序端中 是否已经有了位置权限
|
|
export function handleHavePointInMin() {
|
|
return new Promise((resolve, reject) => {
|
|
uni.getSetting({
|
|
success(res) {
|
|
console.log('handleHavePointInMin', res);
|
|
// 判断现在有没有拿到微信信息的权限 没有的话 就去跳转设置
|
|
if (!res.authSetting["scope.userLocation"] && !res.authSetting["scope.userFuzzyLocation"]) {
|
|
uni.showModal({
|
|
title: "注意",
|
|
content: "拒绝授权位置服务会导致小程序功能无法正常使用,建议授权位置信息",
|
|
confirmText: "去设置",
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
uni.openSetting({
|
|
success(settingRes) {
|
|
console.log("settingRes", settingRes);
|
|
// 如果点击了同意就去拿位置 存起来
|
|
if (settingRes.authSetting["scope.userLocation"]) {
|
|
uni.getLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
isHighAccuracy: true,
|
|
success: async (res) => {
|
|
let seatInfo = {
|
|
latitude: res.latitude,
|
|
longitude: res.longitude,
|
|
};
|
|
uni.setStorageSync(
|
|
"seatInfo",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
uni.setStorageSync(
|
|
"actualLocation",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
resolve(seatInfo)
|
|
// _this.seatInfo = seatInfo;
|
|
// _this.handleGetOnLoad();
|
|
},
|
|
});
|
|
}
|
|
},
|
|
});
|
|
} else if (res.cancel) {
|
|
console.log("用户点击取消");
|
|
}
|
|
},
|
|
});
|
|
} else {
|
|
let seatInfo = uni.getStorageSync("seatInfo")
|
|
if (seatInfo) {
|
|
return JSON.parse(seatInfo)
|
|
} else {
|
|
uni.getLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
isHighAccuracy: true,
|
|
success: async (res) => {
|
|
let seatInfo = {
|
|
latitude: res.latitude,
|
|
longitude: res.longitude,
|
|
};
|
|
uni.setStorageSync(
|
|
"seatInfo",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
uni.setStorageSync(
|
|
"actualLocation",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
resolve(seatInfo)
|
|
},
|
|
});
|
|
}
|
|
}
|
|
},
|
|
});
|
|
})
|
|
}
|
|
|
|
// 判断在 安卓 app端中 是否有了位置权限
|
|
export function handleHavePointInApp() {
|
|
return new Promise((resolve, reject) => {
|
|
const context = plus.android.runtimeMainActivity();
|
|
const PackageManager = plus.android.importClass("android.content.pm.PackageManager");
|
|
const hasPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION") === PackageManager.PERMISSION_GRANTED;
|
|
|
|
// true 的时候 就是有了位置权限
|
|
if (hasPermission) {
|
|
let seatInfo = uni.getStorageSync("seatInfo")
|
|
if (seatInfo) {
|
|
return JSON.parse(seatInfo)
|
|
} else {
|
|
plus.geolocation.getCurrentPosition(
|
|
async function (position) {
|
|
console.log("当前位置:", position);
|
|
let res = position.coords;
|
|
let seatInfo = {
|
|
latitude: res.latitude,
|
|
longitude: res.longitude,
|
|
};
|
|
uni.setStorageSync("seatInfo", JSON.stringify(seatInfo));
|
|
uni.setStorageSync("actualLocation", JSON.stringify(seatInfo));
|
|
_this.seat = seatInfo;
|
|
let latitude = res.latitude;
|
|
let longitude = res.longitude;
|
|
return {
|
|
latitude: latitude,
|
|
longitude: longitude
|
|
}
|
|
}
|
|
);
|
|
}
|
|
} else {
|
|
uni.showModal({
|
|
title: "注意",
|
|
content: "拒绝授权位置服务会导致小程序功能无法正常使用,建议授权位置信息",
|
|
confirmText: "去设置",
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
const main = plus.android.runtimeMainActivity();
|
|
const Intent = plus.android.importClass(
|
|
"android.content.Intent"
|
|
);
|
|
const Settings = plus.android.importClass(
|
|
"android.provider.Settings"
|
|
);
|
|
const intent = new Intent(
|
|
Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
|
);
|
|
const uri = plus.android.invoke(
|
|
"android.net.Uri",
|
|
"parse",
|
|
"package:" + main.getPackageName()
|
|
);
|
|
intent.setData(uri);
|
|
main.startActivity(intent);
|
|
uni.switchTab({
|
|
url: "/pages/home/index"
|
|
})
|
|
}
|
|
},
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
// 判断在ios的app端 是否有了位置权限
|
|
export function hanldeHavePointInIos() {
|
|
plus.geolocation.getCurrentPosition(
|
|
function (position) {
|
|
// 获取成功后的回调
|
|
console.log("获取位置成功:", position);
|
|
const latitude = position.coords.latitude; // 纬度
|
|
const longitude = position.coords.longitude; // 经度
|
|
// 其他业务逻辑
|
|
let res = position.coords;
|
|
let seatInfo = {
|
|
latitude: latitude,
|
|
longitude: longitude,
|
|
};
|
|
uni.setStorageSync("seatInfo", JSON.stringify(seatInfo));
|
|
uni.setStorageSync("actualLocation", JSON.stringify(seatInfo));
|
|
_this.seat = seatInfo;
|
|
return {
|
|
latitude: latitude,
|
|
longitude: longitude
|
|
}
|
|
},
|
|
function (error) {
|
|
// 定位失败的回调
|
|
console.error("获取位置失败:", error);
|
|
if (error.code === 1) {
|
|
// 权限拒绝的情况
|
|
uni.showModal({
|
|
title: "定位失败",
|
|
content: "请开启定位权限以获取您的位置信息。",
|
|
success: function (modalRes) {
|
|
if (modalRes.confirm) {
|
|
// 打开设置界面
|
|
uni.openSetting({
|
|
success: function (settingRes) {
|
|
console.log("用户已打开定位设置", settingRes);
|
|
},
|
|
});
|
|
}
|
|
},
|
|
});
|
|
}
|
|
}, {
|
|
enableHighAccuracy: true
|
|
} // 高精度定位
|
|
);
|
|
|
|
}
|
|
|
|
|
|
// 拿到最近的服务区数据
|
|
export async function handleGetNearService(_this, longitude, latitude) {
|
|
|
|
let seatInfo = JSON.parse(uni.getStorageSync("seatInfo"));
|
|
let req = {
|
|
Province_Code: "340000",
|
|
longitude: longitude ? longitude : seatInfo.longitude ? seatInfo.longitude : '',
|
|
latitude: latitude ? latitude : seatInfo.latitude ? seatInfo.latitude : '',
|
|
};
|
|
// uni.showLoading({
|
|
// title: "查找最近的服务区...",
|
|
// });
|
|
const data = await _this.$api.$get(
|
|
"/CommercialApi/BaseInfo/GetServerpartList",
|
|
req
|
|
);
|
|
|
|
let _data = data.Result_Data.List;
|
|
let obj = handleChangeServiceInfo(_data[0]);
|
|
console.log('publicMethods', obj);
|
|
uni.setStorageSync("currentService", obj); // 当前选中服务区信息
|
|
// uni.hideLoading();
|
|
return obj
|
|
}
|
|
|
|
// 兼容一下老接口要的服务区对象的内容
|
|
function handleChangeServiceInfo(obj) {
|
|
let newObj = {
|
|
...obj,
|
|
Distance: obj.SERVERPART_DISTANCE,
|
|
OwnerUnitId: obj.OWNERUNIT_ID,
|
|
OwnerUnitName: obj.OWNERUNIT_NAME,
|
|
ProvinceCode: "340000",
|
|
ServerPart_Id: obj.SERVERPART_ID,
|
|
ServerPart_Name: obj.SERVERPART_NAME,
|
|
ServerPart_Tel: "",
|
|
ServerPart_X: obj.SERVERPART_X,
|
|
ServerPart_Y: obj.SERVERPART_Y,
|
|
showName: `${obj.SERVERPART_NAME}(${obj.SERVERPART_DISTANCE}km)`,
|
|
};
|
|
return newObj;
|
|
} |