HuaweiLoginView.ets
6.02 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import MethodChannel, { MethodCallHandler } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel';
import PlatformView, { Params } from '@ohos/flutter_ohos/src/main/ets/plugin/platform/PlatformView';
import common from '@ohos.app.ability.common';
import { BinaryMessenger } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BinaryMessenger';
import StandardMethodCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/StandardMethodCodec';
import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall';
import { loginComponentManager, LoginWithHuaweiIDButton } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { JSON } from '@kit.ArkTS';
import { hilog } from '@kit.PerformanceAnalysisKit';
import {
FlutterPlugin,
FlutterPluginBinding,
MessageCodec,
PlatformViewFactory,
StandardMessageCodec
} from '@ohos/flutter_ohos';
const logTag: string = 'HuaweiLoginComponent';
const domainId: number = 0x0000;
const ERROR_CODE_AGREEMENT_STATUS_NOT_ACCEPTED: number = 1005300001;
class HuaweiLoginInfo {
public authorizationCode: string = '';
public openID: string = '';
public unionID: string = '';
public idToken: string = '';
constructor(authorizationCode: string, openID: string, unionID: string, idToken: string) {
this.authorizationCode = authorizationCode;
this.openID = openID;
this.unionID = unionID;
this.idToken = idToken;
}
}
@Component
struct HuaweiLoginButton {
@StorageLink('huaweiAgreementStatus') @Watch('watchAgreementStatus') agreementStatus: boolean = false;
@StorageLink('huaweiContinueLogin') @Watch('watchContinueLogin') continueLoginFlag: boolean = false;
@Prop params: Params;
customView: CustomView = this.params?.platformView as CustomView;
controller = new loginComponentManager.LoginWithHuaweiIDButtonController()
.setAgreementStatus(loginComponentManager.AgreementStatus.NOT_ACCEPTED)
.onClickEvent(() => {
this.customView.sendMessage('loginStart', '');
})
.onClickLoginWithHuaweiIDButton((error: BusinessError, response: loginComponentManager.HuaweiIDCredential) => {
this.customView.sendMessage('loginEnd', '');
if (error) {
hilog.error(domainId, logTag, `HuaweiID login failed. errCode is ${error.code}, message is ${error.message}`);
if (error.code === ERROR_CODE_AGREEMENT_STATUS_NOT_ACCEPTED) {
this.customView.sendMessage('agreementNotAccepted', '');
} else {
this.customView.sendMessage('loginError', `${error.code}:${error.message}`);
}
return;
}
const info = new HuaweiLoginInfo(
response.authorizationCode,
response.openID,
response.unionID,
response.idToken ?? ''
);
this.customView.sendMessage('loginSuccess', JSON.stringify(info));
});
watchAgreementStatus(): void {
if (this.agreementStatus) {
this.controller.setAgreementStatus(loginComponentManager.AgreementStatus.ACCEPTED);
} else {
this.controller.setAgreementStatus(loginComponentManager.AgreementStatus.NOT_ACCEPTED);
}
}
watchContinueLogin(): void {
if (this.continueLoginFlag) {
this.continueLoginFlag = false;
this.controller.continueLogin((error: BusinessError) => {
if (error) {
hilog.error(domainId, logTag, `continueLogin failed. errCode is ${error.code}, message is ${error.message}`);
this.customView.sendMessage('loginError', `${error.code}:${error.message}`);
} else {
hilog.info(domainId, logTag, 'continueLogin succeeded.');
}
});
}
}
build() {
LoginWithHuaweiIDButton({
params: {
style: loginComponentManager.Style.BUTTON_CUSTOM,
extraStyle: {
buttonStyle: new loginComponentManager.ButtonStyle().loadingStyle({
show: true
})
},
loginType: loginComponentManager.LoginType.ID,
supportDarkMode: true
},
controller: this.controller
}).id('login_with_huaweiId_button')
}
}
@Builder
function HuaweiLoginButtonBuilder(params: Params) {
HuaweiLoginButton({ params: params })
}
@Observed
class CustomView extends PlatformView implements MethodCallHandler {
methodChannel: MethodChannel;
constructor(context: common.Context, viewId: number, args: Object, message: BinaryMessenger) {
super();
this.methodChannel =
new MethodChannel(message, `com.naji.huawei/loginView${viewId}`, StandardMethodCodec.INSTANCE);
this.methodChannel.setMethodCallHandler(this);
}
onMethodCall(call: MethodCall): void {
switch (call.method) {
case 'setAgreementStatus': {
const value: boolean = call.args as boolean;
const link: SubscribedAbstractProperty<boolean> = AppStorage.link('huaweiAgreementStatus');
link.set(value);
break;
}
case 'continueLogin': {
const link: SubscribedAbstractProperty<boolean> = AppStorage.link('huaweiContinueLogin');
link.set(true);
break;
}
default: {
break;
}
}
}
public sendMessage = (method: string, data: string) => {
this.methodChannel.invokeMethod(method, data);
}
getView(): WrappedBuilder<[Params]> {
return new WrappedBuilder(HuaweiLoginButtonBuilder);
}
dispose(): void {
}
}
class CustomFactory extends PlatformViewFactory {
message: BinaryMessenger;
constructor(message: BinaryMessenger, createArgsCodes: MessageCodec<Object>) {
super(createArgsCodes);
this.message = message;
}
public create(context: common.Context, viewId: number, args: Object): PlatformView {
return new CustomView(context, viewId, args, this.message);
}
}
export default class HuaweiLoginPlugin implements FlutterPlugin {
getUniqueClassName(): string {
return 'HuaweiLoginPlugin';
}
onAttachedToEngine(binding: FlutterPluginBinding): void {
binding.getPlatformViewRegistry()?.
registerViewFactory('com.naji.huawei/loginView', new CustomFactory(binding.getBinaryMessenger(),
StandardMessageCodec.INSTANCE));
}
onDetachedFromEngine(): void {
}
}