浏览代码

fix: 获取ip地址请求次数改成请求1次

lanjianrong 4 年之前
父节点
当前提交
930866665a
共有 1 个文件被更改,包括 21 次插入31 次删除
  1. 21 31
      app/service/login_logging.js

+ 21 - 31
app/service/login_logging.js

@@ -78,42 +78,32 @@ module.exports = app => {
         /**
          * 发送请求获取详细地址
          * @param {String} ip - ip地址
-         * @param {Number} max - 最大重试次数
          * @return {Object} the result of request
          * @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 {
-                            max--;
-                            start();
+                            resolve(result);
                         }
-                    }).catch(() => {
-                        start();
-                    });
-                };
-                start();
+                    } else {
+                        resolve();
+                    }
+                }).catch(error => {
+                    reject(error);
+                });
             });
         }