|
@@ -78,42 +78,32 @@ module.exports = app => {
|
|
/**
|
|
/**
|
|
* 发送请求获取详细地址
|
|
* 发送请求获取详细地址
|
|
* @param {String} ip - ip地址
|
|
* @param {String} ip - ip地址
|
|
- * @param {Number} max - 最大重试次数
|
|
|
|
* @return {Object} the result of request
|
|
* @return {Object} the result of request
|
|
* @private
|
|
* @private
|
|
*/
|
|
*/
|
|
- async sendRequest(ip, max = 3) {
|
|
|
|
- return new Promise(resolve => {
|
|
|
|
- const start = () => {
|
|
|
|
- if (max <= 0) {
|
|
|
|
- resolve(); // 已达到最大重试次数,返回空的执行承若
|
|
|
|
- }
|
|
|
|
- max--;
|
|
|
|
- this.ctx.curl(`https://api01.aliyun.venuscn.com/ip?ip=${ip}`, {
|
|
|
|
- dateType: 'json',
|
|
|
|
- encoding: 'utf8',
|
|
|
|
- timeout: 2000,
|
|
|
|
- headers: {
|
|
|
|
- Authorization: 'APPCODE 85c64bffe70445c4af9df7ae31c7bfcc',
|
|
|
|
- },
|
|
|
|
- }).then(({ status, data }) => {
|
|
|
|
- if (status === 200) {
|
|
|
|
- const result = JSON.parse(data.toString()).data;
|
|
|
|
- if (!result.ip) {
|
|
|
|
- start();
|
|
|
|
- } else {
|
|
|
|
- max++;
|
|
|
|
- resolve(result);
|
|
|
|
- }
|
|
|
|
|
|
+ async sendRequest(ip) {
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ this.ctx.curl(`https://api01.aliyun.venuscn.com/ip?ip=${ip}`, {
|
|
|
|
+ dateType: 'json',
|
|
|
|
+ encoding: 'utf8',
|
|
|
|
+ timeout: 2000,
|
|
|
|
+ headers: {
|
|
|
|
+ Authorization: 'APPCODE 85c64bffe70445c4af9df7ae31c7bfcc',
|
|
|
|
+ },
|
|
|
|
+ }).then(({ status, data }) => {
|
|
|
|
+ if (status === 200) {
|
|
|
|
+ const result = JSON.parse(data.toString()).data;
|
|
|
|
+ if (!result.ip) {
|
|
|
|
+ resolve();
|
|
} else {
|
|
} else {
|
|
- max--;
|
|
|
|
- start();
|
|
|
|
|
|
+ resolve(result);
|
|
}
|
|
}
|
|
- }).catch(() => {
|
|
|
|
- start();
|
|
|
|
- });
|
|
|
|
- };
|
|
|
|
- start();
|
|
|
|
|
|
+ } else {
|
|
|
|
+ resolve();
|
|
|
|
+ }
|
|
|
|
+ }).catch(error => {
|
|
|
|
+ reject(error);
|
|
|
|
+ });
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|