当前位置:网站首页>Using baijiafan to automatically generate API calls: progress in JS (II)
Using baijiafan to automatically generate API calls: progress in JS (II)
2022-06-26 10:50:00 【InfoQ】
Generate logic description
Entry script
const axios = require("axios");
const config = require("./config.js");
const instance = axios.create(config);
const sso_servlet_organduser = require("./api-sso_servlet_organduser.js");
sso_servlet_organduser.injure(instance);
module.exports = instance;
const axios = require("./axios");
axios.getSsoServletOrganduser().then(res=>{
console.log(res);
}).catch(err=>{
console.log(err);
})
Global configuration script
module.exports = {
// Certification services
baseURL: 'https://puser.zjzwfw.gov.cn/',
// Open the next line , Modify request timeout , The current example is 10s
// timeout: 10 * 1000
}
Interface script
const { verify, timestr,md5 } = require("./functions.js");
const injure = (instance)=>{
var config = {params:{}};
// route :sso/servlet/organduser, Calling method GET
//params The following attributes are required to be transferred , There is no sign ' Have default values ' The arguments to are passed by the caller
//-method( Have default values ): Calling method
//-servicecode: The access resources are SSO Business code after successful registration , from 【SSO Administrators 】 Provide .
//-time( Have default values ): Time stamp , current time Mm / DD / yyyy HHM / S example :2009 year 10 month 10 Japan 12 when 12 branch 12 The second format is 20091010121212
//-sign( Have default values ):MD5(servicecode+servicepwd+time), Put the business code 、 Business key 、 The sending time is connected in sequence and md5 encryption . notes :1、 Parameters servicepwd from 【SSO Administrators 】 Provide ;2、MD5 The algorithm is the standard algorithm , You can use java Internal implementation ;3、MD5 The value after is 32 Bit and lowercase .
//-userid: The user is in SSO Unique identification of
//-datatype( Have default values ): data format xml json. Default xml
//-servicepwd: Assigned password , Be careful , This parameter is only used for sign Calculation
instance.getSsoServletOrganduser = (params = {}) => {
var verify_err = null ||
verify(params.servicecode,"servicecode",{"required":true}) ||
verify(params.userid,"userid",{"required":true}) ||
verify(params.servicepwd,"servicepwd",{"required":true});
if ( verify_err !=null){
return verify_err;
}
var defaultParam = {
datatype: "json",
method: "getUserInfoById",
};
params = Object.assign(defaultParam,params);
if (params.time == undefined){
params.time= timestr("YYYYMMDDHHmmss");
}
if (params.sign == undefined){
params.sign= md5(params.servicecode+params.servicepwd+params.time);
}
return instance.get("sso/servlet/organduser",{params});
}
}
module.exports = {
injure
}
- Input verification , It is defined as mandatory in the interface definition , However, the parameters that do not contain default values are used for judgment , If there is a problem with the inspection , Go straight back to reject Of Promise.
- The default value fills in , The default values are included in the interface definition , And the default value is not a parameter of the computable attribute , Build a default parameter object , Merge with the passed in parameters .
- No value passed in , But in the parameter definition , Defined as a computed property ( Is the default value in ‘=’ The properties of the beginning , for example
timestr("YYYYMMDDHHmmss")), Translate the calculation expression into the actual js Code .
- call axios The original function of , Such as get,post etc. .
Auxiliary function
const dayjs = require("dayjs");
const md5 = require("md5");
module.exports = {
timestr: (fmt) => {
return dayjs().format(fmt);
},
md5: (str) => {
return md5(str);
},
verify: (par, name, option) =>{
if (option.required && par == undefined){
return Promise.reject(" Parameters "+name+" The request must be transmitted , But not into ");
}
}
}
Progress description
Welcome to try , Provide your valuable advice
边栏推荐
- The sixth MySQL job - query data - multiple conditions
- jwt认证协议阐述之——我开了一家怡红院
- How QT uses quazip to compress and decompress files
- MySQL 10th job - View
- MySQL第九次作业-连接查询&子查询
- Is it safe to open an account in the school of Finance and business?
- Notes - simple but adequate series_ KVM quick start
- Alibaba cloud OSS - object storage service (tool)
- Servlet learning notes II
- MySQL backup and restore command
猜你喜欢

MySQL第六次作业-查询数据-多条件

MySQL 11th job - view application

nacos2.x.x启动报错信息Error creating bean with name ‘grpcClusterServer‘;

Using foreach to loop two-dimensional array

2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告

Docker中实现MySQL主从复制

MySQL第十二次作业-存储过程的应用

携程机票 App KMM 跨端 KV 存储库 MMKV-Kotlin | 开源

Vscode environment setup: synchronous configuration

QT连接MySql数据查询失败
随机推荐
近期工作汇报
MySQL第九次作业-连接查询&子查询
一键部署属于自己的社区论坛
Using reflection to export entity data to excel
[echart] i. how to learn echart and its characteristic document reading notes
Idea remote debugger
Win10 start FTP service and set login authentication
量化投资学习——经典书籍介绍
Installing MySQL under Linux [details]
MySQL Chapter 4 Summary
Adaptiveavgpool2d does not support onnx export. Customize a class to replace adaptiveavgpool2d
CentOS安装Redis多主多从集群
JWT certification agreement -- I opened a Yihong hospital
Common interview questions of binary tree
AIX basic operation record
Vscode environment setup: synchronous configuration
Concise course of probability theory and statistics in engineering mathematics second edition review outline
24 个必须掌握的数据库面试问题!
CEPH operation and maintenance common instructions
Redis knowledge mind map