一、简介
做个简单的生成二维码海报分享,我做的时候也找简单的方法看能不能实现页面直接截图那种生成图片,原生小程序不支持,不多介绍下面有全部代码有注释、参数自行替换运行看看,还有需要优化的地方,有问题可以咨询我,我写的已经上线
如图:
二、 js:
// 产品详情
import {
getProductDetails,
getDataList,
getShareData,
getUnlimitedQRCode
} from "../../../../../api/dsxapi";
const ui = require("../../../../../utils/ui");
Page({
data: {
id: null, //跳转传过来的产品id
datas: null, //详情数据
images: null, //轮播图
current: 0,
proTags: null, //产品标签
images2: null, //详情图片
routeId: null, //哪里打开的
show: false, //遮罩层
showShare: false, //分享面板
shareImg: false, //控制分享图标
options: [{
name: '微信',
icon: 'https://wx.applet.style.51dsx.cn/img/share_button_wechat.png',
openType: 'share'
},
{
name: '生成分享图',
icon: 'https://wx.applet.style.51dsx.cn/img/share_button_fxt.png'
},
],
posterDatas: {
width: 281, //画布宽度
height: 460, //画布高度
// 缓冲区,无需手动设定
pic: null,
buttonType: 1,
show: false, // 显示隐藏跳转设置
success: false, // 是否成功生成过海报
canvas: null, // 画布的节点
ctx: null, // 画布的上下文
dpr: 1, // 设备的像素比
},
QRcodeImg: null, //小程序二维码
photoWidth: null //产品图片宽度
},
onLoad(options) {
this.getlength()
this.setData({
id: options.id
})
if (options.routeId) {
this.setData({
routeId: options.routeId
})
}
if (options.scene) { //扫二维码进来的
this.setData({
id: decodeURIComponent(options.scene).split('id=')[1].split('&')[0],
routeId: decodeURIComponent(options.scene).split('routeId=')[1]
})
}
let that = this;
//生成海报初始化
let posterDatas = that.data.posterDatas
const query = wx.createSelectorQuery()
query.select('#firstCanvas').fields({
node: true,
size: true
},
function (res) {
const canvas = res.node
const ctx = canvas.getContext('2d')
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = posterDatas.width * dpr
canvas.height = posterDatas.height * dpr
ctx.scale(dpr, dpr)
posterDatas.canvas = canvas
posterDatas.ctx = ctx
posterDatas.dpr = dpr
//存储
that.setData({
posterDatas
})
}).exec()
this._getProductDetails(this.data.id)
},
onShow() {
// 判断分享打开并且没登录的跳转登录
if (this.data.routeId) {
if (wx.getStorageSync("appuser") == undefined || wx.getStorageSync("appuser") == '') {
wx.navigateTo({
url: "/https/blog.csdn.net/subPackag/pages/me_jump/login/login",
});
}
}
this.getlength()
this._getProductDetails(this.data.id)
this.setData({
show: false
})
},
//自定义导航栏计算
getlength() {
let windowWidth = wx.getSystemInfoSync().windowWidth;
const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
const menuButton = wx.getMenuButtonBoundingClientRect();
let navHeight = (menuButton.height + (menuButton.top - statusBarHeight) * 2) * (750 / windowWidth);
let statusBarTop = statusBarHeight * (750 / windowWidth);
this.setData({
navHeight: navHeight + statusBarTop,
statusBarTop: menuButton.top,
})
},
//自定义返回按钮
onBack() {
if (this.data.routeId && this.data.routeId == 5 || this.data.routeId == 797 || this.data.routeId == 6) { //5好友里打开797朋友圈打开1未登录点击登录后大师兄页面返回
// wx.navigateTo({
// url: `/subPackag/pages/consult_jump/EnterprisePiece/market/market`,
// });
// 判断从分享和扫码打开点击为关闭小程序
wx.exitMiniProgram({
success: function () {},
fail: function () {}
})
} else {
wx.navigateBack();
}
},
//跳转企业/园区详情
goEnterprise(event) {
if (this.data.datas.type == 2) {
//园区
wx.navigateTo({
url: `/subPackag/pages/consult_jump/EnterprisePiece/park/park?id=${event.currentTarget.dataset.id}`,
});
} else {
//企业
wx.navigateTo({
url: `/subPackag/pages/consult_jump/EnterprisePiece/enterprise/enterprise?id=${event.currentTarget.dataset.id}`,
});
}
},
//跳转案例详情
goCase(event) {
let params = {
type: 4,
id: event.currentTarget.dataset.id,
};
getShareData(params)
.then((res) => {
wx.navigateTo({
url: `/sDsxPackag/pages/webview/webview?url=${res.data.shareUrl}&title=${res.data.shareTitle}`
})
})
},
//跳转大师兄详情
goDSXdetails(event) {
getDataList({
brotherName: event.currentTarget.dataset.item.realName,
current: 1,
size: 10
}).then((res) => {
console.log(1111111111111, res);
const nowdata = {
item: res.data.records[0],
};
console.log(111, nowdata);
var queryBean = JSON.stringify(nowdata);
if (wx.getStorageSync('queryBean') !== undefined) {
let qb = wx.getStorageSync("queryBean");
qb = queryBean;
wx.setStorageSync("queryBean", qb);
} else {
wx.setStorageSync("queryBean", queryBean);
}
wx.navigateTo({
url: `/sDsxPackag/pages/dsx/dsxcard/dsxcard?queryBean=${encodeURIComponent(
queryBean
)}`,
});
});
},
//产品详情
_getProductDetails(id) {
let params = {
id: id
};
getProductDetails(params).then((res) => {
this.setData({
datas: res.data,
images: res.data.proImgs.split(","),
proTags: res.data.proTags.split(","),
images2: res.data.proDetailsImgs.split(","),
shareImg: true
});
}).catch(function (imError) {
console.log(imError);
})
},
//客服电话
servicePhone() {
if (this.data.datas.phone) {
wx.makePhoneCall({
phoneNumber: this.data.datas.phone
})
} else {
wx.makePhoneCall({
phoneNumber: '4001512051'
})
}
},
//轮播图预览
tapAvatar(event) {
console.log(event);
wx.previewImage({
current: event.currentTarget.dataset.item,
urls: this.data.images,
})
console.log(event);
},
//轮播数字指示
swiperChange(e) {
var that = this;
if (e.detail.source == 'touch') {
that.setData({
current: e.detail.current
})
}
},
//获取二维码
//产品详情
_getUnlimitedQRCode() {
let params = {
id: this.data.id,
type: 1,
envVersion: 'release' //正式版为 "release",体验版为 "trial",开发版为 "develop
};
getUnlimitedQRCode(params).then((res) => {
const base64 = res.data.qrcode;
const time = new Date().getTime();
//USER_DATA