11import { ocrUid } from "./utils/uid.js" ;
2- import { toMainUi , throwError } from "./utils/tool.js" ;
2+ import { toMainUi , throwError } from "./utils/tool.js" ;
33
44const 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
315321async 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