在日常微信小程序的开发过程中,有时候我们的需求需要获取微信发票的相关信息,本文就来介绍下,在微信小程序中,如何获取我们保存的发票消息,以及用户同意授权或者拒绝授权之后,再次拉起授权等。
选择用户的发票抬头 用到的微信小程序api是 wx.chooseInvoiceTitle(Object object) 链接如下:
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/invoice/wx.chooseInvoiceTitle.html
需要注意的是 调用前需要 用户授权 scope.invoiceTitle
当前小程序必须关联一个公众号,且这个公众号是完成了微信认证的,才能调用 chooseInvoiceTitle。
实现代码如下:
wxml:
<view class="invoiceWechat">
<text bindtap="getWechat">获取微信发票抬头</text>
</view>
js:
getWechat:function(){
let that = this;
wx.getSetting({
success(res) {
if (res.authSetting['scope.invoiceTitle']) {
wx.chooseInvoiceTitle({
success(res) {
if(res){
let getCode = res.type;
if(getCode == 1){//个人
that.setData({
personName:res.title,
isPeople:true,
isCompany:false,
isActive:0
})
}else if(getCode == 0){//单位
that.setData({
companyAdress:res.companyAddress,
companyCode:res.taxNumber,
companyName:res.title,
companyBank:res.bankName,
companyBankaccount:res.bankAccount,
isPeople:false,
isCompany:true
})
}
}
}
})
} else {
if (res.authSetting['scope.invoiceTitle'] == false) {
wx.openSetting({
success(res) {
console.log(res.authSetting)
}
})
} else {
wx.chooseInvoiceTitle({
success(res) {
if(res){
let getCode = res.type;
if(getCode == 1){//个人
that.setData({
personName:res.title,
isPeople:true,
isCompany:false
})
}else if(getCode == 0){//单位
that.setData({
companyAdress:res.companyAddress,
companyCode:res.taxNumber,
companyName:res.title,
companyBank:res.bankName,
companyBankaccount:res.bankAccount,
isPeople:false,
isCompany:true
})
}
}
},
fail:function(){
wx.showToast({
title: '请打开发票抬头权限后重试',
icon:'none',
duration:2000
})
}
})
}
}
}
})
}
其中succes回调中,res返回的对象中有个type属性,这个是发票抬头类型
然后根据获取到的信息,将信息渲染到页面上就可以了,以上就是微信小程序获取发票信息的相关过程,希望对你有所帮助。