wanmeiyizhan/utils/publicMethods.js
2025-01-15 18:39:05 +08:00

240 lines
10 KiB
JavaScript

// 封一些公共方法
// 判断小程序端中 是否已经有了位置权限
export function handleHavePointInMin() {
return new Promise((resolve, reject) => {
uni.getSetting({
success(res) {
console.log('handleHavePointInMin', res);
// 判断现在有没有拿到微信信息的权限 没有的话 就去跳转设置
if (!res.authSetting["scope.userLocation"]) {
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.getFuzzyLocation({
type: "gcj02",
altitude: 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.getFuzzyLocation({
type: "gcj02",
altitude: 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;
}