uniapp调用手机自带短信功能发送短信,打电话,获取本地通讯录

发布时间 2023-05-09 12:01:59作者: Stitchhhhh

1、配置(修改了manifest配置信息,要重新打包自定义基座,并卸载手机上的安装包重新运行)

 2、

// 发短信
sendMsg(phoneNumber) {
    //#ifdef APP-PLUS
    plus.messaging.TYPE_SMS;
    var msg = plus.messaging.createMessage(plus.messaging.TYPE_SMS);
    msg.to = [phoneNumber];
    msg.body = '';
    plus.messaging.sendMessage(msg);
    // #endif
    //#ifdef H5
    window.location.href = `sms:${phoneNumber}`
    // #endif
},
//拨打电话
callPhone(phoneNumber) {
    uni.makePhoneCall({
        phoneNumber,
    })
}

 

// 获取本地联系人
created() {
    //#ifdef APP-PLUS
    this.getLocalContact()
    //#endif
},
computed:{
    contactList(){
        return this.localContact.filter(item=>{
            return (item.realName.includes(this.searchValue)||item.phone.includes(this.searchValue))
        })
    }
},
methods: {
    getLocalContact() {
    let type = plus.contacts.ADDRESSBOOK_PHONE
    plus.contacts.getAddressBook(
        type,
        success => {
                success.find([], res => {
                    this.localContact = res.map(item => {
                        return {
                            realName: item.displayName,
                            phone: item.phoneNumbers[0].value
                        }
                    })
                },
                err => {

                }
            )
        },
        error => {
            console.log(error);
        })
    }
}