|
8 | 8 | const _ = require('underscore'); |
9 | 9 | const ajax = require('./browserify-wrapper/ajax'); |
10 | 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 | +}; |
11 | 24 |
|
12 | 25 | const init = (AV) => { |
13 | 26 |
|
@@ -413,36 +426,44 @@ const init = (AV) => { |
413 | 426 | } |
414 | 427 | } |
415 | 428 |
|
416 | | - dataObject = _.clone(dataObject || {}); |
417 | | - dataObject._ApplicationId = AV.applicationId; |
418 | | - dataObject._ApplicationKey = AV.applicationKey; |
419 | | - if (!AV._isNullOrUndefined(AV.applicationProduction)) { |
420 | | - dataObject._ApplicationProduction = AV.applicationProduction; |
| 429 | + if (method.toLowerCase() === 'get') { |
| 430 | + if (apiURL.indexOf('?') === -1) { |
| 431 | + apiURL += '?'; |
| 432 | + } |
| 433 | + for (let k in dataObject) { |
| 434 | + if (typeof dataObject[k] === 'object') { |
| 435 | + dataObject[k] = JSON.stringify(dataObject[k]); |
| 436 | + } |
| 437 | + apiURL += '&' + k + '=' + encodeURIComponent(dataObject[k]); |
| 438 | + } |
| 439 | + } |
| 440 | + |
| 441 | + var headers = { |
| 442 | + 'X-LC-Id': AV.applicationId, |
| 443 | + 'X-LC-UA': 'LC-Web-' + AV.version, |
| 444 | + 'Content-Type': 'application/json;charset=UTF-8' |
| 445 | + }; |
| 446 | + if (AV.masterKey && AV._useMasterKey) { |
| 447 | + headers['X-LC-Sign'] = sign(AV.masterKey, true); |
| 448 | + } else { |
| 449 | + headers['X-LC-Sign'] = sign(AV.applicationKey); |
421 | 450 | } |
422 | | - if (AV._useMasterKey) { |
423 | | - dataObject._MasterKey = AV.masterKey; |
| 451 | + if (!AV._isNullOrUndefined(AV.applicationProduction)) { |
| 452 | + headers['X-LC-Prod'] = AV.applicationProduction; |
424 | 453 | } |
425 | | - dataObject._ClientVersion = AV.version; |
426 | 454 | return AV.Promise.as().then(function() { |
427 | 455 | // Pass the session token |
428 | 456 | if (sessionToken) { |
429 | | - dataObject._SessionToken = sessionToken; |
| 457 | + headers['X-LC-Session'] = sessionToken; |
430 | 458 | } else if (!AV._config.disableCurrentUser) { |
431 | 459 | return AV.User.currentAsync().then(function(currentUser) { |
432 | 460 | if (currentUser && currentUser._sessionToken) { |
433 | | - dataObject._SessionToken = currentUser._sessionToken; |
| 461 | + headers['X-LC-Session'] = currentUser._sessionToken; |
434 | 462 | } |
435 | 463 | }); |
436 | 464 | } |
437 | 465 | }).then(function() { |
438 | | - // Pass the installation id |
439 | | - if (!AV._config.disableCurrentUser) { |
440 | | - return AV._getInstallationId().then(function(installationId) { |
441 | | - dataObject._InstallationId = installationId; |
442 | | - }); |
443 | | - } |
444 | | - }).then(function() { |
445 | | - return AV._ajax(method, apiURL, dataObject).then(null, function(response) { |
| 466 | + return AV._ajax(method, apiURL, dataObject, headers).then(null, function(response) { |
446 | 467 | // Transform the error into an instance of AV.Error by trying to parse |
447 | 468 | // the error string as JSON. |
448 | 469 | var error; |
|
0 commit comments