Procházet zdrojové kódy

修复v1接口代理https链接出现错误的情况

Dotos před 8 roky
rodič
revize
ec34b31a7a
5 změnil soubory, kde provedl 75 přidání a 57 odebrání
  1. 44 41
      package.json
  2. 5 2
      routes/joke.js
  3. 12 6
      routes/mobile.js
  4. 6 3
      routes/v1.js
  5. 8 5
      routes/weather.js

+ 44 - 41
package.json

@@ -1,42 +1,45 @@
 {
-  "name": "JsonBird",
-  "version": "1.0.0",
-  "description": "A Remote Json Agent Service",
-  "author": "xCss",
-  "license": "MIT",
-  "private": true,
-  "scripts": {
-    "start": "node ./bin/www"
-  },
-  "repository": {
-    "type": "git",
-    "url": "git+https://github.com/xCss/JsonBird.git"
-  },
-  "keywords": [
-    "JsonBird",
-    "JsonProxy"
-  ],
-  "bugs": {
-    "url": "https://github.com/xCss/JsonBird/issues"
-  },
-  "readme": "./readme.md",
-  "homepage": "https://github.com/xCss/JsonBird#readme",
-  "dependencies": {
-    "bcryptjs": "^2.3.0",
-    "body-parser": "~1.15.1",
-    "cookie-parser": "~1.4.3",
-    "debug": "~2.2.0",
-    "express": "~4.13.4",
-    "express-generator": "^4.13.4",
-    "moment": "^2.15.0",
-    "moment-timezone": "^0.5.5",
-    "morgan": "~1.7.0",
-    "mysql": "^2.11.1",
-    "node-sass": "^3.10.0",
-    "node-sass-middleware": "^0.9.8",
-    "pug": "^2.0.0-beta6",
-    "querystring": "^0.2.0",
-    "request": "^2.74.0",
-    "serve-favicon": "~2.3.0"
-  }
-}
+    "name": "JsonBird",
+    "version": "1.3.1",
+    "description": "A Remote Json Agent Service",
+    "author": {
+        "name": "xCss",
+        "mail": "yy.liu@foxmail.com"
+    },
+    "license": "MIT",
+    "private": true,
+    "scripts": {
+        "start": "node ./bin/www"
+    },
+    "repository": {
+        "type": "git",
+        "url": "git+https://github.com/xCss/JsonBird.git"
+    },
+    "keywords": [
+        "JsonBird",
+        "JsonProxy"
+    ],
+    "bugs": {
+        "url": "https://github.com/xCss/JsonBird/issues"
+    },
+    "readme": "./readme.md",
+    "homepage": "https://github.com/xCss/JsonBird#readme",
+    "dependencies": {
+        "bcryptjs": "^2.3.0",
+        "body-parser": "~1.15.1",
+        "cookie-parser": "~1.4.3",
+        "debug": "~2.2.0",
+        "express": "~4.13.4",
+        "express-generator": "^4.13.4",
+        "moment": "^2.15.0",
+        "moment-timezone": "^0.5.5",
+        "morgan": "~1.7.0",
+        "mysql": "^2.11.1",
+        "node-sass": "^3.10.0",
+        "node-sass-middleware": "^0.9.8",
+        "pug": "^2.0.0-beta6",
+        "querystring": "^0.2.0",
+        "request": "^2.74.0",
+        "serve-favicon": "~2.3.0"
+    }
+}

+ 5 - 2
routes/joke.js

@@ -99,8 +99,11 @@ function getJOKE(req, res, next, op) {
             }
         } else {
             var error = {
-                code: -1,
-                message: body.reason
+                data: {},
+                status: {
+                    code: -1,
+                    message: body.reason
+                }
             };
             res.json(error);
         }

+ 12 - 6
routes/mobile.js

@@ -5,24 +5,26 @@ var router = express.Router();
 var bodyParser = require('body-parser');
 var urlencodedParser = bodyParser.urlencoded({ extended: false });
 var key = "9f719ab7014f2cbdc7b394edf70d0f76";
-var url = 'http://apis.juhe.cn/mobile/get?key=' + key;
+var base = 'http://apis.juhe.cn/mobile/get?key=' + key;
 var type = '';
 var callback = '';
+var phone = '';
 router.get('/', function(req, res, next) {
-    url += '&phone=' + req.query.phone;
+    phone = req.query.phone;
     type = req.query.type === 'xml' ? 'xml' : '';
     callback = req.query.callback;
     getMobile(req, res, next);
 });
 router.post('/', urlencodedParser, function(req, res, next) {
-    url += '&phone=' + req.body.phone;
+    phone = req.body.phone;
     type = req.body.type === 'xml' ? 'xml' : '';
     callback = req.body.callback;
     getMobile(req, res, next);
 });
 
 function getMobile(req, res, next) {
-    url += !!type ? '&dtype=' + type : '';
+    var url = !!type ? base + '&dtype=' + type : base;
+    url += '&phone=' + phone;
     request(url, function(err, response, body) {
         if (!type) {
             body = JSON.parse(body);
@@ -41,12 +43,16 @@ function getMobile(req, res, next) {
                 }
             } else {
                 var error = {
-                    code: -1,
-                    message: body.reason
+                    data: {},
+                    status: {
+                        code: -1,
+                        message: body.reason
+                    }
                 };
                 res.json(error);
             }
         } else {
+            res.header('content-type', 'text/xml');
             res.send(body);
         }
     });

+ 6 - 3
routes/v1.js

@@ -33,7 +33,7 @@ router.get('/*', function(req, res, next) {
     } else {
         var url = originalUrl.split('url=')[1];
         url = url.indexOf('?') === -1 ? url.replace('&', '?') : url;
-        url = url.indexOf('http://') === -1 ? 'http://' + url : url;
+        url = /^(http|https)\:\/\//.test(url) ? url : 'http://' + url;
         getJSON(url, next, function(data) {
             var output = {
                 data: data,
@@ -62,8 +62,11 @@ function getJSON(url, next, callback) {
             callback && callback(body);
         } else {
             var error = {
-                code: -1,
-                message: body.reason
+                data: {},
+                status: {
+                    code: -1,
+                    message: body.reason
+                }
             };
             res.json(error);
         }

+ 8 - 5
routes/weather.js

@@ -6,7 +6,7 @@ var router = express.Router();
 var bodyParser = require('body-parser');
 var urlencodedParser = bodyParser.urlencoded({ extended: false });
 var key = "e0540a109f5a73e9df2981cdeb9d106f";
-var url = 'http://op.juhe.cn/onebox/weather/query?key=' + key;
+var base = 'http://op.juhe.cn/onebox/weather/query?key=' + key;
 var type = '';
 var callback = '';
 var city = '';
@@ -24,8 +24,7 @@ router.post('/', urlencodedParser, function(req, res, next) {
 
 function getMobile(req, res, next) {
     city = !!city ? qs.escape(city) : '';
-    console.log(city);
-    url += type === 'xml' ? '&dtype=xml' : '';
+    var url = type === 'xml' ? base + '&dtype=xml' : base;
     url += '&cityname=' + city;
     request(url, function(err, response, body) {
         if (!type) {
@@ -45,12 +44,16 @@ function getMobile(req, res, next) {
                 }
             } else {
                 var error = {
-                    code: -1,
-                    message: body.reason
+                    data: {},
+                    status: {
+                        code: -1,
+                        message: body.reason
+                    }
                 };
                 res.json(error);
             }
         } else {
+            res.header('content-type', 'text/xml');
             res.send(body);
         }
     });