UNPKG

yg-tools

Version:

some convenient APIs and Methods for Youngon

129 lines (128 loc) 5.32 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var WebContext_1 = __importDefault(require("./WebContext")); var GlobalConfig = /** @class */ (function () { function GlobalConfig(context, baseUrl) { var _this = this; if (baseUrl === void 0) { baseUrl = ''; } this.config = { context: { request: function (_opt) { _this.warning('request'); return Promise.resolve({ data: { status: true } }); }, login: function (_opt) { return _this.warning('login'); }, showToast: function (_opt) { return _this.warning('showToast'); }, switchTab: function (_opt) { return _this.warning('switchTab'); }, getStorageSync: function (_opt) { return _this.warning('getStorageSync'); }, setStorageSync: function (_opt) { return _this.warning('setStorageSync'); }, removeStorageSync: function (_opt) { return _this.warning('removeStorageSync'); }, clearStorageSync: function (_opt) { return _this.warning('clearStorageSync'); } }, handleResponse: function (rs) { return rs && rs.data ? rs.data : rs; }, globalAPIErrorHandle: function (msg, err) { if (err) { _this.config.context.showToast({ title: msg || '迷失在茫茫网络中...', icon: 'none', duration: 2000 }); console.info('[yg-tools api error] request error\n', err); } else { _this.config.context.showToast({ title: msg || '网络出现了一点波动...', icon: 'none', duration: 2000 }); } }, baseUrl: '', authToken: '__auth_token__', mpIndexPath: '/pages/index/index', mpAccountPath: '/pages/account/index' }; this.config.context = context; this.config.baseUrl = baseUrl; } /** * 未初始化 context 就调用方法, 给予警告 */ GlobalConfig.prototype.warning = function (method) { console.warn("[yg-tools warn] The " + method + " method of context is not initialized. Please pass an initial value containing the " + method + " method to context"); }; GlobalConfig.prototype.mergeConfig = function (config) { this.config = __assign(__assign({}, this.config), config); return this; }; GlobalConfig.prototype.setContext = function (context) { this.config.context = context; }; GlobalConfig.prototype.getContext = function () { return this.config.context; }; GlobalConfig.prototype.setBaseUrl = function (baseUrl) { this.config.baseUrl = baseUrl; }; GlobalConfig.prototype.getBaseUrl = function () { return this.config.baseUrl; }; GlobalConfig.prototype.setAuthToken = function (authToken) { this.config.authToken = authToken; }; GlobalConfig.prototype.getAuthToken = function () { return this.config.authToken; }; GlobalConfig.prototype.setMPIndexPath = function (mpIndexPath) { this.config.mpIndexPath = mpIndexPath; }; GlobalConfig.prototype.getMPIndexPath = function () { return this.config.mpIndexPath; }; GlobalConfig.prototype.setMPAccountPath = function (mpAccountPath) { this.config.mpAccountPath = mpAccountPath; }; GlobalConfig.prototype.getMPAccountPath = function () { return this.config.mpAccountPath; }; GlobalConfig.prototype.setGlobalAPIErrorHandle = function (globalAPIErrorHandle) { this.config.globalAPIErrorHandle = globalAPIErrorHandle; }; GlobalConfig.prototype.getGlobalAPIErrorHandle = function () { return this.config.globalAPIErrorHandle; }; GlobalConfig.prototype.setHandleResponse = function (handleResponse) { this.config.handleResponse = handleResponse; }; GlobalConfig.prototype.getHandleResponse = function () { return this.config.handleResponse; }; GlobalConfig.prototype.getConfig = function (key) { if (key) { return this.config[key]; } return this.config; }; GlobalConfig.prototype.useWebContext = function (webContext) { return this.mergeConfig({ context: WebContext_1.default.createWebContext(webContext) }); }; GlobalConfig.createConfig = function (config) { return new GlobalConfig().mergeConfig(config); }; return GlobalConfig; }()); exports.default = GlobalConfig;