简单封装万维易源(showapi)ip归属地查询接口

前言

简单封装一下万维易源归属地查询接口,因为官方文档已经写的很好了。

代码

const showapiSdk = require('showapi-sdk');

module.exports = api = {
    getLocalInfoByShow: async function (ip, appId, secret) {
        // {
        //   showapi_res_error: '',
        //   showapi_res_id: '5eb6109a8d57baae12fdf1d5',
        //   showapi_res_code: 0,
        //   showapi_res_body: {
        //     ret_code: 0,
        //     continents: '亚洲',
        //     country: '中国',
        //     region: '广东',
        //     city: '东莞',
        //     county: '',
        //     isp: '电信',
        //     city_code: '441900',
        //     en_name: 'China',
        //     en_name_short: 'CN',
        //     lnt: '113.760234',
        //     lat: '23.048884',
        //     ip: '116.4.201.181'
        //   }
        // }
        //开启debug
        //showapiSdk.debug(true);

        //全局默认设置
        //url 你要调用的API对应接入点的地址,注意需要先订购了相关套餐才能调
        //appId 你的应用id
        //secret 你的密钥
        //timeout http超时设置
        //options 默认请求参数,极少用到
        showapiSdk.setting({
            url: "http://route.showapi.com/20-1",
            appId: appId,
            secret: secret,
            timeout: 5000,
            options: {
                testParam: 'test'
            }
        });

        const request = showapiSdk.request();
        request.appendText('ip', ip);
        return new Promise((resolve, reject) => {
            request.post(function (data) {
                if (data.showapi_res_error) return reject(data.showapi_res_error);
                const ipinfo = data.showapi_res_body;
                resolve ({
                    ip_country: ipinfo.country,
                    ip_region: ipinfo.region,
                    ip_city: ipinfo.city,
                    ip_isp: ipinfo.isp
                })
            });
        });
    }
};

原文地址:https://www.cnblogs.com/xpengp/p/12855845.html