HuaweiAuth.ets
5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { authentication, extendService } from '@kit.AccountKit';
import { AccountManagerUtils, EmitterEventId, EmitterUtils, Logger } from 'wdKit/Index';
import { util } from '@kit.ArkTS';
import { DEFAULT } from '@ohos/hypium';
import { BusinessError } from '@kit.BasicServicesKit';
const TAG = "HuaweiOneKeyAuth"
export default class HuaweiAuth {
// 是否开启
static enable = false
// 匿名手机号
private _anonymousPhone?: string
get anonymousPhone() {
return this._anonymousPhone
}
private static instance: HuaweiAuth
static sharedInstance(): HuaweiAuth {
if (!HuaweiAuth.instance) {
HuaweiAuth.instance = new HuaweiAuth()
}
return HuaweiAuth.instance
}
registerEvents() {
// 注册用户退出登录,取一次用来下次使用
EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, ((str?: string) => {
HuaweiAuth.sharedInstance().rePrefetchAnonymousPhone()
}))
EmitterUtils.receiveEvent(EmitterEventId.APP_ENTER_FOREGROUD, ((str?: string) => {
AccountManagerUtils.isLogin().then((login) => {
if (!login) {
HuaweiAuth.sharedInstance().rePrefetchAnonymousPhone()
}
})
}))
}
// 重新预取手机号(App启动未登录、回前台未登录、和退出登录 时调用提前取号)
rePrefetchAnonymousPhone() {
this._anonymousPhone = undefined
this.fetchAnonymousPhone()
}
// 要登录时,先调用。如果获取到手机号了 则弹起一键登录页面
fetchAnonymousPhone() : Promise<string> {
return new Promise((resolve, fail) => {
if (this.anonymousPhone) {
resolve(this.anonymousPhone)
return
}
let authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
// 权限有 phone 、email、realTimePhone、quickLoginMobilePhone,这里用获取匿名手机号
authRequest.scopes = ['quickLoginAnonymousPhone'];
authRequest.state = util.generateRandomUUID();
authRequest.forceAuthorization = false;
let controller = new authentication.AuthenticationController(getContext(this));
try {
controller.executeRequest(authRequest).then((response: authentication.AuthorizationWithHuaweiIDResponse) => {
let anonymousPhone = response.data?.extraInfo?.quickLoginAnonymousPhone;
if (anonymousPhone) {
Logger.info(TAG, 'get anonymousPhone, ' + JSON.stringify(response));
this._anonymousPhone = anonymousPhone as string
resolve(this._anonymousPhone)
return;
}
Logger.info(TAG, 'get anonymousPhone is empty. ' + JSON.stringify(response));
fail("获取匿名手机号为空")
}).catch((err: BusinessError) => {
Logger.error(TAG, 'get anonymousPhone failed. ' + JSON.stringify(err));
fail("获取匿名手机号失败")
})
} catch (err) {
Logger.error(TAG, 'get anonymousPhone fail. ' + JSON.stringify(err));
fail("获取匿名手机号失败")
}
})
}
// 华为账号一键登录授权
// 返回结果为 Authorization Code
oneKeyLogin() : Promise<string> {
let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
// 当用户未登录华为帐号时,是否强制拉起华为帐号登录界面
loginRequest.forceLogin = true;
loginRequest.state = util.generateRandomUUID();
return new Promise((resolve, fail) => {
try {
let controller = new authentication.AuthenticationController(getContext(this));
controller.executeRequest(loginRequest, (err, data) => {
if (err) {
Logger.error(TAG, 'login fail, ' + JSON.stringify(err))
fail(err)
return;
}
let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
let state = loginWithHuaweiIDResponse.state;
if (state != undefined && loginRequest.state != state) {
Logger.error(TAG, 'login fail, The state is different' + JSON.stringify(loginWithHuaweiIDResponse))
fail({
code: 99999,
name: "一键登录",
message:"状态错误:" + `请求状态 ${loginRequest.state}, 结果状态 ${state}`
} as BusinessError)
return;
}
Logger.info(TAG, 'login success, ' + JSON.stringify(loginWithHuaweiIDResponse));
let loginWithHuaweiIDCredential = loginWithHuaweiIDResponse.data!;
let authorizationCode = loginWithHuaweiIDCredential.authorizationCode;
resolve(authorizationCode!)
});
} catch (error) {
Logger.error(TAG, 'login fail, ' + JSON.stringify(error))
fail(error)
}
});
}
// 打开账户中心
openAccountCenter() {
try {
extendService.startAccountCenter(getContext(this), (err, data) => {
if (err) {
Logger.info(TAG, 'startAccountCenterWithCallback fail,error: ' + JSON.stringify(err));
return;
}
Logger.info(TAG, 'startAccountCenterWithCallback success');
});
} catch (error) {
Logger.error(TAG, 'startAccountCenterWithCallback fail,error: ' + JSON.stringify(error));
}
}
}