import authentication from "./promise.js";
/**
* @description:
* @param {*} callback
* @return {*}
*/
const Location = callback => {
uni.getLocation({
type: "gcj02",
geocode: true,
success: res => {
callback(res);
},
fail: () => {
// #ifdef MP-WEIXIN
// 如果用uni.getLocation没有获取到地理位置,则需要获取当前的授权信息,判断是否有地理授权信息
uni.getSetting({
success: res => {
var status = res.authSetting;
if (!status["scope.userLocation"]) {
// 如果授权信息中没有地理位置的授权,则需要弹窗提示用户需要授权地理信息
ui.showModal({
title: "是否授权当前位置",
content:
"需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
success: tip => {
if (tip.confirm) {
// 如果用户同意授权地理信息,则打开授权设置页面,判断用户的操作
uni.openSetting({
success: data => {
// 如果用户授权了地理信息在,则提示授权成功
if (data.authSetting["scope.userLocation"] === true) {
uni.showToast({
title: "授权成功",
icon: "success",
duration: 1000,
});
// 授权成功后,然后再次getLocation获取信息
uni.getLocation({
type: "gcj02",
geocode: true,
success: res => {
callback(res);
},
});
} else {
uni.showToast({
title: "授权失败",
icon: "none",
duration: 1000,
});
}
},
});
}
},
});
} else {
// 调用getLocation
uni.getLocation({
type: "gcj02",
geocode: true,
success: res => {
callback(res);
},
});
}
},
fail: res => {
uni.showToast({
title: "调用授权窗口失败",
icon: "none",
duration: 1000,
});
},
});
// #endif
},
});
};
module.exports = {
// 选择位置信息
chooseLocation: callback => {
uni.chooseLocation({
success: res => {
callback(res);
},
fail: res => {
// #ifdef MP-WEIXIN
// 如果用uni.chooseLocation没有获取到地理位置,则需要获取当前的授权信息,判断是否有地理授权信息
uni.getSetting({
success: res => {
var status = res.authSetting;
if (!status["scope.userLocation"]) {
// 如果授权信息中没有地理位置的授权,则需要弹窗提示用户需要授权地理信息
ui.showModal({
title: "是否授权当前位置",
content:
"需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
success: tip => {
if (tip.confirm) {
// 如果用户同意授权地理信息,则打开授权设置页面,判断用户的操作
uni.openSetting({
success: data => {
// 如果用户授权了地理信息在,则提示授权成功
if (data.authSetting["scope.userLocation"] === true) {
uni.showToast({
title: "授权成功",
icon: "success",
duration: 1000,
});
// 授权成功后,然后再次chooseLocation获取信息
uni.chooseLocation({
success: res => {
callback(res);
},
});
} else {
uni.showToast({
title: "授权失败",
icon: "none",
duration: 1000,
});
}
},
});
}
},
});
} else {
// 调用chooseLocation
uni.chooseLocation({
success: res => {
callback(res);
},
});
}
},
fail: res => {
uni.showToast({
title: "调用授权窗口失败",
icon: "none",
duration: 1000,
});
},
});
// #endif
// #ifdef APP-PLUS
console.log("授权失败");
// #endif
console.log("授权失败");
},
});
},
getLocation: callback => {
authentication().then(res => {
Location(callback);
});
},
// process.env.NODE_ENV === "development"
// ? "https://gsjytext.gszjxtpt.com/api/home/my/uploadImage"
// : "https://gsjy.gszjxtpt.com/api/home/my/uploadImage",
// 上传文件
upload: (file, filetype, callback) => {
uni.showLoading({
mask: true,
title: "上传中...",
});
// 执行文件上传
uni.uploadFile({
url:
process.env.NODE_ENV === "development"
? "https://gsjytest.gszjxtpt.com/api/home/my/uploadImage"
: "https://gsjy.gszjxtpt.com/api/home/my/uploadImage",
filePath: file,
header: {
token: uni.getStorageSync("access_token"),
},
fileType: filetype,
name: "file",
formData: {
filetype,
},
success: uploadFileRes => {
console.log(JSON.parse(uploadFileRes.data), "图片");
uni.hideLoading();
callback(JSON.parse(uploadFileRes.data).data.url);
},
fail: err => {
uni.hideLoading();
uni.showToast({
title: err.errMsg,
icon: "none",
});
},
});
},
};
- 发布:2023-09-15 16:40
- 更新:2023-09-15 16:40
- 阅读:227
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: windows11
HBuilderX类型: 正式
HBuilderX版本号: 3.8.12
手机系统: Android
手机系统版本号: Android 14
手机厂商: 华为
手机机型: x40
页面类型: vue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
示例代码:
操作步骤:
测试发现 只要关闭手机定位 程序执行到 authentication().then(res => {
Location(callback);
});就停止了 能够拿到authentication resolve出来的数据 没有报错 Location(callback);没有执行
测试发现 只要关闭手机定位 程序执行到 authentication().then(res => {
Location(callback);
});就停止了 能够拿到authentication resolve出来的数据 没有报错 Location(callback);没有执行
bug描述:
uni.getLocation 未打开手机定位 函数fail不执行
0 个回复