WeChatLoginBridge.ets
3.73 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
import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';
import {
BaseReq,
BaseResp,
ErrCode,
SendAuthReq,
SendAuthResp,
WXAPIFactory,
WXApi,
WXApiEventHandler,
} from '@tencent/wechat_open_sdk';
import { FlutterError, Result, WeChatHostApi } from '../pigeon/WeChatApi';
/**
* Fill in the AppID created for the HarmonyOS application in WeChat Open
* Platform. Keep AppSecret on the server only.
*/
const WECHAT_APP_ID = 'wx42b603acf3dd68f5';
/** Bridges the native OpenSDK authorization callback to Flutter. */
export class WeChatLoginBridge extends WeChatHostApi implements WXApiEventHandler {
private readonly context: common.UIAbilityContext;
private readonly wxApi: WXApi;
private pendingResult: Result<string> | null = null;
constructor(context: common.UIAbilityContext) {
super();
this.context = context;
this.wxApi = WXAPIFactory.createWXAPI(WECHAT_APP_ID, context);
}
requestAuthorizationCode(result: Result<string>): void {
if (WECHAT_APP_ID.length === 0) {
result.error(new FlutterError(
'wechat_app_id_missing',
'Set WECHAT_APP_ID in WeChatLoginBridge.ets first.',
'WeChatError',
));
return;
}
if (this.pendingResult !== null) {
result.error(new FlutterError(
'wechat_authorization_in_progress',
'微信授权进行中',
'WeChatError',
));
return;
}
if (!this.wxApi.isWXAppInstalled()) {
result.error(new FlutterError(
'wechat_not_installed',
'微信未安装',
'WeChatError',
));
return;
}
const request = new SendAuthReq();
request.scope = 'snsapi_userinfo';
request.state = 'none';
request.callbackAbility = 'EntryAbility';
this.pendingResult = result;
try {
const sent = this.wxApi.sendReq(this.context, request);
if (sent instanceof Promise) {
sent.then((success) => this.onRequestSent(success)).catch((error: Error) => {
this.finishWithError('wechat_request_failed', error.message);
});
} else {
this.onRequestSent(sent);
}
} catch (error) {
this.finishWithError('wechat_request_failed', (error as Error).message);
}
}
handleWant(want: Want): void {
this.wxApi.handleWant(want, this);
}
/**
* WeChat does not send an authorization response when the user simply
* returns to our app. Complete the pending Pigeon call in that case, so the
* Flutter loading overlay is always dismissed.
*/
cancelPendingAuthorization(): void {
if (this.pendingResult === null) {
return;
}
this.finishWithError(
'wechat_authorization_cancelled',
'微信授权已取消',
);
}
onReq(_request: BaseReq): void {}
onResp(response: BaseResp): void {
if (!(response instanceof SendAuthResp)) {
return;
}
if (response.errCode === ErrCode.ERR_OK && response.code?.length) {
this.finishWithCode(response.code);
return;
}
this.finishWithError(
'wechat_authorization_failed',
response.errStr ?? `WeChat authorization failed with error ${response.errCode}.`,
);
}
private onRequestSent(success: boolean): void {
if (!success) {
this.finishWithError('wechat_request_failed', 'Unable to open WeChat.');
}
}
private finishWithCode(code: string | undefined): void {
const result = this.pendingResult;
this.pendingResult = null;
if (result !== null && code !== undefined) {
result.success(code);
}
}
private finishWithError(code: string, message: string): void {
const result = this.pendingResult;
this.pendingResult = null;
if (result !== null) {
result.error(new FlutterError(code, message, 'WeChatError'));
}
}
}