maguohua 8 ani în urmă
părinte
comite
a359df347b
3 a modificat fișierele cu 44 adăugiri și 25 ștergeri
  1. 1 1
      README.md
  2. 22 22
      app.js
  3. 21 2
      controller/v1/cities.js

+ 1 - 1
README.md

@@ -145,4 +145,4 @@ npm run dev  访问: http://localhost:3000
 
 ## License
 
-[MIT](LICENSE)
+[MIT](https://mit-license.org/)

+ 22 - 22
app.js

@@ -43,31 +43,31 @@ app.use((req, res, next) => {
     next();
 });
 
-app.use(expressWinston.logger({
-    transports: [
-        new (winston.transports.Console)({
-          json: true,
-          colorize: true
-        }),
-        new winston.transports.File({
-          filename: 'logs/success.log'
-        })
-    ]
-}));
+// app.use(expressWinston.logger({
+//     transports: [
+//         new (winston.transports.Console)({
+//           json: true,
+//           colorize: true
+//         }),
+//         new winston.transports.File({
+//           filename: 'logs/success.log'
+//         })
+//     ]
+// }));
 
 router(app);
 
-app.use(expressWinston.errorLogger({
-    transports: [
-        new winston.transports.Console({
-          json: true,
-          colorize: true
-        }),
-        new winston.transports.File({
-          filename: 'logs/error.log'
-        })
-    ]
-}));
+// app.use(expressWinston.errorLogger({
+//     transports: [
+//         new winston.transports.Console({
+//           json: true,
+//           colorize: true
+//         }),
+//         new winston.transports.File({
+//           filename: 'logs/error.log'
+//         })
+//     ]
+// }));
 
 app.use((err, req, res, next) => {
 	res.send('未找到当前路由');

+ 21 - 2
controller/v1/cities.js

@@ -1,10 +1,12 @@
 'use strict';
 
 import Cities from '../../models/v1/cities';
+import http from 'http'  
+
 
 class CityHandle {
 	constructor(){
-
+		this.cityGuess = this.cityGuess.bind(this);
 	}
 	async cityGuess(req, res, next){
 		const type = req.query.type;
@@ -18,6 +20,8 @@ class CityHandle {
 		let cityInfo;
 		switch (type){
 			case 'guess': 
+				const city = this.getCityName(req);
+				// console.log(ip)
 				cityInfo = await Cities.cityGuess('shanghai');
 				break;
 			case 'hot': 
@@ -29,6 +33,21 @@ class CityHandle {
 		}
 		res.send(cityInfo)
 	}
+	getCityName(req){
+		const ip = req.headers['x-forwarded-for'] || 
+ 		req.connection.remoteAddress || 
+ 		req.socket.remoteAddress ||
+ 		req.connection.socket.remoteAddress;
+     	//调用阿里云接口
+		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);  
+		    });  
+		});  
+	}
 }
-
 export default new CityHandle()