wkw 3 tuần trước cách đây
mục cha
commit
f1cdf4660f
1 tập tin đã thay đổi với 16 bổ sung12 xóa
  1. 16 12
      pages/index/index.js

+ 16 - 12
pages/index/index.js

@@ -18,7 +18,11 @@ Page({
     showPoster:false,
     posterImg:'',//生成的海报
   },
-  async onLoad(){
+  onLoad(){
+    // this.getminiProgramConfig();
+  },
+  // 获取配置信息
+  async getminiProgramConfig(){
     const res = await miniProgramConfig();
     wx.setStorageSync('programConfig', res.data);
     app.globalData.programConfig = res.data;
@@ -32,6 +36,9 @@ Page({
     })
   },
   onShow() {
+    if(!wx.getStorageSync('programConfig')){
+      this.getminiProgramConfig();
+    }
     if (typeof this.getTabBar === 'function' && this.getTabBar()) {
       this.getTabBar().setData({
         selected: 0
@@ -101,9 +108,8 @@ Page({
   },
   // 点击海报生成图片
   async openPoster() {
-    const userInfo = app.globalData.userInfo;
-    const programConfig = app.globalData.programConfig;
-    console.log(userInfo)
+    const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
+    const programConfig = app.globalData.programConfig || wx.getStorageSync('programConfig');
     const posterKey = `posterImg_${userInfo.avatar}_${userInfo.nickname}_${programConfig.share_img}_${programConfig.share_qrcode}`;
     const cachedData = wx.getStorageSync('posterCache') || {};
     if (cachedData.key === posterKey && cachedData.path) {
@@ -117,6 +123,8 @@ Page({
   },
 
   async drawPoster() {
+    const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
+    const programConfig = app.globalData.programConfig || wx.getStorageSync('programConfig');
     const ctx = wx.createCanvasContext('posterCanvas', this);
     const canvasWidth = 300;
     const canvasHeight = 600;
@@ -128,7 +136,7 @@ Page({
     this.drawRoundRect(ctx, 0, 0, canvasWidth, canvasHeight, radius);
   
     // 背景图
-    const bgUrl = app.globalData.programConfig.share_img;
+    const bgUrl = programConfig.share_img;
     if (bgUrl) {
       const bgPath = bgUrl.startsWith('http') ? await this.downloadImage(bgUrl) : bgUrl;
       ctx.drawImage(bgPath, 0, 0, canvasWidth, canvasHeight - bottomHeight);
@@ -139,7 +147,7 @@ Page({
     ctx.fillRect(0, canvasHeight - bottomHeight, canvasWidth, bottomHeight);
   
     // 用户头像(圆形)
-    const avatarUrl = app.globalData.userInfo.avatar;
+    const avatarUrl = userInfo.avatar;
     if (avatarUrl) {
       const avatarPath = avatarUrl.startsWith('http') ? await this.downloadImage(avatarUrl) : avatarUrl;
       const avatarSize = 40;
@@ -154,7 +162,7 @@ Page({
     }
   
     // 昵称
-    const nickName = app.globalData.userInfo.nickname || '游客';
+    const nickName = userInfo.nickname || '游客';
     ctx.setFontSize(16);
     ctx.setFillStyle('#000');
     const textX = 20 + 40 + 12;
@@ -162,7 +170,7 @@ Page({
     ctx.fillText(nickName, textX, textY);
   
     // 二维码
-    const qrUrl = app.globalData.programConfig.share_qrcode;
+    const qrUrl = programConfig.share_qrcode;
     if (qrUrl) {
       const qrPath = qrUrl.startsWith('http') ? await this.downloadImage(qrUrl) : qrUrl;
       const qrSize = 50;
@@ -215,10 +223,6 @@ Page({
   },
   // 保存相册
   savePoster() {
-    if (!this.data.posterImg) {
-      wx.showToast({ title: '没有海报图片', icon: 'none' });
-      return;
-    }
     wx.saveImageToPhotosAlbum({
       filePath: this.data.posterImg,
       success() { wx.showToast({ title: '保存成功' }); },