111 lines
4.2 KiB
TypeScript
111 lines
4.2 KiB
TypeScript
import WuhuBase from "../WuhuBase";
|
|
import WuhuConfig from "../WuhuConfig";
|
|
import Log from "../Log";
|
|
import InfoUtils from "../utils/InfoUtils";
|
|
import Alert from "../utils/Alert";
|
|
import MathUtils from "../utils/MathUtils";
|
|
import NOTIFY_HTML from "../../static/html/buyBeer/notify.html";
|
|
import CommonUtils from "../utils/CommonUtils";
|
|
|
|
export default class BuyBeerHelper extends WuhuBase implements BeerMonitorLoop {
|
|
|
|
private isNotifying = false;
|
|
private loopId: number = null;
|
|
private time: number;
|
|
private readonly notifyHtml: string = NOTIFY_HTML.replace('{{}}', MathUtils.getInstance().getRandomInt(0, 99).toString());
|
|
|
|
public constructor() {
|
|
super();
|
|
this.time = WuhuConfig.get('_15AlarmTime') || 30;
|
|
}
|
|
|
|
public start(): void {
|
|
if (this.loopId) {
|
|
Log.info('啤酒助手已在运行');
|
|
} else {
|
|
this.loopId = window.setInterval(async () => {
|
|
// 海外取消提醒
|
|
let { isTravelling, isAbroad } = await InfoUtils.getInstance().getUserState();
|
|
if (isTravelling || isAbroad) {
|
|
this.stop();
|
|
return;
|
|
}
|
|
let dt = new Date();
|
|
// 已选当天不提醒
|
|
const now = [dt.getUTCFullYear(), dt.getUTCMonth(), dt.getUTCDate()];
|
|
const ignore_date = WuhuConfig.get('_15_alarm_ignore') || '{}';
|
|
if (JSON.stringify(now) === JSON.stringify(ignore_date)) return;
|
|
Log.info('不提醒状态', now, ignore_date);
|
|
// 正常提醒
|
|
let m = 14 - (dt.getMinutes() % 15);
|
|
let s = 60 - dt.getSeconds();
|
|
if (m === 0 && s < this.time) {
|
|
// 如从通知点开,则本次通知跳过
|
|
if (location.href.includes('clickfromnotify')) {
|
|
this.isNotifying = true;
|
|
location.hash = '';
|
|
return;
|
|
}
|
|
// 本次已通知
|
|
if (this.isNotifying) return;
|
|
this.isNotifying = true;
|
|
// 发送通知
|
|
const notify = new Alert(this.notifyHtml, {
|
|
timeout: 30,
|
|
sysNotify: true,
|
|
});
|
|
notify.getElement().querySelector('.wh-notify-msg button').addEventListener('click', () => this.skip_today());
|
|
notify.getElement().addEventListener('click', ev => {
|
|
if ((ev.target as HTMLElement).tagName.toLowerCase() === 'a') {
|
|
notify.close();
|
|
}
|
|
});
|
|
let audioPlay = CommonUtils.getInstance().audioPlay;
|
|
window.setTimeout(audioPlay, 800);
|
|
window.setTimeout(audioPlay, 800 * 2);
|
|
window.setTimeout(audioPlay, 800 * 3);
|
|
} else {
|
|
this.isNotifying = false;
|
|
}
|
|
}, 1000);
|
|
}
|
|
}
|
|
|
|
public stop(): void {
|
|
if (this.loopId) {
|
|
window.clearInterval(this.loopId);
|
|
this.loopId = null;
|
|
}
|
|
}
|
|
|
|
public set_time(t: number): void {
|
|
this.time = t;
|
|
}
|
|
|
|
public status(): '已启动' | '未启动' {
|
|
return this.loopId ? '已启动' : '未启动';
|
|
}
|
|
|
|
public is_running(): boolean {
|
|
return this.loopId !== null;
|
|
}
|
|
|
|
public skip_today(): void {
|
|
const date = new Date();
|
|
WuhuConfig.set('_15_alarm_ignore', [date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()], false);
|
|
// 通知
|
|
const notify = new Alert(`明早8点前将不再提醒 <button id="wh-rd-btn-${ MathUtils.getInstance().getRandomInt(0, 100) }">取消</button>`);
|
|
// 通知中的取消按钮
|
|
notify.getElement().querySelector('.wh-notify-msg button').addEventListener('click', () => WuhuConfig.set('_15_alarm_ignore', undefined, true));
|
|
}
|
|
}
|
|
|
|
export interface BeerMonitorLoop {
|
|
start?: Function;
|
|
stop?: Function;
|
|
set_time?: Function;
|
|
status?: Function;
|
|
is_running?: Function;
|
|
skip_today?: Function;
|
|
}
|