weather.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const express = require('express');
  2. const request = require('superagent');
  3. const utils = require('../utils/utils');
  4. const router = express.Router();
  5. const base = 'http://jisutqybmf.market.alicloudapi.com/weather/query';
  6. const APPCODE = process.env.APPCODE ;
  7. router.get('/*', function(req, res, next) {
  8. getWeather(req, res, next);
  9. });
  10. router.post('/*', function(req, res, next) {
  11. getWeather(req, res, next);
  12. });
  13. let getWeather = (req, res, next) => {
  14. let params = utils.convert(req,res,next,base);
  15. let config = params[0];
  16. let protocol = params[1];
  17. let host = params[2];
  18. let cb = params[3];
  19. let _params = params[4];
  20. console.log(config['headers'])
  21. let output = {
  22. data: {},
  23. status: {
  24. code: -1,
  25. message: '请确定你的请求方式像这样:/weather?city=番禺'
  26. }
  27. };
  28. let referer = config['headers']['referer']
  29. let origin = config['headers']['origin']
  30. if(referer.indexOf('180.169.17.10') || origin.indexOf('180.169.17.10')){
  31. output = {
  32. status: {
  33. code: -2,
  34. message: '你的使用量过大,如需解禁请联系hi@big.moe'
  35. }
  36. }
  37. res.send(output)
  38. return
  39. }
  40. if(_params['city']){
  41. config['headers'] = {
  42. "Host":"jisutqybmf.market.alicloudapi.com",
  43. "X-Ca-Timestamp":Date.now(),
  44. "gateway_channel":"http",
  45. "X-Ca-Request-Mode":"debug",
  46. "X-Ca-Key":"24605515",
  47. "X-Ca-Stage":"RELEASE",
  48. "Content-Type":"application/json; charset=utf-8",
  49. "Authorization":`APPCODE ${APPCODE}`
  50. }
  51. //config['gzip'] = '';
  52. utils.createServer(config).then(ret => {
  53. cb && res.jsonp(ret) || res.send(ret);
  54. }).catch(ex => {
  55. output = {
  56. status: {
  57. code: -2,
  58. message: Object.keys(ex).length>0 ? ex : 'unknow error, please checked your city name'
  59. }
  60. }
  61. console.log(`cb:${cb}`)
  62. cb && res.jsonp(output) || res.json(output);
  63. });
  64. }else{
  65. cb && res.jsonp(output) || res.json(output);
  66. }
  67. }
  68. module.exports = router;