minigame-std
Version:
Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.
81 lines (80 loc) • 2.37 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
let happy_rusty = require("happy-rusty");
let src_std_utils_mod_ts = require("./utils.cjs");
let tiny_future = require("tiny-future");
//#region src/macros/env.ts
/**
* 如果在小游戏环境中返回 true,否则返回 false。
*/
const IS_MINA = __MINIGAME_STD_MINA__;
//#endregion
//#region src/std/lbs/mina_lbs.ts
/**
* @internal
* 小游戏平台的地理位置服务实现。
*/
/**
* 获取当前 geo 坐标。
* @returns 异步的经纬度结果。
*/
async function getCurrentPosition$2() {
const hasFuzzy = typeof wx.getFuzzyLocation === "function";
const getLocation = hasFuzzy ? wx.getFuzzyLocation : wx.getLocation;
const scope = hasFuzzy ? "scope.userFuzzyLocation" : "scope.userLocation";
const authRes = await (0, src_std_utils_mod_ts.asyncIOResultify)(wx.authorize)({ scope });
if (authRes.isErr()) return authRes.asErr();
return (await (0, src_std_utils_mod_ts.asyncIOResultify)(getLocation)({ type: "wgs84" })).map((pos) => ({
latitude: pos.latitude,
longitude: pos.longitude
}));
}
//#endregion
//#region src/std/lbs/web_lbs.ts
/**
* @internal
* Web 平台的地理位置服务实现。
*/
/**
* 获取当前 geo 坐标。
* @returns 异步的经纬度结果。
*/
function getCurrentPosition$1() {
const future = new tiny_future.Future();
navigator.geolocation.getCurrentPosition((position) => {
future.resolve((0, happy_rusty.Ok)({
latitude: position.coords.latitude,
longitude: position.coords.longitude
}));
}, (err) => {
future.resolve((0, happy_rusty.Err)(new Error(err.message)));
});
return future.promise;
}
//#endregion
//#region src/std/lbs/mod.ts
/**
* 位置服务模块(Location Based Service),提供获取地理位置坐标的功能。
* @module lbs
*/
/**
* 获取当前 geo 坐标。
* @returns 当前经纬度。
* @since 1.7.0
* @example
* ```ts
* const result = await getCurrentPosition();
* if (result.isOk()) {
* const pos = result.unwrap();
* console.log('纬度:', pos.latitude);
* console.log('经度:', pos.longitude);
* } else {
* console.error('获取位置失败:', result.unwrapErr());
* }
* ```
*/
function getCurrentPosition() {
return IS_MINA ? getCurrentPosition$2() : getCurrentPosition$1();
}
//#endregion
exports.getCurrentPosition = getCurrentPosition;
//# sourceMappingURL=lbs.cjs.map