|
@@ -1,8 +1,9 @@
|
|
|
'use strict';
|
|
|
|
|
|
import Cities from '../../models/v1/cities';
|
|
|
-import http from 'http'
|
|
|
-var pinyin = require("pinyin");
|
|
|
+import http from 'http';
|
|
|
+import pinyin from "pinyin";
|
|
|
+import fetch from 'node-fetch';
|
|
|
|
|
|
|
|
|
class CityHandle {
|
|
@@ -34,35 +35,34 @@ class CityHandle {
|
|
|
res.send(cityInfo)
|
|
|
}
|
|
|
getCityName(req){
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
let ip = req.headers['x-forwarded-for'] ||
|
|
|
req.connection.remoteAddress ||
|
|
|
req.socket.remoteAddress ||
|
|
|
req.connection.socket.remoteAddress;
|
|
|
const ipArr = ip.split(':');
|
|
|
ip = ipArr[ipArr.length -1];
|
|
|
- //调用新浪接口
|
|
|
- http.get('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' + ip,
|
|
|
- (req,res) => {
|
|
|
- let data;
|
|
|
- req.on('data',res => {
|
|
|
- res = res.toString();
|
|
|
- const subIndex = res.indexOf('city');
|
|
|
- data = res.substring(subIndex,res.indexOf(',', subIndex));
|
|
|
- data = data.split(':')[1].replace(/"/gi, '');
|
|
|
- });
|
|
|
- req.on('end',() => {
|
|
|
- data = unescape(data.replace(/\\u/g, '%u'));
|
|
|
- data = pinyin(data, {
|
|
|
- style: pinyin.STYLE_NORMAL,
|
|
|
- });
|
|
|
- let city = '';
|
|
|
- data.forEach(item => {
|
|
|
- city += item[0];
|
|
|
- })
|
|
|
- resolve(city)
|
|
|
- });
|
|
|
- });
|
|
|
+ // ip = '116.231.55.195';
|
|
|
+ /*
|
|
|
+ 调用新浪接口,获取ip地址信息
|
|
|
+ */
|
|
|
+ const url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' + ip;
|
|
|
+ let res;
|
|
|
+ try{
|
|
|
+ res = await fetch(url);
|
|
|
+ res = await res.text();
|
|
|
+ }catch(err){
|
|
|
+ console.log(err)
|
|
|
+ }
|
|
|
+ const cityInfo = JSON.parse(res.split('=')[1].toString().replace(';', ''))
|
|
|
+ const pinyinArr = pinyin(cityInfo.city, {
|
|
|
+ style: pinyin.STYLE_NORMAL,
|
|
|
+ });
|
|
|
+ let city = '';
|
|
|
+ pinyinArr.forEach(item => {
|
|
|
+ city += item[0];
|
|
|
+ })
|
|
|
+ resolve(city)
|
|
|
})
|
|
|
}
|
|
|
}
|