66'use strict' ;
77
88const AVPromise = require ( '../promise' ) ;
9- const AVUtils = require ( '../utils' ) ;
9+ const md5 = require ( 'md5' ) ;
10+
11+ // 计算 X-LC-Sign 的签名方法
12+ const sign = ( key , isMasterKey ) => {
13+ const now = new Date ( ) . getTime ( ) ;
14+ const signature = md5 ( now + key ) ;
15+ if ( isMasterKey ) {
16+ return signature + ',' + now + ',master' ;
17+ } else {
18+ return signature + ',' + now ;
19+ }
20+ } ;
1021
1122const ajax = ( method , url , data , success , error ) => {
1223 const AV = global . AV ;
@@ -21,6 +32,16 @@ const ajax = (method, url, data, success, error) => {
2132 const appKey = AV . applicationKey ;
2233 const masterKey = AV . masterKey ;
2334
35+ // 清理
36+ if ( data ) {
37+ delete data . _ApplicationId ;
38+ delete data . _ApplicationKey ;
39+ delete data . _ApplicationProduction ;
40+ delete data . _MasterKey ;
41+ delete data . _ClientVersion ;
42+ delete data . _InstallationId ;
43+ }
44+
2445 let handled = false ;
2546 const xhr = new global . XMLHttpRequest ( ) ;
2647 xhr . onreadystatechange = ( ) => {
@@ -47,19 +68,34 @@ const ajax = (method, url, data, success, error) => {
4768 }
4869 }
4970 } ;
71+
72+ if ( method . toLowerCase ( ) === 'get' ) {
73+ let i = 0 ;
74+ for ( let k in data ) {
75+ if ( i === 0 ) {
76+ url = url + '?' ;
77+ } else {
78+ url = url + '&' ;
79+ }
80+ url = url + k + '=' + JSON . stringify ( data [ k ] ) ;
81+ i ++ ;
82+ }
83+ }
84+
5085 xhr . open ( method , url , true ) ;
5186 xhr . setRequestHeader ( 'X-LC-Id' , appId ) ;
5287
5388 let signature ;
5489 if ( masterKey ) {
55- signature = AVUtils . sign ( masterKey , true ) ;
90+ signature = sign ( masterKey , true ) ;
5691 } else {
57- signature = AVUtils . sign ( appKey ) ;
92+ signature = sign ( appKey ) ;
5893 }
5994
6095 xhr . setRequestHeader ( 'X-LC-Sign' , signature ) ;
96+ // xhr.setRequestHeader('X-LC-UA', 'AV-web-' + AV.version);
6197 xhr . setRequestHeader ( 'Content-Type' , 'application/json;charset=UTF-8' ) ;
62- xhr . send ( data ) ;
98+ xhr . send ( JSON . stringify ( data ) ) ;
6399 return promise . _thenRunCallbacks ( options ) ;
64100} ;
65101
0 commit comments