UNPKG

@hocgin/taro-kit

Version:
199 lines (161 loc) 9.49 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } import { LangKit as _LangKit } from "@hocgin/hkit"; import qs, { stringifyUrl } from "query-string"; import Taro from "@tarojs/taro"; export var LangKit = /*#__PURE__*/function (_LangKit2) { _inherits(LangKit, _LangKit2); var _super = _createSuper(LangKit); function LangKit() { _classCallCheck(this, LangKit); return _super.apply(this, arguments); } _createClass(LangKit, null, [{ key: "getUrl", value: /** * 获取 url 参数自动编码 * @param url * @returns {string} */ function getUrl(url) { if (!url) { return url; } return stringifyUrl({ url: url, query: {} }); } }, { key: "isBrowser", value: function isBrowser() { return Taro.getEnv() === Taro.ENV_TYPE.WEB; } }, { key: "isAndroid", value: function isAndroid() { var _getSystemInfoSync; if (Taro.getEnv() === Taro.ENV_TYPE.WEB) { var _navigator, _navigator$userAgent, _navigator$userAgent$; return ((_navigator = navigator) === null || _navigator === void 0 ? void 0 : (_navigator$userAgent = _navigator.userAgent) === null || _navigator$userAgent === void 0 ? void 0 : (_navigator$userAgent$ = _navigator$userAgent.toLowerCase()) === null || _navigator$userAgent$ === void 0 ? void 0 : _navigator$userAgent$.indexOf("android")) !== -1; } return /android/i.test(Taro === null || Taro === void 0 ? void 0 : (_getSystemInfoSync = Taro.getSystemInfoSync()) === null || _getSystemInfoSync === void 0 ? void 0 : _getSystemInfoSync.system); } }, { key: "isIOS", value: function isIOS() { var _Taro$getSystemInfoSy; if (Taro.getEnv() === Taro.ENV_TYPE.WEB) { var _navigator2, _navigator2$userAgent; var useragent = (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : (_navigator2$userAgent = _navigator2.userAgent) === null || _navigator2$userAgent === void 0 ? void 0 : _navigator2$userAgent.toLowerCase(); return (useragent === null || useragent === void 0 ? void 0 : useragent.indexOf("ios")) !== -1 || (useragent === null || useragent === void 0 ? void 0 : useragent.indexOf("ipad")) !== -1; } return /ios/i.test(Taro === null || Taro === void 0 ? void 0 : (_Taro$getSystemInfoSy = Taro.getSystemInfoSync()) === null || _Taro$getSystemInfoSy === void 0 ? void 0 : _Taro$getSystemInfoSy.system); } }, { key: "getSystemInfo", value: function getSystemInfo() { var sysInfo = { STATUS_BAR_HEIGHT: 0, DEFAULT_HEADER_HEIGHT: 46, IS_IOS: LangKit.isIOS(), IS_ANDROID: LangKit.isAndroid(), DEVICE_PIXEL_RATIO: 1 }; if (Taro.getEnv() === Taro.ENV_TYPE.WEB) { sysInfo.WIN_WIDTH = window.screen.width; sysInfo.WIN_HEIGHT = window.screen.height; } else { var _Taro$getSystemInfoSy2; var res = (Taro === null || Taro === void 0 ? void 0 : (_Taro$getSystemInfoSy2 = Taro.getSystemInfoSync) === null || _Taro$getSystemInfoSy2 === void 0 ? void 0 : _Taro$getSystemInfoSy2.call(Taro)) || {}; sysInfo.WIN_WIDTH = res === null || res === void 0 ? void 0 : res.screenWidth; sysInfo.WIN_HEIGHT = res === null || res === void 0 ? void 0 : res.screenHeight; sysInfo.STATUS_BAR_HEIGHT = res === null || res === void 0 ? void 0 : res.statusBarHeight; sysInfo.DEVICE_PIXEL_RATIO = res === null || res === void 0 ? void 0 : res.devicePixelRatio; } // res.screenHeight - res.windowHeight - res.statusBarHeight sysInfo.DEFAULT_CONTENT_HEIGHT = sysInfo.WIN_HEIGHT - sysInfo.STATUS_BAR_HEIGHT - (sysInfo === null || sysInfo === void 0 ? void 0 : sysInfo.DEFAULT_HEADER_HEIGHT); sysInfo.IS_APP = true; sysInfo.showAlert = function (options) { options.showCancel = false; sysInfo.showModal(options); }; sysInfo.showConfirm = function (options) { sysInfo.showModal(options); }; return sysInfo; } /** * 获取 appbar 的高度 */ }, { key: "getAppBarHeight", value: function getAppBarHeight() { var sysInfo = LangKit.getSystemInfo(); var headerHeight = sysInfo === null || sysInfo === void 0 ? void 0 : sysInfo.DEFAULT_HEADER_HEIGHT; var statusBarHeight = sysInfo.STATUS_BAR_HEIGHT; return headerHeight + statusBarHeight; } }, { key: "rem", value: function rem(v) { if (v === undefined) { return v; } if ("".concat(v).endsWith("px")) { return v; } return "".concat(v, "px"); } }, { key: "createShare", value: function createShare(e) { var _Taro$getCurrentInsta, _router$params; var title = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '分享'; var router = (_Taro$getCurrentInsta = Taro.getCurrentInstance()) === null || _Taro$getCurrentInsta === void 0 ? void 0 : _Taro$getCurrentInsta.router; return { title: title, path: "".concat(router === null || router === void 0 ? void 0 : router.path, "?").concat(qs.stringify((_router$params = router === null || router === void 0 ? void 0 : router.params) !== null && _router$params !== void 0 ? _router$params : {})) }; } /** * 版本是否合规 * @param serverVersion * @param appVersion * @deprecated 废弃 可以用通用版本的 */ }, { key: "passVersion", value: function passVersion() { var serverVersion = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '1.0.0'; var appVersion = arguments.length > 1 ? arguments[1] : undefined; try { var rV = LangKit.versionToArray(appVersion); var pV = LangKit.versionToArray(serverVersion); for (var i = 0; i < rV.length; i++) { if (parseInt(rV[i]) < parseInt(pV[i])) { return true; } } } catch (e) { console.warn("\u7248\u672C\u89E3\u6790\u5931\u8D25: serverVersion=".concat(serverVersion, ", appVersion=").concat(appVersion), e); } return false; } }, { key: "versionToArray", value: function versionToArray(v) { return "".concat(v).replace('v', '').split('.'); } }]); return LangKit; }(_LangKit);