]*>(.*?)<\/title>/is', $content, $m)) {
return trim(html_entity_decode($m[1], ENT_QUOTES, 'UTF-8'));
}
}
return basename($dir);
}
// ===== 查找分享图标 =====
function findShareIcon($dir)
{
$files = ['share.jpg', 'share.png', 'share.jpeg', 'favicon.ico'];
foreach ($files as $file) {
if (file_exists($dir . '/' . $file)) return $file;
}
return null;
}
// ===== 初始化 =====
$gameDir = __DIR__;
$gameTitle = detectGameTitle($gameDir);
$shareIcon = findShareIcon($gameDir);
$sdkPath = findWxSdk($gameDir);
// 读取站点配置
$siteTitle = '游戏中心';
$siteConfigFile = dirname($sdkPath ?: __DIR__) . '/data/config.json';
if (file_exists($siteConfigFile)) {
$siteCfg = json_decode(file_get_contents($siteConfigFile), true);
$siteTitle = $siteCfg['site']['title'] ?? '游戏中心';
}
// 加载微信 SDK
$wxEnabled = false;
$wxUser = null;
$shareData = null;
if ($sdkPath) {
require_once $sdkPath;
WxSDK::init();
$wxEnabled = true;
$wxUser = WxSDK::getUser();
$shareData = WxSDK::autoShareData($gameDir, [
'title' => $gameTitle,
'icon' => $shareIcon ?: 'share.jpg',
'desc' => '我在《' . $gameTitle . '》中得了 {score} 分!手速 {speed},你能超过我吗?',
]);
}
// 构建微信用户信息 JSON
$wxUserInfoJson = '{}';
if ($wxUser) {
$wxUserInfoJson = json_encode([
'openid' => $wxUser['openid'] ?? '',
'nickname' => $wxUser['nickname'] ?? '',
'avatar' => $wxUser['avatar'] ?? '',
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
// 读取 index.html 完整内容
$htmlFile = __DIR__ . '/index.html';
$html = file_exists($htmlFile) ? file_get_contents($htmlFile) : '';
// 替换占位符为真实微信数据
$wxDataScript = '<script>window.WX_USER_INFO = ' . $wxUserInfoJson . ';</script>';
$html = str_replace('<!-- WX_USER_DATA_PLACEHOLDER -->', $wxDataScript, $html);
// 替换页面标题
$html = preg_replace('/<title>[^<]*<\/title>/i', '<title>' . htmlspecialchars($gameTitle) . '
游戏