wkw vor 2 Wochen
Ursprung
Commit
80453a571d
4 geänderte Dateien mit 45 neuen und 10 gelöschten Zeilen
  1. 4 3
      components/Header/Header.wxss
  2. 14 5
      pages/index/index.js
  3. 2 2
      pages/index/index.wxml
  4. 25 0
      utils/util.js

+ 4 - 3
components/Header/Header.wxss

@@ -20,12 +20,13 @@
   right: 0;
 }
 .header-icon-left,.header-icon-right{
-  width: 14rpx;
+  width:40rpx;
   height: 48rpx;
+  text-align: center;
 }
 .header-icon{
-  width:100%;
-  height: 100%;
+  width: 16rpx;
+  height: 48rpx;
   flex-shrink: 0;
   color: #000;
 }

+ 14 - 5
pages/index/index.js

@@ -1,7 +1,7 @@
-import {miniProgramConfig} from '../../api/other';
+import {miniProgramConfig,sharePoster} from '../../api/other';
 import {BASE_URL} from '../../utils/request';
 import {isLoggedIn,doLogin} from '../../utils/auth';
-import { REPORT_BEHAVIOR,DRAW_POSTER } from '../../utils/util.js';
+import { REPORT_BEHAVIOR,DRAW_POSTER,base64ToFilePath } from '../../utils/util.js';
 const app = getApp();
 Page({
   data: {
@@ -42,6 +42,9 @@ Page({
       })
     }
   },
+  onHide(){
+    this.setData({ showShare: false });
+  },
   updateData(data){
     this.setData({
       banners:data.carousels,
@@ -125,9 +128,15 @@ Page({
     });
     if (!needGenerate) return;
     try {
-      const path = await DRAW_POSTER(); 
-      wx.setStorageSync('posterCache', { key: posterKey, path });
-      this.setData({ posterImg: path });
+      // const path = await DRAW_POSTER();
+      const res = await sharePoster();
+      if(res.code == 200){
+        let path = await base64ToFilePath(res.data);
+        wx.setStorageSync('posterCache', { key: posterKey, path });
+        this.setData({ posterImg: path });
+      }else{
+        throw new Error();
+      }
     } catch (err) {
       this.setData({ posterLoading: false });
       wx.showToast({ title: '生成海报失败', icon: 'none' });

+ 2 - 2
pages/index/index.wxml

@@ -84,8 +84,8 @@
   <view class="poster-container" wx:if="{{showPoster}}" bindtap="closePoster">
     <view class="poster-content">
       <van-loading wx:if="{{posterLoading}}" color="#FFA100" class="poster-loading">海报生成中...</van-loading>
-      <canvas wx:if="{{posterLoading}}" canvas-id="posterCanvas"
-        style="width:300px;height:600px;position:absolute;top:-9999px;"></canvas>
+      <!-- <canvas wx:if="{{posterLoading}}" canvas-id="posterCanvas"
+        style="width:300px;height:600px;position:absolute;top:-9999px;"></canvas> -->
       <image 
         wx:if="{{posterImg}}" 
         src="{{posterImg}}" 

+ 25 - 0
utils/util.js

@@ -138,3 +138,28 @@ function drawRoundRect(ctx, x, y, w, h, r) {
   ctx.arcTo(x, y, x + w, y, r);
   ctx.closePath();
 }
+
+// base64 转临时文件路径
+export function base64ToFilePath(base64Data, fileName = 'poster.png') {
+  return new Promise((resolve, reject) => {
+    try {
+      const fs = wx.getFileSystemManager();
+      const filePath = `${wx.env.USER_DATA_PATH}/${fileName}`;
+      
+      // 去掉可能存在的 data:image/jpeg;base64, 前缀
+      const pureBase64 = base64Data.replace(/^data:image\/\w+;base64,/, "");
+      const buffer = wx.base64ToArrayBuffer(pureBase64);
+
+      fs.writeFile({
+        filePath,
+        data: buffer,
+        encoding: 'binary',
+        success: () => resolve(filePath), // 返回本地路径
+        fail: reject
+      });
+    } catch (err) {
+      reject(err);
+    }
+  });
+}
+