UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

287 lines (280 loc) 15 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _classCallCheck = require('@babel/runtime/helpers/classCallCheck'); var _createClass = require('@babel/runtime/helpers/createClass'); var _regeneratorRuntime = require('@babel/runtime/regenerator'); var tslib_es6 = require('../tslib.es6-0d92ef81.js'); var location_locationDefaultOptions = require('./location-default-options.js'); var location_locationHandleWeb = require('./location-handle-web.js'); var location_locationInterface = require('./location-interface.js'); var location_locationStorage = require('./location-storage.js'); var location_platform_tencentMapApiLocation = require('./platform/tencent-map-api-location.js'); require('../env/env.js'); require('./platform/h5-geolocation-location.js'); require('./platform/msdk-location.js'); require('../msdk/msdk.js'); require('@babel/runtime/helpers/typeof'); require('../loader/little-loader.js'); require('./platform/slug-sdk-location.js'); require('./platform/tencent-map-location.js'); require('../city/city.js'); require('../city/data.js'); require('../loader/loader-unity.js'); require('../loader/loader.js'); require('./platform/tip-sdk-location.js'); require('./platform/wechat-sdk-location.js'); require('../storage/persist-data.js'); require('axios'); require('../lodash-mini/get.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck); var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass); var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime); // import Dialog from '../widget/vant/dialog'; var LocationWeb = /*#__PURE__*/function () { /** * @classdesc 定位工具,支持腾讯地图定位、微信的sdk定位、游戏人生sdk定位 * @constructor */ function LocationWeb() { _classCallCheck__default["default"](this, LocationWeb); this.locationHandle = null; } return _createClass__default["default"](LocationWeb, null, [{ key: "getInstance", value: function getInstance() { return this.instance; } }, { key: "getLocationPreCheck", value: function getLocationPreCheck(options) { return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() { return _regeneratorRuntime__default["default"].wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: return _context.abrupt("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); } })); case 1: case "end": return _context.stop(); } }, _callee); })); } /** * 获取定位信息 * @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(() => { * }); * */ }, { key: "getLocation", value: function getLocation(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 : location_locationDefaultOptions.LocationDefaultOptions.defaultTimeout; options.useWxSdk = (_b = options.useWxSdk) !== null && _b !== void 0 ? _b : location_locationDefaultOptions.LocationDefaultOptions.defaultUseWxSdk; options.useTipSdk = (_c = options.useTipSdk) !== null && _c !== void 0 ? _c : location_locationDefaultOptions.LocationDefaultOptions.defaultUseTipSdk; options.useSlugSdk = (_d = options.useSlugSdk) !== null && _d !== void 0 ? _d : location_locationDefaultOptions.LocationDefaultOptions.defaultUseSlugSdk; options.useMsdk = (_e = options.useMsdk) !== null && _e !== void 0 ? _e : location_locationDefaultOptions.LocationDefaultOptions.defaultUseMsdk; options.useTencentMap = (_f = options.useTencentMap) !== null && _f !== void 0 ? _f : location_locationDefaultOptions.LocationDefaultOptions.defaultUseTencentMap; options.useTencentMapIfFail = (_g = options.useTencentMapIfFail) !== null && _g !== void 0 ? _g : location_locationDefaultOptions.LocationDefaultOptions.defaultUseTencentMapIfFail; options.useTencentMapApi = (_h = options.useTencentMapApi) !== null && _h !== void 0 ? _h : location_locationDefaultOptions.LocationDefaultOptions.defaultUseTencentMapApi; options.sessionCache = (_j = options.sessionCache) !== null && _j !== void 0 ? _j : location_locationDefaultOptions.LocationDefaultOptions.defaultCache; options.localCache = (_k = options.localCache) !== null && _k !== void 0 ? _k : location_locationDefaultOptions.LocationDefaultOptions.defaultLocalCache; options.localExpireMs = (_l = options.localExpireMs) !== null && _l !== void 0 ? _l : location_locationDefaultOptions.LocationDefaultOptions.defaultLocalExpireMs; options.tipLocationUsageDesc = (_m = options.tipLocationUsageDesc) !== null && _m !== void 0 ? _m : location_locationDefaultOptions.LocationDefaultOptions.defaultTipLocationUsageDesc; options.needAllowLocation = (_o = options.needAllowLocation) !== null && _o !== void 0 ? _o : location_locationDefaultOptions.LocationDefaultOptions.defaultNeedAllowLocation; options.forceUpdateLocation = (_p = options.forceUpdateLocation) !== null && _p !== void 0 ? _p : location_locationDefaultOptions.LocationDefaultOptions.defaultForceUpdateLocation; // eslint-disable-next-line no-async-promise-executor return new Promise(function (resolve) { return tslib_es6.__awaiter(_this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() { var _this2 = this; var isAllowLocation, flag, _flag, storedTime, failCallback; return _regeneratorRuntime__default["default"].wrap(function (_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: if (!options.needAllowLocation) { _context2.next = 2; break; } _context2.next = 1; return this.getLocationPreCheck(options); case 1: isAllowLocation = _context2.sent; if (!(isAllowLocation == false)) { _context2.next = 2; break; } resolve({ location: location_locationDefaultOptions.LocationDefaultOptions.defaultLocation, flag: location_locationInterface.LocationFlag.LocationFailed }); return _context2.abrupt("return"); case 2: if (options.forceUpdateLocation) { _context2.next = 4; break; } if (!(options.sessionCache && location_locationStorage["default"].getLocationFromSessionStorage())) { _context2.next = 3; break; } flag = location_locationInterface.LocationFlag.LocationSuccess; if (location_locationStorage["default"].getLocationFromSessionStorage().type === 'ip') { flag = location_locationInterface.LocationFlag.LocationIpSuccess; } resolve({ location: location_locationStorage["default"].getLocationFromSessionStorage(), flag: flag }); return _context2.abrupt("return"); case 3: if (!(options.localCache && location_locationStorage["default"].getLocationFromLocalStorage())) { _context2.next = 4; break; } _flag = location_locationInterface.LocationFlag.LocationSuccess; if (location_locationStorage["default"].getLocationFromLocalStorage().type === 'ip') { _flag = location_locationInterface.LocationFlag.LocationIpSuccess; } resolve({ location: location_locationStorage["default"].getLocationFromLocalStorage(), flag: _flag }); return _context2.abrupt("return"); case 4: this.getInstance().locationHandle = location_locationHandleWeb["default"](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); _this2.storeLocationInfo(res.location, options); })["catch"](function (error) { failCallback(error); }); } else { resolve({ location: location_locationDefaultOptions.LocationDefaultOptions.defaultLocation, flag: location_locationInterface.LocationFlag.LocationFailed }); } // 特殊处理下特权sdk定位失败,降级到腾讯地图定位 failCallback = function failCallback(error) { var failHandle = location_locationHandleWeb["default"].failHandle(options, _this2.getInstance().locationHandle); if (failHandle) { _this2.getInstance().locationHandle = failHandle; _this2.getInstance().locationHandle.getLocation(options).then(function (res) { resolve(res); _this2.storeLocationInfo(res.location, options); })["catch"](function (error) { console.log(error); resolve({ location: location_locationDefaultOptions.LocationDefaultOptions.defaultLocation, flag: location_locationInterface.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: location_locationDefaultOptions.LocationDefaultOptions.defaultLocation, flag: location_locationInterface.LocationFlag.LocationFailed, errorMsg: (error === null || error === void 0 ? void 0 : error.errorMsg) || (error === null || error === void 0 ? void 0 : error.errMsg) }); } }; case 5: case "end": return _context2.stop(); } }, _callee2, this); })); }); } }, { key: "getAreaInfoByLatAndLng", value: function getAreaInfoByLatAndLng(lat, lng) { return location_platform_tencentMapApiLocation["default"].getAreaInfoByLatAndLng(lat, lng); } /** * 存储定位成功和缓存位置信息 * * @param {object} location 位置信息 * @param {object} options 定位配置信息 * @private */ }, { key: "storeLocationInfo", value: function storeLocationInfo(location, options) { // 缓存位置信息 if (location) { // 定位成功后,再读取当前时间,并存储 this.locateTime = new Date().getTime(); window.localStorage.setItem('tip_locate_time', "".concat(this.locateTime)); location_locationStorage["default"].setLocationToStorage(location, options.localExpireMs); } } }]); }(); LocationWeb.locationFlag = location_locationInterface.LocationFlag; LocationWeb.instance = new LocationWeb(); LocationWeb.locateTime = 0; exports.LocationWeb = LocationWeb;