WeChatApi.ets
3.94 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
/*
* // Copyright 2013 The Flutter Authors. All rights reserved.
*/
import StandardMessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/StandardMessageCodec';
import BasicMessageChannel, { Reply } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BasicMessageChannel';
import { BinaryMessenger } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BinaryMessenger';
import MessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/MessageCodec';
import { ByteBuffer } from '@ohos/flutter_ohos/src/main/ets/util/ByteBuffer';
/** Error class for passing custom error details to Flutter via a thrown PlatformException. */
export class FlutterError implements Error {
/** The error code. */
public code: string;
/** The error name. */
public name: string;
/** The error message. */
public message: string;
/** The error stack. */
public stack?: string;
constructor(code: string, name: string, message: string, stack?: string) {
this.code = code;
this.name = name;
this.message = message;
this.stack = stack;
}
}
function wrapError(error: Error): Array<Object | null> {
let errorList: Array<Object | null> = new Array<Object | null>(3);
if (error instanceof FlutterError) {
let err: FlutterError = error as FlutterError;
errorList[0] = err.code;
errorList[1] = err.name;
errorList[2] = err.message;
} else {
errorList[0] = error.toString();
errorList[1] = error.name;
errorList[2] = "Cause: " + error.message + ", Stacktrace: " + error.stack;
}
return errorList;
}
export class PigeonCodec extends StandardMessageCodec {
static readonly INSTANCE: PigeonCodec = new PigeonCodec();
getByte(n: number): number {
let byteArray = new Uint8Array(1);
byteArray[0] = n;
return byteArray[0] as number;
}
private constructor() {
super();
}
readValueOfType(type: number, buffer: ByteBuffer): ESObject {
switch (type) {
default:
return super.readValueOfType(type, buffer);
}
}
writeValue(stream: ByteBuffer, value: ESObject): ESObject {
{
super.writeValue(stream, value);
}
}
}
export interface Result<T> {
success(result: T): void;
error(error: Error): void;
}
/* Generated abstract class from Pigeon that represents a handler of messages from Flutter.*/
export abstract class WeChatHostApi {
/* Opens WeChat and returns its one-time authorization code.*/
abstract requestAuthorizationCode(result: Result<string>): void;
/** The codec used by WeChatHostApi. */
static getCodec(): MessageCodec<Object> {
return PigeonCodec.INSTANCE;
}
/*Sets up an instance of `WeChatHostApi` to handle messages through the `binaryMessenger`.*/
static setup(binaryMessenger: BinaryMessenger, api: WeChatHostApi | null, messageChannelSuffix: string = ''): void {
const separatedMessageChannelSuffix: string = messageChannelSuffix !== '' ? '.' + messageChannelSuffix : '';
{
let channel: BasicMessageChannel<Object> =
new BasicMessageChannel(
binaryMessenger, 'dev.flutter.pigeon.doublefeel_flutter.WeChatHostApi.requestAuthorizationCode' + separatedMessageChannelSuffix, WeChatHostApi.getCodec());
if (api != null) {
channel.setMessageHandler({
onMessage(message: Object, reply: Reply<Object>) {
class ResultImp implements Result<string>{
success(result: string): void {
let res: Array<Object | null> = [];
res.push(result);
reply.reply(res);
}
error(error: Error): void {
let wrappedError: Array<Object | null> = wrapError(error);
reply.reply(wrappedError);
}
}
let resultCallback: Result<string> = new ResultImp();
api!.requestAuthorizationCode(resultCallback);
} });
} else {
channel.setMessageHandler(null);
}
}
}
}