alipay-mini-program-open-api-request-options
v0.3.3
Published
基于Java OpenApi服务器开发框架以及alipay-mini-program-request-options包的请求拓展
Downloads
9
Readme
alipay-mini-program-open-api-request-options
基于Easy Open Api服务器开发框架以及alipay-mini-program-request-options包的请求拓展
alipay-mini-program-request-options
网络请求基于支付宝小程序网络请求Api的二次封装
NPM信息
使用
添加默认配置
const OpenApi = require('alipay-mini-program-open-api-request-options')
// 默认配置
const defaultConfig = {
version: '1.0',
app_key: 'test',
format: 'json',
app_secret: '123456',
url: `请求链接URL`
}
// 创建实例
const openApiInstance = new OpenApi(defaultConfig)
// 添加默认配置
openApiInstance.setConfig(defaultConfig)
网络请求
// 创建请求实例
openApiInstance.newOptions()
.setName('apiName')
.setData({
keyword: 'test'
})
.setHeaders({
'content-type': 'application/json;charset=UTF-8'
})
.setTimeout(60000)
.request(function () {
console.log('completed');
})
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
Api
|Name|Param name array|Param type array|Return| | ------ | ------ | ------ | --- | | setUrl | [url] | [String] | this | | setName | [name] | [String] | this | | setMethod | [method] | [String] | this | | setHeaders | [headers] | [Object] | this | | setTimeout | [timeout] | [Integer] | this | | setData | [data] | [Object] | this | | request | [completeCallback] | [Function] | Promise |
文件上传
const { FileTypes } = require("alipay-mini-program-request-options");
// 目前支付宝只支持图片、视频、音频3种文件类型
const { IMAGE, VIDEO, AUDIO } = FileTypes;
openApiInstance.newUploadOptions()
.setName('apiName')
.setHeaders({})
.setFile(`文件路径`, `文件名`, IMAGE)
.upload(function () {
console.log('completed');
})
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
Api
|Name|Param name array|Param type array|Return| | ------ | ------ | ------ | --- | | setUrl | [url] | [String] | this | | setName | [name] | [String] | this | | setHeaders | [headers] | [Object] | this | | setFile | [filePath, fileName, fileType] | [String, String, String] | this | | setFilePath | [filePath] | [String] | this | | setFileName | [fileName] | [String] | this | | setFileType | [fileType] | [String] | this | | upload | [completeCallback] | [Function] | Promise |