@@ -247,16 +247,105 @@ impl QuotaSegment {
247247 }
248248 }
249249
250- fn format_reset_info ( & self , reset_times : u32 , auto_reset : bool ) -> String {
251- if reset_times > 0 {
252- if auto_reset {
253- format ! ( "↻ {} AutoReset" , reset_times )
254- } else {
255- format ! ( "↻ {}" , reset_times )
256- }
250+ fn format_reset_info ( & self , reset_times : u32 , auto_reset_when_zero : bool , user_auto_reset_enabled : bool ) -> String {
251+ let status = if user_auto_reset_enabled {
252+ // 用户在TUI中启用了Auto Reset
253+ "[reset on auto]"
254+ } else if auto_reset_when_zero {
255+ // API返回的auto_reset_when_zero为true
256+ "[reset on zero]"
257257 } else {
258- String :: new ( )
258+ "[reset off]"
259+ } ;
260+ format ! ( "↻ {} {}" , reset_times, status)
261+ }
262+
263+ /// 检查当前是否在重置时间窗口内
264+ /// - 18:55-18:59:必须剩余2次或更多重置机会才进行重置
265+ /// - 23:55-23:59:只要有重置次数(>=1)就进行重置
266+ fn is_in_reset_window ( & self , reset_times : u32 ) -> bool {
267+ use chrono:: { Local , Timelike } ;
268+
269+ let now = Local :: now ( ) ;
270+ let hour = now. hour ( ) ;
271+ let minute = now. minute ( ) ;
272+
273+ // 18:55 - 18:59:需要2次或更多重置机会
274+ if hour == 18 && minute >= 55 && minute <= 59 {
275+ return reset_times >= 2 ;
276+ }
277+
278+ // 23:55 - 23:59:只要有重置次数就可以
279+ if hour == 23 && minute >= 55 && minute <= 59 {
280+ return reset_times >= 1 ;
259281 }
282+
283+ false
284+ }
285+
286+ /// 执行重置操作(调用API)
287+ /// 返回重置是否成功
288+ fn perform_reset ( & self , api_key : & str , subscription_id : u32 ) -> bool {
289+ let url = format ! ( "https://www.88code.org/api/reset-credits/{}" , subscription_id) ;
290+ let bearer_token = format ! ( "Bearer {}" , api_key) ;
291+ let debug = env:: var ( "C88_DEBUG" ) . is_ok ( ) ;
292+
293+ if debug {
294+ eprintln ! ( "[DEBUG] Attempting to reset credits for subscription {}" , subscription_id) ;
295+ }
296+
297+ let result = ureq:: post ( & url)
298+ . set ( "accept" , "*/*" )
299+ . set ( "content-type" , "application/json" )
300+ . set ( "Authorization" , & bearer_token)
301+ . timeout ( Duration :: from_secs ( 5 ) )
302+ . call ( ) ;
303+
304+ match result {
305+ Ok ( response) => {
306+ if response. status ( ) == 200 {
307+ if debug {
308+ eprintln ! ( "[DEBUG] Reset successful for subscription {}" , subscription_id) ;
309+ }
310+ true
311+ } else {
312+ if debug {
313+ eprintln ! ( "[DEBUG] Reset failed with status {}" , response. status( ) ) ;
314+ }
315+ false
316+ }
317+ }
318+ Err ( e) => {
319+ if debug {
320+ eprintln ! ( "[DEBUG] Reset error: {}" , e) ;
321+ }
322+ false
323+ }
324+ }
325+ }
326+
327+ /// 检查并执行自动重置(如果需要)
328+ fn check_and_auto_reset (
329+ & self ,
330+ api_key : & str ,
331+ subscription_id : u32 ,
332+ reset_times : u32 ,
333+ auto_reset_enabled : bool ,
334+ ) -> bool {
335+ if !auto_reset_enabled {
336+ return false ;
337+ }
338+
339+ if reset_times == 0 {
340+ return false ;
341+ }
342+
343+ if !self . is_in_reset_window ( reset_times) {
344+ return false ;
345+ }
346+
347+ // 在重置窗口内,执行重置
348+ self . perform_reset ( api_key, subscription_id)
260349 }
261350}
262351
@@ -271,6 +360,19 @@ impl Segment for QuotaSegment {
271360 {
272361 let api_key = self . load_api_key ( ) ?;
273362
363+ // 加载配置获取auto_reset_enabled选项
364+ let auto_reset_enabled = if let Ok ( config) = crate :: config:: Config :: load ( ) {
365+ config
366+ . segments
367+ . iter ( )
368+ . find ( |s| s. id == SegmentId :: Quota )
369+ . and_then ( |sc| sc. options . get ( "auto_reset_enabled" ) )
370+ . and_then ( |v| v. as_bool ( ) )
371+ . unwrap_or ( false )
372+ } else {
373+ false
374+ } ;
375+
274376 // 使用静态方法进行端点检测
275377 if let Some ( ( endpoint_url, response) ) =
276378 SmartEndpointDetector :: detect_endpoint_static ( & api_key)
@@ -282,7 +384,19 @@ impl Segment for QuotaSegment {
282384 // 获取重置次数信息
283385 let reset_info = if let Some ( sub_id) = response. subscription_id {
284386 if let Some ( sub_info) = self . fetch_subscription_info ( & api_key, sub_id) {
285- self . format_reset_info ( sub_info. reset_times , sub_info. auto_reset_when_zero )
387+ // 检查并执行自动重置(内部会进行所有必要的检查)
388+ let _ = self . check_and_auto_reset (
389+ & api_key,
390+ sub_id,
391+ sub_info. reset_times ,
392+ auto_reset_enabled,
393+ ) ;
394+
395+ self . format_reset_info (
396+ sub_info. reset_times ,
397+ sub_info. auto_reset_when_zero ,
398+ auto_reset_enabled
399+ )
286400 } else {
287401 String :: new ( )
288402 }
0 commit comments