ContantsSelectPlugin.ets
2.78 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
import {
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
} from '@ohos/flutter_ohos';
import { contact } from '@kit.ContactsKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { ArrayList } from '@kit.ArkTS';
/** ContantsSelectPlugin **/
export default class ContantsSelectPlugin implements FlutterPlugin, MethodCallHandler {
private channel: MethodChannel | null = null;
constructor() {
}
getUniqueClassName(): string {
return "ContantsSelectPlugin"
}
onAttachedToEngine(binding: FlutterPluginBinding): void {
this.channel = new MethodChannel(binding.getBinaryMessenger(), "contants_select");
this.channel.setMethodCallHandler(this)
}
onDetachedFromEngine(binding: FlutterPluginBinding): void {
if (this.channel != null) {
this.channel.setMethodCallHandler(null)
}
}
onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method == "getPlatformVersion") {
result.success("OpenHarmony ^ ^ ")
}else if (call.method == "getContact") {
const isMultiSelect= call.argument("isMultiSelect") as boolean || false
contact.selectContacts({
isMultiSelect:isMultiSelect
},(err: BusinessError, data) => {
if (err) {
console.error(`selectContact callback: err->${JSON.stringify(err)}`);
result.error(err.code.toString(),err.message,err.data)
}
console.log(`selectContact callback: success data->${JSON.stringify(data)}`);
if (data && data.length > 0) {
// // 创建联系人结果映射
let tempContacts: ArrayList< Record<string, Object> >= new ArrayList();
data.forEach((contact:contact.Contact) => {
const contactInfo = new Map<string, string | string[]>();
contactInfo.set("fullName", contact.name?.fullName || "");
const phoneNumbers: string[] = contact.phoneNumbers?.map(item => item.phoneNumber) || [];
contactInfo.set("phoneNumbers", phoneNumbers);
if (contact.emails && contact.emails.length > 0) {
const emails: string[] = contact.emails.map(item => item.email);
contactInfo.set("emails", emails);
}
let jsonObject: Record<string, Object> = {};
contactInfo.forEach((value, key) => {
if (key !== undefined && value !== undefined) {
jsonObject[key] = value;
}
})
tempContacts.add(jsonObject);
});
console.info(`Succeeded in selecting Contacts. data->${JSON.stringify( tempContacts)}`);
result.success( tempContacts)
}else{
//没有选择
result.success([])
}
});
} else {
result.notImplemented()
}
}
}