Skip to content

Commit 92e0cfb

Browse files
committed
feat: 更新 API 调用示例,优化复制链接功能;添加 API 路径说明和样式
1 parent 47eb872 commit 92e0cfb

6 files changed

Lines changed: 60 additions & 16 deletions

File tree

public/assets/home.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ function copyText(text, btn) {
1818
document.addEventListener('click', function(e) {
1919
var btn = e.target.closest('.copy-btn');
2020
if (!btn) return;
21-
copyText(btn.getAttribute('data-copy'), btn);
21+
var text = btn.getAttribute('data-copy');
22+
var path = btn.getAttribute('data-copy-path');
23+
if (!text && path) text = window.location.origin + path;
24+
copyText(text, btn);
2225
});
2326

2427
var modal = document.getElementById('apiModal');

public/assets/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,15 @@ code {
534534
display: grid;
535535
}
536536

537+
.api-card .api-row {
538+
grid-template-columns: 86px minmax(0, 1fr) auto;
539+
}
540+
541+
.api-card .muted {
542+
margin-bottom: 12px;
543+
line-height: 1.6;
544+
}
545+
537546
.admin-page {
538547
background: var(--bg);
539548
}

src/imageStore.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export class ImageStore {
136136
const dimensions = await getImageDimensions(absolutePath);
137137

138138
const image = {
139-
url: `${config.publicBaseUrl}${publicImagePath(gallery, device, file.name)}`,
140139
path: publicImagePath(gallery, device, file.name),
141140
gallery,
142141
device,
@@ -288,10 +287,10 @@ export class ImageStore {
288287
}
289288
}
290289

291-
export function publicImageJson(image, total) {
290+
export function publicImageJson(image, total, baseUrl) {
292291
if (!image) return null;
293292
return {
294-
url: image.url,
293+
url: `${baseUrl || config.publicBaseUrl}${image.path}`,
295294
gallery: image.gallery,
296295
device: image.device,
297296
filename: image.filename,

src/imageStore.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('publicImageJson', () => {
6464

6565
it('should format image correctly', () => {
6666
const image = {
67-
url: 'http://localhost:3000/image/images/anime/pc/001.jpg',
67+
path: '/image/images/anime/pc/001.jpg',
6868
gallery: 'anime',
6969
device: 'pc',
7070
filename: '001.jpg',
@@ -73,9 +73,9 @@ describe('publicImageJson', () => {
7373
height: 1080,
7474
type: 'jpg'
7575
};
76-
const result = publicImageJson(image, 10);
77-
expect(result.url).toBe(image.url);
76+
const result = publicImageJson(image, 10, 'https://img.example.com');
77+
expect(result.url).toBe('https://img.example.com/image/images/anime/pc/001.jpg');
7878
expect(result.gallery).toBe('anime');
7979
expect(result.total).toBe(10);
8080
});
81-
});
81+
});

src/routes/publicRoutes.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ function parseLimit(value) {
1616
return Math.min(parsed, 100);
1717
}
1818

19+
function requestBaseUrl(req) {
20+
const forwardedHost = req.get('x-forwarded-host');
21+
const host = forwardedHost ? forwardedHost.split(',')[0].trim() : req.get('host');
22+
const forwardedProto = req.get('x-forwarded-proto');
23+
const protocol = forwardedProto ? forwardedProto.split(',')[0].trim() : req.protocol;
24+
return host ? `${protocol}://${host}` : config.publicBaseUrl;
25+
}
26+
1927
function galleryRows(galleries) {
2028
if (galleries.length === 0) {
2129
return '<tr><td colspan="5">暂无图库,请在后台创建图库或手动放入图片目录。</td></tr>';
@@ -101,7 +109,7 @@ export function createPublicRouter(store) {
101109
}
102110

103111
if (type === 'json') {
104-
return res.json(publicImageJson(image, total));
112+
return res.json(publicImageJson(image, total, requestBaseUrl(req)));
105113
}
106114
if (type === 'redirect') {
107115
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
@@ -142,7 +150,7 @@ export function createPublicRouter(store) {
142150
res.json({
143151
total: images.length,
144152
limit,
145-
images: images.map((image) => publicImageJson(image, undefined)).map(({ total, ...rest }) => rest)
153+
images: images.map((image) => publicImageJson(image, undefined, requestBaseUrl(req))).map(({ total, ...rest }) => rest)
146154
});
147155
} catch (error) {
148156
next(error);

views/index.html

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,38 @@ <h2>图库统计</h2>
6868

6969
<article class="panel api-card">
7070
<h2>API 调用示例</h2>
71+
<p class="muted">以下为不指定图库的全局随机接口;指定图库的调用方式可在左侧图库统计中点击“查看 API”。</p>
7172
<div class="code-list">
72-
<code>/image/api/random</code>
73-
<code>/image/api/random?gallery=luotianyi</code>
74-
<code>/image/api/random?gallery=luotianyi&amp;device=pc</code>
75-
<code>/image/api/random?gallery=luotianyi&amp;device=mobile</code>
76-
<code>/image/api/random?gallery=luotianyi&amp;device=pc&amp;type=json</code>
77-
<code>/image/api/random?gallery=luotianyi&amp;device=mobile&amp;type=redirect</code>
73+
<div class="api-row">
74+
<span class="api-label">全局随机</span>
75+
<code class="api-url">/image/api/random</code>
76+
<button type="button" class="copy-btn" data-copy-path="/image/api/random">复制</button>
77+
</div>
78+
<div class="api-row">
79+
<span class="api-label">电脑随机</span>
80+
<code class="api-url">/image/api/random?device=pc</code>
81+
<button type="button" class="copy-btn" data-copy-path="/image/api/random?device=pc">复制</button>
82+
</div>
83+
<div class="api-row">
84+
<span class="api-label">手机随机</span>
85+
<code class="api-url">/image/api/random?device=mobile</code>
86+
<button type="button" class="copy-btn" data-copy-path="/image/api/random?device=mobile">复制</button>
87+
</div>
88+
<div class="api-row">
89+
<span class="api-label">JSON 数据</span>
90+
<code class="api-url">/image/api/random?type=json</code>
91+
<button type="button" class="copy-btn" data-copy-path="/image/api/random?type=json">复制</button>
92+
</div>
93+
<div class="api-row">
94+
<span class="api-label">图片本体</span>
95+
<code class="api-url">/image/api/random?type=image</code>
96+
<button type="button" class="copy-btn" data-copy-path="/image/api/random?type=image">复制</button>
97+
</div>
98+
<div class="api-row">
99+
<span class="api-label">显式跳转</span>
100+
<code class="api-url">/image/api/random?type=redirect</code>
101+
<button type="button" class="copy-btn" data-copy-path="/image/api/random?type=redirect">复制</button>
102+
</div>
78103
</div>
79104
</article>
80105
</section>

0 commit comments

Comments
 (0)