@@ -12,16 +12,19 @@ module.exports = function(AV) {
1212 // 挂载一些配置
1313 let AVConfig = AV . _config ;
1414
15- _ . extend ( AVConfig , {
15+ // 服务器请求的节点 host
16+ const API_HOST = {
17+ cn : 'https://api.leancloud.cn' ,
18+ us : 'https://us-api.leancloud.cn'
19+ } ;
1620
17- // 服务器请求的节点 host
18- apiHost : {
19- cn : 'https://api.leancloud.cn' ,
20- us : 'https://us-api.leancloud.cn'
21- } ,
21+ _ . extend ( AVConfig , {
2222
2323 // 服务器节点地区,默认中国大陆
24- region : 'cn'
24+ region : 'cn' ,
25+
26+ // 服务器的 URL,默认初始化时被设置为大陆节点地址
27+ apiServerUrl : AVConfig . apiServerUrl || ''
2528 } ) ;
2629
2730 /**
@@ -95,28 +98,29 @@ module.exports = function(AV) {
9598 * @param {String } applicationId Your AV Application ID.
9699 * @param {String } applicationKey Your AV Application Key
97100 */
98- const initialize = ( applicationId , applicationKey , masterKey ) => {
99- if ( AV . applicationId !== undefined &&
100- applicationId !== AV . applicationId &&
101- applicationKey !== AV . applicationKey &&
102- masterKey !== AV . masterKey ) {
103- console . warn ( 'LeanCloud SDK is already initialized, please don\'t reinitialize it.' ) ;
104- }
105- AV . applicationId = applicationId ;
106- AV . applicationKey = applicationKey ;
101+ const initialize = ( appId , appKey , masterKey ) => {
102+ if ( AV . applicationId && appId !== AV . applicationId && appKey !== AV . applicationKey && masterKey !== AV . masterKey ) {
103+ console . warn ( 'LeanCloud SDK is already initialized, please do not reinitialize it.' ) ;
104+ }
105+ AV . applicationId = appId ;
106+ AV . applicationKey = appKey ;
107107 AV . masterKey = masterKey ;
108108 AV . _useMasterKey = false ;
109109 } ;
110110
111- const setRegion = ( region ) => {
111+ const setRegionServer = ( region ) => {
112112 // 服务器地区选项,默认为中国大陆
113113 switch ( region ) {
114- case 'cn' :
115- AVConfig . region = 'cn' ;
116- break ;
117114 case 'us' :
118115 AVConfig . region = 'us' ;
119116 break ;
117+ case 'cn' :
118+ default :
119+ AVConfig . region = 'cn' ;
120+ break ;
121+ }
122+ if ( ! AVConfig . apiServerUrl ) {
123+ AVConfig . apiServerUrl = API_HOST [ AVConfig . region ] ;
120124 }
121125 } ;
122126
@@ -139,7 +143,7 @@ module.exports = function(AV) {
139143 throw new Error ( 'AV.init(): Master Key is only used in Node.js.' ) ;
140144 }
141145 initialize ( options . appId , options . appKey , options . masterKey ) ;
142- setRegion ( options . region ) ;
146+ setRegionServer ( options . region ) ;
143147 } else {
144148 throw new Error ( 'AV.init(): Parameter is not correct.' ) ;
145149 }
@@ -152,6 +156,7 @@ module.exports = function(AV) {
152156 throw new Error ( 'AV.init(): Master Key is only used in Node.js.' ) ;
153157 }
154158 initialize ( ...args ) ;
159+ setRegionServer ( 'cn' ) ;
155160 break ;
156161 }
157162 } ;
@@ -194,7 +199,7 @@ module.exports = function(AV) {
194199 **/
195200 // TODO: 后续不再暴露此接口
196201 AV . useAVCloudCN = function ( ) {
197- AVConfig . region = 'cn' ;
202+ setRegionServer ( 'cn' ) ;
198203 console . warn ( 'Do not use AV.useAVCloudCN. Please use AV.init(), you can set the region of server.' ) ;
199204 } ;
200205
@@ -203,7 +208,7 @@ module.exports = function(AV) {
203208 **/
204209 // TODO: 后续不再暴露此接口
205210 AV . useAVCloudUS = function ( ) {
206- AVConfig . region = 'us' ;
211+ setRegionServer ( 'us' ) ;
207212 console . warn ( 'Do not use AV.useAVCloudUS. Please use AV.init(), you can set the region of server.' ) ;
208213 } ;
209214
@@ -344,7 +349,11 @@ module.exports = function(AV) {
344349 }
345350
346351 // 兼容 AV.serverURL 旧方式设置 API Host,后续去掉
347- let apiUrl = AV . serverURL || AVConfig . apiHost [ AVConfig . region ] ;
352+ let apiUrl = AV . serverURL || AVConfig . apiServerUrl ;
353+ if ( AV . serverURL ) {
354+ AVConfig . apiServerUrl = AV . serverURL ;
355+ console . warn ( 'Please use AV._config.apiServerUrl replace AV.serverURL .' ) ;
356+ }
348357 if ( apiUrl . charAt ( apiUrl . length - 1 ) !== "/" ) {
349358 apiUrl += "/" ;
350359 }
0 commit comments