|
|
@@ -30,9 +30,15 @@ Page({
|
|
|
memberphoto: options.memberphoto ? options.memberphoto : user.memberphoto,
|
|
|
user
|
|
|
})
|
|
|
- this.getCode();
|
|
|
+ if (wx.getStorageSync('invitationCodeImg')) {
|
|
|
+ that.setData({
|
|
|
+ invitationCodeImg: wx.getStorageSync('invitationCodeImg'),
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ this.getCode();
|
|
|
+ }
|
|
|
wx.setNavigationBarTitle({
|
|
|
- title: options.inviteCode ? '他/她的会员码' : '我的会员码'
|
|
|
+ title: options.inviteCode ? '他/她的会员码' : '会员码'
|
|
|
});
|
|
|
},
|
|
|
downloadQR() {
|
|
|
@@ -153,23 +159,23 @@ Page({
|
|
|
// 带重试机制的图片绘制方法
|
|
|
drawImageWithRetry(ctx, imageUrl, x, y, width, height, isAvatar, callback) {
|
|
|
const that = this;
|
|
|
-
|
|
|
+
|
|
|
// 检查是否为base64格式的图片数据
|
|
|
if (imageUrl && (imageUrl.startsWith('data:image') || imageUrl.startsWith('/9j/'))) {
|
|
|
// 处理base64格式的图片
|
|
|
that.drawBase64Image(ctx, imageUrl, x, y, width, height, isAvatar, callback);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 如果imageUrl为空或无效,直接回调
|
|
|
if (!imageUrl) {
|
|
|
callback && callback();
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
wx.getImageInfo({
|
|
|
src: imageUrl,
|
|
|
- success: function(res) {
|
|
|
+ success: function (res) {
|
|
|
try {
|
|
|
// 检查是否为二维码图片,如果是则添加圆角
|
|
|
if (!isAvatar && that.data.invitationCodeImg && imageUrl === that.data.invitationCodeImg) {
|
|
|
@@ -193,7 +199,7 @@ Page({
|
|
|
// 如果是头像,需要圆形裁剪
|
|
|
ctx.save();
|
|
|
ctx.beginPath();
|
|
|
- ctx.arc(x + width/2, y + height/2, width/2, 0, 2 * Math.PI);
|
|
|
+ ctx.arc(x + width / 2, y + height / 2, width / 2, 0, 2 * Math.PI);
|
|
|
ctx.clip();
|
|
|
ctx.drawImage(res.path, x, y, width, height);
|
|
|
ctx.restore();
|
|
|
@@ -207,7 +213,7 @@ Page({
|
|
|
callback && callback();
|
|
|
}
|
|
|
},
|
|
|
- fail: function(err) {
|
|
|
+ fail: function (err) {
|
|
|
console.log('获取图片失败', err, imageUrl);
|
|
|
// 如果是头像且获取失败,使用默认头像
|
|
|
if (isAvatar) {
|
|
|
@@ -298,12 +304,12 @@ Page({
|
|
|
const that = this;
|
|
|
wx.canvasToTempFilePath({
|
|
|
canvasId: 'myCanvas',
|
|
|
- success: function(res) {
|
|
|
+ success: function (res) {
|
|
|
wx.hideLoading();
|
|
|
// 保存图片到相册
|
|
|
that.saveImageToPhotosAlbum(res.tempFilePath);
|
|
|
},
|
|
|
- fail: function(err) {
|
|
|
+ fail: function (err) {
|
|
|
wx.hideLoading();
|
|
|
console.log('生成海报失败', err);
|
|
|
wx.showToast({
|
|
|
@@ -313,18 +319,18 @@ Page({
|
|
|
}
|
|
|
}, that);
|
|
|
},
|
|
|
-
|
|
|
+
|
|
|
// 保存图片到相册
|
|
|
saveImageToPhotosAlbum(filePath) {
|
|
|
wx.saveImageToPhotosAlbum({
|
|
|
filePath: filePath,
|
|
|
- success: function() {
|
|
|
+ success: function () {
|
|
|
wx.showToast({
|
|
|
title: '已保存到相册',
|
|
|
icon: 'success'
|
|
|
});
|
|
|
},
|
|
|
- fail: function(err) {
|
|
|
+ fail: function (err) {
|
|
|
console.log('保存失败', err);
|
|
|
wx.showToast({
|
|
|
title: '保存失败',
|
|
|
@@ -445,6 +451,7 @@ Page({
|
|
|
that.setData({
|
|
|
invitationCodeImg: res.data
|
|
|
})
|
|
|
+ wx.setStorageSync('invitationCodeImg', res.data);
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
@@ -465,5 +472,45 @@ Page({
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将网络图片转换为base64格式
|
|
|
+ * @param {string} url - 网络图片地址
|
|
|
+ * @returns {Promise<string>} base64格式字符串
|
|
|
+ */
|
|
|
+ convertImageToBase64(url) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ // 下载网络图片
|
|
|
+ wx.downloadFile({
|
|
|
+ url: url,
|
|
|
+ success: (res) => {
|
|
|
+ if (res.statusCode === 200) {
|
|
|
+ const tempFilePath = res.tempFilePath;
|
|
|
+ // 读取本地文件内容并转换为base64
|
|
|
+ const fs = wx.getFileSystemManager();
|
|
|
+ fs.readFile({
|
|
|
+ filePath: tempFilePath,
|
|
|
+ encoding: 'base64',
|
|
|
+ success: (readRes) => {
|
|
|
+ // 构造完整的base64数据URI
|
|
|
+ const base64Data = 'data:image/png;base64,' + readRes.data;
|
|
|
+ resolve(base64Data);
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('读取图片文件失败:', error);
|
|
|
+ reject(error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ reject(new Error('下载图片失败'));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('下载图片失败:', error);
|
|
|
+ reject(error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
})
|