| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import { getLastBuildDate } from '@/utils/localStorage/app';
- import { windowReload } from 'billd-utils';
- import { useTip } from './use-tip';
- export const useCheckUpdate = () => {
- function handleHtmlCheckUpdate(data: {
- htmlUrl: string;
- lastBuildDate: string;
- }) {
- return new Promise<{ shouldTip: boolean }>((resolve) => {
- const xhr = new XMLHttpRequest();
- xhr.open('GET', data.htmlUrl, true);
- xhr.onreadystatechange = function () {
- try {
- if (this.readyState !== 4) return;
- if (this.status !== 200) return; // or whatever error handling you want
- const reg = /\('最后构建日期:', "(.+)"\)/;
- const res = reg.exec(this.responseText);
- if (res?.[1] !== data.lastBuildDate) {
- resolve({ shouldTip: true });
- console.log('提示更新');
- useTip({
- content: '发现新内容可用,是否刷新页面?',
- confirmButtonText: '刷新',
- })
- .then(() => {
- windowReload();
- })
- .catch(() => {});
- } else {
- resolve({ shouldTip: false });
- console.log('不提示更新');
- }
- } catch (error) {
- console.error(error);
- }
- };
- xhr.send();
- });
- }
- function checkUpdate(data: { htmlUrl: string }) {
- setInterval(
- async () => {
- const lastBuildDate = getLastBuildDate();
- if (lastBuildDate) {
- const res = await handleHtmlCheckUpdate({
- htmlUrl: data.htmlUrl,
- lastBuildDate,
- });
- return res;
- }
- },
- 1000 * 60 * 5
- );
- }
- return { checkUpdate };
- };
|