enc-framework
Version:
enc-framework 核心组件.
251 lines (232 loc) • 8.37 kB
JavaScript
import AjmRouter from './router'
import AjmStore from './store'
import axios from "axios";
import UserApi from './api/userApi'
import LoginApi from './api/loginApi'
import SystemApi from './api/systemApi'
import PlatformApi from './api/platformApi'
import AuthApi from './api/authApi'
import AjmConfig from './utils/ajm-config';
import SecurityManager from './manager/SecurityManager';
import Directive from './directive';
import Monitor from './monitor/ijm-monitor';
import TokenMonitor from './monitor/ijm-token-monitor';
// 安装方法
const install = function(Vue,options,callback){
let _this = this;
this.options = options || {};
// 主框架
//this.main = this.options.main;
// 登录地址
this.options.loginPath = this.loginPath = this.options.loginPath;
this.options.baseUrl = this.baseUrl = this.options.baseUrl;
this.options.contextPath = this.contextPath = this.options.contextPath || "";
// 系统名称
this.options.systemCode = this.systemCode = this.options.systemCode || "platform";
this.options.systemName = this.systemName = this.options.systemName || "爱加密基础开发平台";
// 首页地址
this.options.homePath = this.homePath = this.options.homePath || "/_home";
this.options.viewFilePath = this.viewFilePath = this.options.viewFilePath || "/api/file/viewFile/";
this.options.viewFileUrl = this.viewFileUrl = this.options.viewFileUrl || this.baseUrl+this.contextPath+this.viewFilePath;
this.options.logoLinkUrl = this.logoLinkUrl = this.options.logoLinkUrl || this.baseUrl + this.contextPath + this.viewFilePath;
this.options.oauthUrl = this.oauthUrl = this.options.oauthUrl || this.options.baseUrl || "";
this.options.isRemoteOauth = this.isRemoteOauth = this.options.isRemoteOauth || false;
this.options.autoUpdateToken = this.autoUpdateToken = this.options.autoUpdateToken || false;
let defaultClientInfo = {
clientId:"admin",
clientSecret:"secret"
};
//初始化 是否自定义cookie Patch , 是开启登录IP限制
let checkTokenPath =false, checkIP=false,cookiePath ='/';
if(window.config){
if(window.config.checkTokenPath){
checkTokenPath=window.config.checkTokenPath
}
if(window.config.checkFilterIp){
checkIP=window.config.checkFilterIp
}
}else if(localStorage.getItem('_CONFIG')){
let configInfo = JSON.parse(localStorage.getItem('_CONFIG'));
if(configInfo.checkTokenPath){
checkIP=configInfo.checkTokenPath
}
}
this.options.checkTokenPath = this.checkTokenPath= checkTokenPath||this.options.checkTokenPath;
this.options.checkIP = this.checkIP = checkIP || this.options.checkIP
if(this.options.checkTokenPath){
let pathname = window.location.pathname;
if(pathname.length>1){
if(pathname.indexOf('/')>0){
cookiePath=pathname.substr(0,pathname.indexOf('/',pathname.indexOf('/')+1))
}else if(pathname.indexOf('/')==0){
cookiePath=pathname
}
}
}
this.options.cookiePath=cookiePath;
this.options.clientInfo = this.clientInfo = this.options.clientInfo || defaultClientInfo;
// api选项
let apiOptions = {
baseUrl: this.baseUrl,
contextPath: this.contextPath,
loginPath : this.loginPath,
homePath : this.homePath,
viewFilePath : this.viewFilePath,
viewFileUrl : this.viewFileUrl,
// 授权地址
oauthUrl: this.oauthUrl,
// 是否远程授权
isRemoteOauth: this.isRemoteOauth
}
// 创建存储
this.ajmStore = new AjmStore(this.options);
// 获取存储对象
let store = this.ajmStore.getStore();
// 创建用户api接口
this.loginApi = new LoginApi(store, this.options);
// api接口
this.userApi = new UserApi(store, this.options);
// 系统接口
this.systemApi = new SystemApi(store, this.options);
// 平台信息接口
this.platformApi = new PlatformApi(store,this.options);
// 授权接口
this.authApi = new AuthApi(store, this.options);
// 安全管理对象
let securityManager = new SecurityManager(store);
// 创建路由对象
this.ajmRouter = new AjmRouter(store,{
main: this.options.main,
// 登录地址
loginPath: this.options.loginPath,
// 授权地址
oauthUrl: this.options.oauthUrl,
// 是否远程授权
isRemoteOauth: this.options.isRemoteOauth,
//客户端信息
clientInfo: this.options.clientInfo,
// 用户接口
userApi: this.userApi,
loginApi: this.loginApi,
homePath: this.homePath,
});
//
Vue.use(Directive,store);
// 上下文信息
Vue.prototype.$context = this.options;
// 挂载loginApi对象
Vue.prototype.$loginApi = this.loginApi;
// 挂载userApi对象
Vue.prototype.$userApi = this.userApi;
// 挂载systemApi对象
Vue.prototype.$systemApi = this.systemApi;
// 挂载authApi对象
Vue.prototype.$authApi = this.authApi;
// 挂载安全管理对象
Vue.prototype.$securityManager = securityManager;
// 更改平台配置信息
let changePlatformConfigInfo = function(configureInfo) {
if(!configureInfo){
return;
}
if(configureInfo.systemName){
document.title = configureInfo.systemName;
}
let faviconPath = configureInfo.browserLogo;
var faviconUrl = AjmConfig.getApiUrl() +
AjmConfig.get("BASE_CONTEXT_PATH") +
"/common/file/viewFile/" + faviconPath;
var link = document.querySelector("link[rel*='icon']");
if(!link){
link = document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
if(!faviconPath){
link.href = "static/images/favicon.ico";
}else{
link.href = faviconUrl;
}
document.getElementsByTagName('head')[0].appendChild(link);
}else{
if(!faviconPath){
link.href = "static/images/favicon.ico";
}else{
link.href = faviconUrl;
}
}
};
Vue.prototype.changeSystemInfo = changePlatformConfigInfo;
//执行token监听
if(this.autoUpdateToken){
let monitor = new Monitor(this.loginApi);
Vue.prototype.$monitor=monitor
}
//token有请求自动更新
//是否启动请求自动更新token
if(AjmConfig.get('refshToken')){
let tokenOptions={
refshTime:AjmConfig.get('refshTokenTime')
}
let tokenMonitor = new TokenMonitor(this.loginApi,tokenOptions);
window.$tokenMonitor=Vue.prototype.$tokenMonitor=tokenMonitor
}
//加载外置配置文件
// axios.get("./static/config.json").then((result) => {
// for (var key in result.data) {
// localStorage.setItem(key, result.data[key]);
// }
// }).catch((error) => {
// console.log("读取配置文件失败!")
// });
// 初始化
function _init() {
_this.platformApi.loadPlatformInfo(_this.options.systemCode).then(data => {
if(data.status != 500){
var newPlatformInfo = Object.assign({}, data)
//配置平台基本信息
if(window.config&&window.config.platformInfo&&window.config.platformInfo.configureVO){
newPlatformInfo.configureVO=window.config.platformInfo.configureVO
}
store.dispatch("platform/savePlatformInfo", newPlatformInfo);
store.dispatch("system/saveSystemInfo", newPlatformInfo.system);
changePlatformConfigInfo(newPlatformInfo.configureVO);
}
}).catch((err) => {
document.title = _this.systemName;
}).then(() =>{
if (callback) {
callback();
}
});
}
// 执行初始化 先检查是否设置了IP限制
if(_this.checkIP){
_this.platformApi.checkFilterIp().then(data => {
if(data&&data.result){
_init();
}else{
store.dispatch("user/saveUserIp",data)
if (callback) {
callback()
}
}
})
}else{
_init();
}
}
const ajmCore = {
install,
getRouter(){
return this.ajmRouter;
},
getStore(){
return this.ajmStore;
},
completed(){
this.ajmRouter.completed();
}
}
// 导出路由,存储对象
export {AjmRouter, AjmStore};
export default ajmCore;