提交 59b4e4d1 编写于 作者: WodahsOrez's avatar WodahsOrez

update: 服务

上级 35c2d194
...@@ -9,17 +9,20 @@ ...@@ -9,17 +9,20 @@
"dependencies": { "dependencies": {
"@ant-design/icons-vue": "^6.0.1", "@ant-design/icons-vue": "^6.0.1",
"ant-design-vue": "^3.0.0-alpha.14", "ant-design-vue": "^3.0.0-alpha.14",
"axios": "^0.24.0",
"element-plus": "^1.2.0-beta.6", "element-plus": "^1.2.0-beta.6",
"font-awesome": "^4.7.0", "font-awesome": "^4.7.0",
"qs": "^6.10.2",
"qx-util": "^0.0.10", "qx-util": "^0.0.10",
"ramda": "^0.27.1", "ramda": "^0.27.1",
"rxjs": "^7.4.0", "rxjs": "^7.4.0",
"vue": "^3.2.23", "vue": "^3.2.23",
"vue-router": "^4.0.12", "vue-global-api": "^0.4.1",
"vue-global-api": "^0.4.1" "vue-router": "^4.0.12"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.12", "@types/node": "^16.11.12",
"@types/qs": "^6.9.7",
"@types/ramda": "^0.27.60", "@types/ramda": "^0.27.60",
"@types/sass": "^1.43.1", "@types/sass": "^1.43.1",
"@vitejs/plugin-vue": "^1.10.2", "@vitejs/plugin-vue": "^1.10.2",
......
import { Http } from '@ibiz-core';
/** /**
* 实体服务基类 * 实体服务基类
* @export * @export
...@@ -5,7 +6,12 @@ ...@@ -5,7 +6,12 @@
*/ */
export class EntityService{ export class EntityService{
// TODO constructor(opts?: any) {
}
protected http = Http.getInstance();
// TODO
public GET(){} public GET(){}
} }
\ No newline at end of file
export * from './util'; export * from './util';
export * from './view-util'; export * from './view-util';
export { Http } from './net/http';
\ No newline at end of file
import { hasValue } from '@ibiz-core';
import axios from 'axios';
import qs from 'qs';
import { Subject } from 'rxjs';
/**
* Http net 对象
* 调用 getInstance() 获取实例
*
* @class Http
*/
export class Http {
/**
* 单例变量声明
*
* @private
* @static
* @type {Http}
* @memberof Http
*/
private static Http: Http;
/**
* 获取 Http 单例对象
*
* @static
* @returns {Http}
* @memberof Http
*/
public static getInstance(): Http {
if (!Http.Http) {
Http.Http = new Http();
}
return this.Http;
}
/**
* Creates an instance of Http.
* 私有构造,拒绝通过 new 创建对象
*
* @memberof Http
*/
private constructor() { }
/**
* 网络请求对象
*
* @memberof Http
*/
public static getHttp() {
return axios;
}
/**
* 统计加载
*
* @type {number}
* @memberof Http
*/
private loadingCount: number = 0;
/**
* 数据传递对象
*
* @type {Subject}
* @memberof Http
*/
private subject: Subject<any> = new Subject<any>();
/**
* post请求
*
* @param {string} url
* @param {*} [params={}]
* @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof Http
*/
public post(url: string, params: any = {}, isloading?: boolean): Promise<any> {
params = this.handleRequestData(params);
// url = this.handleAppParam(url, params);
return new Promise((resolve: any, reject: any) => {
axios({
method: 'post',
url: url,
data: params,
headers: { 'Content-Type': 'application/json;charset=UTF-8', Accept: 'application/json' },
})
.then((response: any) => {
this.doResponseResult(response, resolve);
})
.catch((response: any) => {
this.doResponseResult(response, reject);
});
});
}
/**
* 获取
*
* @param {string} url
* @param {*} [params={}]
* @param {boolean} [isloading]
* @param {number} [serialnumber]
* @returns {Promise<any>}
* @memberof Http
*/
public get(url: string, params: any = {}, isloading?: boolean): Promise<any> {
params = this.handleRequestData(params);
if (params.srfparentdata) {
Object.assign(params, params.srfparentdata);
delete params.srfparentdata;
}
if (Object.keys(params).length > 0) {
let tempParam: any = {};
Object.keys(params).forEach((item: any) => {
if (params[item] || Object.is(params[item], 0)) {
tempParam[item] = params[item];
}
});
url += `?${qs.stringify(tempParam)}`;
}
return new Promise((resolve: any, reject: any) => {
axios
.get(url)
.then((response: any) => {
this.doResponseResult(response, resolve);
})
.catch((response: any) => {
this.doResponseResult(response, reject);
});
});
}
/**
* 获取模型
*
* @param {string} url
* @param {*} [headers={}]
* @returns {Promise<any>}
* @memberof Http
*/
public getModel(url: string, headers: any = {}): Promise<any> {
return new Promise((resolve: any, reject: any) => {
axios
.get(url, {
headers: headers
})
.then((response: any) => {
this.doResponseResult(response, resolve);
})
.catch((response: any) => {
this.doResponseResult(response, reject);
});
});
}
/**
* 删除
*
* @param {string} url
* @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof Http
*/
public delete(url: string, isloading?: boolean, params?: any): Promise<any> {
return new Promise((resolve: any, reject: any) => {
url = this.handleAppParam(url, params);
if (!params) {
axios
.delete(url)
.then((response: any) => {
this.doResponseResult(response, resolve);
})
.catch((response: any) => {
this.doResponseResult(response, reject);
});
} else {
axios
.delete(url, { data: params })
.then((response: any) => {
this.doResponseResult(response, resolve);
})
.catch((response: any) => {
this.doResponseResult(response, reject);
});
}
});
}
/**
* 修改数据
*
* @param {string} url
* @param {*} data
* @param {boolean} [isloading]
* @param {number} [serialnumber]
* @returns {Promise<any>}
* @memberof Http
*/
public put(url: string, params: any, isloading?: boolean): Promise<any> {
params = this.handleRequestData(params);
// url = this.handleAppParam(url, params);
return new Promise((resolve: any, reject: any) => {
axios
.put(url, params)
.then((response: any) => {
this.doResponseResult(response, resolve);
})
.catch((response: any) => {
this.doResponseResult(response, reject);
});
});
}
/**
* 处理响应结果
*
* @private
* @param {*} response
* @param {Function} fn
* @param {boolean} [isloading]
* @memberof Http
*/
private doResponseResult(response: any, fn: Function): void {
if (response.status === 200) {
response.ok = true;
}
fn(response);
}
/**
* 处理请求数据
*
* @private
* @param data
* @memberof Http
*/
private handleRequestData(data: any) {
if (data.srfsessionkey) {
delete data.srfsessionkey;
}
if (data.srfsessionid) {
delete data.srfsessionid;
}
return data;
}
/**
* 处理系统级数据(以srf开始的字段)
*
* @private
* @param url 原始路径
* @param params 原始参数
* @memberof Http
*/
private handleAppParam(url: string, params: any): string {
if (params && (Object.keys(params).length > 0)) {
let tempParam: any = {};
Object.keys(params).forEach((item: string) => {
if (item.startsWith('srf') && hasValue(params[item])) {
tempParam[item] = params[item];
}
});
// 过滤前端标识属性
if (tempParam && Object.is(tempParam['srfinsttag'], '__srfstdinst__')) {
delete tempParam['srfinsttag'];
}
if (tempParam && (Object.keys(tempParam).length > 0)) {
url += `?${qs.stringify(tempParam)}`;
}
return url;
}
return url;
}
}
...@@ -23,4 +23,13 @@ export const ViewConfig = { ...@@ -23,4 +23,13 @@ export const ViewConfig = {
}, },
{{/eq}} {{/eq}}
{{/page.ctrls}} {{/page.ctrls}}
controlsAction:{
{{#page.ctrls}}
{{#eq controlType "FORM"}}
{{codeName}}:{
'loadAction': 'Get'
},
{{/eq}}
{{/page.ctrls}}
}
}; };
\ No newline at end of file
...@@ -51,7 +51,7 @@ const { state, handleToolbarEvent } = new EditView(ViewConfig).moduleInstall(pro ...@@ -51,7 +51,7 @@ const { state, handleToolbarEvent } = new EditView(ViewConfig).moduleInstall(pro
:showBusyIndicator="true" :showBusyIndicator="true"
:context="state.context" :context="state.context"
:viewParams="state.viewParams" :viewParams="state.viewParams"
:controlAction="state.controlsAction" :controlAction="state.controlsAction.{{codeName}}"
:viewSubject="state.viewSubject" :viewSubject="state.viewSubject"
></{{codeName}}Form> ></{{codeName}}Form>
{{/eq}} {{/eq}}
......
import { Http, EntityService } from '@ibiz-core';
/**
* {{appEntity.logicName}}服务对象基类
*
* @export
* @class {{appEntity.codeName.pascalCase}}
* @extends {EntityBaseService}
*/
export class {{appEntity.codeName.pascalCase}}ServiceBase extends EntityService {
constructor(opts?: any) {
super(opts);
}
protected APPDENAME = "{{appEntity.codeName.pascalCase}}";
{{#eq appEntity.keyPSAppDEField.codeName.lowerCase}}
protected APPDEKEY = "{{appEntity.keyPSAppDEField.codeName.pascalCase}}";
{{/eq}}
{{#each appEntity.allPSAppDEMethods as |singleAppMethod| }}
/**
* {{singleAppMethod.codeName}}
*
* @param {*} [_context={}]
* @param {*} [_data = {}]
* @returns {Promise<any>}
* @memberof {{appEntity.codeName.pascalCase}}ServiceBase
*/
async {{singleAppMethod.codeName}}(_context: any = {}, _data: any = {}): Promise<any> {
//{{singleAppMethod}}
//{{singleAppMethod.pSDEServiceAPIMethod}}
//{{singleAppMethod.pSDEServiceAPIMethod.requestMethod}}
{{#eq singleAppMethod.methodType "SELECT" }}
const res = await this.http.post(`/{{appEntity.codeName.pluralize}}/${encodeURIComponent(_context.{{appEntity.codeName.lowerCase}})}{{singleAppMethod.requestPath}}`, _data);
return res;
{{/eq}}
{{#eq singleAppMethod.methodType "FETCH" }}
const res = await this.http.post(`/{{appEntity.codeName.pluralize}}/${encodeURIComponent(_context.{{appEntity.codeName.lowerCase}})}{{singleAppMethod.requestPath}}`, _data);
return res;
{{/eq}}
{{#eq singleAppMethod.methodType "GET" }}
const res = await this.http.get(`/{{appEntity.codeName.pluralize}}/${encodeURIComponent(_context.{{appEntity.codeName.lowerCase}})}{{singleAppMethod.requestPath}}`);
return res;
{{/eq}}
{{#eq singleAppMethod.codeName "GetDraft" }}
_data[this.APPDENAME?.toLowerCase()] = undefined;
_data[this.APPDEKEY] = undefined;
const res = await this.http.post(`/{{appEntity.codeName.pluralize}}/${encodeURIComponent(_context.{{appEntity.codeName.lowerCase}})}{{singleAppMethod.requestPath}}`, _data);
return res;
{{/eq}}
{{#eq singleAppMethod.codeName "Update" }}
const res = await this.http.post(`/{{appEntity.codeName.pluralize}}/${encodeURIComponent(_context.{{appEntity.codeName.lowerCase}})}{{singleAppMethod.requestPath}}`, _data);
return res;
{{/eq}}
{{#eq singleAppMethod.codeName "Get" }}
const res = await this.http.get(`/{{appEntity.codeName.pluralize}}/${encodeURIComponent(_context.{{appEntity.codeName.lowerCase}})}{{singleAppMethod.requestPath}}`, _data);
return res;
{{/eq}}
}
{{/each}}
}
\ No newline at end of file
判断 import { {{appEntity.codeName.pascalCase}}ServiceBase } from './{{appEntity.codeName.spinalCase}}-service-base';
{{#eq appEntity.logicName "字典"}}
成功{{appEntity.logicName}} /**
{{else}} * ${appEntity.getLogicName()}服务
不成功{{appEntity.logicName}} *
{{/eq}} * @export
* @class {{appEntity.codeName.pascalCase}}Service
小于 * @extends {{appEntity.codeName.pascalCase}}ServiceBase
{{#lt appEntity.storageMode 0}} */
成功{{appEntity.storageMode}} 小于0 export class {{appEntity.codeName.pascalCase}}Service extends {{appEntity.codeName.pascalCase}}ServiceBase {
{{else}} /**
不成功{{appEntity.storageMode}} 不小于0 * Creates an instance of {{appEntity.codeName.pascalCase}}Service.
{{/lt}} * @memberof {{appEntity.codeName.pascalCase}}Service
*/
判断 constructor(opts?: any) {
{{#eq appEntity.defaultMode true}} {{!-- const { context: context, tag: cacheKey } = opts; --}}
成功{{appEntity.defaultMode}} =真 super(opts);
{{else}} }
不成功{{appEntity.defaultMode}} =假
{{/eq}} /**
* 获取实例
*
目前支持的判断有 eq 等于, neq 不等于, gt 大于, gte 大于等于, lt 小于 , lte 小于等于 * @static
* @param 应用上下文
{{#appEntity.ctrls}} * @return {*} { {{appEntity.codeName.pascalCase}}Service }
* @memberof {{appEntity.codeName.pascalCase}}Service
这里是应用实体ctrl的代码名称 {{codeName}} */
static getInstance(context?: any): {{appEntity.codeName.pascalCase}}Service {
lowercase {{lowerCase codeName}} return new {{appEntity.codeName.pascalCase}}Service({ context: context });
}
UPPERCASE {{upperCase codeName}} }
export default {{appEntity.codeName.pascalCase}}Service;
PascalCase {{pascalCase codeName}} \ No newline at end of file
camelCase {{camelCase codeName}}
snake_case {{snakeCase codeName}}
spinal-case {{spinalCase codeName}}
pluralize {{pluralize codeName}}
另一种便捷写法,只支持codeName和name
lowercase {{codeName.lowerCase}}
UPPERCASE {{codeName.upperCase}}
PascalCase {{codeName.pascalCase}}
camelCase {{codeName.camelCase}}
snake_case {{codeName.snakeCase}}
spinal-case {{codeName.spinalCase}}
pluralize {{codeName.pluralize}}
{{/appEntity.ctrls}}
\ No newline at end of file
import { ControlVOBase } from '@ibiz-core';
import { ControlService } from './{{ctrl.codeName.lowerCase}}-form-service';
export const CtrlConfig = { export const CtrlConfig = {
controlCodeName: '{{ctrl.codeName}}', controlCodeName: '{{ctrl.codeName}}',
controlName: '{{ctrl.name}}', controlName: '{{ctrl.name}}',
controlService: new ControlService(),
data: {}, data: {},
}; };
/**
* 部件展示数据对象
* @export
* @class ControlVO
*/
export class ControlVO extends ControlVOBase {
// 表单里映射了属性的字段
{{#each ctrl.pSDEFormItems as | formItem | }}
{{!-- // 表单formItem
{{formItem.name}}
{{formItem.id}} --}}
{{#neq formItem.pSAppDEField null }}
get {{lowerCase formItem.id}}() {
return this.$DO.{{lowerCase formItem.pSAppDEField.codeName}};
}
set {{lowerCase formItem.id}}(value: any) {
this.$DO.{{lowerCase formItem.pSAppDEField.codeName}} = value;
}
{{/neq}}
{{/each}}
// 表单里没有映射实体属性的字段
{{#each ctrl.pSDEFormItems as | formItem | }}
{{#eq formItem.pSAppDEField null }}
{{lowerCase formItem.id}}: any;
{{/eq}}
{{/each}}
}
\ No newline at end of file
import { ControlServiceBase } from '@ibiz-core';
import { {{pascalCase ctrl.pSAppDataEntity.codeName}}Service } from '@service/{{spinalCase ctrl.pSAppDataEntity.codeName}}/{{spinalCase ctrl.pSAppDataEntity.codeName}}-service';
import { ControlVO } from './{{ctrl.codeName.lowerCase}}-form-config';
export class ControlService extends ControlServiceBase {
/**
* 实体服务对象
*/
public entityService: {{pascalCase ctrl.pSAppDataEntity.codeName}}Service = new {{pascalCase ctrl.pSAppDataEntity.codeName}}Service();
/**
* 加载数据
*
* @param [context={}] 上下文参数
* @param [data={}] 视图参数
* @param opts
* @return {*}
*/
public async get(context: any, data: any, opts: { action: string; isLoading?: boolean }): Promise<any> {
const { context: Context, data: Data } = this.handleRequestData(context, data, opts);
let response: any;
const _entityService: any = this.entityService;
if (_entityService[opts.action] && _entityService[opts.action] instanceof Function) {
response = await _entityService[opts.action](Context, Data, opts.isLoading);
} else {
response = await this.entityService.Get(Context, Data, opts.isLoading);
}
// this.setRemoteCopyData(response);
response.data = new ControlVO(response.data);
return this.handleResponse(response, opts);
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册