同时支持微信小程序和一般浏览器的 Restful Http Client。
npm install hsnet-apikit
或者直接引用 src 文件夹中的文件
基础客户端(client.js)构造函数:
let apiClient = new HSNetApiClient({
mode: 'browser', //默认browser,小程序'mp'
baseUrl: 'http://xxx', //host
onBusinessError: model => {
//业务错误handler
},
onUnauthorized: response => {
//401验证错误handler
},
commonHeader: () => {
//用于加密的http header 验证实现 Sample
var timestamp = (Math.round(new Date().getTime() / 1000) + 28800).toString();
var nonce = newGuid();
var signature = hex_md5("" + timestamp + nonce).toUpperCase();
return {
timestamp,
nonce,
signature
}
},
isTokenExp = function (token) {
//判断token是否过期,注意token为null或者解析错误时,返回false
return false;
},
onTokenExp = (request) => {
//token过期时的处理函数,requset:过期前的最后一个请求
this.token = null;
// 重试获取token并继续上一个请求,返回值为Promise
return new Promise();
}
});
Api 配置文件构造函数:
export class ApiProvider {
//token以及POST方法
static token(username, password) {
let headers = {
"content-type": "application/x-www-form-urlencoded"
};
let data = new URLSearchParams();
data.set("grant_type", "password");
data.set("username", username);
data.set("password", password);
return new HSNetApiRequest("/api/Token")
.post()
.data(data)
.headers(headers)
.verifyModel(false);
}
//get方法
static getUserInfo() {
let urlSearchParams = new URLSearchParams();
urlSearchParams.set("id", "1");
return new HSNetApiRequest("/api/app/user/get_user_info").search(
urlSearchParams
);
}
}
请求 api:
let doLogin = function() {
this.apiClient
.request(ApiProvider.token(this.username, this.password))
.then(model => {
console.log(model);
this.apiClient.token = model.access_token;
})
.catch(error => {
console.log(error);
});
};
let getUserInfo = function() {
this.apiClient
.request(ApiProvider.getUserInfo())
.then(model => {
alert(model.data.name);
})
.catch(error => {
console.log(error);
});
};