bool, 'message' => string, 'data' => ['image_url' => string]] */ public function getUserPoster($userId) { try { // 1. 验证用户ID if ($userId <= 0) { return ['status' => false, 'message' => '无效的用户ID']; } // 2. 获取系统配置 $config = getConfig('base'); $posterPath = $config['poster_template'] ?? '/img_poster.jpg'; $cacheExpire = $config['poster_cache_expire'] ?? (86400 * 100); // 默认缓存1天 // 3. 获取模板文件信息 $templateFile = getcwd() . $posterPath; if (!file_exists($templateFile)) { return ['status' => false, 'message' => '海报模板文件不存在']; } $templateHash = md5_file($templateFile); // 4. 获取小程序配置 // 使用新的MiniprogramHelper获取当前域名对应的小程序配置 $miniprogram = \app\common\helper\MiniprogramHelper::getMiniprogramConfig(); $appId = $miniprogram['appid'] ?? ''; if (empty($appId)) { $wechat_setting = getConfig("wechat_setting"); $appId = $wechat_setting['appid'] ?? ''; } // 5. 查询缓存记录 $cacheRecord = UserPosterCache::findValidCache($userId, $templateHash, $appId); // 6. 如果存在有效缓存,直接返回 if ($cacheRecord) { return [ 'status' => 1, 'message' => '海报获取成功', 'data' => ['image_url' => $cacheRecord['cos_url']] ]; } // 7. 生成推广链接 $wxServer = new Wx(app()); $urlLink = $wxServer->generateUrlLinks($userId); if (empty($urlLink)) { return ['status' => false, 'message' => '生成推广链接失败']; } // 8. 生成海报图片 $autoload = new autoload(); $imageData = $autoload->generatePosterWithQR($templateFile, $urlLink); if (!$imageData) { return ['status' => false, 'message' => '海报生成失败']; } // 9. 上传到腾讯云COS $cosConfig = getConfig('uploads'); $tencentUploader = new TencentCosUploader($cosConfig); // 创建文件存储路径 $filePath = 'poster/' . date('Ymd') . '/' . $userId . '_' . substr($templateHash, 0, 8) . '_' . uniqid() . '.png'; try { $uploadResult = $tencentUploader->uploadFile($imageData, $filePath); $cosUrl = $uploadResult['full_url']; $fileSize = strlen($imageData); // 10. 存储缓存记录 $expiresAt = date('Y-m-d H:i:s', time() + $cacheExpire); $cacheData = [ 'user_id' => $userId, 'app_id' => $appId, 'template_hash' => $templateHash, 'cos_url' => $cosUrl, 'file_size' => $fileSize, 'mime_type' => 'image/png', 'status' => 1, 'expires_at' => $expiresAt ]; // 11. 失效该用户的其他海报缓存 UserPosterCache::invalidateOldCaches($userId, $templateHash); // 12. 保存新缓存记录 $posterCache = new UserPosterCache(); $posterCache->save($cacheData); // 13. 随机清理过期缓存 if (mt_rand(1, 100) <= 5) { UserPosterCache::cleanExpiredCaches(); } return [ 'status' => true, 'message' => '海报生成成功', 'data' => ['image_url' => $cosUrl] ]; } catch (\Exception $e) { Log::error('用户海报上传COS失败:' . $e->getMessage()); return ['status' => false, 'message' => '海报上传失败']; } } catch (\Exception $e) { Log::error('获取用户海报异常:' . $e->getMessage()); return ['status' => false, 'message' => '系统异常,请稍后重试']; } } }