Skip to content

Commit b7a14d4

Browse files
author
zoyafng
committed
feat(miniapp): 🎨 resetPlatformLoginCode 添加错误返回
1 parent 1aa9b2c commit b7a14d4

File tree

1 file changed

+84
-66
lines changed

1 file changed

+84
-66
lines changed

packages/miniapp/src/Authing.ts

Lines changed: 84 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
LoginByCodeConnection,
3838
LoginByPhoneCodeConnection,
3939
PlatformsMenu,
40-
GetDouyinPhoneOptions,
40+
GetDouyinPhoneOptions
4141
} from './types'
4242

4343
import { returnSuccess, returnError } from './helpers/return'
@@ -72,7 +72,6 @@ export class Authing {
7272
}
7373

7474
async getLoginState(): Promise<SDKResponse<LoginState>> {
75-
7675
try {
7776
const res = await this.storage.get(getLoginStateKey(this.options.appId))
7877
const loginState: LoginState = res.data
@@ -175,14 +174,27 @@ export class Authing {
175174
}
176175
}
177176

178-
private async resetPlatformLoginCode(): Promise<string> {
177+
private async resetPlatformLoginCode(): Promise<SDKResponse<string>> {
179178
const next = async () => {
180179
try {
181180
const platformLoginRes = await AuthingMove.login()
182181
await this.cachePlatformLoginCode(platformLoginRes.code)
183-
return true
182+
if (platformLoginRes.code) {
183+
return {
184+
status: true,
185+
result: platformLoginRes
186+
}
187+
} else {
188+
return {
189+
status: false,
190+
error: platformLoginRes
191+
}
192+
}
184193
} catch (e) {
185-
return false
194+
return {
195+
status: false,
196+
error: e
197+
}
186198
}
187199
}
188200

@@ -196,10 +208,17 @@ export class Authing {
196208
// await next()
197209
// }
198210
// } catch (e) {
199-
this.storage.remove(getPlatformLoginCodeKey(this.options.appId,this.options.platform))
200-
await next()
201-
} finally {
202-
return await this.getCachedPlatformLoginCode()
211+
this.storage.remove(
212+
getPlatformLoginCodeKey(this.options.appId, this.options.platform)
213+
)
214+
const res = await next()
215+
if (res.status) {
216+
return returnSuccess(await this.getCachedPlatformLoginCode())
217+
} else {
218+
return returnError(res.error)
219+
}
220+
} catch (e) {
221+
return returnError(e)
203222
}
204223
}
205224

@@ -224,27 +243,33 @@ export class Authing {
224243
options
225244
} = data
226245

227-
const code = await this.resetPlatformLoginCode()
246+
const [error,code] = await this.resetPlatformLoginCode()
228247

229248
if (!code) {
230249
return returnError({
231-
message: 'get wx login code error'
250+
message: 'get wx login code error' + JSON.stringify(error)
232251
})
233252
}
234253

235254
const _data: PlatformCodeLoginOptions = {
236255
connection: connection || this.getDefaultLoginByCodeConnection(),
237256
extIdpConnidentifier,
238-
wechatMiniProgramCodePayload: PlatformsMenu.wx == this.options.platform ? {
239-
encryptedData: wechatMiniProgramCodePayload?.encryptedData || '',
240-
iv: wechatMiniProgramCodePayload?.iv || '',
241-
code
242-
} : undefined,
243-
douyinMiniProgramCodePayload: PlatformsMenu.tt == this.options.platform ? {
244-
encryptedData: douyinMiniProgramCodePayload?.encryptedData || '',
245-
iv: douyinMiniProgramCodePayload?.iv || '',
246-
code
247-
} : undefined,
257+
wechatMiniProgramCodePayload:
258+
PlatformsMenu.wx == this.options.platform
259+
? {
260+
encryptedData: wechatMiniProgramCodePayload?.encryptedData || '',
261+
iv: wechatMiniProgramCodePayload?.iv || '',
262+
code
263+
}
264+
: undefined,
265+
douyinMiniProgramCodePayload:
266+
PlatformsMenu.tt == this.options.platform
267+
? {
268+
encryptedData: douyinMiniProgramCodePayload?.encryptedData || '',
269+
iv: douyinMiniProgramCodePayload?.iv || '',
270+
code
271+
}
272+
: undefined,
248273
options
249274
}
250275

@@ -349,40 +374,40 @@ export class Authing {
349374

350375
switch (this.options.platform) {
351376
case PlatformsMenu.wx:
352-
353-
const wxPhoneInfo = wechatMiniProgramCodeAndPhonePayload?.wxPhoneInfo || miniProgramCodeAndPhonePayload?.wxPhoneInfo
377+
const wxPhoneInfo =
378+
wechatMiniProgramCodeAndPhonePayload?.wxPhoneInfo ||
379+
miniProgramCodeAndPhonePayload?.wxPhoneInfo
354380
if (!wxPhoneInfo || !wxPhoneInfo.code) {
355381
return returnError({
356382
message: 'wxPhoneInfo.code is required'
357383
})
358384
}
359385

360-
const wxLoginCode = await this.resetPlatformLoginCode()
386+
const [error,code] = await this.resetPlatformLoginCode()
361387

362-
if (!wxLoginCode) {
388+
if (!code) {
363389
return returnError({
364-
message: 'get wx login code error'
390+
message: 'get wx login code error' + JSON.stringify(error)
365391
})
366392
}
367393

368-
_data = {
369-
connection:this.getDefaultLoginByPhoneCodeConnection(),
394+
_data = {
395+
connection: this.getDefaultLoginByPhoneCodeConnection(),
370396
extIdpConnidentifier,
371397
wechatMiniProgramCodeAndPhonePayload: {
372398
wxPhoneInfo,
373399
wxLoginInfo: {
374400
encryptedData:
375-
wechatMiniProgramCodeAndPhonePayload?.wxLoginInfo?.encryptedData ||
376-
'',
401+
wechatMiniProgramCodeAndPhonePayload?.wxLoginInfo
402+
?.encryptedData || '',
377403
iv: wechatMiniProgramCodeAndPhonePayload?.wxLoginInfo?.iv || '',
378-
code: wxLoginCode
404+
code
379405
}
380406
},
381407
options
382408
}
383409
break
384410
case PlatformsMenu.tt:
385-
386411
const phoneParams = miniProgramCodeAndPhonePayload?.phoneParams
387412

388413
if (!phoneParams) {
@@ -391,27 +416,26 @@ export class Authing {
391416
})
392417
}
393418

394-
395-
const loginParamsCode = await this.resetPlatformLoginCode()
419+
const [resetError,loginParamsCode] = await this.resetPlatformLoginCode()
396420

397421
if (!loginParamsCode) {
398422
return returnError({
399-
message: 'get tt login params error ,please use the miniProgramCodeAndPhonePayload'
423+
message: 'get tt login code error' + JSON.stringify(resetError)
400424
})
401425
}
402426

403427
_data = {
404-
connection:this.getDefaultLoginByPhoneCodeConnection(),
428+
connection: this.getDefaultLoginByPhoneCodeConnection(),
405429
extIdpConnidentifier,
406430
douyinMiniProgramCodeAndPhonePayload: {
407-
loginParams:{
431+
loginParams: {
408432
encryptedData: '',
409433
iv: '',
410434
code: loginParamsCode
411435
},
412436
phoneParams: {
413-
encryptedData: phoneParams?.encryptedData || '' ,
414-
iv: phoneParams?.iv ||'',
437+
encryptedData: phoneParams?.encryptedData || '',
438+
iv: phoneParams?.iv || ''
415439
}
416440
},
417441
options
@@ -422,7 +446,6 @@ export class Authing {
422446
return await this.login(_data, 'phone')
423447
}
424448

425-
426449
async logout(): Promise<SDKResponse<boolean>> {
427450
const [loginStateError, loginState] = await this.getLoginState()
428451

@@ -762,20 +785,19 @@ export class Authing {
762785
})
763786
}
764787

765-
const code = await this.resetPlatformLoginCode()
788+
const [resetError,code] = await this.resetPlatformLoginCode()
766789

767790
if (!code) {
768791
return returnError({
769-
message: 'get wx login code error'
792+
message: 'get wx login code error' + JSON.stringify(resetError)
770793
})
771794
}
772795

773796
const _data: BindWxByCodeOptions = {
774-
code,
775-
...data,
797+
code,
798+
...data
776799
}
777800

778-
779801
const [err, res] = await request({
780802
method: 'POST',
781803
url: `${this.options.host}/connections/social/wechat-miniprogram/bind`,
@@ -809,21 +831,19 @@ export class Authing {
809831
message: 'Token has expired, please login again'
810832
})
811833
}
812-
813-
const code = await this.resetPlatformLoginCode()
834+
const [resetError,code] = await this.resetPlatformLoginCode()
814835

815836
if (!code) {
816837
return returnError({
817-
message: 'get wx login code error'
838+
message: 'get wx login code error' + JSON.stringify(resetError)
818839
})
819840
}
820841

821842
const _data: BindPlatformByCodeOptions = {
822-
code,
823-
...data,
843+
code,
844+
...data
824845
}
825846

826-
827847
const [err, res] = await request({
828848
method: 'POST',
829849
url: `${this.options.host}${this.getApiUrlMapping('bindPlatformByCode')}`,
@@ -1121,8 +1141,6 @@ export class Authing {
11211141
async getPhone(
11221142
data: GetPhoneOptions | GetDouyinPhoneOptions
11231143
): Promise<SDKResponse<GetUserPhoneResponseData>> {
1124-
1125-
11261144
let _data: GetPhoneOptions | GetDouyinPhoneOptions = {
11271145
...data
11281146
}
@@ -1142,20 +1160,25 @@ export class Authing {
11421160
})
11431161
}
11441162

1145-
const loginParamsCode = await this.resetPlatformLoginCode()
1163+
const [resetError, loginParamsCode] = await this.resetPlatformLoginCode()
1164+
1165+
if (!loginParamsCode) {
1166+
return returnError({
1167+
message: 'get tt login code error' + JSON.stringify(resetError)
1168+
})
1169+
}
11461170
_data = {
11471171
..._data,
1148-
code:loginParamsCode
1172+
code: loginParamsCode
11491173
}
11501174

11511175
break
11521176
}
11531177

1154-
11551178
const [getPhoneError, getPhoneRes] = await request({
11561179
method: 'POST',
11571180
url: `${this.options.host}${this.getApiUrlMapping('getPhone')}`,
1158-
data:_data,
1181+
data: _data,
11591182
header: {
11601183
'x-authing-userpool-id': this.options.userPoolId
11611184
}
@@ -1189,26 +1212,23 @@ export class Authing {
11891212
return returnSuccess(res)
11901213
}
11911214

1192-
11931215
/**
11941216
* 因为会有一下接口地址变更
11951217
* 涉及到的 api 接口地址
11961218
* */
11971219
private getApiUrlMapping(route: string): string {
1198-
11991220
let url = ''
12001221
switch (route) {
1201-
12021222
case 'getPhone':
1203-
url = '/api/v3/get-wechat-miniprogram-phone'
1223+
url = '/api/v3/get-wechat-miniprogram-phone'
12041224
switch (this.options.platform) {
12051225
case PlatformsMenu.tt:
12061226
url = '/api/v3/decrypt-douyin-miniprogram-phone'
12071227
break
12081228
}
12091229
break
12101230
case 'bindPlatformByCode':
1211-
url = '/connections/social/wechat-miniprogram/bind'
1231+
url = '/connections/social/wechat-miniprogram/bind'
12121232
switch (this.options.platform) {
12131233
case PlatformsMenu.tt:
12141234
url = '/connections/social/douyin-miniprogram/bind'
@@ -1217,15 +1237,15 @@ export class Authing {
12171237
break
12181238

12191239
case 'decryptData':
1220-
url = '/api/v3/decrypt-wechat-miniprogram-data'
1240+
url = '/api/v3/decrypt-wechat-miniprogram-data'
12211241
switch (this.options.platform) {
12221242
case PlatformsMenu.tt:
12231243
url = '/api/v3/decrypt-douyin-miniprogram-data'
12241244
break
12251245
}
12261246
break
12271247
case 'getAccessTokenInfo':
1228-
url = '/api/v3/get-wechat-access-token-info'
1248+
url = '/api/v3/get-wechat-access-token-info'
12291249
switch (this.options.platform) {
12301250
case PlatformsMenu.tt:
12311251
url = '/api/v3/get-douyin-access-token-info'
@@ -1234,8 +1254,6 @@ export class Authing {
12341254
break
12351255
}
12361256

1237-
12381257
return url
1239-
12401258
}
12411259
}

0 commit comments

Comments
 (0)