天天看點

contacts 子產品管理系統通訊錄contacts 子產品管理系統通訊錄

contacts 子產品管理系統通訊錄

contacts子產品管理系統通訊錄, 用于可對系統通訊錄進行增删改查等操作. 通過plus.contacts擷取系統通訊錄管理對象

文章目錄

  • contacts 子產品管理系統通訊錄
    • 一.擷取通訊錄方法: getAddressBook
    • 1.1參數
    • 二.常量
    • 2.1 手機通訊錄 ADDRESSBOOK_PHONE
    • 2.2 SIM卡通訊錄 ADDRESSBOOK_SIM
    • 三.通訊錄管理對象 AddressBook
    • 3.1 方法:
        • 3.1.1 create: 建立聯系人
        • 3.1.2 find: 在通訊錄中查找聯系人
      • 3.2 Contact 聯系人對象
        • 3.2.1 屬性:
        • 3.2.2 方法

一.擷取通訊錄方法: getAddressBook

getAddressBook 擷取通訊錄對象

1.1參數

  1. type(Number):(必選)要擷取的通訊錄類型
  2. successCB: (AddressBookSuccessCallback)(必選)擷取通訊錄對象成功回調.
  3. errorCB: (ContactsErrorCallback)(可選)擷取通訊錄對象失敗回調

二.常量

2.1 手機通訊錄 ADDRESSBOOK_PHONE

  1. 說明:

    Number類型, 通訊錄類型常量, 數值類型, 固定值為0, 使用者擷取系統的聯系人資訊.

2.2 SIM卡通訊錄 ADDRESSBOOK_SIM

  1. 說明:

    Number類型, 通訊錄類型常量, 數值類型, 固定值為1, 用于擷取SIM卡上的聯系人資訊.

三.通訊錄管理對象 AddressBook

通訊錄管理對象, 可對系統通訊錄進行聯系人的增删改查操作.
interface AddressBook{
    function Contact create();
    function void find( contactFields,successCB,errorCB,findOptions );
}
           
<template>
	<view>
		<button type="default" @tap='get'>擷取</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			};
		},
		methods: {
			get() {
				  plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function( addressbook ) {
					 //  console.log(addressbook)
					  addressbook.find(["displayName","phoneNumbers"],res=>{
						console.log(res)
				        for(var i in res){
				          console.log(res[i]);
				        }
					  })
				  }, function(error) {
				    console.log(error.message);
				  });
			},
			onSuccess(res) {
				console.log(res)
			}
		}
	}
</script>

<style >

</style>

           

3.1 方法:

3.1.1 create: 建立聯系人

建立一個系統聯系人, 并傳回聯系人對象, 可對聯系人對象進行操作設定聯系人資訊, 如名稱 位址 電話等.

3.1.2 find: 在通訊錄中查找聯系人

在通訊錄中安裝指定的規則查找聯系人, contactFields可設定查找傳回的聯系人中包含的字段值.
<template>
	<view>
		<button type="default" @tap='get'>擷取</button>
		<view class="">
			{{content}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: ''
			};
		},
		methods: {
			get() {
				  plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function( addressbook ) {
					  // 向通訊錄中添加聯系人
					  var contact = addressbook.create();
					  contact.displayName = "A";
					  contact.phoneNumbers = [{type: "手機",value:"12345678911",preferred: true}];
					  contact.save();
					 //  console.log(addressbook)
					  addressbook.find(["displayName","phoneNumbers"],res=>{
						  // console.log(res)
				        for(var i in res){
				          console.log(res[i]);
				        }
					  })
				  }, function(error) {
				    console.log(error.message);
				  });
			},
			onSuccess(res) {
				console.log(res)
			}
		}
	}
</script>

<style >

</style>

           

