Skip to content

Commit a8505de

Browse files
refactor(auto-exchange): 优化抽卡资源自动兑换的时间记录逻辑
- 移除重复的时间记录代码,将逻辑集中到兑换成功后执行 - 调整代码结构,先获取当前时间再进行兑换操作 - 简化最后一次执行时间的查找逻辑 - 删除无用的注释代码 - 统一时间戳更新流程,确保数据一致性 fix(auto-exchange): 解决UID识别验证问题 - 添加UID类型检查确保为正整数 - 验证OCR识别结果的有效性 - 在UID识别失败时抛出具体错误信息 - 防止无效UID导致后续兑换流程异常 ``` refactor(exchange): 优化每月兑换抽卡资源任务逻辑 - 将代码块包裹在 try-finally 结构中确保调试日志始终输出 - 移除多余的注释代码以提高代码整洁性 - 保持原有的业务逻辑不变,仅调整代码结构 - 确保 contentList 调试信息在所有情况下都能正确记录 ``` fix(auto-exchange): 修复抽卡资源自动兑换的时间处理逻辑 - 修正了查找用户最后兑换时间的变量名错误 - 更新了时间数据的获取方式,从字符串改为数字格式 - 修复了时间对象创建的逻辑以正确处理时间戳 - 确保当前时间戳能正确更新到用户记录中
1 parent 6c7004d commit a8505de

1 file changed

Lines changed: 48 additions & 42 deletions

File tree

  • repo/js/每月自动兑换抽卡资源

repo/js/每月自动兑换抽卡资源/main.js

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ocrUid} from "./utils/uid.js";
2-
import {toMainUi,throwError} from "./utils/tool.js";
2+
import {toMainUi, throwError} from "./utils/tool.js";
33

44
const config = {
55
tryRe: {
@@ -9,7 +9,7 @@ const config = {
99
user: {
1010
uid: undefined
1111
},
12-
send_notification:false
12+
send_notification: false
1313
}
1414

1515
/**
@@ -43,33 +43,27 @@ async function isTaskRefreshed(filePath, options = {}) {
4343
throw new Error("已重试" + (try_count - 1) + "次数,超出最大重试" + try_count_max + "次数");
4444
}
4545
if (!config.user.uid) {
46-
config.user.uid = await ocrUid();
46+
const resolvedUid = await ocrUid();
47+
if (!Number.isInteger(resolvedUid) || resolvedUid <= 0) {
48+
throw new Error(`UID 识别失败: ${resolvedUid}`);
49+
}
50+
config.user.uid = resolvedUid;
4751
}
4852
const uid = config.user.uid;
4953
const current = {uid: uid, time: undefined}
5054
// 读取文件内容
5155
let contentList = [];
5256
try {
53-
contentList = JSON.parse(file.readTextSync(filePath))
54-
}catch (e) {
55-
log.debug("warn:"+e.message)
57+
contentList = JSON.parse(file.readTextSync(filePath))
58+
} catch (e) {
59+
log.debug("warn:" + e.message)
5660
}
57-
61+
const last = contentList.find(item => item.uid === uid)
62+
const lastTimeNumber = last?.time
63+
const lastTime = lastTimeNumber ? new Date(lastTimeNumber) : new Date(0);
5864
try {
59-
const lastTimeStr = contentList.find(item => item.uid === current.uid)
60-
//todo:uid取lastTime
61-
const lastTime = lastTimeStr ? new Date(lastTimeStr) : new Date(0);
6265
const nowTime = new Date();
63-
current.time=nowTime.getTime();
64-
if (contentList.some(item => item.uid === current.uid)) {
65-
contentList.forEach(item => {
66-
if (item.uid === current.uid) {
67-
item.time = current.time;
68-
}
69-
})
70-
}else {
71-
contentList.push(current);
72-
}
66+
current.time = nowTime.getTime();
7367

7468
let shouldRefresh = false;
7569

@@ -146,29 +140,41 @@ async function isTaskRefreshed(filePath, options = {}) {
146140
// // await file.writeText(filePath, JSON.stringify(contentList));
147141
// shouldRefresh = true;
148142
// }
143+
try {
144+
if (shouldRefresh) {
145+
const message = `任务已刷新,执行每月兑换抽卡资源`;
146+
log.info(message)
147+
if (config.send_notification) {
148+
notification.send(message);
149+
}
150+
await exchangeGoods();
149151

150-
if (shouldRefresh) {
151-
const message = `任务已刷新,执行每月兑换抽卡资源`;
152-
log.info( message)
153-
if (config.send_notification){
154-
notification.send(message);
155-
}
156-
await exchangeGoods();
157-
// 更新最后完成时间
158-
await file.writeText(filePath, JSON.stringify(contentList));
159-
return true;
160-
} else {
161-
const message = `任务未刷新,跳过每月兑换抽卡资源`;
162-
log.info( message)
163-
if (config.send_notification){
164-
notification.send(message);
152+
if (contentList.some(item => item.uid === current.uid)) {
153+
contentList.forEach(item => {
154+
if (item.uid === current.uid) {
155+
item.time = current.time;
156+
}
157+
})
158+
} else {
159+
contentList.push(current);
160+
}
161+
162+
// 更新最后完成时间
163+
await file.writeText(filePath, JSON.stringify(contentList));
164+
return true;
165+
} else {
166+
const message = `任务未刷新,跳过每月兑换抽卡资源`;
167+
log.info(message)
168+
if (config.send_notification) {
169+
notification.send(message);
170+
}
171+
return false;
165172
}
166-
return false;
173+
} finally {
174+
log.debug("contentList:", JSON.stringify(contentList))
167175
}
168-
log.debug("contentList:", JSON.stringify(contentList))
169176
} catch (error) {
170177
log.error(`刷新任务失败: ${error}`);
171-
// 如果文件不存在,创建新文件并返回true(视为需要刷新)
172178
const createResult = await file.writeText(filePath, JSON.stringify(contentList));
173179
if (createResult) {
174180
log.debug("创建新文件成功");
@@ -264,7 +270,7 @@ async function exchangeGoods() {
264270

265271
let validatedMaterialQuantity = positiveIntegerJudgment(materialQuantity);
266272
if (validatedMaterialQuantity < 750) {
267-
throwError(`星尘数量为:${validatedMaterialQuantity},数量不足,无法全部兑换`,config.send_notification)
273+
throwError(`星尘数量为:${validatedMaterialQuantity},数量不足,无法全部兑换`, config.send_notification)
268274
// notification.send(`星尘数量为:${validatedMaterialQuantity},无法全部兑换`);
269275
// throw new Error(`星尘数量为:${validatedMaterialQuantity},不能完全兑换`);
270276
}
@@ -307,15 +313,15 @@ async function exchangeGoods() {
307313
}
308314
const message = `商城抽卡资源兑换完成`;
309315
log.info(message)
310-
if (config.send_notification){
316+
if (config.send_notification) {
311317
notification.send(message);
312318
}
313319
}
314320

315321
async function main() {
316322
try {
317323
config.tryRe.max = parseInt(settings.try_count_max + "") || config.tryRe.max
318-
config.send_notification= settings.send_notification
324+
config.send_notification = settings.send_notification
319325
} catch (e) {
320326
}
321327
try {
@@ -324,7 +330,7 @@ async function main() {
324330
monthlyDay: 1, // 每月第1天(默认值,可省略)
325331
monthlyHour: 4 // 凌晨4点(默认值,可省略)
326332
});
327-
}finally {
333+
} finally {
328334
await toMainUi()
329335
}
330336
}

0 commit comments

Comments
 (0)