Commit 8163427c by yexing

[yx]update

parent c1160bee
import { xlsx2csv } from "./xlsx.js";
import { Plan, createZip, checkResponse } from "./util.js";
import { createZip, checkResponse } from "./util.js";
import { Plan, makeLogEntry } from "./timezone.js";
import { uploadFile } from "./upload.js";
import { TABLE } from "./conf.js";
const checkOrigin = (u1, u2) => {
const checkURL = (u1, u2) => {
if (!u1 || !u2) return false;
return u1.startsWith(new URL(u2).origin);
}
return u1.startsWith(new URL(u2).origin) && !u1.includes("signin");
};
const sendTabMsg = (msg, callback) => {
const uri = msg.data.uri;
chrome.tabs.query({}, (tabs) => {
const tab = tabs.filter(x => checkOrigin(x.url, uri))[0];
const tab = tabs.filter(x => checkURL(x.url, uri))[0];
if (!tab) {
const errorMsg = `tab not found, msg: ${JSON.stringify(msg)}`
callback ? callback(errorMsg) : console.error(errorMsg);
chrome.notifications.create({
type: "basic",
title: "插件提醒",
message: "请重新打开页面!",
iconUrl: chrome.runtime.getURL('icon.png'),
requireInteraction: true,
});
return;
}
console.log(`send ${JSON.stringify(msg)} to ${tab.url}`);
......@@ -106,10 +114,7 @@ function startTask() {
const ackLen = logs.filter(x => x.message.includes("完成")).length;
chrome.runtime.sendMessage({ type: 'progress', logs });
if (ackLen === taskNum) {
logs.push({
time: new Date().toLocaleTimeString(),
message: "已全部完成"
});
logs.push(makeLogEntry("已全部完成"));
chrome.runtime.sendMessage({ type: 'progress', logs });
// copy logs
chrome.storage.local.set({ isRunning: false, logs });
......@@ -124,8 +129,8 @@ function startTask() {
const initTimedTask = () => {
for (const [key, { uri, params }] of Object.entries(TABLE)) {
const plan = new Plan(null, params);
const period = plan.get()?.period;
const when = plan.next().dt.getTime();
const period = plan.get();
const when = plan.next().dt.valueOf();
chrome.alarms.create(key, { when });
console.log(`[timedTask] uri: ${uri} period: ${period} when: ${when}`);
}
......@@ -135,9 +140,9 @@ chrome.alarms.onAlarm.addListener((alarm) => {
if (!Object.keys(TABLE).includes(key)) return;
const { uri, params } = TABLE[key];
const plan = new Plan(null, params);
const period = !fromDate || !toDate ? plan.get()?.period : [fromDate, toDate];
const period = !fromDate || !toDate ? plan.get() : [fromDate, toDate];
if (!when) {
when = plan.next().dt.getTime();
when = plan.next().dt.valueOf();
chrome.alarms.create(alarm.name, { when });
console.log(`[timedTask] uri: ${uri} period: ${period} when: ${when}`);
}
......
......@@ -3,13 +3,13 @@ const p1 = [
[`11 ${hour}:`, ["01", "10"]],
[`21 ${hour}:`, ['11', '20']],
[`01 ${hour}:`, ['21', 0]],
[`03 00:`, ['01', 0]]
];
const p2 = [
[`11 ${hour}:`, ["01", "10"]],
[`21 ${hour}:`, ['11', '20']],
[`03 00:`, ['01', 0]]
[`05 00:`, ['01', 0]]
];
// const p2 = [
// [`11 ${hour}:`, ["01", "10"]],
// [`21 ${hour}:`, ['11', '20']],
// [`05 00:`, ['01', 0]]
// ];
export const TABLE = {
WFS: {
name: "WFS",
......@@ -19,7 +19,7 @@ export const TABLE = {
PAYMENT: {
name: "放款表",
uri: "https://seller.walmart.com/aurora/v1/",
params: p2
params: p1
},
GQL: {
name: "营销表",
......
import { sleep, xpath, createZip, fmt0, addLog } from "./util.js";
import { sleep, xpath, createZip, fmt0 } from "./util.js";
import { addLog } from "./timezone.js";
import { createTasks } from "./base.js";
import { Task } from "./task.js";
import { uploadFile } from "./upload.js";
......@@ -21,7 +22,9 @@ async function run(options = {}) {
const [key, { name }] = Object.entries(TABLE).find((item) => item[1].uri === uri);
let tasks = await createTasks(uri, period);
tasks = sn ? [tasks.at(sn)] : tasks;
let moment = 3000, idx = 0, len = tasks.length;
// 在季度周期过滤掉月度任务
// tasks = tasks.filter((x)=>!x.isMonthly);
let moment = 2000, idx = 0, len = tasks.length;
const allData = [], allFn = [], zipFn = `${Task.partnerId} #${key}# ${fromDate}_${toDate}.zip`;
for (const task of tasks) {
const pf = `${++idx}/${len}`;
......
......@@ -10,6 +10,80 @@ dayjs.extend(isSameOrBefore);
const CST = "Asia/Shanghai";
const PT = "America/Los_Angeles";
export class Plan {
constructor(dt, params) {
this.dt = dayjs.tz(dt ?? new Date(), CST);
this.setHandler();
this.table = new Proxy({}, this.handler);
this.params = [];
params?.forEach(param => this._addParam(...param));
}
setHandler() {
this.handler = {
dt: this.dt,
order: [], // 逆序
get(obj, prop) {
if (prop in obj) return obj[prop];
const idx = this.order.find(x => prop - x >= 0);
return obj[idx];
},
set(obj, prop, value) {
const result = Reflect.set(...arguments);
this.order = Object.keys(obj).sort((a, b) => b - a);
let [x1, x2] = value.period, dt = this.dt;
if (this.dt.date() < parseInt(x2) || x2 === 0) {
dt = this.dt.subtract(1, "month");
}
let year = dt.year();
let month = dt.month() + 1;
x2 = x2 === 0 ? dt.daysInMonth() : x2;
month = (month + '').padStart(2, "0");
value.period = [
`${year}-${month}-${x1}`,
`${year}-${month}-${x2}`
];
return result;
}
};
}
get(day = null) {
const it = day ? this.table[day] : this.table[this.dt.date()];
return it?.period;
}
next() {
let dt = this.dt;
const order = this.handler.order;
const idx = (
order.indexOf(this.get().dt.date() + '') - 1 + order.length // 向上
) % order.length;
if (idx == order.length - 1) {
dt = dt.add(1, "month");
}
let year = dt.year();
let month = dt.month();
dt = this.table[order[idx]].dt
.set("year", year)
.set("month", month);
return new Plan(dt, this.params);
}
_addParam(execTime, period) {
// MM-dd HH:mm:ss
const dt = dayjs.tz(new Date(), CST)
.set("date", +execTime.substring(0, 2))
.set("hour", +execTime.substring(3, 5));
this.table[dt.date()] = { period, dt };
this.params.push(arguments);
}
}
export const makeLogEntry = (message) => {
return {
time: dayjs.tz(new Date(), CST).format("HH:mm:ss"),
message
}
};
export const addLog = (message, push) => chrome.runtime.sendMessage({ type: "addLog", data: makeLogEntry(message), push });
const checkTime = (time) => {
if (!time.isValid()) throw new Error("无效时间");
}
......
......@@ -131,70 +131,6 @@ export function downloadFile(fn, blob) {
a.remove();
URL.revokeObjectURL(url);
}
export class Plan {
constructor(dt, params) {
this.dt = dt = dt && new Date(dt) || new Date();
this.handler = {
order: [], // 逆序
get(obj, prop) {
if (prop in obj) return obj[prop];
const idx = this.order.find(x => prop - x >= 0);
return obj[idx];
},
set(obj, prop, value) {
const result = Reflect.set(...arguments);
this.order = Object.keys(obj).sort((a, b) => b - a);
let [x1, x2] = value.period;
let year = dt.getFullYear();
let month = dt.getMonth();
if (dt.getDate() < parseInt(x2) || x2 === 0) {
month = month == 0 ? 11 : month - 1;
year = month == 0 ? year - 1 : year;
}
month++;
const endDate = new Date(year, month, 0).getDate() + '';
x2 = x2 === 0 ? endDate : x2;
month = (month + '').padStart(2, '0');
value.period = [
`${year}-${month}-${x1}`,
`${year}-${month}-${x2}`
];
return result;
}
}
this.table = new Proxy({}, this.handler);
this.params = [];
params?.forEach(args => this._addParam(...args));
}
get(day = null) {
return day ? this.table[day] : this.table[this.dt.getDate()];
}
next() {
let year = this.dt.getFullYear();
let month = this.dt.getMonth() + 1;
const order = this.handler.order;
const idx = (
order.indexOf(this.get().day) - 1 + order.length // 向上
) % order.length;
if (idx == order.length - 1) {
month = month == 12 ? 1 : month + 1;
year = month == 12 ? year + 1 : year;
}
const info = this.table[order[idx]];
return new Plan(new Date(`${year}-${month}-${info.day} ${info.time}`), this.params);
}
_addParam(execTime, period) {
// MM-dd HH:mm:ss
const dt = new Date(`01-${execTime}`);
this.table[dt.getDate()] = {
period,
day: dt.getDate() + '',
time: `${dt.toTimeString().substring(0, 8)}`,
};
this.params.push(arguments);
}
}
export function fmt0(d1, diff = 0) {
const dt1 = new Date(d1);
dt1.setDate(d1.getDate() + diff);
......@@ -224,10 +160,3 @@ export function ffmt1(d1, d2) {
const dt2 = new Date(d2);
return `${dt1.getMonth() + 1}${dt1.getDate()}-${dt2.getDate()}`;
}
export const addLog = (message, push) => {
const logEntry = {
time: new Date().toLocaleTimeString(),
message
};
chrome.runtime.sendMessage({ type: "addLog", data: logEntry, push });
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment