This commit is contained in:
2023-09-26 16:55:20 +08:00
parent 386a66a324
commit 3748f042cb
19 changed files with 322 additions and 416 deletions

View File

@@ -1,249 +1,187 @@
/*
* 手太欠
* 愿这世界都如故事里一样 美好而动人~
*/
Page({
/**
* 页面的初始数据
*/
data: {
shareSee : false, //分享弹出
identity : '', //1为普通
userInfo : '', //用户信息
inviteText : '', //邀请码
inviteCode : '', //二维码
//海报
posterDatas: {
width : 375, //画布宽度
height : 800, //画布高度
// 缓冲区,无需手动设定
pic : null,
buttonType : 1,
show : false, // 显示隐藏海报弹窗
success : false, // 是否成功生成过海报
canvas : null, // 画布的节点
ctx : null, // 画布的上下文
dpr : 1, // 设备的像素比
},
nameInfo: '', // 用户名
invite : '', // 邀请码
qrcode : '', // 二维码
posters : [], // 海报数组
current : 0, // 选项卡下标
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
//生成海报初始化
var that = this;
var 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()
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
// 获取推广码
this.inviteInfo();
onLoad() {
let miniShareFun = wx.$api.user.poster({ url: 'pages/mall/index' })
// wx.$api.user.poster({ url: 'pages/mall/index' }).then(res => {
// console.log(res.data)
// this.setData({
// nameInfo : res.data.name,
// posters : res.data.posters,
// qrcode : res.data.qrcode
// })
// })
// 小程序码
this.ShareInfo();
},
/**
* 推广码
*/
inviteInfo() {
wx.$api.user.invite().then(res => {
this.setData({
identity : res.data.code,
userInfo : res.data.user_info,
inviteText : res.data.invite
})
}).catch(err => {})
},
/**
* 小程序码
*/
ShareInfo() {
wx.$api.user.miniShare({
url: '/pages/login/index'
}).then(res => {
this.setData({
inviteCode: res.data.qrcode
})
}).catch(err => {})
},
//海报生成 //画布 生成 海报[海报]
saveImg () {
var that = this;
var posterDatas = that.data.posterDatas
var canvas = posterDatas.canvas
var ctx = posterDatas.ctx
wx.showLoading({
title: '海报生成中',
mask: true
});
title: '加载中...',
mask : true
})
//二维码
var codeImg = new Promise(function (resolve, reject) {
const photo = canvas.createImage();
photo.src = that.data.inviteCode;
photo.onload = (e) => {
resolve(photo);
}
});
//背景素材
var backImg = new Promise(function (resolve, reject) {
const photo = canvas.createImage();
photo.src = "https://api.siyuankunlun.cn/storage/images/2023/03/14/7777441f7a2b25353f2d6de61452418c.png";
photo.onload = (e) => {
resolve(photo);
}
});
Promise.all([codeImg, backImg]).then(res => {
// 绘制背景
ctx.drawImage(res[1], 0, 0, posterDatas.width, posterDatas.height);
// 绘制[二维码-白色背景]
ctx.fillStyle = "#ffffff";
ctx.fillRect(200, 540, 120, 120);
// 绘制[二维码-白色背景黑框]
ctx.strokeStyle = "black";
ctx.strokeRect(199, 539, 122, 122);
// 绘制[二维码]
ctx.drawImage(res[0], 210, 550, 100, 100);
// 文字
ctx.font = "bold 14px Arial"; //字体大小
ctx.fillStyle = "#000"; //字体颜色
ctx.textAlign = "center"
ctx.fillText('扫描二维码了解更多', 260, 690);
// 关闭loading
wx.hideLoading();
//显示海报
posterDatas.success = true;
that.setData({
posterDatas
Promise.all([miniShareFun]).then(res => {
console.log(res)
let miniShareData = res[0].data
this.setData({
posters : miniShareData.posters,
qrcode : miniShareData.code,
invite : miniShareData.invite,
current : 0
})
this.onDownloadImges();
}).catch(err=>{})
},
//下载图片[海报]
onDownloadImges () {
wx.showLoading({
title: '保存中',
mask: true
});
var that = this;
var posterDatas = that.data.posterDatas;
if (!posterDatas.pic) {
that.onCanvasBuildImges();
return;
}
//可写成函数调用 这里不做解释
wx.saveImageToPhotosAlbum({
filePath: posterDatas.pic,
success(res) {
wx.hideLoading();
wx.showToast({
icon: 'none',
title: '已保存到相册,快去分享吧',
})
that.setData({
posterDatas,
shareSee: !that.data.shareSee
})
},
fail() {
wx.hideLoading();
wx.showToast({
icon: 'none',
title: '进入设置页,开启“保存到相册”',
})
that.setData({
posterDatas
})
return;
}
// wx.getImageInfo({
// src : miniShareData.qrcode,
// success : qrcodePath => {
// this.setData({
// qrcode : qrcodePath.path,
// posters : miniShareData.posters,
// nameName : miniShareData.name,
// current: 0
// })
// }
// })
wx.hideLoading()
})
},
//画布 转 图片[海报]
onCanvasBuildImges () {
var that = this;
var posterDatas = that.data.posterDatas;
wx.canvasToTempFilePath({
canvas: posterDatas.canvas,
width: posterDatas.width,
height: posterDatas.height,
destWidth: posterDatas.width * 3,
destHeight: posterDatas.height * 3,
success: res=> {
posterDatas["pic"] = res.tempFilePath;
that.setData({
posterDatas
})
that.onDownloadImges();
},
fail() {
wx.hideLoading();
wx.showToast({
icon: 'none',
title: 'sorry 保存失败,请稍后再试.',
})
return;
}
});
},
/**
* 分享弹出
*/
shareTap() {
swiperChange(e) {
let index = e.detail.current
this.setData({
shareSee: !this.data.shareSee
current: index
})
},
/**
* 检查授权信息
*/
onCheckSetting(){
wx.getSetting({
success: res => {
if(res.authSetting['scope.writePhotosAlbum'] || res.authSetting['scope.writePhotosAlbum'] === undefined){
this.onCanvas()
return
}
wx.showModal({
title : '提示',
content : '暂未授权小程序写入您的相册,无法存储海报',
confirmColor: '#144592',
confirmText : '去设置',
success: res => {
if (res.confirm) {
wx.openSetting()
}
}
})
}
})
},
/**
* 生成海报
*/
onCanvas(){
wx.showLoading({
title: '加载中...',
mask : true
})
wx.getImageInfo({
src: this.data.posters[this.data.current].cover,
success: imgInfo => {
wx.hideLoading()
wx.showLoading({
title : '生成海报中...',
mask : true
})
let posterSrc = imgInfo.path
let posterW = this.data.posters[this.data.current].width
let posterH = this.data.posters[this.data.current].height
let posterY = this.data.posters[this.data.current].position.y
let posterX = this.data.posters[this.data.current].position.x
let qrcodeSize = this.data.posters[this.data.current].position.size
let qrcodeSrc = this.data.qrcode
wx.createSelectorQuery().select('#canvas').fields({ node: true, size: true }).exec(res => {
const canvas = res[0].node;
const ctx = canvas.getContext("2d");
const img = canvas.createImage()
const code = canvas.createImage()
const dpr = wx.getSystemInfoSync().pixelRatio
// 设置画布
canvas.width = posterW * dpr
canvas.height = posterH * dpr
ctx.scale(dpr, dpr)
// 清理画布
ctx.clearRect(0, 0, canvas.width, canvas.height)
// 绘制背景
img.src = posterSrc
img.onload = () => {
ctx.drawImage(img, 0, 0, posterW, posterH)
// 绘制图片二维码
code.src = qrcodeSrc
code.onload = () => {
ctx.drawImage(code, posterX, posterY, qrcodeSize, qrcodeSize)
// 保存海报
wx.canvasToTempFilePath({
canvas : canvas,
width : canvas.width,
height : canvas.height,
destWidth : canvas.width,
destHeight : canvas.height,
quality : 1,
success : paths => {
wx.saveImageToPhotosAlbum({
filePath: paths.tempFilePath,
success: res => {
wx.showToast({
title : '海报已保存',
})
}
})
},
fail: () => {
wx.hideLoading()
wx.showModal({
title: '提示',
content: '暂未授权小程序写入您的相册,无法存储海报',
confirmColor: '#e50d01',
confirmText: '去设置',
success: res => {
if (res.confirm) {
wx.openSetting()
}
}
})
}
})
}
}
})
}
})
},
/**
* 微信分享
*/
onShareAppMessage(){
return {
title : this.data.userInfo.nickname + '邀请您了解锶源昆仑',
path : "/pages/index/index?invite=" + this.data.userInfo.inviteText,
title : '锶源昆仑',
path : "/pages/index/index?invite=" + this.data.invite,
imageUrl: "http://cdn.siyuankunlun.com/materials/2022/09/14/code.jpg"
}
}