參數:

  1. contactFields: (String[])(必選) 查找傳回聯系人中需要包含的資訊

    可取Contact對象的屬性名稱, 若指定為null或"“或空數組則包含所有聯系人資訊. 可通過”."來分割子項, 如"name.familyName"指定擷取聯系人名稱中的姓.

  2. successCB:(FindSuccessCallback)(必選)查找聯系人操作成功回調
  3. errorCB: (ContactsErrorCallback)(可選)查找聯系人操作失敗回調
  4. findOptions: (ContactFindOption)(可選)查找聯系人的參數

    ContactFindOption JSON對象, 查找聯系人參數

    interface ContactFindOption{
        attribute ContactFindFilte[] filter;//數組, 查找時的過濾器
        attribute Boolean multiple;//是否查找多個聯系人, 預設值為true
    }
               

3.2 Contact 聯系人對象

interface Contact {
    readonly attribute String id;
    attribute String displayName;
    attribute ContactName name;
    attribute String nickname;
    attribute ContackField[] phoneNumbers;
    attribute ContackField[] emails;
    attribute ContackAddress[] addresses;
    attribute ContackField[] ims;
    attribute ContackOriganization[] organizations;
    attribute Date birthday;
    attribute String note;
    attribute ContactField[] photos;
    attribute ContactField[] categories;
    attribute ContactField[] categories;
    function Contact clone();
    function void remove(SuccessCB, errorCB);
    function void save(successCB, errorCB);
}
           

3.2.1 屬性:

  1. id: 聯系人的id
  2. displayName: 聯系人顯示的名字
  3. name: 聯系人的名稱

    ContactName類型

    interface ContactName{
        attribute String formatted;//聯系人的完整姓名, 由其它字段組合生成
        attribute String familyNmae;//聯系人的姓
        attribute String givenName;//聯系人的名
        attribute String middleName;//聯系人的中間名
        attribute String honorificPrefix;//聯系人的字首
        attribute String honorificSuffix;//聯系人的字尾
    }
               
  4. nickname: 聯系人的昵稱
  5. phoneNumbers: 數組, 聯系人的電話
  6. emails: 數組, 聯系人的郵箱
  7. addresses: 數組, 聯系人的位址

    ContactAddress 類型

    interface ContactAddress{
        attribute String type;//聯系人位址類型, 如"home"表示家庭位址,"company"表示機關位址
        attribute String formatted;//完整位址
        attribute String streetAddress;//完整的街道位址
        attribute String locality; //城市或地區
        attribute String region; //省或地區
        attribute String country; //國家
        attribute String postalCode; //郵政編碼
        attribute Boolean preferred; //是否為首選項
    }
               
  8. ims: 數組, 聯系人的即時通訊位址
  9. organizations: 數組, 聯系人所屬組織資訊

    ContactOrganization 類型

    interface ContactOrganization {
        attribute String type;//聯系人所屬組織類型
        attribute String name;//聯系人所屬組織名稱
        attribute String department;//聯系人所屬組織部門
        attribute String title;//聯系人在組織中的職位
        attribute String preferred;//是否為首選項
    }
               
  10. birthday: 聯系人的生日
  11. note: 聯系人的備注
  12. photos: 數組, 聯系人的頭像
  13. categories: 數組, 聯系人的組名
  14. urls: 數組, 聯系人網址

3.2.2 方法

  1. clone: 克隆聯系人
克隆聯系人, 建立出一個新的聯系人對象
  1. remove: 删除聯系人
get() {
    plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function( addressbook ) {
        addressbook.find(null,res=>{
        // console.log(res)
            for(var i in res){
                console.log(res[i]);
				//找到聯系人顯示的名字為A
                if(res[i].displayName=="A"){
                    //删除這個聯系人
                    res[i].remove(()=>{
                    	console.log(res[i].id)
                    })
                }
            }
        })
    }, function(error) {
    	console.log(error.message);
    });
},
           
  1. save: 儲存聯系人
contact.save( function () {
    alert( "儲存聯系人成功" );
}, function ( e ) {
    alert( "儲存聯系人失敗:" + e.message );
} );