1. 引入
npm install weixin-js-sdk -S
2. main.js
import wx from 'weixin-js-sdk'
Vue.prototype.$wx = wx;
3. App.vue获取签名进行注册
let params =
{
debug: "false",
jsApiList:
"scanQRCode,openTagList,chooseImage,uploadImage,getLocalImgData",
url: encodeURIComponent(window.location.href.split("#")[0])
}
this.$wx.config({
debug,
appId,
timestamp,
nonceStr,
signature,
jsApiList
});
this.$wx.ready(() => {
console.log("wx.ready");
});
this.$wx.error(err => {
console.log("wx.error", err);
});
4. 其他页面使用(选择图片并上传到自己的服务器)
wxChooseImg(){
this.$wx.chooseImage({
count: 1,
sizeType: ["compressed"],
sourceType: ["album", "camera"],
success: function(res) {
let localIds = res.localIds;
that.$wx.getLocalImgData({
localId: localIds[0],
success: function(r) {
let localData = r.localData;
that.uploadImage(localData);
}
});
}
});
},
upload(){
let blob = convertBase64UrlToBlob(result);
let formData = new FormData();
formData.append("file", blob, new Date().getTime() + ".jpeg");
this.$store.dispatch("common/countAdd");
let res = await axios.post(
baseUrl + "/icr/recognize_id_card",
formData,
{
Headers: {
"content-type": "multipart/form-data"
}
}
);
this.$store.dispatch("common/countReduce");
}