@@ -156,11 +156,8 @@ async function createLocalServer(): Promise<ServerRes> {
156156}
157157
158158// 打开云开发控制台,获取授权
159- export async function getAuthTokenFromWeb (
160- options : ILoginOptions
161- ) : Promise < Credential > {
162- console . log ( options )
163- const { authUrl } = options
159+ export async function getAuthTokenFromWeb ( options : ILoginOptions ) : Promise < Credential > {
160+ const { getAuthUrl } = options
164161 return new Promise ( async ( resolve , reject ) => {
165162 const loading = loadingFactory ( )
166163 loading . start ( '正在打开腾讯云获取授权' )
@@ -175,39 +172,43 @@ export async function getAuthTokenFromWeb(
175172 throw new CloudBaseError ( '获取 Mac 地址失败,无法登录!' )
176173 }
177174
178- const CliAuthUrl =
179- authUrl ||
180- `${ CliAuthBaseUrl } ?port=${ port } &hash=${ hash } &mac=${ mac } &os=${ os } `
181- await open ( CliAuthUrl )
182-
183- loading . succeed (
184- '已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!'
185- )
186-
187- server . on (
188- 'request' ,
189- ( req : IncomingMessage , res : ServerResponse ) => {
190- const { url } = req
191- const { query } = queryString . parseUrl ( url )
192-
193- // CORS
194- res . writeHead ( 200 , {
195- 'Access-Control-Allow-Origin' : '*' ,
196- 'Content-Type' : 'text/plain' ,
197- // 立即关闭 http 连接
198- Connection : 'close'
199- } )
200-
201- res . end ( 'ok' )
202-
203- // 防止接受到异常请求导致本地服务关闭
204- if ( query && query . tmpToken ) {
205- server . close ( )
206- }
207-
208- resolve ( query as Credential )
175+ let cliAuthUrl = `${ CliAuthBaseUrl } ?port=${ port } &hash=${ hash } &mac=${ mac } &os=${ os } `
176+
177+ if ( getAuthUrl ) {
178+ try {
179+ cliAuthUrl = getAuthUrl (
180+ `${ CliAuthBaseUrl } ?port=${ port } &hash=${ hash } &mac=${ mac } &os=${ os } `
181+ )
182+ } catch ( error ) {
183+ // 忽略错误
209184 }
210- )
185+ }
186+
187+ await open ( cliAuthUrl )
188+
189+ loading . succeed ( '已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!' )
190+
191+ server . on ( 'request' , ( req : IncomingMessage , res : ServerResponse ) => {
192+ const { url } = req
193+ const { query } = queryString . parseUrl ( url )
194+
195+ // CORS
196+ res . writeHead ( 200 , {
197+ 'Access-Control-Allow-Origin' : '*' ,
198+ 'Content-Type' : 'text/plain' ,
199+ // 立即关闭 http 连接
200+ Connection : 'close'
201+ } )
202+
203+ res . end ( 'ok' )
204+
205+ // 防止接受到异常请求导致本地服务关闭
206+ if ( query && query . tmpToken ) {
207+ server . close ( )
208+ }
209+
210+ resolve ( query as Credential )
211+ } )
211212 } catch ( err ) {
212213 logger . error ( err . message )
213214 loading . fail ( '获取授权失败!' )
0 commit comments