当前位置:网站首页>使用‘百家饭’自动生成API调用:JS部分进展(二)
使用‘百家饭’自动生成API调用:JS部分进展(二)
2022-06-26 09:49:00 【InfoQ】
生成逻辑说明
入口脚本
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);
})
全局配置脚本
module.exports = {
// 认证服务
baseURL: 'https://puser.zjzwfw.gov.cn/',
// 打开下一行,修改请求超时时间,当前例子为10s
// timeout: 10 * 1000
}
接口脚本
const { verify, timestr,md5 } = require("./functions.js");
const injure = (instance)=>{
var config = {params:{}};
// 路径:sso/servlet/organduser, 调用方法GET
//params要求传输以下属性,没有标注'有默认值'的参数由调用方传输
//-method(有默认值):调用方法
//-servicecode:接入资源在SSO注册成功后的业务代码,由【SSO管理员】提供。
//-time(有默认值):时间戳,当前时间 年月日时分秒 例:2009年10月10日 12时12分12秒格式为20091010121212
//-sign(有默认值):MD5(servicecode+servicepwd+time),将业务代码、业务密钥、发送时间按顺序连接并进行md5加密。注:1、参数servicepwd由【SSO管理员】提供;2、MD5算法是标准算法,可使用java内部实现;3、MD5后的值为32位并且小写。
//-userid:用户在SSO的身份唯一标示
//-datatype(有默认值):数据格式 xml json。默认xml
//-servicepwd:分配的密码,注意,该参数仅用于sign计算
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
}
- 入参校验,对接口定义中定义为必传,但是不包含默认值的参数进行判断,如果检查有问题,直接返回reject的Promise。
- 默认值填充,对接口定义中包含了默认值,且默认值不是可计算属性的参数,构建一个默认参数对象,与传入参数进行合并。
- 对没有传入数值,但是参数定义中,定义为计算属性(就是默认值以‘=’开头的属性,例如
timestr("YYYYMMDDHHmmss")),将计算表达式翻译成实际js代码。
- 调用axios的原始函数,如get,post等。
辅助函数
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("参数"+name+"要求必传,但未传入");
}
}
}
进展说明
欢迎试用,提供您宝贵的意见
边栏推荐
- Use of exec series functions (EXECL, execlp, execle, execv, execvp)
- Search engine advanced search method records
- Progressive Web 应用程序PWA是应用程序开发的未来
- [binary search] 4 Find the median of two positive arrays
- Cmake / set command
- The fourteenth MySQL operation - e-mall project
- [echart] i. how to learn echart and its characteristic document reading notes
- 搜索引擎高级搜索方法记录
- Under the double reduction, the amount of online education has plummeted. Share 12 interesting uses of webrtc
- echo $?
猜你喜欢

MySQL第十一作业-视图的应用

MySQL 13th job - transaction management

Call API interface to generate QR code of wechat applet with different colors

JS reverse | four libraries and one platform response data encryption

DataBinding使用与原理分析

Little red book - Summary of internal sales spike project

Write data to local file

MySQL seventh job - update data

Using reflection to export entity data to excel
![[software project management] sorting out knowledge points for final review](/img/13/823faa0607b88374820be3fce82ce7.png)
[software project management] sorting out knowledge points for final review
随机推荐
[online simulation] Arduino uno PWM controls the speed of DC motor
Tape library simple record 1
Quantitative investment learning - Introduction to classic books
MySQL 9th job - connection Query & sub query
Plookup table in appliedzkp zkevm (8)
MySQL project 8 summary
Progressive web application PWA is the future of application development
SQL Server foundation introduction collation
About multi table query of MySQL
The fourteenth MySQL operation - e-mall project
Getting started with postman
六月集训(第26天) —— 并查集
Global and Chinese market of electronic pet door 2022-2028: Research Report on technology, participants, trends, market size and share
[difficult and miscellaneous diseases] @transitional failure summary
Blog article index summary -- Software Engineering
Renesas electronics launched a complete intelligent sensor solution for Internet of things applications
The difference between NPM and yarn
Redis (IV) redis association table caching
Use of exec series functions (EXECL, execlp, execle, execv, execvp)
MySQL第六章总结