cities.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. import Cities from '../../models/v1/cities';
  3. import http from 'http'
  4. class CityHandle {
  5. constructor(){
  6. this.cityGuess = this.cityGuess.bind(this);
  7. }
  8. async cityGuess(req, res, next){
  9. const type = req.query.type;
  10. if (!type) {
  11. res.json({
  12. name: 'ERROR_QUERY_TYPE',
  13. message: '参数错误',
  14. })
  15. return
  16. }
  17. let cityInfo;
  18. switch (type){
  19. case 'guess':
  20. const city = this.getCityName(req);
  21. // console.log(ip)
  22. cityInfo = await Cities.cityGuess('shanghai');
  23. break;
  24. case 'hot':
  25. cityInfo = await Cities.cityHot();
  26. break;
  27. case 'group':
  28. cityInfo = await Cities.cityGroup();
  29. break;
  30. }
  31. res.send(cityInfo)
  32. }
  33. getCityName(req){
  34. const ip = req.headers['x-forwarded-for'] ||
  35. req.connection.remoteAddress ||
  36. req.socket.remoteAddress ||
  37. req.connection.socket.remoteAddress;
  38. //调用阿里云接口
  39. http.get('http://saip.market.alicloudapi.com/ip?ip=' + ip,function(req,res){
  40. var html='';
  41. req.on('data',function(data){
  42. html+=data;
  43. });
  44. req.on('end',function(){
  45. console.info(html);
  46. });
  47. });
  48. }
  49. }
  50. export default new CityHandle()