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"; import Popup from "../utils/Popup"; import ResponseInject from "../../interface/ResponseInject"; export default class BuyBeerHelper extends WuhuBase implements BeerMonitorLoop, ResponseInject { className = 'BuyBeerHelper'; 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; // 正常提醒 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点前将不再提醒 `); // 通知中的取消按钮 notify.getElement().querySelector('.wh-notify-msg button').addEventListener('click', () => WuhuConfig.set('_15_alarm_ignore', undefined, true)); } public setTimeHandler(): void { let popup = new Popup(`

区间为 1 ~ 60,默认 50

`, '啤酒提醒时间设定'); let confirm = document.createElement('button'); confirm.innerHTML = '确定'; confirm.style.float = 'right'; confirm.addEventListener('click', () => { let input: HTMLInputElement = popup.getElement().querySelector('input'); let num = (input.value as any) | 0; if (num === WuhuConfig.get('_15AlarmTime')) return; if (num < 1 || num > 60) num = 50; input.value = num.toString(); WuhuConfig.set('_15AlarmTime', num); this.set_time(num); // 之前的运行状态 if (this.is_running()) this.start(); popup.close(); }); popup.getElement().appendChild(confirm); } public responseHandler(url: string, body: { json: unknown; text: string; isModified: boolean }, opt: { method: "GET" | "POST"; requestBody: string }) { if (url.includes('shops.php') && opt?.method === 'POST') { let req = opt.requestBody; if (req && req.includes('step=buyShopItem') && req.includes('ID=180') && body.json && body.json['success']) { new Alert('检测到已成功购买啤酒'); BuyBeerHelper.getInstance().skip_today(); } } } } export interface BeerMonitorLoop { start?: Function; stop?: Function; set_time?: Function; status?: Function; is_running?: Function; skip_today?: Function; }