当前位置:网站首页>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
边栏推荐
- Développeur, quelle est l'architecture des microservices?
- AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
- SwiftUI 开发经验之为离线优先的应用程序设计数据层
- 基础-MySQL
- The difference between NPM and yarn
- Global and Chinese markets of children's electronic thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL project 7 Summary
- Global and Chinese market of amateur football helmets 2022-2028: Research Report on technology, participants, trends, market size and share
- Reasons for "unresolved external symbols" during vs or QT compilation link:
- Moore vote, leetcode169, leetcode229
猜你喜欢

Redis knowledge mind map

Linux下安裝Mysql【詳細】

MySQL第十一作業-視圖的應用

Basic MySQL

量化投资学习——经典书籍介绍

Using reflection to export entity data to excel

Flutter and native communication (Part 1)

Using foreach to loop two-dimensional array

Enter a positive integer with no more than 5 digits, and output the last digit in reverse order

开发者,微服务架构到底是什么?
随机推荐
CentOS installs redis multi master multi slave cluster
MySQL 13th job - transaction management
Notes - simple but adequate series_ KVM quick start
Mysql 30条军规
mysql性能监控和sql语句
服务器单、双向可调一键互信脚本!
Global and Chinese market of electronic pet door 2022-2028: Research Report on technology, participants, trends, market size and share
ceph运维常用指令
Common interview questions of binary tree
工作汇报(2)
Using reflection to export entity data to excel
JWT (SSO scheme) + three ways of identity authentication
MySQL第七次作业-更新数据
Redis knowledge mind map
Redis中执行Lua脚本
Développeur, quelle est l'architecture des microservices?
Introduction to sysbench Basics
Matrix fast power notes
【在线仿真】Arduino UNO PWM 控制直流电机转速
哪些PHP开源作品值得关注