|
7 | 7 |
|
8 | 8 | const _ = require('underscore'); |
9 | 9 | const ajax = require('./browserify-wrapper/ajax'); |
| 10 | +const Cache = require('./cache'); |
| 11 | +const md5 = require('md5'); |
| 12 | +const debug = require('debug')('utils'); |
| 13 | + |
| 14 | +// 计算 X-LC-Sign 的签名方法 |
| 15 | +const sign = (key, isMasterKey) => { |
| 16 | + const now = new Date().getTime(); |
| 17 | + const signature = md5(now + key); |
| 18 | + if (isMasterKey) { |
| 19 | + return signature + ',' + now + ',master'; |
| 20 | + } else { |
| 21 | + return signature + ',' + now; |
| 22 | + } |
| 23 | +}; |
10 | 24 |
|
11 | 25 | const init = (AV) => { |
12 | 26 |
|
@@ -115,18 +129,35 @@ const init = (AV) => { |
115 | 129 | AV._useMasterKey = false; |
116 | 130 | }; |
117 | 131 |
|
118 | | - const setRegionServer = (region) => { |
119 | | - // 服务器地区选项,默认为中国大陆 |
120 | | - switch (region) { |
121 | | - case 'us': |
122 | | - AVConfig.region = 'us'; |
123 | | - break; |
124 | | - default: |
125 | | - AVConfig.region = 'cn'; |
126 | | - break; |
127 | | - } |
128 | | - if (!AVConfig.APIServerURL) { |
129 | | - AVConfig.APIServerURL = API_HOST[AVConfig.region]; |
| 132 | + const setRegionServer = (region = 'cn') => { |
| 133 | + AVConfig.region = region; |
| 134 | + // 如果用户在 init 之前设置了 APIServerURL,则跳过请求 router |
| 135 | + if (AVConfig.APIServerURL) { |
| 136 | + return; |
| 137 | + } |
| 138 | + AVConfig.APIServerURL = API_HOST[region]; |
| 139 | + if (region === 'cn') { |
| 140 | + Cache.get('APIServerURL').then(cachedServerURL => { |
| 141 | + if (cachedServerURL) { |
| 142 | + return cachedServerURL; |
| 143 | + } else { |
| 144 | + return ajax('get', `https://app-router.leancloud.cn/1/route?appId=${AV.applicationId}`) |
| 145 | + .then(servers => { |
| 146 | + if (servers.api_server) { |
| 147 | + Cache.set( |
| 148 | + 'APIServerURL', |
| 149 | + servers.api_server, |
| 150 | + (typeof servers.ttl ==='number' ? servers.ttl : 3600) * 1000); |
| 151 | + return servers.api_server; |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + }).then(serverURL => { |
| 156 | + // 如果用户在 init 之后设置了 APIServerURL,保持用户设置 |
| 157 | + if (AVConfig.APIServerURL === API_HOST[region]) { |
| 158 | + AVConfig.APIServerURL = `https://${serverURL}`; |
| 159 | + } |
| 160 | + }) |
130 | 161 | } |
131 | 162 | }; |
132 | 163 |
|
@@ -390,36 +421,44 @@ const init = (AV) => { |
390 | 421 | } |
391 | 422 | } |
392 | 423 |
|
393 | | - dataObject = _.clone(dataObject || {}); |
394 | | - dataObject._ApplicationId = AV.applicationId; |
395 | | - dataObject._ApplicationKey = AV.applicationKey; |
396 | | - if (!AV._isNullOrUndefined(AV.applicationProduction)) { |
397 | | - dataObject._ApplicationProduction = AV.applicationProduction; |
| 424 | + if (method.toLowerCase() === 'get') { |
| 425 | + if (apiURL.indexOf('?') === -1) { |
| 426 | + apiURL += '?'; |
| 427 | + } |
| 428 | + for (let k in dataObject) { |
| 429 | + if (typeof dataObject[k] === 'object') { |
| 430 | + dataObject[k] = JSON.stringify(dataObject[k]); |
| 431 | + } |
| 432 | + apiURL += '&' + k + '=' + encodeURIComponent(dataObject[k]); |
| 433 | + } |
| 434 | + } |
| 435 | + |
| 436 | + var headers = { |
| 437 | + 'X-LC-Id': AV.applicationId, |
| 438 | + 'X-LC-UA': 'LC-Web-' + AV.version, |
| 439 | + 'Content-Type': 'application/json;charset=UTF-8' |
| 440 | + }; |
| 441 | + if (AV.masterKey && AV._useMasterKey) { |
| 442 | + headers['X-LC-Sign'] = sign(AV.masterKey, true); |
| 443 | + } else { |
| 444 | + headers['X-LC-Sign'] = sign(AV.applicationKey); |
398 | 445 | } |
399 | | - if (AV._useMasterKey) { |
400 | | - dataObject._MasterKey = AV.masterKey; |
| 446 | + if (!AV._isNullOrUndefined(AV.applicationProduction)) { |
| 447 | + headers['X-LC-Prod'] = AV.applicationProduction; |
401 | 448 | } |
402 | | - dataObject._ClientVersion = AV.version; |
403 | 449 | return AV.Promise.as().then(function() { |
404 | 450 | // Pass the session token |
405 | 451 | if (sessionToken) { |
406 | | - dataObject._SessionToken = sessionToken; |
| 452 | + headers['X-LC-Session'] = sessionToken; |
407 | 453 | } else if (!AV._config.disableCurrentUser) { |
408 | 454 | return AV.User.currentAsync().then(function(currentUser) { |
409 | 455 | if (currentUser && currentUser._sessionToken) { |
410 | | - dataObject._SessionToken = currentUser._sessionToken; |
| 456 | + headers['X-LC-Session'] = currentUser._sessionToken; |
411 | 457 | } |
412 | 458 | }); |
413 | 459 | } |
414 | 460 | }).then(function() { |
415 | | - // Pass the installation id |
416 | | - if (!AV._config.disableCurrentUser) { |
417 | | - return AV._getInstallationId().then(function(installationId) { |
418 | | - dataObject._InstallationId = installationId; |
419 | | - }); |
420 | | - } |
421 | | - }).then(function() { |
422 | | - return AV._ajax(method, apiURL, dataObject).then(null, function(response) { |
| 461 | + return AV._ajax(method, apiURL, dataObject, headers).then(null, function(response) { |
423 | 462 | // Transform the error into an instance of AV.Error by trying to parse |
424 | 463 | // the error string as JSON. |
425 | 464 | var error; |
|
0 commit comments