HTML5通讯录获取指定多个人的信息方法分析

作者:袖梨 2022-06-25

本文介绍了HTML5通讯录获取指定多个人的信息,具体如下:

一、获取多个人的信息:要解决通讯录导入多个人的信息之前,要解决获取多个人的信息。我通过plus.contacts.getAddressBook和address.find的套用把通讯录里面的所有联系人的iddisplayName获取出来,然后通过自己写的通讯录获取页面显示出来。

1、解决这个问题首先你要自己写一个js通讯录,这样可以把你所有联系人首字母分离出来,并且旁边可以跳转到你要的首字母。

2、解决获取所有联系人信息

plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE,function(addressbook) {//获取通讯录信息
                // 可通过addressbook进行通讯录操作
                addressbook.find(null,function(contacts) {
                    varusername =newArray();
                    varLinkList =newLinkedList();
                    if(contacts.length > 0) {//获取当前通讯录里面所有人
                        for(vari = 0; i < contacts.length; i  ) {
                            username[i] = contacts[i].displayName  "-"  contacts[i].id;//连接id和username,为后面筛选最准备
                        }
                        //这下面的代码是把所有联系人的信息分类,这就涉及到了自己写的JS页面代码
                        LinkList = sortPY(username);//把联系人数组分类
                        //LinkList.show();
                        createLiCheckBox(LinkList);//分类信息显示至页面,我使用checkBox进行多个联系人选择
                    }
 
                },function(e) {
                    alert("Find contact error: "  e.message);
                });
 
            },function(e) {
 
    });

二、从通讯录导入多个选定的个人信息:解决这个问题在先前创建通讯录页面的时候就一定要把联系人的id放在在页面上(使用display隐藏),这样我获取被选中的checkBox的时候就可以直接获取id,并且把这些id放到一个数组里面。再通过plus.contacts.getAddressBook和address.find的套用把这些id的联系人信息筛选出来。

1、解决使用checkBox获取联系人id,这里我使用了JQuery

//筛选已经被选中的checkbox
    $("input:checked").each(function() {
            varindex = $(this).parent().prev().children('label').text();//获取id
            varname = $(this).parent().prev().children('p').text();//获取姓名
            username.push(name);
            usernameIndex.push(index);
    });

2、解决把这些index放到find里面进行筛选信息,把特定id下面的联系人信息拿出来

plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE,function(addressbook) {//获取通讯录信息
            for(varj = 0; j < username.length; j  ) {//循环所选取的联系人,记得循环一定要放在这里,一开始我放在        plus.contacts.getAddressBook外面是错误
                        addressbook.find(null,function(contacts) {
                            console.log("进入查询");
                            for(vari = 0; i < contacts.length; i  ) {//无论是否为多个信息,一定要循环数组
                                console.log("进入循环");
                                //var id = contacts[i].id;
                                vardisplayname = contacts[i].displayName;
                                varphone ="";
                                varemails ="";
                                vardates ="";
                                varremark ="";
                                if(contacts[i].phoneNumbers.length > 0) {//这里需要判断是否为空,为空的数组没有index=0;
                                    phone = contacts[i].phoneNumbers[0].value;
                                }else{
                                    phone = contacts[i].phoneNumbers;
                                }
 
                                if(contacts[i].emails.length > 0) {//这里需要判断是否为空,为空的数组没有index=0;
                                    emails = contacts[i].emails[0].value;
                                }else{
                                    emails = contacts[i].emails;
                                }
 
                                vardateNum =newDate(contacts[i].birthday);//这里的birthday是number类型!!!官方手册坑爹?
                                dates = dateNum.getFullYear()  "."  (dateNum.getMonth()   1)  "."  dateNum.getDate();
                                remark = contacts[i].note;
 
                                vargetContact = {//把所有信息放到一个json里面
                                    contactName: displayname,
                                    sex:"",
                                    department:"",
                                    positions:"",
                                    tel:"",
                                    phone: phone,
                                    eMail: emails,
                                    birthday: dates,
                                    hobby:"",
                                    remark: remark
                                };
 
                                //这下面是我的业务代码了,这里大家可以写自己的信息
                                //createContactTable(db);
                                //InsertContact(db, getContact); //多个信息插入有线程安全的问题出现!!!!!!!
                            }
                            //console.log(username.length);                        
                        },function(e) {
                            console.log("查询错误");
                        }, { 
                                                         //这里面的筛选非常重要!!!这样才能选出匹配的信息
                            filter: [{
                                logic:"or",
                                field:"id",
                                value: usernameIndex[j]
                            }],
                            multi:false
                        });
                    }
                },function(e) {
                    console.log("打开通讯录错误");
                });

相关文章

精彩推荐