Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Using promise to avoid waiting API's response too long #367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 62 additions & 54 deletions packages/admin/src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function getInitialState() {
}
const initData = await fetchInitData(option);

const { latestVersion, updatedAt, baseUrl, allowDomains, version } = initData;
const { updatedAt, baseUrl, allowDomains, version } = initData;

if (baseUrl && !checkUrl(baseUrl)) {
Modal.warn({
Expand All @@ -72,64 +72,72 @@ export async function getInitialState() {
),
});
}
// 来一个横幅提示
if (version && latestVersion && version != 'dev') {
if (version >= latestVersion) {
} else {
const skipVersion = localStorage.getItem('skipVersion');
if (skipVersion != latestVersion) {
// 老的
notification.info({
duration: 3000,
message: (
<div>
<p style={{ marginBottom: 4 }}>有新版本!</p>
<p style={{ marginBottom: 4 }}>{`当前版本:\t${version}`}</p>
<p style={{ marginBottom: 4 }}>{`最新版本:\t${latestVersion}`}</p>
<p style={{ marginBottom: 4 }}>{`更新时间:\t${moment(updatedAt).format(
'YYYY-MM-DD HH:mm:ss',
)}`}</p>
<p style={{ marginBottom: 4 }}>
{`更新日志:\t`}
<a
target={'_blank'}
href="https://vanblog.mereith.com/ref/changelog.html"
rel="noreferrer"
>
点击查看
</a>
</p>
<p style={{ marginBottom: 4 }}>
{`更新方法:\t`}

const updateAlert=async ()=>{
const initDataUpstream = await fetchAllMetaUpstream(option);
const { latestVersion, updatedAt } = initDataUpstream;

// 来一个横幅提示
if (version && latestVersion && version != 'dev') {
if (version >= latestVersion) {
} else {
const skipVersion = localStorage.getItem('skipVersion');
if (skipVersion != latestVersion) {
// 老的
notification.info({
duration: 3000,
message: (
<div>
<p style={{ marginBottom: 4 }}>有新版本!</p>
<p style={{ marginBottom: 4 }}>{`当前版本:\t${version}`}</p>
<p style={{ marginBottom: 4 }}>{`最新版本:\t${latestVersion}`}</p>
<p style={{ marginBottom: 4 }}>{`更新时间:\t${moment(updatedAt).format(
'YYYY-MM-DD HH:mm:ss',
)}`}</p>
<p style={{ marginBottom: 4 }}>
{`更新日志:\t`}
<a
target={'_blank'}
href="https://vanblog.mereith.com/ref/changelog.html"
rel="noreferrer"
>
点击查看
</a>
</p>
<p style={{ marginBottom: 4 }}>
{`更新方法:\t`}
<a
target={'_blank'}
href="https://vanblog.mereith.com/guide/update.html#%E5%8D%87%E7%BA%A7%E6%96%B9%E6%B3%95"
rel="noreferrer"
>
点击查看
</a>
</p>
<p style={{ marginBottom: 4 }}>
PS: 更新后如后台一直 loading 或出现 Fetch error 请手动清理一下浏览器缓存
</p>
<a
target={'_blank'}
href="https://vanblog.mereith.com/guide/update.html#%E5%8D%87%E7%BA%A7%E6%96%B9%E6%B3%95"
rel="noreferrer"
onClick={() => {
window.localStorage.setItem('skipVersion', latestVersion);
message.success('跳过此版本成功!下次进入后台将不会触发此版本的升级提示');
const el = document.querySelector('.ant-notification-notice-close-x');
if (el) {
el.click();
}
}}
>
点击查看
跳过此版本
</a>
</p>
<p style={{ marginBottom: 4 }}>
PS: 更新后如后台一直 loading 或出现 Fetch error 请手动清理一下浏览器缓存
</p>
<a
onClick={() => {
window.localStorage.setItem('skipVersion', latestVersion);
message.success('跳过此版本成功!下次进入后台将不会触发此版本的升级提示');
const el = document.querySelector('.ant-notification-notice-close-x');
if (el) {
el.click();
}
}}
>
跳过此版本
</a>
</div>
),
});
</div>
),
});
}
}
}
}
};
updateAlert();

// 暗色模式
const theme = getInitTheme();
const sysTheme = mapTheme(theme);
Expand Down
6 changes: 6 additions & 0 deletions packages/admin/src/services/van-blog/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export async function fetchAllMeta(options) {
...(options || {}),
});
}
export async function fetchAllMetaUpstream(options) {
return request('/api/admin/meta_upstream', {
method: 'GET',
...(options || {}),
});
}
export async function activeISR() {
return request('/api/admin/isr', {
method: 'POST',
Expand Down
23 changes: 20 additions & 3 deletions packages/server/src/controller/admin/meta/meta.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ export class MetaController {
@Get()
async getAllMeta(@Req() req: Request) {
const meta = await this.metaProvider.getAll();
const serverData = await getVersionFromServer();
const data = {
version: version,
latestVersion: serverData?.version || version,
updatedAt: serverData?.updatedAt || new Date(),
user: req.user,
baseUrl: meta.siteInfo.baseUrl,
enableComment: meta.siteInfo.enableComment || 'true',
Expand All @@ -32,3 +29,23 @@ export class MetaController {
};
}
}

@ApiTags('meta')
@UseGuards(...AdminGuard)
@ApiToken
@Controller('/api/admin/meta_upstream')
export class MetaController {
constructor(private readonly metaProvider: MetaProvider) {}
@Get()
async getAllMetaUpstream(@Req() req: Request) {
const serverData = await getVersionFromServer();
const data = {
latestVersion: serverData?.version || version,
updatedAt: serverData?.updatedAt || new Date(),
};
return {
statusCode: 200,
data,
};
}
}