1.新增显示分红说明文档菜单
2.文档新增英文版
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace app\admin\controller\docs;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\docs\MarkdownDocReader;
|
||||
use support\Response;
|
||||
use Webman\Http\Request;
|
||||
|
||||
@@ -13,7 +14,17 @@ use Webman\Http\Request;
|
||||
*/
|
||||
class Doc36ZiHuaMobileApi extends Backend
|
||||
{
|
||||
private const DOC_RELATIVE = 'docs' . DIRECTORY_SEPARATOR . '36字花-移动端接口设计草案.md';
|
||||
private const DOC_ZH = [
|
||||
'relative' => 'docs' . DIRECTORY_SEPARATOR . '36字花-移动端接口设计草案.md',
|
||||
'filename' => '36字花-移动端接口设计草案.md',
|
||||
'ascii_fallback' => '36zihua-mobile-api-design-draft.md',
|
||||
];
|
||||
|
||||
private const DOC_EN = [
|
||||
'relative' => 'docs' . DIRECTORY_SEPARATOR . 'en' . DIRECTORY_SEPARATOR . '36zihua-mobile-api-design-draft.md',
|
||||
'filename' => '36zihua-mobile-api-design-draft.md',
|
||||
'ascii_fallback' => '36zihua-mobile-api-design-draft.md',
|
||||
];
|
||||
|
||||
public function content(Request $request): Response
|
||||
{
|
||||
@@ -22,19 +33,20 @@ class Doc36ZiHuaMobileApi extends Backend
|
||||
return $response;
|
||||
}
|
||||
|
||||
$path = $this->docAbsolutePath();
|
||||
if (!is_file($path)) {
|
||||
$doc = MarkdownDocReader::resolve($request, self::DOC_ZH, self::DOC_EN);
|
||||
if (!is_file($doc['path'])) {
|
||||
return $this->error(__('Document file not found'));
|
||||
}
|
||||
|
||||
$raw = file_get_contents($path);
|
||||
$raw = file_get_contents($doc['path']);
|
||||
if ($raw === false) {
|
||||
return $this->error(__('Failed to read document'));
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'markdown' => $raw,
|
||||
'filename' => '36字花-移动端接口设计草案.md',
|
||||
'filename' => $doc['filename'],
|
||||
'lang' => $doc['lang'],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -45,22 +57,20 @@ class Doc36ZiHuaMobileApi extends Backend
|
||||
return $response;
|
||||
}
|
||||
|
||||
$path = $this->docAbsolutePath();
|
||||
if (!is_file($path)) {
|
||||
$doc = MarkdownDocReader::resolve($request, self::DOC_ZH, self::DOC_EN);
|
||||
if (!is_file($doc['path'])) {
|
||||
return $this->error(__('Document file not found'));
|
||||
}
|
||||
|
||||
$body = file_get_contents($path);
|
||||
$body = file_get_contents($doc['path']);
|
||||
if ($body === false) {
|
||||
return $this->error(__('Failed to read document'));
|
||||
}
|
||||
|
||||
$utf8Name = '36字花-移动端接口设计草案.md';
|
||||
$asciiFallback = '36zihua-mobile-api-design-draft.md';
|
||||
$disposition = sprintf(
|
||||
'attachment; filename="%s"; filename*=UTF-8\'\'%s',
|
||||
$asciiFallback,
|
||||
rawurlencode($utf8Name)
|
||||
$doc['ascii_fallback'],
|
||||
rawurlencode($doc['filename'])
|
||||
);
|
||||
|
||||
return new Response(200, [
|
||||
@@ -69,9 +79,4 @@ class Doc36ZiHuaMobileApi extends Backend
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
], $body);
|
||||
}
|
||||
|
||||
private function docAbsolutePath(): string
|
||||
{
|
||||
return rtrim(base_path(), DIRECTORY_SEPARATOR . '/') . DIRECTORY_SEPARATOR . self::DOC_RELATIVE;
|
||||
}
|
||||
}
|
||||
|
||||
82
app/admin/controller/docs/DocCommissionShare.php
Normal file
82
app/admin/controller/docs/DocCommissionShare.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\docs;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\docs\MarkdownDocReader;
|
||||
use support\Response;
|
||||
use Webman\Http\Request;
|
||||
|
||||
/**
|
||||
* 后台只读展示《分红说明文档》Markdown,并提供下载。
|
||||
*/
|
||||
class DocCommissionShare extends Backend
|
||||
{
|
||||
private const DOC_ZH = [
|
||||
'relative' => 'docs' . DIRECTORY_SEPARATOR . '分红说明文档.md',
|
||||
'filename' => '分红说明文档.md',
|
||||
'ascii_fallback' => 'commission-share-guide.md',
|
||||
];
|
||||
|
||||
private const DOC_EN = [
|
||||
'relative' => 'docs' . DIRECTORY_SEPARATOR . 'en' . DIRECTORY_SEPARATOR . 'commission-share-guide.md',
|
||||
'filename' => 'commission-share-guide.md',
|
||||
'ascii_fallback' => 'commission-share-guide.md',
|
||||
];
|
||||
|
||||
public function content(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$doc = MarkdownDocReader::resolve($request, self::DOC_ZH, self::DOC_EN);
|
||||
if (!is_file($doc['path'])) {
|
||||
return $this->error(__('Document file not found'));
|
||||
}
|
||||
|
||||
$raw = file_get_contents($doc['path']);
|
||||
if ($raw === false) {
|
||||
return $this->error(__('Failed to read document'));
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'markdown' => $raw,
|
||||
'filename' => $doc['filename'],
|
||||
'lang' => $doc['lang'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function download(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$doc = MarkdownDocReader::resolve($request, self::DOC_ZH, self::DOC_EN);
|
||||
if (!is_file($doc['path'])) {
|
||||
return $this->error(__('Document file not found'));
|
||||
}
|
||||
|
||||
$body = file_get_contents($doc['path']);
|
||||
if ($body === false) {
|
||||
return $this->error(__('Failed to read document'));
|
||||
}
|
||||
|
||||
$disposition = sprintf(
|
||||
'attachment; filename="%s"; filename*=UTF-8\'\'%s',
|
||||
$doc['ascii_fallback'],
|
||||
rawurlencode($doc['filename'])
|
||||
);
|
||||
|
||||
return new Response(200, [
|
||||
'Content-Type' => 'text/markdown; charset=UTF-8',
|
||||
'Content-Disposition' => $disposition,
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
], $body);
|
||||
}
|
||||
}
|
||||
@@ -132,4 +132,7 @@ return [
|
||||
'36字花移动端接口文档' => '36 Zihua mobile API design draft',
|
||||
'拉取正文' => 'Load document body',
|
||||
'下载 Markdown' => 'Download Markdown',
|
||||
|
||||
// 文档:分红说明
|
||||
'分红说明文档' => 'Commission share guide',
|
||||
];
|
||||
|
||||
@@ -54,5 +54,9 @@ return [
|
||||
'manualSettle' => '手动结算',
|
||||
'batchSettlePending' => '批量结算待结算渠道',
|
||||
'walletAdjust' => '钱包加减点',
|
||||
'Markdown文档' => 'Markdown文档',
|
||||
'分红说明文档' => '分红说明文档',
|
||||
'拉取正文' => '拉取正文',
|
||||
'下载 Markdown' => '下载 Markdown',
|
||||
];
|
||||
|
||||
|
||||
82
app/common/library/docs/MarkdownDocReader.php
Normal file
82
app/common/library/docs/MarkdownDocReader.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\docs;
|
||||
|
||||
use Webman\Http\Request;
|
||||
|
||||
/**
|
||||
* 按请求语言读取 docs 目录下的 Markdown 文件(英文缺失时回退中文)。
|
||||
*/
|
||||
final class MarkdownDocReader
|
||||
{
|
||||
/**
|
||||
* @param array{relative:string,filename:string,ascii_fallback?:string} $zh
|
||||
* @param array{relative:string,filename:string,ascii_fallback?:string} $en
|
||||
* @return array{path:string,filename:string,ascii_fallback:string,lang:string}
|
||||
*/
|
||||
public static function resolve(Request $request, array $zh, array $en): array
|
||||
{
|
||||
$lang = self::resolveLang($request);
|
||||
$useEn = $lang === 'en';
|
||||
$spec = $useEn ? $en : $zh;
|
||||
$base = rtrim(base_path(), DIRECTORY_SEPARATOR . '/');
|
||||
$path = $base . DIRECTORY_SEPARATOR . str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $spec['relative']);
|
||||
if ($useEn && !is_file($path)) {
|
||||
$spec = $zh;
|
||||
$path = $base . DIRECTORY_SEPARATOR . str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $zh['relative']);
|
||||
$lang = 'zh-cn';
|
||||
}
|
||||
$filename = strval($spec['filename'] ?? 'document.md');
|
||||
$asciiFallback = strval($spec['ascii_fallback'] ?? $filename);
|
||||
|
||||
return [
|
||||
'path' => $path,
|
||||
'filename' => $filename,
|
||||
'ascii_fallback' => $asciiFallback,
|
||||
'lang' => $lang,
|
||||
];
|
||||
}
|
||||
|
||||
public static function resolveLang(Request $request): string
|
||||
{
|
||||
$thinkRaw = $request->header('think-lang', '');
|
||||
$thinkLang = is_string($thinkRaw) ? trim($thinkRaw) : '';
|
||||
if ($thinkLang === '' && is_array($thinkRaw) && isset($thinkRaw[0]) && is_string($thinkRaw[0])) {
|
||||
$thinkLang = trim($thinkRaw[0]);
|
||||
}
|
||||
|
||||
$queryRaw = $request->get('lang');
|
||||
if ($queryRaw === null || $queryRaw === '') {
|
||||
$queryRaw = $request->post('lang');
|
||||
}
|
||||
$queryLang = is_string($queryRaw) ? trim($queryRaw) : (is_scalar($queryRaw) ? trim(strval($queryRaw)) : '');
|
||||
|
||||
$headerRaw = $request->header('lang', '');
|
||||
$headerLang = is_string($headerRaw) ? trim($headerRaw) : '';
|
||||
|
||||
$normalize = static function (string $raw): string {
|
||||
$s = str_replace('_', '-', strtolower(trim($raw)));
|
||||
if ($s === 'zh') {
|
||||
return 'zh-cn';
|
||||
}
|
||||
return $s;
|
||||
};
|
||||
|
||||
foreach ([$thinkLang, $headerLang, $queryLang] as $candidate) {
|
||||
if ($candidate === '') {
|
||||
continue;
|
||||
}
|
||||
$normalized = $normalize($candidate);
|
||||
if ($normalized === 'en') {
|
||||
return 'en';
|
||||
}
|
||||
if ($normalized === 'zh-cn') {
|
||||
return 'zh-cn';
|
||||
}
|
||||
}
|
||||
|
||||
return 'zh-cn';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user