61 lines
2.7 KiB
TypeScript
61 lines
2.7 KiB
TypeScript
import CommonUtils from "../utils/CommonUtils";
|
|
import Log from "../Log";
|
|
import WuhuBase from "../WuhuBase";
|
|
import UserScriptEngine from "../../enum/UserScriptEngine";
|
|
import Popup from "../utils/Popup";
|
|
import STOCK_IMG_HTML from "../../static/html/stock_img.html";
|
|
import * as FILTER from "../../static/json/for_stock_item_filter.json";
|
|
import WindowActiveState from "./WindowActiveState";
|
|
|
|
export default class TravelItem extends WuhuBase {
|
|
className = 'TravelItem';
|
|
private readonly apiUrl: string = 'https://yata.yt/api/v1/travel/export/';
|
|
private foreignStockInfo: any = null;
|
|
|
|
public constructor() {
|
|
super();
|
|
window.setInterval(async () => {
|
|
if (!WindowActiveState.getInstance().get()) return;
|
|
Log.info('fetching ', this.apiUrl);
|
|
this.foreignStockInfo = JSON.parse(await CommonUtils.COFetch(this.apiUrl));
|
|
Log.info({ 'fetch returned': this.foreignStockInfo });
|
|
}, 30 * 1000);
|
|
}
|
|
|
|
// 呈现内容
|
|
public async clickHandler(): Promise<void> {
|
|
if (CommonUtils.getScriptEngine() === UserScriptEngine.RAW) {
|
|
new Popup(STOCK_IMG_HTML.replace('{{}}', performance.now().toString()), '飞花库存');
|
|
} else {
|
|
const popup = new Popup("请稍后 " + CommonUtils.loading_gif_html(), '飞花库存');
|
|
let table = `<table><tr><th colspan="2">目的地 - 更新时间</th><th colspan="3">库存</th></tr>`;
|
|
const dest = FILTER.default;
|
|
const now = new Date();
|
|
const res = await this.get();
|
|
Log.info({ res })
|
|
if (!res || !res.stocks) return;
|
|
dest.forEach(el => {
|
|
const update = (now.getTime() - new Date(res.stocks[el.name]['update'] * 1000).getTime()) / 1000 | 0
|
|
table += `<tr><td>${ el.show }</td><td>${ update / 60 | 0 }分${ update % 60 | 0 }秒前</td>`;
|
|
let count = 0;
|
|
res.stocks[el.name]['stocks'].forEach(stock => {
|
|
if (el.stocks[stock.name]) {
|
|
table += `<td${ stock['quantity'] === 0 ? ' style="background-color:#f44336;color:white;border-color:#000;"' : '' }>${ el.stocks[stock.name] } (${ stock['quantity'] })</td>`;
|
|
count++;
|
|
}
|
|
});
|
|
while (count < 3) {
|
|
count++;
|
|
table += '<td></td>';
|
|
}
|
|
table += '</tr>';
|
|
});
|
|
table += '</table>';
|
|
popup.getElement().innerHTML = table;
|
|
}
|
|
}
|
|
|
|
private async get() {
|
|
return this.foreignStockInfo ||= JSON.parse(await CommonUtils.COFetch(this.apiUrl));
|
|
}
|
|
} |