提交 b4112668 编写于 作者: tony001's avatar tony001

调整数据服务基类

上级 9c309f51
import { Http, Util, HttpResponse } from '../utils'; import { Store } from 'vuex';
import { DBService } from './db-service'; import { Http } from '../utils';
import CodeListService from "@service/app/codelist-service";
/** /**
* 实体服务基类 * 实体服务基类
* *
* @export * @export
* @class EntityServiceBase * @class EntityService
*/ */
export class EntityServiceBase { export default class EntityService {
/** /**
* 实体本地数据库 * Vue 状态管理器
* *
* @protected * @private
* @type {DBService} * @type {(any | null)}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected db: DBService; private $store: Store<any> | null = null;
/** /**
* http请求工具实例 * 获取实体数据服务
* *
* @protected * @protected
* @type {Http} * @param {string} name 实体名称
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
protected http: Http = Http.getInstance(); public getService(name: string): Promise<any> {
return (window as any)['entityServiceRegister'].getService(name);
}
/** /**
* 所有从实体 * 零时储存,localStorage指向
* *
* @protected * @public
* @type {*} * @type {(string)}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected allMinorAppEntity: any = {}; public tempStorage:any;
/** /**
* 当前实体主键标识 * 当前DE标识
* *
* @protected * @public
* @type {(string)} * @type {(string)}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected readonly key: string = ''; public APPDEKEY:string= '';
/** /**
* 当前实体名称 * 当前APPDE标识
* *
* @protected * @public
* @type {(string)} * @type {(string)}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected readonly dePath: string = ''; public APPLYDEKEY:string= '';
/** /**
* 实体名称 * 当前NAME
* *
* @protected * @public
* @type {string} * @type {(string)}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected readonly deName: string; public APPDENAME:string= '';
/** /**
* 当前实体主信息标识 * 当前主信息名
* *
* @protected * @public
* @type {(string)} * @type {(string)}
* @memberof EntityServiceBase * @memberof EntityService
*/
public APPDETEXT:string= '';
/**
* 当前系统名
*
* @public
* @type {(string)}
* @memberof EntityService
*/
public SYSTEMNAME:string= '';
/**
* 当前应用名
*
* @public
* @type {(string)}
* @memberof EntityService
*/
public APPNAME:string= '';
/**
* Creates an instance of EntityService.
*
* @param {*} [opts={}]
* @memberof EntityService
*/ */
protected readonly text: string = ''; constructor(opts: any = {}) {
this.$store = opts.$store;
this.tempStorage = localStorage;
this.initBasicData();
}
/** /**
* 请求根路径 * 获取状态管理器
* *
* @protected * @returns {(any | null)}
* @type {string} * @memberof EntityService
* @memberof EntityServiceBase
*/ */
protected readonly rootUrl: string = ''; public getStore(): Store<any> | null {
return this.$store;
}
/** /**
* 当前应用名 * 获取代码表
* *
* @protected * @memberof EntityService
* @type {string}
* @memberof EntityServiceBase
*/ */
protected readonly appName: string = ''; public getCodeList(tag:string,codelistType:string,context:any = {},param:any ={}){
return new Promise((resolve:any,reject:any) =>{
if(tag && Object.is(codelistType,"STATIC")){
let returnItems:Array<any> = [];
const codelist = (this.getStore() as Store<any>).getters.getCodeList(tag);
if (codelist) {
returnItems = [...JSON.parse(JSON.stringify(codelist.items))];
} else {
console.log(`----${tag}----代码表不存在`);
}
resolve(returnItems);
}else if(tag && Object.is(codelistType,"DYNAMIC")){
let codeListService = new CodeListService({ $store: this.$store });
codeListService.getItems(tag,context,param).then((res:any) => {
resolve(res);
}).catch((error:any) => {
reject(`${tag}代码表不存在`);
console.log(`----${tag}----代码表不存在`);
});
}
})
}
/** /**
* 当前系统名 * 初始化基础数据
* *
* @protected * @memberof EntityService
* @type {string}
* @memberof EntityServiceBase
*/ */
protected readonly systemName: string = ''; public initBasicData(){
}
/** /**
* Creates an instance of EntityServiceBase. * Select接口方法
* @param {string} deName *
* @memberof EntityServiceBase * @param {*} [context={}]
* @param {*} [data={}]
* @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof EntityService
*/ */
constructor(deName: string) { public async Select(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
this.deName = deName; return this.SelectTemp(context,data,isloading);
this.db = new DBService(deName);
} }
/** /**
* 设置从实体本地缓存 * SelectTemp接口方法
* *
* @protected
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data] * @param {*} [data={}]
* @returns {Promise<void>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
*/ * @memberof EntityService
protected async setMinorLocalCache(context: any = {}, data: any): Promise<void> { */
if (data) { public async SelectTemp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
for (const key in this.allMinorAppEntity) { if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME),'undefined')){
if (data.hasOwnProperty(key) && this.allMinorAppEntity.hasOwnProperty(key)) { let result:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME) as any);
const config: any = this.allMinorAppEntity[key]; if(result){
await this.setLocalCache(context, data[key], config.name); let tempResult:any = result.filter((item:any) =>{
} return Object.is(item[this.APPDEKEY],data[this.APPDEKEY]);
} })
let tempResultData:any = tempResult.length>0?tempResult[0]:Object.assign({},data);
return {"status":200,"data":tempResultData};
}else{
return {"status":500,"data":null};
}
} }
} }
/** /**
* 设置本地缓存 * CreateTemp接口方法
* *
* @protected
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} data * @param {*} [data={}]
* @param {string} [entityName] 指定实体 * @param {boolean} [isloading]
* @returns {Promise<boolean>} * @returns {Promise<any>}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected async setLocalCache(context: any = {}, data: any, entityName?: string): Promise<boolean> { public async CreateTemp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
if (data) { if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME),'undefined')){
if (entityName) { let tempData:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME) as any);
const service: EntityServiceBase = await this.getService(entityName); data.srffrontuf = "0";
if (service) { tempData.push(data);
return service.setLocalCache(context, data); this.tempStorage.setItem(context.srfsessionkey+'_'+this.APPDENAME,JSON.stringify(tempData));
} return {"status":200,"data":data};
} else { }else{
data = Util.deepCopy(data); return {"status":200,"data":{}};
return this.db.set({ srfsessionkey: context.srfsessionkey, data });
}
} }
return false;
} }
/** /**
* 获取所有从实体本地缓存数据 * GetTemp接口方法
* *
* @protected
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}]
* @param {boolean} [isloading]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected async getMinorLocalCache(context: any = {}): Promise<any> { public async GetTemp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
const data: any = {}; if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME),'undefined')){
for (const key in this.allMinorAppEntity) { let result:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME) as any);
if (this.allMinorAppEntity.hasOwnProperty(key)) { if(result){
const config: any = this.allMinorAppEntity[key]; let tempResult:any = result.filter((item:any) =>{
const results: any[] = await this.getLocalCache(context, config.name); return Object.is(item[this.APPDEKEY],data[this.APPDEKEY]);
if (results) { })
results.forEach((item: any) => { let tempResultData:any = tempResult.length>0?tempResult[0]:Object.assign({},data);
if (item.srfuf == 0) { return {"status":200,"data":tempResultData};
delete item[this.key]; }else{
} return {"status":500,"data":null};
}); }
}
data[key] = results || [];
}
} }
return data;
} }
/** /**
* 获取本地缓存数据 * Update接口方法
* *
* @protected
* @param {*} [context={}] * @param {*} [context={}]
* @param {string} [entityName] 指定实体 * @param {*} [data={}]
* @param {boolean} [isloading]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
protected async getLocalCache(context: any = {}, entityName?: string): Promise<any> { public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
if (entityName) { return this.UpdateTemp(context,data,isloading);
const service: EntityServiceBase = await this.getService(entityName);
if (service) {
return service.getLocalCache(context);
}
} else {
const item: any = await this.db.get(context.srfsessionkey);
if (item) {
return item.data;
}
}
} }
/** /**
* 删除本地缓存 * UpdateTempMajor接口方法
* *
* @protected
* @param {*} [context={}] * @param {*} [context={}]
* @returns {Promise<boolean>} * @param {*} [data={}]
* @memberof EntityServiceBase * @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof EntityService
*/ */
protected async removeLocalCache(context: any = {}, entityName?: string): Promise<boolean> { public async UpdateTempMajor(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
if (entityName) { return this.Update(context,data,isloading);
const service: EntityServiceBase = await this.getService(entityName);
if (service) {
return service.removeLocalCache(context);
}
} else {
return this.db.remove(context.srfsessionkey);
}
return false;
} }
/** /**
* Select接口方法 * Save接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async Select(context: any = {}, data: any = {}): Promise<HttpResponse> { public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「Select」未实现' }); return { status: 500, data: { title: '失败', message: '预置实体行为 Save 未实现' }};
} }
/** /**
* SelectTemp接口方法(暂未支持) * UpdateTemp接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
*/ * @memberof EntityService
public async SelectTemp(context: any = {}, data: any = {}): Promise<HttpResponse> { */
if (context.srfsessionkey) { public async UpdateTemp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
const result: any[] = await this.getLocalCache(context) || []; if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME),'undefined')){
if (result) { let result:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME) as any);
return new HttpResponse(200, result); let tempData:Array<any> = [];
if(result && result.length >0){
result.forEach((item:any) => {
let singleData:any = {};
if(Object.is(item[this.APPDEKEY],data[this.APPDEKEY])){
Object.keys(item).forEach((field:any) =>{
singleData[field] = data[field]?data[field]:item[field];
})
}
if(Object.keys(singleData).length >0){
tempData.push(singleData);
}else{
tempData.push(item);
}
});
this.tempStorage.setItem(context.srfsessionkey+'_'+this.APPDENAME,JSON.stringify(tempData));
return {"status":200,"data":data};
}else{
return {"status":500,"data":null};
} }
} }
return new HttpResponse(500, null, { code: 5001 });
} }
/** /**
* CreateTemp接口方法 * RemoveTemp接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
*/ * @memberof EntityService
public async CreateTemp(context: any = {}, data: any = {}): Promise<HttpResponse> { */
if (data && context.srfsessionkey) { public async RemoveTemp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
const result: any[] = await this.getLocalCache(context) || []; if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME),'undefined')){
data.srfuf = 0; let result:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME) as any);
result.push(data); if(result){
if (await this.setLocalCache(context, result)) { let tempResult:any = result.filter((item:any) =>{
return new HttpResponse(200, data); // return !( Object.is(item[this.APPDEKEY],data[this.APPDEKEY]) && Object.is(item[this.APPDETEXT],data[this.APPDETEXT]));
} return !Object.is(item[this.APPDEKEY],data[this.APPDEKEY]);
})
this.tempStorage.setItem(context.srfsessionkey+'_'+this.APPDENAME,JSON.stringify(tempResult));
return {"status":200,"data":data};
}else{
return {"status":500,"data":null};
}
} }
return new HttpResponse(500, null, { code: 5003 });
} }
/** /**
* GetTemp接口方法 * CheckKey接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
*/ * @memberof EntityService
public async GetTemp(context: any = {}, data: any = {}): Promise<HttpResponse> { */
if (context.srfsessionkey) { public async CheckKey(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
const result: any[] = await this.getLocalCache(context) || []; if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME),'undefined')){
const self: any = result.find((item: any) => Object.is(item[this.key], context[this.deName])); let result:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_'+this.APPDENAME) as any);
if (self) { if(result){
return new HttpResponse(200, self); let flag:boolean = false;
} result.forEach((item:any) => {
if(Object.is(item[this.APPDEKEY],data[this.APPDEKEY])){
flag = true;
}
});
return {"status":200,"data":flag};
}else{
return {"status":500,"data":null};
}
} }
return new HttpResponse(500, null, { code: 5001 });
} }
/** /**
* UpdateTemp接口方法 * GetDraft接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async UpdateTemp(context: any = {}, data: any = {}): Promise<HttpResponse> { public async GetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
if (context.srfsessionkey) { return this.GetDraftTemp(context,data,isloading);
const result: any[] = await this.getLocalCache(context) || [];
const num: number = result.findIndex((item: any) => Object.is(item[this.key], data[this.key]));
if (num !== -1) {
result[num] = data;
if (await this.setLocalCache(context, result)) {
return new HttpResponse(200, data);
}
}
}
return new HttpResponse(500, null, { code: 5004 });
} }
/** /**
* RemoveTemp接口方法 * Remove接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async RemoveTemp(context: any = {}, data: any = {}): Promise<HttpResponse> { public async Remove(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
if (context.srfsessionkey) { return this.RemoveTemp(context,data,isloading);
const result: any[] = await this.getLocalCache(context) || [];
const str: string = context[this.deName];
if (str && !Object.is(str, '')) {
const keys: string[] = str.split(';');
keys.forEach((key: string) => {
const num: number = result.findIndex((item: any) => item[this.key] == key);
if (num !== -1) {
result.splice(num, 1);
}
});
if (await this.setLocalCache(context, result)) {
return new HttpResponse(200, data);
}
}
}
return new HttpResponse(500, null, { code: 5005 });
} }
/** /**
* UpdateTempMajor接口方法 * CreateTempMajor接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async UpdateTempMajor(context: any = {}, data: any = {}): Promise<HttpResponse> { public async CreateTempMajor(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.Update(context, data); return this.Create(context,data,isloading);
} }
/** /**
* CreateTempMajor接口方法 * RemoveTempMajor接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async CreateTempMajor(context: any = {}, data: any = {}): Promise<HttpResponse> { public async RemoveTempMajor(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.Create(context, data); return this.Remove(context,data,isloading);
} }
/** /**
* RemoveTempMajor接口方法 * Get接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async RemoveTempMajor(context: any = {}, data: any = {}): Promise<HttpResponse> { public async Get(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.Remove(context, data); return this.GetTemp(context,data,isloading);
} }
/** /**
...@@ -384,35 +430,38 @@ export class EntityServiceBase { ...@@ -384,35 +430,38 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async GetDraftTempMajor(context: any = {}, data: any = {}): Promise<HttpResponse> { public async GetDraftTempMajor(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.GetDraft(context, data); return this.GetDraft(context,data,isloading);
} }
/** /**
* GetTempMajor接口方法 * Create接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async GetTempMajor(context: any = {}, data: any = {}): Promise<HttpResponse> { public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.Get(context, data); return this.CreateTemp(context,data,isloading);
} }
/** /**
* FetchTempDefault接口方法 * GetTempMajor接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FetchTempDefault(context: any = {}, data: any = {}): Promise<HttpResponse> { public async GetTempMajor(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「FetchTempDefault」未实现' }); return this.Get(context,data,isloading);
} }
/** /**
...@@ -420,203 +469,247 @@ export class EntityServiceBase { ...@@ -420,203 +469,247 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async GetDraftTemp(context: any = {}, data: any = {}): Promise<HttpResponse> { public async GetDraftTemp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
const tempData = { srfsessionkey: data.srfsessionkey }; let tempData = {srfsessionkey:context.srfsessionkey};
Object.defineProperty(tempData, this.key, { Object.defineProperty(tempData, this.APPDEKEY, {
enumerable: true, enumerable: true,
value: data[this.key] value: data[this.APPDEKEY]
}); });
Object.assign(data, tempData); Object.assign(data,tempData);
return new HttpResponse(200, data); return {"status":200,"data":data};
} }
/** /**
* Update接口方法 * FetchDefault接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async Update(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FetchDefault(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「Update」未实现' }); return { status: 500, data: { title: '失败', message: '预置实体行为 FetchDefault 未实现' }};
} }
/** /**
* Save接口方法 * FetchTempDefault接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async Save(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FetchTempDefault(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「Save」未实现' }); return { status: 500, data: { title: '失败', message: '预置实体行为 FetchDefault 未实现' }};
} }
/** /**
* CheckKey接口方法 * FilterUpdate接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async CheckKey(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FilterUpdate(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
if (context.srfsessionkey) { return this.Update(context,data,isloading);
const result = await this.getLocalCache(context)
if (result) {
return new HttpResponse(200, true);
}
}
return new HttpResponse(200, false);
} }
/** /**
* GetDraft接口方法 * FilterSearch接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async GetDraft(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FilterSearch(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「GetDraft」未实现' }); return this.FetchDefault(context,data,isloading);
} }
/** /**
* Remove接口方法 * FilterGet接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async Remove(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FilterGet(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「Remove」未实现' }); return this.Get(context,data,isloading);
} }
/** /**
* Get接口方法 * FilterCreate接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async Get(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FilterCreate(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「Get」未实现' }); return this.Create(context,data,isloading);
} }
/** /**
* Create接口方法 * FilterGetDraft接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async Create(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FilterGetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「Create」未实现' }); return this.GetDraft(context,data,isloading);
} }
/** /**
* FetchDefault接口方法 * FilterRemove接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FetchDefault(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FilterRemove(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return new HttpResponse(500, null, { code: 100, message: '预置实体行为「FetchDefault」未实现' }); return this.Remove(context,data,isloading);
} }
/** /**
* FilterUpdate接口方法 * FilterFetch接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FilterUpdate(context: any = {}, data: any = {}): Promise<HttpResponse> { public async FilterFetch(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.Update(context, data); return this.FetchDefault(context,data,isloading);
} }
/** /**
* FilterSearch接口方法 * ImportData接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FilterSearch(context: any = {}, data: any = {}): Promise<HttpResponse> { public async ImportData(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.FetchDefault(context, data); let _data:Array<any> = [];
if (data && data.importData) _data = data.importData;
return Http.getInstance().post(`/${this.APPDENAME}/import?config=${data.name}`,_data,isloading);
} }
/** /**
* FilterGet接口方法 * createBatch接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FilterGet(context: any = {}, data: any = {}): Promise<HttpResponse> { public async createBatch(context: any = {},data: any, isloading?: boolean): Promise<any> {
return this.Get(context, data); return Http.getInstance().post(`/${this.APPDENAME}/batch`,data,isloading);
} }
/** /**
* FilterCreate接口方法 * saveBatch接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FilterCreate(context: any = {}, data: any = {}): Promise<HttpResponse> { public async saveBatch(context: any = {},data: any, isloading?: boolean): Promise<any> {
return this.Create(context, data); return Http.getInstance().post(`/${this.APPDENAME}/savebatch`,data,isloading);
} }
/** /**
* FilterGetDraft接口方法 * updateBatch接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FilterGetDraft(context: any = {}, data: any = {}): Promise<HttpResponse> { public async updateBatch(context: any = {},data: any, isloading?: boolean): Promise<any> {
return this.GetDraft(context, data); return Http.getInstance().put(`/${this.APPDENAME}/batch`,data,isloading);
} }
/** /**
* FilterRemove接口方法 * removeBatch接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FilterRemove(context: any = {}, data: any = {}): Promise<HttpResponse> { public async removeBatch(context: any = {},data: any, isloading?: boolean): Promise<any> {
return this.Remove(context, data); return Http.getInstance().delete(`/${this.APPDENAME}/batch`,isloading,data[this.APPDEKEY]);
} }
/** /**
* FilterFetch接口方法 * getDataInfo接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/
public async getDataInfo(context: any = {},data: any, isloading?: boolean):Promise<any> {
if(context[this.APPLYDEKEY]){
return this.Get(context,data, isloading);
}
}
/**
* getDynaModel(获取动态模型)接口方法
*
* @param {*} [context={}]
* @param {*} [data]
* @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof EntityService
*/ */
public async FilterFetch(context: any = {}, data: any = {}): Promise<HttpResponse> { public async getDynaModel(context: any = {},data: any, isloading?: boolean):Promise<any> {
return this.FetchDefault(context, data); if(data && data.configType && data.targetType){
return Http.getInstance().get(`/configs/${data.configType}/${data.targetType}`);
}
}
/**
* setDynaModel(设置动态模型)接口方法
*
* @param {*} [context={}]
* @param {*} [data]
* @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof EntityService
*/
public async setDynaModel(context: any = {},data: any, isloading?: boolean):Promise<any> {
if(data && data.configType && data.targetType){
return Http.getInstance().put(`/configs/${data.configType}/${data.targetType}`,{model:data.model});
}
} }
/** /**
...@@ -624,47 +717,63 @@ export class EntityServiceBase { ...@@ -624,47 +717,63 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @param {*} [localdata]
*/ * @returns {Promise<any>}
public async WFStart(context: any = {}, data: any = {}): Promise<HttpResponse> { * @memberof EntityService
return this.http.post(`/${this.dePath}/wfstart`, { wfdata: data }); */
public async WFStart(context: any = {},data: any = {}, isloading?: boolean,localdata?:any): Promise<any> {
if(localdata && Object.keys(localdata).length > 0){
const requestData:any = {};
Object.assign(requestData,{activedata:data});
Object.assign(requestData,localdata);
return Http.getInstance().post(`/wfcore/${this.SYSTEMNAME}-app-${this.APPNAME}/${this.APPDENAME}/${data[this.APPDEKEY]}/process-instances`,requestData,isloading);
}else{
const requestData:any = {};
Object.assign(requestData,{wfdata:data});
return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfstart`,requestData,isloading);
}
} }
/** /**
* WFClose接口方法 * WFClose接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async WFClose(context: any = {}, data: any = {}): Promise<HttpResponse> { public async WFClose(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.http.post(`/${this.dePath}/${context[this.key]}/wfclose`, data); return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfclose`,data,isloading);
} }
/** /**
* WFMarkRead接口方法 * WFMarkRead接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async WFMarkRead(context: any = {}, data: any = {}): Promise<HttpResponse> { public async WFMarkRead(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.http.post(`/${this.dePath}/${context[this.key]}/wfmarkread`, data); return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfmarkread`,data,isloading);
} }
/** /**
* WFGoto接口方法 * WFGoto接口方法
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async WFGoto(context: any = {}, data: any = {}): Promise<HttpResponse> { public async WFGoto(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.http.post(`/${this.dePath}/${context[this.key]}/wfgoto`, data); return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfgoto`,data,isloading);
} }
/** /**
...@@ -672,11 +781,12 @@ export class EntityServiceBase { ...@@ -672,11 +781,12 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async WFRollback(context: any = {}, data: any = {}): Promise<HttpResponse> { public async WFRollback(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.http.post(`/${this.dePath}/${context[this.key]}/wfrollback`, data); return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfrollback`,data,isloading);
} }
/** /**
...@@ -684,11 +794,12 @@ export class EntityServiceBase { ...@@ -684,11 +794,12 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async WFRestart(context: any = {}, data: any = {}): Promise<HttpResponse> { public async WFRestart(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.http.post(`/${this.dePath}/${context[this.key]}/wfrestart`, data); return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfrestart`,data,isloading);
} }
/** /**
...@@ -696,35 +807,26 @@ export class EntityServiceBase { ...@@ -696,35 +807,26 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async WFReassign(context: any = {}, data: any = {}): Promise<HttpResponse> { public async WFReassign(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return this.http.post(`/${this.dePath}/${context[this.key]}/wfreassign`, data); return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfreassign`,data,isloading);
} }
/** /**
* WFSubmit接口方法 * WFGetWorkFlow接口方法(获取工作流定义)
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @param {*} [wfdata] * @param {boolean} [isloading]
* @returns {Promise<HttpResponse>} * @returns {Promise<any>}
* @memberof EntityServiceBase * @memberof EntityService
*/ */
public async WFSubmit(context: any = {}, data: any = {}, wfdata?: any): Promise<HttpResponse> { public async WFGetWorkFlow(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
try { return Http.getInstance().get(`/wfcore/${this.SYSTEMNAME}-app-${this.APPNAME}/${this.APPDENAME}/process-definitions`);
let res: any;
if (wfdata && Object.keys(wfdata).length !== 0) {
const _data: any = { ...{ activedata: data }, ...wfdata };
res = await this.http.post(`/wfcore/${this.systemName}-app-${this.appName}/${this.dePath}/${data[this.key]}/tasks/${wfdata['taskId']}`, _data);
} else {
res = await this.http.post(`/${this.dePath}/${context[this.key]}/wfsubmit`, data);
}
return res;
} catch (res) {
return new HttpResponse(res.status, null);
}
} }
/** /**
...@@ -732,16 +834,12 @@ export class EntityServiceBase { ...@@ -732,16 +834,12 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async WFGetWFStep(context: any = {}, data: any = {}): Promise<HttpResponse> { public async WFGetWFStep(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
try { return Http.getInstance().get(`/wfcore/${this.SYSTEMNAME}-app-${this.APPNAME}/${this.APPDENAME}/process-definitions-nodes`);
const res = await this.http.get(`/wfcore/${this.systemName}-app-${this.appName}/${this.dePath}/process-definitions-nodes`);
return res;
} catch (res) {
return new HttpResponse(res.status, null);
}
} }
/** /**
...@@ -749,16 +847,12 @@ export class EntityServiceBase { ...@@ -749,16 +847,12 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async GetWFLink(context: any = {}, data: any = {}): Promise<HttpResponse> { public async GetWFLink(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
try { return Http.getInstance().get(`/wfcore/${this.SYSTEMNAME}-app-${this.APPNAME}/${this.APPDENAME}/${context[this.APPLYDEKEY]}/usertasks/${data['taskDefinitionKey']}/ways`);
const res = await this.http.get(`/wfcore/${this.systemName}-app-${this.appName}/${this.dePath}/${context[this.deName]}/usertasks/${data['taskDefinitionKey']}/ways`);
return res;
} catch (res) {
return new HttpResponse(res.status, null);
}
} }
/** /**
...@@ -766,28 +860,131 @@ export class EntityServiceBase { ...@@ -766,28 +860,131 @@ export class EntityServiceBase {
* *
* @param {*} [context={}] * @param {*} [context={}]
* @param {*} [data={}] * @param {*} [data={}]
* @returns {Promise<HttpResponse>} * @param {boolean} [isloading]
* @memberof EntityServiceBase * @returns {Promise<any>}
* @memberof EntityService
*/ */
public async GetWFHistory(context: any = {}, data: any = {}): Promise<HttpResponse> { public async GetWFHistory(context: any = {},data: any = {}, isloading?: boolean):Promise<any> {
try { return Http.getInstance().get(`/wfcore/${this.SYSTEMNAME}-app-${this.APPNAME}/${this.APPDENAME}/${context[this.APPLYDEKEY]}/process-instances/alls/history`);
const res = await this.http.get(`/wfcore/${this.systemName}-app-${this.appName}/${this.dePath}/${context[this.deName]}/process-instances/alls/history`); }
return res;
} catch (res) { /**
return new HttpResponse(res.status, null); * WFSubmit接口方法
*
* @param {*} [context={}]
* @param {*} [data={}]
* @param {boolean} [isloading]
* @param {*} [localdata]
* @returns {Promise<any>}
* @memberof EntityService
*/
public async WFSubmit(context: any = {},data: any = {}, isloading?: boolean,localdata?:any): Promise<any> {
if(localdata && Object.keys(localdata).length > 0){
const requestData:any = {};
if(data.viewparams){
delete data.viewparams;
}
Object.assign(requestData,{activedata:data});
Object.assign(requestData,localdata);
return Http.getInstance().post(`/wfcore/${this.SYSTEMNAME}-app-${this.APPNAME}/${this.APPDENAME}/${data[this.APPDEKEY]}/tasks/${localdata['taskId']}`,requestData,isloading);
}else{
const requestData:any = {};
if(data.srfwfmemo){
requestData.srfwfmemo = JSON.parse(JSON.stringify(data)).srfwfmemo;
delete data.srfwfmemo;
}
if(data.viewparams){
delete data.viewparams;
}
Object.assign(requestData,{wfdata:data});
Object.assign(requestData,{opdata:{srfwfiatag:context.srfwfiatag,srfwfstep:context.srfwfstep}});
return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/wfsubmit`,requestData,isloading);
} }
} }
/** /**
* 获取其他实体服务 * WFGetProxyData接口方法
* *
* @protected * @param {*} [context={}]
* @param {string} name 实体名称 * @param {*} [data={}]
* @param {boolean} [isloading]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof EntityServiceBase * @memberof EntityService
*/
public async WFGetProxyData(context: any = {},data: any = {}, isloading?: boolean): Promise<any>{
return Http.getInstance().get(`/${this.APPDENAME}/${context[this.APPLYDEKEY]}/wfgetproxydata`,data,isloading);
}
/**
* 测试数据是否在工作流中
*
* @param context
* @param data
* @param isloading
*/
public async testDataInWF(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
if(!context.stateField || !context.stateValue) return false;
if(context.stateValue == data[context.stateField]){
return true;
}
return false;
}
/**
* 测试当前用户是否提交过工作流
*
* @param context
* @param data
* @param isloading
*/
public async testUserWFSubmit(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return true;
}
/**
* 测试当前用户是否存在待办列表
*
* @param context
* @param data
* @param isloading
*/
public async testUserExistWorklist(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
const requestData:any = {};
Object.assign(requestData,{wfdata:data});
return Http.getInstance().post(`/${this.APPDENAME}/${data[this.APPDEKEY]}/testuserexistworklist`,requestData,isloading);
}
/**
* 获取所有应用数据
*
* @param context
* @param data
* @param isloading
*/
public async getAllApp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return Http.getInstance().get(`uaa/access-center/app-switcher/default`,data,isloading);
}
/**
* 更新已选择的应用
*
* @param context
* @param data
* @param isloading
*/
public async updateChooseApp(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return Http.getInstance().put(`uaa/access-center/app-switcher/default`,data,isloading);
}
/**
* 修改密码
*
* @param context
* @param data
* @param isloading
*/ */
protected getService(name: string): Promise<any> { public async changPassword(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
return window.appEntityServiceConstructor.getService(name); return Http.getInstance().post(`v7/changepwd`,data,isloading);
} }
} }
\ No newline at end of file
import axios, { AxiosResponse } from 'axios'; import axios, { AxiosResponse } from 'axios';
import qs from 'qs'; import qs from 'qs';
import { HttpResponse } from './http-response';
import { Util } from '../util/util';
/** /**
* Http net 对象 * Http net 对象
* 调用 getInstance() 获取实例 * 调用 getInstance() 获取实例
...@@ -12,158 +9,163 @@ import { Util } from '../util/util'; ...@@ -12,158 +9,163 @@ import { Util } from '../util/util';
export class Http { export class Http {
/** /**
* 唯一实例 * 获取 Http 单例对象
* *
* @private
* @static * @static
* @type {Http} * @returns {Http}
* @memberof Http * @memberof Http
*/ */
private static readonly instance: Http = new Http(); public static getInstance(): Http {
if (!Http.Http) {
Http.Http = new Http();
}
return this.Http;
}
/** /**
* Creates an instance of Http. * 单例变量声明
* 私有构造,拒绝通过 new 创建对象 *
* * @private
* @static
* @type {Http}
* @memberof Http * @memberof Http
*/ */
constructor() { private static Http: Http;
if (Http.instance) {
return Http.instance;
}
}
/** /**
* 获取 * 统计加载
* *
* @param {string} url * @type {number}
* @param {*} [params]
* @returns {Promise<any>}
* @memberof Http * @memberof Http
*/ */
public get(url: string, params?: any): Promise<any> { private loadingCount: number = 0;
if (params) {
params = this.handleRequestData(params);
if ((Object.keys(params)).length > 0) {
let tempParam: any = {};
let sort: any = null;
Object.keys(params).forEach((item: any) => {
if (params[item] || Object.is(params[item], 0)) {
if (Object.is(item, 'sort')) {
sort = params[item];
} else {
tempParam[item] = params[item];
}
}
})
url += `?${qs.stringify(tempParam)}`;
if (sort) {
url += '&sort=' + sort;
}
}
}
return new Promise((resolve: any, reject: any) => {
axios.get(url).then((response: AxiosResponse) => {
resolve(this.handelResponseResult(response));
}).catch((response: AxiosResponse) => {
reject(this.handelResponseResult(response));
});
});
}
/** /**
* 删除 * load状态管理器
* *
* @param {string} url * @private
* @returns {Promise<any>} * @type {(ElLoadingComponent | any)}
* @memberof Http * @memberof Http
*/ */
public delete(url: string): Promise<any> { private elLoadingComponent: any;
return new Promise((resolve: any, reject: any) => {
axios.delete(url).then((response: AxiosResponse) => {
resolve(this.handelResponseResult(response));
}).catch((response: AxiosResponse) => {
reject(this.handelResponseResult(response));
});
});
}
/** /**
* Creates an instance of Http.
* 私有构造,拒绝通过 new 创建对象
* *
*
* @param {string} url
* @returns {Promise<any>}
* @memberof Http * @memberof Http
*/ */
public head(url: string): Promise<any> { private constructor() { }
return new Promise((resolve: any, reject: any) => {
axios.head(url).then((response: AxiosResponse) => {
resolve(this.handelResponseResult(response));
}).catch((response: AxiosResponse) => {
reject(this.handelResponseResult(response));
});
});
}
/** /**
* * post请求
* *
* @param {string} url * @param {string} url
* @param {*} [params={}]
* @param {boolean} [isloading]
* @param {number} [serialnumber]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof Http * @memberof Http
*/ */
public options(url: string): Promise<any> { public post(url: string, params: any = {}, isloading?: boolean, serialnumber?: number): Promise<any> {
if (isloading) {
this.beginLoading();
}
params = this.handleRequestData(params);
return new Promise((resolve: any, reject: any) => { return new Promise((resolve: any, reject: any) => {
axios.options(url).then((response: AxiosResponse) => { axios({
resolve(this.handelResponseResult(response)); method: 'post',
}).catch((response: AxiosResponse) => { url: url,
reject(this.handelResponseResult(response)); data: params,
headers: { 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/json' },
// transformResponse: [(data: any) => {
// let _data: any = null;
// try {
// _data = JSON.parse(JSON.stringify(data));
// } catch (error) {
// }
// return _data;
// }],
}).then((response: any) => {
this.doResponseRresult(response, resolve, isloading, serialnumber);
}).catch((response: any) => {
this.doResponseRresult(response, reject, isloading, serialnumber);
}); });
}); });
} }
/** /**
* post请求 * 获取
* *
* @param {string} url * @param {string} url
* @param {*} [data] * @param {*} [params={}]
* @param {boolean} [isloading]
* @param {number} [serialnumber]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof Http * @memberof Http
*/ */
public post(url: string, data?: any): Promise<any> { public get(url: string,params: any = {}, isloading?: boolean, serialnumber?: number): Promise<any> {
if (data) { params = this.handleRequestData(params);
data = this.handleRequestData(data); if(params.srfparentdata){
Object.assign(params,params.srfparentdata);
delete params.srfparentdata;
}
if((Object.keys(params)).length>0){
let tempParam:any = {};
let sort:any = null;
Object.keys(params).forEach((item:any) =>{
if( params[item] || Object.is(params[item],0) ){
if (Object.is(item,'sort')){
sort = params[item];
}else{
tempParam[item] = params[item];
}
}
})
url += `?${qs.stringify(tempParam)}`;
if(sort){
url += '&sort='+sort;
}
}
if (isloading) {
this.beginLoading();
} }
return new Promise((resolve: any, reject: any) => { return new Promise((resolve: any, reject: any) => {
axios.post(url, axios.get(url).then((response: any) => {
data, this.doResponseRresult(response, resolve, isloading, serialnumber);
{ headers: { 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/json' } } }).catch((response: any) => {
).then((response: AxiosResponse) => { this.doResponseRresult(response, reject, isloading, serialnumber);
resolve(this.handelResponseResult(response));
}).catch((response: AxiosResponse) => {
reject(this.handelResponseResult(response));
}); });
}); });
} }
/** /**
* 修改数据 * 删除
* *
* @param {string} url * @param {string} url
* @param {*} data * @param {boolean} [isloading]
* @param {number} [serialnumber]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof Http * @memberof Http
*/ */
public put(url: string, data: any): Promise<any> { public delete(url: string, isloading?: boolean,data?:any, serialnumber?: number): Promise<any> {
if (data) { if (isloading) {
data = this.handleRequestData(data); this.beginLoading();
} }
return new Promise((resolve: any, reject: any) => { return new Promise((resolve: any, reject: any) => {
axios.put(url, data).then((response: AxiosResponse) => { if(!data){
resolve(this.handelResponseResult(response)); axios.delete(url).then((response: any) => {
}).catch((response: AxiosResponse) => { this.doResponseRresult(response, resolve, isloading, serialnumber);
reject(this.handelResponseResult(response)); }).catch((response: any) => {
}); this.doResponseRresult(response, reject, isloading, serialnumber);
});
}else{
axios.delete(url,{data:data}).then((response: any) => {
this.doResponseRresult(response, resolve, isloading, serialnumber);
}).catch((response: any) => {
this.doResponseRresult(response, reject, isloading, serialnumber);
});
}
}); });
} }
...@@ -172,18 +174,21 @@ export class Http { ...@@ -172,18 +174,21 @@ export class Http {
* *
* @param {string} url * @param {string} url
* @param {*} data * @param {*} data
* @param {boolean} [isloading]
* @param {number} [serialnumber]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof Http * @memberof Http
*/ */
public patch(url: string, data: any): Promise<any> { public put(url: string, data: any, isloading?: boolean, serialnumber?: number): Promise<any> {
if (data) { if (isloading) {
data = this.handleRequestData(data); this.beginLoading();
} }
data = this.handleRequestData(data);
return new Promise((resolve: any, reject: any) => { return new Promise((resolve: any, reject: any) => {
axios.patch(url, data).then((response: AxiosResponse) => { axios.put(url, data).then((response: any) => {
resolve(this.handelResponseResult(response)); this.doResponseRresult(response, resolve, isloading, serialnumber);
}).catch((response: AxiosResponse) => { }).catch((response: any) => {
reject(this.handelResponseResult(response)); this.doResponseRresult(response, reject, isloading, serialnumber);
}); });
}); });
} }
...@@ -193,16 +198,46 @@ export class Http { ...@@ -193,16 +198,46 @@ export class Http {
* *
* @private * @private
* @param {*} response * @param {*} response
* @param {Function} funt
* @param {boolean} [isloading]
* @param {number} [serialnumber]
* @memberof Http * @memberof Http
*/ */
private handelResponseResult(response: any): HttpResponse { private doResponseRresult(response: any, funt: Function, isloading?: boolean, serialnumber?: number): void {
if (response) { if (isloading) {
if (response.status === 200) { this.endLoading();
return new HttpResponse(200, response.data, undefined, response.headers); }
} if (serialnumber) {
return new HttpResponse(response.status, response.data, { code: 101, message: HttpResponse.getStatusMessage(response.status) }, response.headers) Object.assign(response, { serialnumber: serialnumber });
} }
return new HttpResponse(500, null, { code: 100, message: '请求发生异常,无返回结果!' }, response.headers); funt(response);
}
/**
* 开始加载
*
* @private
* @memberof Http
*/
private beginLoading(): void {
// todo 开始加载
}
/**
* 加载结束
*
* @private
* @memberof Http
*/
private endLoading(): void {
if (this.loadingCount > 0) {
this.loadingCount--;
}
setTimeout(() => {
if (this.loadingCount === 0) {
// todo 停止加载
}
}, 500);
} }
/** /**
...@@ -212,26 +247,14 @@ export class Http { ...@@ -212,26 +247,14 @@ export class Http {
* @param data * @param data
* @memberof Http * @memberof Http
*/ */
private handleRequestData(data: any) { private handleRequestData(data:any){
data = Util.deepCopy(data); if(data.srfsessionkey){
if (data.srfsessionkey) {
delete data.srfsessionkey; delete data.srfsessionkey;
} }
if (data.srfsessionid) { if(data.srfsessionid){
delete data.srfsessionid; delete data.srfsessionid;
} }
return data; return data;
} }
/**
* 获取 Http 单例对象
*
* @static
* @returns {Http}
* @memberof Http
*/
public static getInstance(): Http {
return this.instance;
}
} }
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册