1. 逻辑代码
async qcFn() {
console.log('生成海报');
let that = this
const houseId = this.data.houseDetail._id
const refferId = app.globalData.openid
const url = 'cloud://renthouse-3gy8st7ycb91abf9.7265-renthouse-3gy8st7ycb91abf9-1308122562/素材/编组 11.png'
wx.showToast({
title: '海报生成中...',
icon: 'loading',
duration: 1000
});
setTimeout(function () {
wx.hideToast()
that.setData({
maskHidden: true
});
}, 1000);
console.log('生成海报22');
this.setData({
userInfo: app.globalData.userInfo,
})
let { houseDetail, title, location, tagselected, houseMoney } = this.data.houseDetail
const ctx = wx.createCanvasContext('starkImg')
ctx.clearRect(0, 0, 375, 670);
ctx.setFillStyle("#fff")
ctx.fillRect(0, 0, 375, 670)
const bgc_tmp = await this.downloadFile(url)
ctx.drawImage(bgc_tmp, 0, 0, 375, 670)
ctx.setStrokeStyle('rgba(0, 0, 0, 0.06)');
ctx.save();
ctx.setShadow(10, 50, 50, 'rgba(255, 255, 255, 1)')
const x = 40
const y = 44
const w = 300
const h = 488
const r = 10
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + r, 10);
ctx.arcTo(x + w, y + h, x + w - r, y + h, r);
ctx.arcTo(x, y + h, x, y + h - r, r);
ctx.arcTo(x, y, x + r, y, 10);
ctx.fill()
ctx.closePath()
ctx.clip();
console.log('生成海报33');
const house_tmp = await this.getImageInfoFn(this.data.houseDetail.pictures[0])
console.log('生成海报44', house_tmp);
ctx.drawImage(house_tmp, 40, 44, 300, 400)
ctx.restore()
ctx.setFontSize(16)
ctx.setFillStyle('rgba(51, 51, 51, 1)')
ctx.setTextBaseline('top')
const titlestr = this.fittingString(ctx, title, 290)
ctx.fillText(titlestr, 56, 452)
console.log('house_tmp');
const mapicon = 'cloud://renthouse-3gy8st7ycb91abf9.7265-renthouse-3gy8st7ycb91abf9-1308122562/素材/map3.png'
const mapicon_tmp = await this.getImageInfoFn(mapicon)
ctx.drawImage(mapicon_tmp, 53, 500, 29, 29,)
ctx.setTextBaseline('top')
ctx.setFillStyle('rgba(102, 102, 102, 1)')
ctx.setFontSize(14)
ctx.fillText(location, 87, 510)
console.log('房源标签');
ctx.setFillStyle('rgba(153, 153, 153, 0.8)')
ctx.setFontSize(14)
ctx.fillText(tagselected[0], 56, 480);
ctx.fillText(tagselected[1], 118, 480);
ctx.setFillStyle('rgba(86, 119, 248, 1)')
ctx.setFontSize(16)
ctx.fillText(`${houseMoney}元/月`, 260, 510);
ctx.restore()
let qrResult = await wx.cloud.callFunction({
name: "qrCodeFile",
data: { type: "createQRCode", houseId, refferId }
})
const qrcode_tmp = await this.downloadFile(qrResult.result)
ctx.drawImage(qrcode_tmp, 68.29, 559.49, 72.16, 72.16);
var qcInfo = "长按识别二维码";
ctx.setFontSize(14)
ctx.setTextBaseline('top')
ctx.setFillStyle('#333');
ctx.fillText(qcInfo, 161.08, 573.48);
ctx.stroke('red');
let titledel = "查看房源详情";
ctx.setFontSize(14);
ctx.setFillStyle('#333333');
ctx.fillText(titledel, 161.08, 607.056);
ctx.draw();
console.log('生成海报55');
setTimeout(function () {
console.log('737');
wx.canvasToTempFilePath({
destWidth: 375,
destHeight: 670,
canvasId: 'starkImg',
success: function (res) {
console.log('制期间耗时', res);
console.log(res.tempFilePath);
that.setData({
imagePath: res.tempFilePath,
hidden: false
})
console.log('imagespath', that.data.imagePath);
wx.hideLoading()
},
fail: function (res) {
console.log(res)
}
})
}, 500);
},
2. 在线链接转本地链接
async getImageInfoFn(Image) {
let temp = await wx.getImageInfo({
src: Image,
})
return temp.path
}
3. 云函数
const cloud = require("wx-server-sdk")
const uuid = require('uuid')
cloud.init()
const db = cloud.database()
const wxContext = cloud.getWXContext()
exports.main = async (event, ctx) => {
const uuidStr = uuid.v4().replace(/-/g, "")
console.log('uuidStr', uuidStr);
db.collection('qrcodeOptions').add({
data: {
uuid: uuidStr,
options: event
}
})
console.log('生成二维码', event);
const result = await cloud.openapi.wxacode.getUnlimited({
page: 'packageA/pages/houseDetail/houseDetail',
scene:`${uuidStr}`,
"checkPath": false,
"envVersion": 'develop'
})
const upload = await cloud.uploadFile({
cloudPath: `QRCodeFolder/${Math.floor(Math.random() * 1000)}.png`,
fileContent: result.buffer
})
console.log('upload', upload);
return upload.fileID
}
4. html页面
html页面
<view class='imagePathBox' hidden="{{maskHidden == false}}">
<image src="{{imagePath}}" class='shengcheng' ></image>
<button class='baocun' bindtap='baocun'>点击保存至相册,分享到朋友圈</button>
</view>
<view hidden="{{maskHidden == false}}" class="mask"></view>
<view class="canvas-box">
<canvas style="width:375px;height:670px;position:fixed;top:9999px" canvas-id="starkImg"/>
</view>
|