@@ -130,39 +130,31 @@ const init = (AV) => {
130130 } ;
131131
132132 const setRegionServer = ( region = 'cn' ) => {
133- // AVConfig.region = region;
134- // AVConfig.APIServerURL = API_HOST[region];
135- // if (region === 'cn') {
136- // Cache.get('APIServerURL').then(cachedServerURL => {
137- // if (cachedServerURL) {
138- // return cachedServerURL;
139- // } else {
140- // return ajax('get', `http://app-router.leancloud.cn/route?appId=${AV.applicationId}`)
141- // .then(servers => {
142- // if (servers.api_server) {
143- // Cache.set('APIServerURL', servers.api_server, 30 * 24 * 3600000);
144- // return servers.api_server;
145- // }
146- // });
147- // }
148- // }).then(serverURL => {
149- // if (AVConfig.APIServerURL === API_HOST[region]) {
150- // AVConfig.APIServerURL = serverURL;
151- // }
152- // })
153- // }
154-
155- // 服务器地区选项,默认为中国大陆
156- switch ( region ) {
157- case 'us' :
158- AVConfig . region = 'us' ;
159- break ;
160- default :
161- AVConfig . region = 'cn' ;
162- break ;
163- }
164- if ( ! AVConfig . APIServerURL ) {
165- AVConfig . APIServerURL = API_HOST [ AVConfig . region ] ;
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 ( 'APIServerURL' , servers . api_server , ( servers . ttl || 3600 ) * 1000 ) ;
148+ return servers . api_server ;
149+ }
150+ } ) ;
151+ }
152+ } ) . then ( serverURL => {
153+ // 如果用户在 init 之后设置了 APIServerURL,保持用户设置
154+ if ( AVConfig . APIServerURL === API_HOST [ region ] ) {
155+ AVConfig . APIServerURL = `https://${ serverURL } ` ;
156+ }
157+ } )
166158 }
167159 } ;
168160
0 commit comments