微信小程序一键获取发票抬头

在日常微信小程序的开发过程中,有时候我们的需求需要获取微信发票的相关信息,本文就来介绍下,在微信小程序中,如何获取我们保存的发票消息,以及用户同意授权或者拒绝授权之后,再次拉起授权等。

选择用户的发票抬头 用到的微信小程序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属性,这个是发票抬头类型

然后根据获取到的信息,将信息渲染到页面上就可以了,以上就是微信小程序获取发票信息的相关过程,希望对你有所帮助。

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_流年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值