@mamba-le/auth
Version:
211 lines (187 loc) • 5.13 kB
JavaScript
/**
* @description 认证模块
* @version 0.3.7
* @author lengyingxin <lengyingxin8966@gmail.com> (https://github.com/LengYXin)
*/
import _toLower from 'lodash/toLower';
import _includes from 'lodash/includes';
import _some from 'lodash/some';
import _isObject from 'lodash/isObject';
import _filter from 'lodash/filter';
import { __spreadArray } from 'tslib';
import debug from 'debug';
import localforage from 'localforage';
import { create } from 'mobx-persist';
var AuthOptions =
/** @class */
function () {
function AuthOptions() {}
/**
* 日志输出
* @param namespace
* @param args
*/
AuthOptions.log = function (namespace) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
if (!AuthOptions.browser && process.env.NODE_ENV === 'production') {
args = _filter(args, function (x) {
return !_isObject(x);
});
}
var log = debug("\u3010 Auth \u3011- " + namespace + " -");
log.enabled = true;
var formatter = args[0],
ars = args.slice(1);
log.apply(void 0, __spreadArray([formatter], ars));
log.destroy();
};
/**
* 日志输出带堆栈记录
* @static
* @param {string} namespace
* @param {*} args
* @memberof AuthOptions
*/
AuthOptions.trace = function (namespace) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
AuthOptions.log.apply(AuthOptions, __spreadArray([namespace], args));
if (AuthOptions.browser) {
console === null || console === void 0 ? void 0 : console.groupCollapsed("%c -- [\u5806\u6808\u8BB0\u5F55] --", "color:#f5222d");
console === null || console === void 0 ? void 0 : console.trace(); // hidden in collapsed group
console === null || console === void 0 ? void 0 : console.groupEnd();
}
};
Object.defineProperty(AuthOptions, "isWeappBowser", {
/**
* 微信 小程序 浏览器
* @readonly
* @memberof XTGlobal
*/
get: function get() {
if (AuthOptions.browser) {
return _some(['MiniProgramEnv', 'miniProgram'], function (agent) {
return _includes(_toLower(window.navigator.userAgent), _toLower(agent));
});
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(AuthOptions, "browser", {
/**
* 是否浏览器环境
* @readonly
* @memberof AppConfig
*/
get: function get() {
return typeof window !== 'undefined';
},
enumerable: false,
configurable: true
});
Object.defineProperty(AuthOptions, "micro", {
/**
* 微前端环境
* @readonly
* @static
* @memberof AuthOptions
*/
get: function get() {
if (AuthOptions.browser) {
return window.__MICRO_APP_ENVIRONMENT__ || false;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(AuthOptions, "LocalForage", {
/**
* localforage 访问器
* @readonly
* @private
* @static
* @memberof AuthOptions
*/
get: function get() {
if (AuthOptions._LocalForage) {
return AuthOptions._LocalForage;
}
var LocalForage = localforage.createInstance({
// driver: localforage.LOCALSTORAGE, // Forc
name: AuthOptions.LocalforageKey // storeName: AuthOptions.StorageKey,
});
AuthOptions._LocalForage = LocalForage;
return LocalForage;
},
enumerable: false,
configurable: true
});
/**
* 创建 storage
* @static
* @return {*}
* @memberof AuthOptions
*/
AuthOptions.createStorage = function () {
if (!AuthOptions.browser) {
return {
clear: function clear() {
AuthOptions.log('clear');
},
setItem: function setItem(key, value) {
AuthOptions.log('setItem', key, value);
},
removeItem: function removeItem(key) {
AuthOptions.log('removeItem', key);
},
getItem: function getItem(key) {
AuthOptions.log('getItem', key);
}
};
}
var LocalForage = AuthOptions.LocalForage;
return {
clear: LocalForage.clear,
setItem: LocalForage.setItem,
removeItem: LocalForage.removeItem,
getItem: LocalForage.getItem
};
};
/**
* 持久化 mobx-persist
* @static
* @memberof basesOptions
*/
AuthOptions.createHydrate = function () {
var createStorage = AuthOptions.createStorage,
jsonify = AuthOptions.jsonify;
return create({
jsonify: jsonify,
storage: createStorage()
});
};
/**
* 写入校验
* @static
* @memberof AuthOptions
*/
AuthOptions.writeCheck = function () {
if (AuthOptions.micro) {
AuthOptions.log('微应用禁止写入');
throw '微应用禁止写入';
}
};
/** 持久化序列化 */
AuthOptions.jsonify = true;
AuthOptions.LocalforageKey = 'mamba-portal-auth';
AuthOptions._LocalForage = undefined;
return AuthOptions;
}();
export { AuthOptions };
//# sourceMappingURL=options.js.map