|
@@ -1,7 +1,8 @@
|
|
'use strict';
|
|
'use strict';
|
|
|
|
|
|
import Cities from '../../models/v1/cities';
|
|
import Cities from '../../models/v1/cities';
|
|
-import http from 'http'
|
|
|
|
|
|
+import http from 'http'
|
|
|
|
+var pinyin = require("pinyin");
|
|
|
|
|
|
|
|
|
|
class CityHandle {
|
|
class CityHandle {
|
|
@@ -20,9 +21,8 @@ class CityHandle {
|
|
let cityInfo;
|
|
let cityInfo;
|
|
switch (type){
|
|
switch (type){
|
|
case 'guess':
|
|
case 'guess':
|
|
- const city = this.getCityName(req);
|
|
|
|
- // console.log(ip)
|
|
|
|
- cityInfo = await Cities.cityGuess('shanghai');
|
|
|
|
|
|
+ const city = await this.getCityName(req);
|
|
|
|
+ cityInfo = await Cities.cityGuess(city);
|
|
break;
|
|
break;
|
|
case 'hot':
|
|
case 'hot':
|
|
cityInfo = await Cities.cityHot();
|
|
cityInfo = await Cities.cityHot();
|
|
@@ -34,23 +34,37 @@ class CityHandle {
|
|
res.send(cityInfo)
|
|
res.send(cityInfo)
|
|
}
|
|
}
|
|
getCityName(req){
|
|
getCityName(req){
|
|
- 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];
|
|
|
|
- console.log(ip)
|
|
|
|
- //调用阿里云接口
|
|
|
|
- http.get('http://saip.market.alicloudapi.com/ip?ip=' + ip,function(req,res){
|
|
|
|
- var html='';
|
|
|
|
- req.on('data',function(data){
|
|
|
|
- html+=data;
|
|
|
|
- });
|
|
|
|
- req.on('end',function(){
|
|
|
|
- console.info(html);
|
|
|
|
- });
|
|
|
|
- });
|
|
|
|
|
|
+ return new Promise((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];
|
|
|
|
+ ip = "116.231.55.195";
|
|
|
|
+ //调用新浪接口
|
|
|
|
+ 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)
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
export default new CityHandle()
|
|
export default new CityHandle()
|