实现点击按钮获取发票抬头信息回显到页面
步骤:
1.在项目中引入 jweixin.js
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
2.主要代码:
//发票抬头接口
doReady () {
let _this = this
wx.invoke('chooseInvoiceTitle', {
"scene": "1"
}, function (res) {
// 这里处理调用结果X``
var info = JSON.parse(JSON.stringify(res)).choose_invoice_title_info;
var infoJ = JSON.parse(JSON.parse(JSON.stringify(info)));
_this.aa = infoJ
_this.invoiceSave.title = infoJ.title //发票抬头
_this.invoiceSave.taxFileNumber = infoJ.taxNumber; //税号
_this.invoiceSave.address = infoJ.companyAddress //公司地址
_this.invoiceSave.tel = infoJ.telephone //电话
_this.invoiceSave.bankname = infoJ.bankAccount //开户银行
_this.invoiceSave.bankAccount = infoJ.bankName // 银行账号
_this.invoiceSave.titleType = infoJ.type //发票抬头类型
// _this.$message.success('成功'+info)
})
},
//点击按钮获取微信参数配置
getInvoiceList(){
let _this = this
getwxticket(_this.url).then(res=>{
let data = res.data
wx.config({
beta: true,//用于方法未对外公开的情况
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来
appId: data.appId, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr:data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名,见附录1
jsApiList: [
"chooseInvoiceTitle"
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
_this.doReady();
});
wx.error(function(res){
console.log(res,'失败!')
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
})
},
3.注:ios会获取不到发票抬头,会报签名无效,原因是ios只识别第一次进入的页面路径
解决方法是在路由守卫加判断
router.beforeEach((to, from, next) => {
if (window.entryUrl == undefined) {
window.entryUrl = location.href.split('#')[0]
}
})
在调用获取配置参数接口之前做机型判断
let u= navigator.userAgent;
let isAndroid = u.indexOf('Android')>-1||u.indexOf('Adr')>-1
if (isAndroid) {
_this.url= Base64.encode(location.href.split('#')[0])
}else{
_this.url= Base64.encode(window.entryUrl)
}