云服务器内容精选

  • 样例代码解读 通过下面详细的脚本代码内容解读,使您对脚本有一个更具体的认识。 一般情况下,编写脚本的大致流程为: 按需引入平台标准库。 图2 引入平台标准库 定义出参、入参结构。 图3 定义入参 图4 定义出参 定义方法以及使用的对象。 图5 定义方法及使用对象 进行数据库操作。 图6 数据库相关操作 下面通过解读以下脚本样例,了解一个脚本的总体结构框架、编写要求。 import * as decimal from 'decimal'; @action.object({type: "param"}) export class ActionInput { @action.param({type: 'String', required: true, label: 'your name', description: 'please input your name'}) name: string; @action.param({type: 'Number', required: true, min: 1, max: 100, message: 'age must during [1, 100]'}) age: decimal.Decimal; @action.param({type: 'Date', pattern: 'yyyy-MM-dd'}) birthday: Date; @action.param({type: 'String', isCollection: true}) schools: string[]; @action.param({type: 'Boolean'}) married: boolean; @action.param({type: 'MyObject'}) obj: MyObject; } @action.object({type: "param"}) export class MyObject { @action.param({type: 'String'}) something: string; @action.param({type: 'Number'}) otherthing: decimal.Decimal; } @action.object({type: "param"}) export class ActionOutput { @action.param({type: 'String', isCollection: true}) greets: string[]; } @action.object({type: "method"}) export class ActionDemo { @action.method({ label: 'greeting something', description: 'greeting something.', input: 'ActionInput', output: 'ActionOutput' }) public greet(inarg: ActionInput): ActionOutput { console.log('name = ', inarg.name); console.log('age = ', inarg.age); console.log('birthday = ', inarg.birthday); console.log('schools = ', inarg.schools); console.log('married = ', inarg.married); console.log('obj = ', inarg.obj); let out = new ActionOutput(); out.greets = ['hello', 'hi', 'how are you', 'how old are you', 'long time no see']; return out; } } 上述示例脚本包括三部分内容: 导入标准库或其他模块。 示例中第1行,表示将使用平台提供的decimal库。 import * as decimal from 'decimal'; 除了平台预置标准库,还可以声明对其他自定义模块的引用。例如,已经提前开发了一个脚本circle,可以用如下方式加载。 import * as circle from './circle'; 定义输入、输出变量。 脚本可以有多个输入、输出参数,也可以没有。所有的输入或输出参数必须封装在一个class中,作为实例成员。 本例中,脚本有6个输入参数,被封装为ActionInput。每个参数都必须定义其参数类型,同时还可以定义是否必填、标签、最大值、最小值等可选属性。 @action.object({type: "param"}) export class ActionInput { @action.param({type: 'String', required: true, label: 'your name', description: 'please input your name'}) name: string; @action.param({type: 'Number', required: true, min: 1, max: 100, message: 'age must during [1, 100]'}) age: decimal.Decimal; @action.param({type: 'Date', pattern: 'yyyy-MM-dd'}) birthday: Date; @action.param({type: 'String', isCollection: true}) schools: string[]; @action.param({type: 'Boolean'}) married: boolean; @action.param({type: 'MyObject'}) obj: MyObject; } 因为第6个输入参数“obj”的参数类型为自定义对象,所以还需要给出“MyObject”的定义。 @action.object({type: "param"}) export class MyObject { @action.param({type: 'String'}) something: string; @action.param({type: 'Number'}) otherthing: decimal.Decimal; } 脚本有1个输出参数,被封装为ActionOutput。 @action.object({type: "param"}) export class ActionOutput { @action.param({type: 'String', isCollection: true}) greets: string[]; } 定义方法。 样例中,ActionDemo是外部调用的class,使用export导出。ActionDemo定义了一个action method,使用action.method装饰,表明调用脚本时从此方法入口。greet是class的实例方法,其输入、输出参数就是前面定义的ActionInput和ActionOutput。 在一个脚本文件里面,action.method只能使用一次。 @action.object({type: "method"}) export class ActionDemo { @action.method({ label: 'greeting something', description: 'greeting something.', input: 'ActionInput', output: 'ActionOutput' }) public greet(inarg: ActionInput): ActionOutput { console.log('name = ', inarg.name); console.log('age = ', inarg.age); console.log('birthday = ', inarg.birthday); console.log('schools = ', inarg.schools); console.log('married = ', inarg.married); console.log('obj = ', inarg.obj); let out = new ActionOutput(); out.greets = ['hello', 'hi', 'how are you', 'how old are you', 'long time no see']; return out; } } 脚本编辑页面不支持单步调试,样例里的console.log可实现在日志里打印过程输出,方便代码调试。
  • 公共接口 公共接口是对脚本、服务编排和对象进行再包装的一种方式。将创建的脚本、服务编排、对象包装成一个新公共服务,可以使得接口的URL地址的表达形式更规范,方便让前端页面或第三方系统进行调用。 因此,在创建完后台逻辑后(服务编排、脚本、对象),就需要先将此接口包装成标准的公共接口,才能被调用。本节中创建了2个业务脚本,因此需要对应创建2个公共接口,供前端页面调用。 创建公共接口的入口:在APP视图下,单击下方“服务”,即可进入公共接口创建页面。 图1 创建公共接口入口 图2 公共接口基本信息 图3 公共接口URL 父主题: 背景和原理