跳到主要内容

Backend SDK

Backend SDK 是 JSF 脚本访问 77hub 数据和内部服务的主要方式。脚本结构、上下文和返回值规则见后端 JSF 函数开发说明

目前backend sdk支持以下函数

  • 查询对象:query(objectType: string, fields: string[], criteriaStr: string, sorts: object, offset: number, limit: number, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>
  • 新增对象:create(objectType: string, data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>
  • 修改对象: update(objectType: string, data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>
  • 删除对象: remove(objectType: string, id: string, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>
  • Gql查询:graphql(gql: string, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>
  • 通用api调用: restapi(appName: string, method: string, url: string, data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>
  • 发送邮件: emailTo(data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>
  • 新增变更单:change(objectType: string, data: object, headers: Map<string, string>): Promise<RestResponse>
  • 异步批量触发 JSF 函数:batch(objectType: string, execUserId: string, criteriaStr: string, bindVars: Record<string, any>, batchSize: number, jsfDefName: string, remarks: string, headers: Map<string, string>): Promise<RestResponse>

其中返回值 RestResponse对象结构为:

{
status: string;
code: string;
message: string;
description: string;
data: any;
args: any;
}

字段说明如下:

字段名称字段说明类型必填备注
status执行函数状态stringY函数是否成功执行的状态, 取值为 success/error/warning
code错误码string-如果执行不成功时,提供的错误码
message错误信息string-如果执行不成功时,提供的错误提示信息
description错误描述string-如果执行不成功时,提供的错误详细描述
data返回的数据any-成功时需要返回的数据对象
args错误附加参数map-如果出错时,需要返回的其他错误数据

查询对象 query:

query(objectType: string, fields: string[], criteriaStr: string, sorts: object, offset: number, limit: number, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>

参数说明:

字段名称字段说明类型必填备注
objectType查询对象名stringY
fields查询的字段列表string[]Y至少指定一个字段
criteriaStr查询条件表达式string-指定查询的eql条件表达式, 与postgresql sql条件表达式一致, 只字段使用的是对象属性名,并且支持级联(a.b.c), 不指定条件表示无条件查询
sorts排序object-要排序的字段列表,例如:[{name: "name"}, {name: "createdTime", isDesending: true}]
offset查询起始行integer-从0开始的起始行,不指定表示从0开始
limit返回最大的行数integer-不指定表示返回所有记录,但受系统最大行10000限制
asAdmin以管理员权限运行boolean-如果不指定或者为false,表示以当前用户身份执行
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

成功执行后返回值中data值为查询的对象的数据列表

执行 query('User', ['id', 'name', 'createdTime'], "id='1'") 返回值示例:

{
"status": "success",
"code": null,
"message": null,
"description": null,
"data": [
{
"id": "1",
"name": "小企",
"createdTime": 1626916982389
}
],
"args": null
}

新增对象 create:

create(objectType: string, data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>

参数说明:

字段名称字段说明类型必填备注
objectType新增对象名stringY
data新增对象结构化数据objectY为一个Json对象,包括key为字段名,值为字段值的数据项
asAdmin以管理员权限运行boolean-如果不指定或者为false,表示以当前用户身份执行
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

成功执行后返回值中data值新增加的对象的ID

执行 create('Role', {code: '001', name: 'manager'}) 返回值示例:

{
"status": "success",
"code": null,
"message": null,
"description": null,
"data": "NEP8HV515KG0008",
"args": null
}

执行 create('User', {code: '001', name: 'testuser'}) 执行出错返回值示例:

{
"status": "error",
"code": "0010",
"message": "提交的数据存在问题",
"description": "Object validation found 2 problems, error codes: [0011, baseapp-200108]",
"data": null,
"args": {
"problems": {
"default": {
"ERROR": [
{
"fieldPath": "department",
"type": "error",
"code": "0011",
"message": "字段的值必须指定"
},
{
"fieldPath": "jobRelationships",
"type": "error",
"code": "baseapp-200108",
"message": "用户任职记录中必须包含一条主职记录",
"args": {
"jobRelationships": "用户任职记录中必须包含一条主职记录"
}
}
]
}
}
}
}

修改对象 update:

update(objectType: string, data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>

参数说明:

字段名称字段说明类型必填备注
objectType新增对象名stringY
data修改对象结构化数据objectY为一个Json对象,包括key为字段名,值为字段值的数据项, 其中id必须指定,表示要修改的对象ID
asAdmin以管理员权限运行boolean-如果不指定或者为false,表示以当前用户身份执行
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

执行 update('Role', {id: 'NEP8HV515KG0008', code: '001', name: 'manager2'}) 返回值示例:

{
"status": "success",
"code": null,
"message": null,
"description": null,
"data": "",
"args": null
}

删除对象 remove:

remove(objectType: string, id: string, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>

参数说明:

字段名称字段说明类型必填备注
objectType新增对象名stringY
id删除的对象的IDstringY
asAdmin以管理员权限运行boolean-如果不指定或者为false,表示以当前用户身份执行
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

执行 remove('Role','NEP8HV515KG0008') 返回值示例:

{
"status": "success",
"code": null,
"message": null,
"description": null,
"data": "",
"args": null
}

执行 remove('Role','NEP8HV515KG0008') 出错返回值示例:

{
"status": "error",
"code": "0100",
"message": "对象不存在或已被删除",
"description": "entity Role not found: NEP8HV515KG0008",
"data": null,
"args": {
"objectType": "Role",
"objectId": "NEP8HV515KG0008"
}
}

Gql查询 graphql:

graphql(gql: string, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>

参数说明:

字段名称字段说明类型必填备注
gqlgql查询语法stringY
asAdmin以管理员权限运行boolean-如果不指定或者为false,表示以当前用户身份执行
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

执行 graphql('{User{id,name}}') 返回值示例:

{
"status": "success",
"code": null,
"message": null,
"description": null,
"data": {
"User": [
{
"id": "1",
"name": "小企"
}
]
},
"args": null,
"logs": []
}

执行 graphql('{User{id,name1}} 出错返回值示例:

{
"status": "error",
"code": "400",
"message": "查询出错",
"description": "[{\"message\":\"Validation error of type FieldUndefined: Field 'name1' in type 'UserSchemaType' is undefined\",\"locations\":[{\"line\":1,\"column\":10}],\"description\":\"Field 'name1' in type 'UserSchemaType' is undefined\",\"validationErrorType\":\"FieldUndefined\"}]",
"data": null,
"args": null,
"logs": []
}

通用api调用 restapi:

restapi(appName: string, method: string, url: string, data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>

该方法为支持特定的场景使用的方法,可以访问租户服务的api, 调用成功后,返回值的data中为接口实际的返回结果。

参数说明:

字段名称字段说明类型必填备注
appNamerestapi所在的服务名,eg: baseappstringY
method请求类型:get,post,put,deletestringY
urlrestapi相对于服务的url, 比如:"/User/1"stringY
data请求数据object-getdelete 请求会将键值对作为 URL 查询参数,其他请求会将其作为 JSON 请求体
asAdmin以管理员权限运行boolean-如果不指定或者为false,表示以当前用户身份执行
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

执行 restapi('graphql', 'post', '/', {query:'{User{id,name}}'}) 返回值示例:

{
"status": "success",
"code": null,
"message": null,
"description": null,
"data": {
"data": {
"User": [
{
"id": "1",
"name": "小企"
}
]
},
"errors": []
},
"args": null,
"logs": []
}

发送邮件 emailTo

emailTo(data: object, asAdmin: boolean, headers: Map<string, string>): Promise<RestResponse>

参数说明:

字段名称字段说明类型必填备注
data邮件内容objectY
asAdmin以管理员权限运行boolean-如果不指定或者为false,表示以当前用户身份执行
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

data参数说明:

字段名称字段说明类型必填备注
objectType单据对象stringY
objectId单据idstringY
receiverDefIds接收人角色idsstringN“,”分隔
userIds接收用户idsstringN“,”分隔
purposeId邮件用途stringYMailPurpose.billIssued-出账;MailPurpose.dunning-催缴; MailPurpose.share-分享
messageTmplId消息模板booleanY
subject邮件主题stringY
content邮件内容stringY
remarks备注stringN
attachmentIds附件idsstringN“,”分隔
billTypeTemplateId打印模板idstringN
mailSenderId发件人邮件账户stringN
isFeedbackRead是否需要反馈已读booleanY

新增变更单 change:

change(objectType: string, data: object, headers: Map<string, string>): Promise<RestResponse>

change 函数支持: 自定义单据新增变更单

参数说明:

字段名称字段说明类型必填备注
objectType新增变更单对象stringY
data对象结构化数据objectY数据以增量形式来提交,其中baseBillId 和baseBillItemId无需指定,系统会自动根据id 补齐,和修改接口参数一致
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

执行 change('CsZiDingYiBianGengDan01', {id: 'NEP8HV515KG0008', code: '001', name: 'manager2'}) 返回示例值:

{
"status":"success",
"code":null,
"message":null,
"description":null,
"data":"SXTF6662XC500EH", // 新增的变更单id
"args":null
}

执行 change('CsZiDingYiBianGengDan01', {id: 'NEP8HV515KG0008', code: '001', name: 'manager2'}) 执行出错返回值示例:

{
"status":"error",
"code":"500",
"message":"服务器端运行您的请求出现错误!",
"description":"post http://inventory/inventory/PickingApply/changeBill: NoSuchElementException No value present",
"data":null,
"args":null
}

异步批量触发jsf函数 batch:

batch(objectType: string, execUserId: string, criteriaStr: string, bindVars: Record<string, any>, batchSize: number, jsfDefName: string, remarks: string, headers: Map<string, string>): Promise<RestResponse>

异步批量触发jsf函数。当jsf 函数批量处理大量数据时,可以考虑使用此函数来拆分,防止jsf 函数执行超时。

参数说明:

字段名称字段说明类型必填备注
objectTypejsf 待处理的对象stringY
execUserId执行人stringY以执行人的身份触发jsf 函数
criteriaStr包含占位符的gql查询条件stringY
bindVars查询条件中的占位符绑定变量Record<string, any>-
batchSize每批数据量大小number-默认值10
jsfDefNamejsf 函数名stringY异步批量调用的jsf 函数
remarks备注string-
headers附加请求头Map<string,string>-目前支持 Ignore-Warn=true,表示忽略警告错误

执行 batch("CsZiDingYiBianGengDan01", "GK63GT50E7U0032", "billFullStatus=:billStatusTest", { billStatusTest: "BillStatus.draft" }, 1, "testJsfDSL", "测试一下", null) 返回示例值:

{
"status":"success",
"code":null,
"message":null,
"description":null,
"data":"J1SF6662XC5007X", // 用于查询异步任务的执行情况
"args":null
}

可以根据返回的J1SF6662XC5007X 使用 GQL 查看异步任务的执行情况

{
JsfBatch(criteriaStr:"id='J1SF6662XC5007X'") {
id
backlogNbr // 按criteriaStr查询出的符合条件的记录总数
asyncTaskId
asyncTask{
beanName
params
isCompleted
successRate
asyncTaskItems{
batchNo
lastErrorMsg
}
}
}
}

执行 batch("CsZiDingYiBianGengDan01", "GK63GT50E7U0032", "billFullStatus=:billStatusTest", { billStatusTest: "BillStatus.draft" }, 1, "testJsfDSL", "测试一下", null) 执行出错返回值示例:

{
"status":"error",
"code":"500",
"message":"服务器端运行您的请求出现错误!",
"description":"post http://baseapp/baseapp/JsfBatch: StringIndexOutOfBoundsException String index out of range: -1",
"data":null,
"args":null
}