t-comm
Version:
专业、稳定、纯粹的工具库
243 lines (240 loc) • 11.7 kB
JavaScript
import { b as __awaiter, c as __generator } from '../tslib.es6-096fffdd.js';
import { LocationDefaultOptions } from './location-default-options.mjs';
import locationHandle from './location-handle-web.mjs';
import { LocationFlag } from './location-interface.mjs';
import LocationStorage from './location-storage.mjs';
import TencentMapApiLocation from './platform/tencent-map-api-location.mjs';
import '../env/env.mjs';
import './platform/h5-geolocation-location.mjs';
import './platform/msdk-location.mjs';
import '../msdk/msdk.mjs';
import '@babel/runtime/helpers/typeof';
import '../loader/little-loader.mjs';
import './platform/slug-sdk-location.mjs';
import './platform/tencent-map-location.mjs';
import '../city/city.mjs';
import '../city/data.mjs';
import '../loader/loader-unity.mjs';
import '../loader/loader.mjs';
import './platform/tip-sdk-location.mjs';
import './platform/wechat-sdk-location.mjs';
import '../storage/persist-data.mjs';
import 'axios';
import '../lodash-mini/get.mjs';
// import Dialog from '../widget/vant/dialog';
var LocationWeb = /** @class */function () {
/**
* @classdesc 定位工具,支持腾讯地图定位、微信的sdk定位、游戏人生sdk定位
* @constructor
*/
function LocationWeb() {
this.locationHandle = null;
}
LocationWeb.getInstance = function () {
return this.instance;
};
LocationWeb.getLocationPreCheck = function (options) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
var _a;
var alertTips = window.sessionStorage.getItem('tip_is_show_location_usage_alert') || '0';
if (options.showTipLocationUsageAlert && alertTips !== '1') {
window.sessionStorage.setItem('tip_is_show_location_usage_alert', '1');
if (!options.Dialog) {
resolve(false);
}
(_a = options.Dialog) === null || _a === void 0 ? void 0 : _a.confirm({
title: '地理位置信息使用申请',
message: options.tipLocationUsageDesc
}).then(function () {
resolve(true);
})["catch"](function () {
resolve(false);
});
} else {
resolve(true);
}
})];
});
});
};
/**
* 获取定位信息
* @static
* @param {object} options 定位参数
* @param {number} options.timeout 定位超时时间
* @param {boolean} options.useWxSdk 在微信环境下,是否优先使用微信Sdk定位,微信Sdk定位返回的信息将没有省市信息
* @param {boolean} options.useTipSdk 在游戏内环境,是否优先使用特权Sdk定位,目前只有和平和地主游戏支持
* @param {boolean} options.useSlugSdk 在游戏内环境,是否优先使用微社区Sdk定位,目前只有和平游戏支持
* @param {boolean} options.useMsdk 在游戏内环境,是否优先使用Msdk定位,目前只有地主支持
* @param {boolean} options.useTencentMap 是否用腾讯地图定位请求经纬度(默认为此种方式,走的是前端授权定位)
* @param {boolean} options.useTencentMapIfFail 使用特权Sdk定位失败的情况下,是否用腾讯地图定位兜底
* @param {boolean} options.useTencentMapApi 是否使用腾讯地图api定位(后台IP定位)
* @param {boolean} options.sessionCache 是否使用sessionStorage缓存,推荐使用
* @param {boolean} options.localCache 是否使用localStorage缓存
* @param {number} options.localExpireMs localStorage缓存过期时间
*
* @example
*
* import Location from 'src/common/location';
*
* Location.getLocation({
* timeout: 3000,
* useWxSdk: false,
* useTipSdk: false,
* useSlugSdk: false,
* useTencentMap: true,
* useTencentMapIfFail: false,
* useTencentMapApi: false,
* sessionCache: false,
* localCache: false,
* localExpireMs: 100000
* })
* .then((res) => {
* window.app.position = res.location;
* })
* .catch(() => {
* });
*
*/
LocationWeb.getLocation = function (options) {
var _this = this;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
// 填充默认数据
options.timeout = (_a = options.timeout) !== null && _a !== void 0 ? _a : LocationDefaultOptions.defaultTimeout;
options.useWxSdk = (_b = options.useWxSdk) !== null && _b !== void 0 ? _b : LocationDefaultOptions.defaultUseWxSdk;
options.useTipSdk = (_c = options.useTipSdk) !== null && _c !== void 0 ? _c : LocationDefaultOptions.defaultUseTipSdk;
options.useSlugSdk = (_d = options.useSlugSdk) !== null && _d !== void 0 ? _d : LocationDefaultOptions.defaultUseSlugSdk;
options.useMsdk = (_e = options.useMsdk) !== null && _e !== void 0 ? _e : LocationDefaultOptions.defaultUseMsdk;
options.useTencentMap = (_f = options.useTencentMap) !== null && _f !== void 0 ? _f : LocationDefaultOptions.defaultUseTencentMap;
options.useTencentMapIfFail = (_g = options.useTencentMapIfFail) !== null && _g !== void 0 ? _g : LocationDefaultOptions.defaultUseTencentMapIfFail;
options.useTencentMapApi = (_h = options.useTencentMapApi) !== null && _h !== void 0 ? _h : LocationDefaultOptions.defaultUseTencentMapApi;
options.sessionCache = (_j = options.sessionCache) !== null && _j !== void 0 ? _j : LocationDefaultOptions.defaultCache;
options.localCache = (_k = options.localCache) !== null && _k !== void 0 ? _k : LocationDefaultOptions.defaultLocalCache;
options.localExpireMs = (_l = options.localExpireMs) !== null && _l !== void 0 ? _l : LocationDefaultOptions.defaultLocalExpireMs;
options.tipLocationUsageDesc = (_m = options.tipLocationUsageDesc) !== null && _m !== void 0 ? _m : LocationDefaultOptions.defaultTipLocationUsageDesc;
options.needAllowLocation = (_o = options.needAllowLocation) !== null && _o !== void 0 ? _o : LocationDefaultOptions.defaultNeedAllowLocation;
options.forceUpdateLocation = (_p = options.forceUpdateLocation) !== null && _p !== void 0 ? _p : LocationDefaultOptions.defaultForceUpdateLocation;
// eslint-disable-next-line no-async-promise-executor
return new Promise(function (resolve) {
return __awaiter(_this, void 0, void 0, function () {
var isAllowLocation, flag, flag, storedTime, failCallback;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!options.needAllowLocation) return [3 /*break*/, 2];
return [4 /*yield*/, this.getLocationPreCheck(options)];
case 1:
isAllowLocation = _a.sent();
if (isAllowLocation == false) {
resolve({
location: LocationDefaultOptions.defaultLocation,
flag: LocationFlag.LocationFailed
});
return [2 /*return*/];
}
_a.label = 2;
case 2:
// 如果不是强制刷新定位信息,则走缓存判断,否则忽略缓存
if (!options.forceUpdateLocation) {
// 使用sessionStorage存储的位置信息
if (options.sessionCache && LocationStorage.getLocationFromSessionStorage()) {
flag = LocationFlag.LocationSuccess;
if (LocationStorage.getLocationFromSessionStorage().type === 'ip') {
flag = LocationFlag.LocationIpSuccess;
}
resolve({
location: LocationStorage.getLocationFromSessionStorage(),
flag: flag
});
return [2 /*return*/];
}
// 使用localStorage存储的位置信息
if (options.localCache && LocationStorage.getLocationFromLocalStorage()) {
flag = LocationFlag.LocationSuccess;
if (LocationStorage.getLocationFromLocalStorage().type === 'ip') {
flag = LocationFlag.LocationIpSuccess;
}
resolve({
location: LocationStorage.getLocationFromLocalStorage(),
flag: flag
});
return [2 /*return*/];
}
}
this.getInstance().locationHandle = locationHandle(options);
storedTime = parseInt(window.localStorage.getItem('tip_locate_time') || '0');
if (this.locateTime < storedTime) {
this.locateTime = storedTime;
}
// 如果当前时间距离上次定位时间超过10s,则可以重新定位(这里避免的是重复请求,其他情况可以打开localCache)
if (new Date().getTime() - this.locateTime > 10000 && this.getInstance().locationHandle) {
this.getInstance().locationHandle.getLocation(options).then(function (res) {
resolve(res);
_this.storeLocationInfo(res.location, options);
})["catch"](function (error) {
failCallback(error);
});
} else {
resolve({
location: LocationDefaultOptions.defaultLocation,
flag: LocationFlag.LocationFailed
});
}
failCallback = function failCallback(error) {
var failHandle = locationHandle.failHandle(options, _this.getInstance().locationHandle);
if (failHandle) {
_this.getInstance().locationHandle = failHandle;
_this.getInstance().locationHandle.getLocation(options).then(function (res) {
resolve(res);
_this.storeLocationInfo(res.location, options);
})["catch"](function (error) {
console.log(error);
resolve({
location: LocationDefaultOptions.defaultLocation,
flag: LocationFlag.LocationFailed,
errorMsg: (error === null || error === void 0 ? void 0 : error.errorMsg) || (error === null || error === void 0 ? void 0 : error.errMsg)
});
});
} else {
console.log(error);
resolve({
location: LocationDefaultOptions.defaultLocation,
flag: LocationFlag.LocationFailed,
errorMsg: (error === null || error === void 0 ? void 0 : error.errorMsg) || (error === null || error === void 0 ? void 0 : error.errMsg)
});
}
};
return [2 /*return*/];
}
});
});
});
};
LocationWeb.getAreaInfoByLatAndLng = function (lat, lng) {
return TencentMapApiLocation.getAreaInfoByLatAndLng(lat, lng);
};
/**
* 存储定位成功和缓存位置信息
*
* @param {object} location 位置信息
* @param {object} options 定位配置信息
* @private
*/
LocationWeb.storeLocationInfo = function (location, options) {
// 缓存位置信息
if (location) {
// 定位成功后,再读取当前时间,并存储
this.locateTime = new Date().getTime();
window.localStorage.setItem('tip_locate_time', "".concat(this.locateTime));
LocationStorage.setLocationToStorage(location, options.localExpireMs);
}
};
LocationWeb.locationFlag = LocationFlag;
LocationWeb.instance = new LocationWeb();
LocationWeb.locateTime = 0;
return LocationWeb;
}();
export { LocationWeb };