提交 a7cae7ca 编写于 作者: Mosher's avatar Mosher

update:更新支持预定义代码表服务缓存

上级 a34f407b
...@@ -39,7 +39,7 @@ export default class CodeListService { ...@@ -39,7 +39,7 @@ export default class CodeListService {
* @type {Map<string,any>} * @type {Map<string,any>}
* @memberof CodeListService * @memberof CodeListService
*/ */
public static codelistCache:Map<string,any> = new Map(); public static codelistCache: Map<string, any> = new Map();
/** /**
* 动态代码表缓存(已完成) * 动态代码表缓存(已完成)
...@@ -47,7 +47,7 @@ export default class CodeListService { ...@@ -47,7 +47,7 @@ export default class CodeListService {
* @type {Map<string,any>} * @type {Map<string,any>}
* @memberof CodeListService * @memberof CodeListService
*/ */
public static codelistCached:Map<string,any> = new Map(); public static codelistCached: Map<string, any> = new Map();
/** /**
* 数据服务基类 * 数据服务基类
...@@ -55,7 +55,7 @@ export default class CodeListService { ...@@ -55,7 +55,7 @@ export default class CodeListService {
* @type {Minorentity} * @type {Minorentity}
* @memberof CodeListService * @memberof CodeListService
*/ */
public entityService:EntityService = new EntityService(); public entityService: EntityService = new EntityService();
/** /**
* 获取代码表服务 * 获取代码表服务
...@@ -79,15 +79,15 @@ export default class CodeListService { ...@@ -79,15 +79,15 @@ export default class CodeListService {
* @returns {Promise<any[]>} * @returns {Promise<any[]>}
* @memberof CodeListService * @memberof CodeListService
*/ */
public async getDataItems(codelist:any,context?:any, data?: any, isloading?: boolean){ public async getDataItems(codelist: any, context?: any, data?: any, isloading?: boolean) {
let dataItems:Array<any> = []; let dataItems: Array<any> = [];
try{ try {
if(codelist.tag && Object.is(codelist.type,"STATIC")){ if (codelist.tag && Object.is(codelist.type, "STATIC")) {
dataItems = await this.getStaticItems(codelist.tag); dataItems = await this.getStaticItems(codelist.tag);
}else{ } else {
dataItems = await this.getItems(codelist.tag,codelist.context,codelist.viewparam,codelist.isloading); dataItems = await this.getItems(codelist.tag, codelist.context, codelist.viewparam, codelist.isloading);
} }
}catch(error){ } catch (error) {
console.warn("代码表加载异常" + error); console.warn("代码表加载异常" + error);
} }
return dataItems; return dataItems;
...@@ -100,8 +100,8 @@ export default class CodeListService { ...@@ -100,8 +100,8 @@ export default class CodeListService {
* @returns {Promise<any[]>} * @returns {Promise<any[]>}
* @memberof CodeListService * @memberof CodeListService
*/ */
public getStaticItems(tag: string):Promise<any[]>{ public getStaticItems(tag: string): Promise<any[]> {
return new Promise((resolve:any,reject:any) =>{ return new Promise((resolve: any, reject: any) => {
const codelist = this.$store.getters.getCodeList(tag); const codelist = this.$store.getters.getCodeList(tag);
if (codelist) { if (codelist) {
let items: Array<any> = [...JSON.parse(JSON.stringify(codelist.items))]; let items: Array<any> = [...JSON.parse(JSON.stringify(codelist.items))];
...@@ -117,33 +117,17 @@ export default class CodeListService { ...@@ -117,33 +117,17 @@ export default class CodeListService {
* @returns {Promise<any[]>} * @returns {Promise<any[]>}
* @memberof CodeListService * @memberof CodeListService
*/ */
public getPredefinedItems(tag: string,data?: any, isloading?: boolean):Promise<any[]>{ public getPredefinedItems(tag: string, data?: any, isloading?: boolean): Promise<any[]> {
return new Promise((resolve:any,reject:any) =>{ return new Promise((resolve: any, reject: any) => {
if(CodeListService.codelistCached.get(`${tag}`)){ this.entityService.getPredefinedCodelist(tag).then((response: any) => {
let items:any = CodeListService.codelistCached.get(`${tag}`).items; if (response && response.status === 200 && response.data && response.data.items) {
if(items.length >0) resolve(items); resolve(response.data.items);
} } else {
const callback:Function = (tag:string,promise:Promise<any>) =>{ resolve([]);
promise.then((res:any) =>{ }
let result:any = res.data; }).catch((error: any) => {
if(result.items && result.items.length > 0){ resolve([]);
CodeListService.codelistCached.set(`${tag}`,{items:result.items}); });
return resolve(result.items);
}else{
return resolve([]);
}
}).catch((result:any) =>{
return reject(result);
})
}
// 加载中,UI又需要数据,解决连续加载同一代码表问题
if(CodeListService.codelistCache.get(`${tag}`)){
callback(tag,CodeListService.codelistCache.get(`${tag}`));
}else{
let result:Promise<any> = this.entityService.getPredefinedCodelist(tag);
CodeListService.codelistCache.set(`${tag}`,result);
callback(tag,result);
}
}) })
} }
...@@ -155,70 +139,77 @@ export default class CodeListService { ...@@ -155,70 +139,77 @@ export default class CodeListService {
* @returns {Promise<any[]>} * @returns {Promise<any[]>}
* @memberof CodeListService * @memberof CodeListService
*/ */
public getItems(tag: string,context:any = {}, data?: any, isloading?: boolean): Promise<any[]> { public getItems(tag: string, context: any = {}, data?: any, isloading?: boolean): Promise<any[]> {
let _this: any = this; let _this: any = this;
if(context && context.srfsessionid){ if (context && context.srfsessionid) {
delete context.srfsessionid; delete context.srfsessionid;
} }
return new Promise((resolve:any,reject:any) =>{ return new Promise((resolve: any, reject: any) => {
this.getService(tag).then((codelist:any) =>{ this.getService(tag).then((codelist: any) => {
if(Object.is(codelist.predefinedType,"RUNTIME")){ let isEnableCache: boolean = codelist.isEnableCache;
this.getPredefinedItems(tag).then((res:any) =>{ let cacheTimeout: any = codelist.cacheTimeout;
resolve(res);
})
return;
}
let isEnableCache:boolean = codelist.isEnableCache;
let cacheTimeout:any = codelist.cacheTimeout;
// 启用缓存 // 启用缓存
if(isEnableCache){ if (isEnableCache) {
const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{ const callback: Function = (context: any = {}, data: any = {}, tag: string, promise: Promise<any>) => {
const callbackKey:string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`; const callbackKey: string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`;
promise.then((result:any) =>{ promise.then((result: any) => {
if(result.length > 0){ if (result.length > 0) {
CodeListService.codelistCached.set(callbackKey,{items:result}); CodeListService.codelistCached.set(callbackKey, { items: result });
CodeListService.codelistCache.delete(callbackKey); CodeListService.codelistCache.delete(callbackKey);
return resolve(result); return resolve(result);
}else{ } else {
return resolve([]); return resolve([]);
} }
}).catch((result:any) =>{ }).catch((result: any) => {
return reject(result); return reject(result);
}) })
} }
// 加载完成,从本地缓存获取 // 加载完成,从本地缓存获取
const key:string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`; const key: string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`;
if(CodeListService.codelistCached.get(key)){ if (CodeListService.codelistCached.get(key)) {
let items:any = CodeListService.codelistCached.get(key).items; let items: any = CodeListService.codelistCached.get(key).items;
if(items.length >0){ if (items.length > 0) {
if(new Date().getTime() <= codelist.getExpirationTime()){ if (new Date().getTime() <= codelist.getExpirationTime()) {
return resolve(items); return resolve(items);
} }
} }
} }
if (codelist) { if (codelist) {
// 加载中,UI又需要数据,解决连续加载同一代码表问题 // 加载中,UI又需要数据,解决连续加载同一代码表问题
if(CodeListService.codelistCache.get(key)){ if (CodeListService.codelistCache.get(key)) {
callback(context,data,tag,CodeListService.codelistCache.get(key)); callback(context, data, tag, CodeListService.codelistCache.get(key));
}else{ } else {
let result:Promise<any> = codelist.getItems(context,data,isloading); let result: Promise<any>;
CodeListService.codelistCache.set(key,result); if (Object.is(codelist.predefinedType, "RUNTIME")) {
result = this.getPredefinedItems(tag);
} else {
result = codelist.getItems(context, data, isloading);
}
CodeListService.codelistCache.set(key, result);
codelist.setExpirationTime(new Date().getTime() + cacheTimeout); codelist.setExpirationTime(new Date().getTime() + cacheTimeout);
callback(context,data,tag,result); callback(context, data, tag, result);
} }
} }
}else{ } else {
if (codelist) { if (codelist) {
codelist.getItems(context,data,isloading).then((result:any) =>{ if (Object.is(codelist.predefinedType, "RUNTIME")) {
this.getPredefinedItems(tag).then((res: any) => {
resolve(res);
}).catch((error: any) => {
Promise.reject([]);
})
} else {
codelist.getItems(context, data, isloading).then((result: any) => {
resolve(result); resolve(result);
}).catch((error:any) =>{ }).catch((error: any) => {
Promise.reject([]); Promise.reject([]);
}) })
}else{ }
} else {
return Promise.reject([]); return Promise.reject([]);
} }
} }
}).catch((error:any) =>{ }).catch((error: any) => {
console.warn("获取代码表异常"); console.warn("获取代码表异常");
return Promise.reject([]); return Promise.reject([]);
}) })
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册