第一步:将网页图片保存为图片
saveQrcode() {
this.loadingFlag = true;
window.pageYOffset = 0;
document.documentElement.scrollTop = 0;
document.body.scrollTop = 0;
let htmlDom = document.getElementById("qrCodeContainer");
html2canvas(htmlDom, {
backgroundColor: null,
width: htmlDom.clientWidth,
height: htmlDom.clientHeight,
scrollY: 0,
scrollX: 0,
allowTaint: false,
useCORS: true,
}).then((canvas) => {
const imgUrl = canvas.toDataURL("image/jpeg");
let picFile = this.dataURLtoBlob(imgUrl, "图片");
this.upload(picFile, "medAdvice/").then((res) => {
this.getMediaId(res.Url);
});
});
},
将base64转换为blob(上面代码里要用到)
dataURLtoBlob(dataurl, fileName) {
var arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
var fileBlob = new Blob([u8arr], { type: mime });
fileBlob.lastModifiedDate = new Date();
fileBlob.name = fileName;
return fileBlob;
},
图片上传到COS上
upload(e,cosFilePath) {
let _this = this;
return new Promise((resolve, reject) => {
var file = e;
if (!file) return;
let slotImgName = md5(file.name + new Date().getTime());
_this.cos.putObject(
{
Bucket: _this.cosKeyJson.bucket,
Region: _this.cosKeyJson.region,
Key: cosFilePath + slotImgName,
Body: file,
onProgress: function (progressData, callback) {
logger.log(JSON.stringify(progressData));
}
},
function (err, data) {
_this.cos.getObjectUrl(
{
Key: cosFilePath + slotImgName,
Bucket: _this.cosKeyJson.bucket,
Sign: false,
Region: _this.cosKeyJson.region
},
function (err, data) {
resolve(data);
reject(err);
}
);
}
)
});
};
第二步:用调后端接口,传参是图片(看后端需要什么格式,需要base64格式则传上面的imgUrl,若需要file文件格式,则传上面的picFile),获取mediaId。
getMediaId(url) {
let needData = {
fileStream: url,
};
BaseHttpS.uploadFileToWeChat(needData).then((res) => {
if (res.data.code == "0000000") {
console.log('前端拿到的mediaId:',res.data.data)
this.sendQRcode(res.data.data);
} else {
this.loadingFlag = false;
this.$toast.fail(res.data.message);
}
});
},
第三步:调发企业微信API,使用mediaId把图片发送给用户
sendQRcode(mediaId) {
var local = location.href.split("#")[0];
let params = {
url: local,
};
BaseHttpS.qiyeWeiXinUrlSignature(params).then((res) => {
this.loadingFlag = false;
let sdkConfigInfo = res.data.data;
wx.config({
beta: true,
debug: false,
appId: sdkConfigInfo.corpid,
timestamp: sdkConfigInfo.timestamp,
nonceStr: sdkConfigInfo.nonceStr,
signature: sdkConfigInfo.signature,
jsApiList: [
"getCurExternalChat",
"sendChatMessage",
"openEnterpriseChat",
],
openTagList: ["wx-open-launch-weapp"],
success: function (res) {
},
fail: function (res) {
if (res.errMsg.indexOf("function not exist") > -1) {
_this.$toast.fail("版本过低请升级");
}
},
});
wx.ready(function () {
wx.agentConfig({
corpid: sdkConfigInfo.corpid,
agentid: sdkConfigInfo.agentid,
timestamp: sdkConfigInfo.timestamp,
nonceStr: sdkConfigInfo.nonceStr,
signature: sdkConfigInfo.signatureAgent,
jsApiList: [
"getCurExternalChat",
"sendChatMessage",
"openEnterpriseChat",
"getCurExternalContact",
],
success: function (res) {
wx.invoke(
"sendChatMessage",
{
msgtype: "image",
image: {
mediaid: mediaId,
},
},
function (res) {
console.log("sendChatMessage==" + res);
if (res.err_msg == "sendChatMessage:ok") {
}
console.log(res);
}
);
},
fail: function (res) {
if (res.errMsg.indexOf("function not exist") > -1) {
_this.$toast.fail("版本过低请升级");
}
},
});
});
wx.error(function (res) {
console.log(res, "error - config");
});
});
},