This repository was archived by the owner on Nov 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyc
More file actions
executable file
·2462 lines (2072 loc) · 77.2 KB
/
yc
File metadata and controls
executable file
·2462 lines (2072 loc) · 77.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# YesCode CLI
# 依赖: curl, jq
set -e
# ============================================================
# 颜色定义
# ============================================================
# ANSI 颜色码
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
GRAY='\033[0;90m'
BOLD='\033[1m'
RESET='\033[0m'
# ============================================================
# 配置管理
# ============================================================
prompt_api_key() {
local show_message="${1:-true}" # 默认显示提示信息
local config_dir="$HOME/.yescode"
local config_file="$config_dir/config.json"
if [[ "$show_message" == "true" ]]; then
echo
echo -e "${YELLOW}未配置 API Key${RESET}"
echo
fi
# 创建配置目录
mkdir -p "$config_dir"
# 提示用户输入
read -p "请输入您的 API Key: " api_key
# 验证输入
if [[ -z "$api_key" ]]; then
echo -e "${RED}错误: API Key 不能为空${RESET}"
exit 1
fi
# 保存配置
echo "{\"api_key\": \"$api_key\"}" > "$config_file"
chmod 600 "$config_file"
echo -e "${GREEN}✓ 配置已保存到: $config_file${RESET}"
echo
}
load_config() {
local config_file="$HOME/.yescode/config.json"
# 如果配置文件不存在,提示用户输入
if [[ ! -f "$config_file" ]]; then
prompt_api_key
fi
# 使用 jq 读取 API Key
API_KEY=$(jq -r '.api_key' "$config_file" 2>/dev/null)
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 配置文件格式错误${RESET}"
echo "请确保 $config_file 是有效的 JSON 格式"
exit 1
fi
# 如果 API Key 为空或 null,提示用户输入
if [[ -z "$API_KEY" || "$API_KEY" == "null" ]]; then
prompt_api_key
# 重新读取配置
API_KEY=$(jq -r '.api_key' "$config_file" 2>/dev/null)
fi
# 固定的 API Base URL
API_BASE_URL="https://co.yes.vg"
}
# ============================================================
# Loading 动画函数
# ============================================================
show_loading() {
local pid=$1
local spin='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
local i=0
# debug 模式下不显示动画,直接等待
if [[ "$DEBUG" == "true" ]]; then
wait $pid
return
fi
# 隐藏光标
tput civis
while kill -0 $pid 2>/dev/null; do
i=$(( (i+1) % 10 ))
printf "\r${CYAN}${spin:$i:1}${RESET}"
sleep 0.1
done
# 清除loading行
printf "\r\033[K"
# 恢复光标
tput cnorm
}
# ============================================================
# Debug 模式显示函数
# ============================================================
display_raw_response() {
# 只在 debug 模式下显示
if [[ "$DEBUG" != "true" ]]; then
return
fi
local method="$1"
local url="$2"
local api_key_masked="$3"
local http_code="$4"
local headers="$5"
local body="$6"
local request_body="$7" # 可选的请求体
# 所有输出重定向到 stderr,避免污染 API 函数的返回值
{
echo
echo -e "${BOLD}════════════════════════ 原始 HTTP 请求/响应 ════════════════════════${RESET}"
echo
echo -e "${CYAN}${BOLD}📤 请求信息${RESET}"
echo -e " 方法: ${YELLOW}${method}${RESET}"
echo -e " URL: ${BLUE}${url}${RESET}"
echo -e " 请求头:"
echo -e " ${WHITE}accept: application/json${RESET}"
echo -e " ${WHITE}X-API-Key: ${api_key_masked}${RESET}"
if [[ "$method" == "PUT" || "$method" == "POST" ]]; then
echo -e " ${WHITE}Content-Type: application/json${RESET}"
fi
# 显示请求体(如果有)
if [[ -n "$request_body" ]]; then
echo
echo -e "${CYAN}${BOLD}📄 请求体(JSON):${RESET}"
if command -v jq &> /dev/null; then
echo "$request_body" | jq -C . 2>/dev/null || echo "$request_body"
else
echo "$request_body"
fi
fi
echo
echo -e "${CYAN}${BOLD}📥 响应信息${RESET}"
echo -e " 状态码: ${GREEN}${http_code}${RESET}"
if [[ -n "$headers" && "$headers" != "null" ]]; then
echo -e " 响应头:"
echo "$headers" | while IFS= read -r line; do
if [[ -n "$line" ]]; then
echo -e " ${WHITE}${line}${RESET}"
fi
done
fi
echo
echo -e "${CYAN}${BOLD}📄 响应体(JSON):${RESET}"
# 使用 jq 格式化 JSON(带颜色)
if command -v jq &> /dev/null; then
echo "$body" | jq -C . 2>/dev/null || echo "$body"
else
echo "$body"
fi
echo
echo -e "${BOLD}═══════════════════════════════════════════════════════════════${RESET}"
echo
} >&2
}
# ============================================================
# 颜色工具函数
# ============================================================
get_balance_color() {
local balance=$1
local threshold_low=20
local threshold_medium=50
if (( $(echo "$balance < $threshold_low" | bc -l) )); then
echo "$RED"
elif (( $(echo "$balance < $threshold_medium" | bc -l) )); then
echo "$YELLOW"
else
echo "$GREEN"
fi
}
get_spending_color() {
local spent_percent=$1
if (( $(echo "$spent_percent >= 95" | bc -l) )); then
echo "$RED"
elif (( $(echo "$spent_percent >= 80" | bc -l) )); then
echo "$YELLOW"
else
echo "$GREEN"
fi
}
create_progress_bar() {
local current=$1
local total=$2
local width=30
if (( $(echo "$total == 0" | bc -l) )); then
local percent=0
local filled=0
else
local percent=$(echo "scale=1; ($current / $total) * 100" | bc)
local filled=$(echo "scale=0; ($current / $total) * $width" | bc)
fi
local empty=$((width - filled))
local bar=$(printf '█%.0s' $(seq 1 $filled))$(printf '░%.0s' $(seq 1 $empty))
local color=$(get_spending_color "$percent")
echo -e "[${color}${bar}${RESET}] ${color}${BOLD}${percent}%${RESET}"
}
# ============================================================
# 系统检测与依赖安装
# ============================================================
detect_package_manager() {
# 检测系统的包管理器并返回安装命令
if command -v apt &> /dev/null; then
echo "apt"
elif command -v dnf &> /dev/null; then
echo "dnf"
elif command -v yum &> /dev/null; then
echo "yum"
elif command -v brew &> /dev/null; then
echo "brew"
elif command -v pacman &> /dev/null; then
echo "pacman"
elif command -v zypper &> /dev/null; then
echo "zypper"
else
echo "unknown"
fi
}
auto_install_dependency() {
local dep=$1
local pkg_manager=$(detect_package_manager)
if [[ "$pkg_manager" == "unknown" ]]; then
return 1
fi
read -p "是否自动安装 $dep? (y/n): " -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
return 1
fi
echo "正在安装 $dep..."
local install_cmd=""
case "$pkg_manager" in
apt)
install_cmd="sudo apt install -y $dep"
;;
dnf)
install_cmd="sudo dnf install -y $dep"
;;
yum)
install_cmd="sudo yum install -y $dep"
;;
brew)
install_cmd="brew install $dep"
;;
pacman)
install_cmd="sudo pacman -S --noconfirm $dep"
;;
zypper)
install_cmd="sudo zypper install -y $dep"
;;
esac
if eval "$install_cmd"; then
echo -e "${GREEN}✓ $dep 安装成功${RESET}"
return 0
else
echo -e "${RED}✗ $dep 安装失败${RESET}"
return 1
fi
}
# ============================================================
# API 调用函数
# ============================================================
api_get_balance() {
local url="${API_BASE_URL}/api/v1/user/balance"
local method="GET"
# 根据 debug 模式选择不同的 curl 参数
local response
if [[ "$DEBUG" == "true" ]]; then
# debug 模式:获取响应头
response=$(curl -s -i -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
else
# 普通模式:不获取响应头
response=$(curl -s -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 请求失败${RESET}" >&2
return 1
fi
# 分离响应体和状态码
local http_code=$(echo "$response" | tail -n1)
local full_response=$(echo "$response" | head -n -1)
# 在 debug 模式下分离响应头和响应体
local headers=""
local body=""
if [[ "$DEBUG" == "true" ]]; then
# 删除回车符(HTTP 响应使用 CRLF)
full_response=$(echo "$full_response" | tr -d '\r')
# 查找空行位置(响应头和响应体的分隔)
local empty_line=$(echo "$full_response" | grep -n "^$" | head -1 | cut -d: -f1)
if [[ -n "$empty_line" ]]; then
headers=$(echo "$full_response" | head -n $((empty_line - 1)))
body=$(echo "$full_response" | tail -n +$((empty_line + 1)))
else
# 如果没有找到空行,可能响应格式异常,全部作为 body
body="$full_response"
fi
else
body="$full_response"
fi
# 处理认证错误
if [[ "$http_code" == "401" ]]; then
echo "AUTH_ERROR_401"
return 2
fi
# 处理其他错误
if [[ "$http_code" != "200" ]]; then
echo -e "${RED}获取余额失败: HTTP $http_code${RESET}" >&2
echo "响应内容: $body" >&2
return 1
fi
# 显示原始响应(仅在 debug 模式)
if [[ "$DEBUG" == "true" ]]; then
local api_key_masked="${API_KEY:0:8}********"
display_raw_response "$method" "$url" "$api_key_masked" "$http_code" "$headers" "$body"
fi
echo "$body"
}
api_get_providers() {
local url="${API_BASE_URL}/api/v1/user/available-providers"
local method="GET"
# 根据 debug 模式选择不同的 curl 参数
local response
if [[ "$DEBUG" == "true" ]]; then
response=$(curl -s -i -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 请求失败${RESET}" >&2
return 1
fi
# 分离响应体和状态码
local http_code=$(echo "$response" | tail -n1)
local full_response=$(echo "$response" | head -n -1)
# 在 debug 模式下分离响应头和响应体
local headers=""
local body=""
if [[ "$DEBUG" == "true" ]]; then
full_response=$(echo "$full_response" | tr -d '\r')
local empty_line=$(echo "$full_response" | grep -n "^$" | head -1 | cut -d: -f1)
if [[ -n "$empty_line" ]]; then
headers=$(echo "$full_response" | head -n $((empty_line - 1)))
body=$(echo "$full_response" | tail -n +$((empty_line + 1)))
else
body="$full_response"
fi
else
body="$full_response"
fi
# 处理认证错误
if [[ "$http_code" == "401" ]]; then
echo "AUTH_ERROR_401"
return 2
fi
# 处理其他错误
if [[ "$http_code" != "200" ]]; then
echo -e "${RED}获取分组信息失败: HTTP $http_code${RESET}" >&2
echo "响应内容: $body" >&2
return 1
fi
# 显示原始响应(仅在 debug 模式)
if [[ "$DEBUG" == "true" ]]; then
local api_key_masked="${API_KEY:0:8}********"
display_raw_response "$method" "$url" "$api_key_masked" "$http_code" "$headers" "$body"
fi
echo "$body"
}
api_get_provider_alternatives() {
local provider_id=$1
if [[ -z "$provider_id" ]]; then
echo -e "${RED}错误: provider_id 不能为空${RESET}" >&2
return 1
fi
local url="${API_BASE_URL}/api/v1/user/provider-alternatives/${provider_id}"
local method="GET"
# 根据 debug 模式选择不同的 curl 参数
local response
if [[ "$DEBUG" == "true" ]]; then
response=$(curl -s -i -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 请求失败${RESET}" >&2
return 1
fi
# 分离响应体和状态码
local http_code=$(echo "$response" | tail -n1)
local full_response=$(echo "$response" | head -n -1)
# 在 debug 模式下分离响应头和响应体
local headers=""
local body=""
if [[ "$DEBUG" == "true" ]]; then
full_response=$(echo "$full_response" | tr -d '\r')
local empty_line=$(echo "$full_response" | grep -n "^$" | head -1 | cut -d: -f1)
if [[ -n "$empty_line" ]]; then
headers=$(echo "$full_response" | head -n $((empty_line - 1)))
body=$(echo "$full_response" | tail -n +$((empty_line + 1)))
else
body="$full_response"
fi
else
body="$full_response"
fi
# 处理认证错误
if [[ "$http_code" == "401" ]]; then
echo "AUTH_ERROR_401"
return 2
fi
# 处理其他错误
if [[ "$http_code" != "200" ]]; then
echo -e "${RED}获取分组失败: HTTP $http_code${RESET}" >&2
echo "响应内容: $body" >&2
return 1
fi
# 显示原始响应(仅在 debug 模式)
if [[ "$DEBUG" == "true" ]]; then
local api_key_masked="${API_KEY:0:8}********"
display_raw_response "$method" "$url" "$api_key_masked" "$http_code" "$headers" "$body"
fi
echo "$body"
}
api_get_provider_selection() {
local provider_id=$1
if [[ -z "$provider_id" ]]; then
echo -e "${RED}错误: provider_id 不能为空${RESET}" >&2
return 1
fi
local url="${API_BASE_URL}/api/v1/user/provider-alternatives/${provider_id}/selection"
local method="GET"
# 根据 debug 模式选择不同的 curl 参数
local response
if [[ "$DEBUG" == "true" ]]; then
response=$(curl -s -i -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 请求失败${RESET}" >&2
return 1
fi
# 分离响应体和状态码
local http_code=$(echo "$response" | tail -n1)
local full_response=$(echo "$response" | head -n -1)
# 在 debug 模式下分离响应头和响应体
local headers=""
local body=""
if [[ "$DEBUG" == "true" ]]; then
full_response=$(echo "$full_response" | tr -d '\r')
local empty_line=$(echo "$full_response" | grep -n "^$" | head -1 | cut -d: -f1)
if [[ -n "$empty_line" ]]; then
headers=$(echo "$full_response" | head -n $((empty_line - 1)))
body=$(echo "$full_response" | tail -n +$((empty_line + 1)))
else
body="$full_response"
fi
else
body="$full_response"
fi
# 处理认证错误
if [[ "$http_code" == "401" ]]; then
echo "AUTH_ERROR_401"
return 2
fi
# 处理其他错误
if [[ "$http_code" != "200" ]]; then
echo -e "${RED}获取当前选择失败: HTTP $http_code${RESET}" >&2
echo "响应内容: $body" >&2
return 1
fi
# 显示原始响应(仅在 debug 模式)
if [[ "$DEBUG" == "true" ]]; then
local api_key_masked="${API_KEY:0:8}********"
display_raw_response "$method" "$url" "$api_key_masked" "$http_code" "$headers" "$body"
fi
echo "$body"
}
api_set_provider_selection() {
local provider_id=$1
local selected_alternative_id=$2
if [[ -z "$provider_id" ]]; then
echo -e "${RED}错误: provider_id 不能为空${RESET}" >&2
return 1
fi
if [[ -z "$selected_alternative_id" ]]; then
echo -e "${RED}错误: selected_alternative_id 不能为空${RESET}" >&2
return 1
fi
local url="${API_BASE_URL}/api/v1/user/provider-alternatives/${provider_id}/selection"
local method="PUT"
local json_data="{\"selected_alternative_id\": ${selected_alternative_id}}"
# 根据 debug 模式选择不同的 curl 参数
local response
if [[ "$DEBUG" == "true" ]]; then
response=$(curl -s -i -w "\n%{http_code}" \
-X PUT \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "X-API-Key: ${API_KEY}" \
-d "$json_data" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" \
-X PUT \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "X-API-Key: ${API_KEY}" \
-d "$json_data" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 请求失败${RESET}" >&2
return 1
fi
# 分离响应体和状态码
local http_code=$(echo "$response" | tail -n1)
local full_response=$(echo "$response" | head -n -1)
# 在 debug 模式下分离响应头和响应体
local headers=""
local body=""
if [[ "$DEBUG" == "true" ]]; then
full_response=$(echo "$full_response" | tr -d '\r')
local empty_line=$(echo "$full_response" | grep -n "^$" | head -1 | cut -d: -f1)
if [[ -n "$empty_line" ]]; then
headers=$(echo "$full_response" | head -n $((empty_line - 1)))
body=$(echo "$full_response" | tail -n +$((empty_line + 1)))
else
body="$full_response"
fi
else
body="$full_response"
fi
# 处理认证错误
if [[ "$http_code" == "401" ]]; then
echo "AUTH_ERROR_401"
return 2
fi
# 处理其他错误
if [[ "$http_code" != "200" ]]; then
echo -e "${RED}设置选择失败: HTTP $http_code${RESET}" >&2
echo "响应内容: $body" >&2
return 1
fi
# 显示原始响应(仅在 debug 模式)
if [[ "$DEBUG" == "true" ]]; then
local api_key_masked="${API_KEY:0:8}********"
display_raw_response "$method" "$url" "$api_key_masked" "$http_code" "$headers" "$body" "$json_data"
fi
echo "$body"
}
api_set_balance_preference() {
local balance_preference=$1
if [[ -z "$balance_preference" ]]; then
echo -e "${RED}错误: balance_preference 不能为空${RESET}" >&2
return 1
fi
# 验证参数值
if [[ "$balance_preference" != "subscription_first" && "$balance_preference" != "payg_only" ]]; then
echo -e "${RED}错误: balance_preference 只能是 'subscription_first' 或 'payg_only'${RESET}" >&2
return 1
fi
local url="${API_BASE_URL}/api/v1/user/balance-preference"
local method="PUT"
local json_data="{\"balance_preference\": \"${balance_preference}\"}"
# 根据 debug 模式选择不同的 curl 参数
local response
if [[ "$DEBUG" == "true" ]]; then
response=$(curl -s -i -w "\n%{http_code}" \
-X PUT \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "X-API-Key: ${API_KEY}" \
-d "$json_data" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" \
-X PUT \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "X-API-Key: ${API_KEY}" \
-d "$json_data" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 请求失败${RESET}" >&2
return 1
fi
# 分离响应体和状态码
local http_code=$(echo "$response" | tail -n1)
local full_response=$(echo "$response" | head -n -1)
# 在 debug 模式下分离响应头和响应体
local headers=""
local body=""
if [[ "$DEBUG" == "true" ]]; then
full_response=$(echo "$full_response" | tr -d '\r')
local empty_line=$(echo "$full_response" | grep -n "^$" | head -1 | cut -d: -f1)
if [[ -n "$empty_line" ]]; then
headers=$(echo "$full_response" | head -n $((empty_line - 1)))
body=$(echo "$full_response" | tail -n +$((empty_line + 1)))
else
body="$full_response"
fi
else
body="$full_response"
fi
# 处理认证错误
if [[ "$http_code" == "401" ]]; then
echo "AUTH_ERROR_401"
return 2
fi
# 处理其他错误
if [[ "$http_code" != "200" ]]; then
echo -e "${RED}设置余额使用偏好失败: HTTP $http_code${RESET}" >&2
echo "响应内容: $body" >&2
return 1
fi
# 显示原始响应(仅在 debug 模式)
if [[ "$DEBUG" == "true" ]]; then
local api_key_masked="${API_KEY:0:8}********"
display_raw_response "$method" "$url" "$api_key_masked" "$http_code" "$headers" "$body" "$json_data"
fi
echo "$body"
}
api_get_profile() {
local url="${API_BASE_URL}/api/v1/auth/profile"
local method="GET"
# 根据 debug 模式选择不同的 curl 参数
local response
if [[ "$DEBUG" == "true" ]]; then
response=$(curl -s -i -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" \
-X GET \
-H "accept: application/json" \
-H "X-API-Key: ${API_KEY}" \
--connect-timeout 10 \
--max-time 30 \
"$url" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}错误: 请求失败${RESET}" >&2
return 1
fi
# 分离响应体和状态码
local http_code=$(echo "$response" | tail -n1)
local full_response=$(echo "$response" | head -n -1)
# 在 debug 模式下分离响应头和响应体
local headers=""
local body=""
if [[ "$DEBUG" == "true" ]]; then
full_response=$(echo "$full_response" | tr -d '\r')
local empty_line=$(echo "$full_response" | grep -n "^$" | head -1 | cut -d: -f1)
if [[ -n "$empty_line" ]]; then
headers=$(echo "$full_response" | head -n $((empty_line - 1)))
body=$(echo "$full_response" | tail -n +$((empty_line + 1)))
else
body="$full_response"
fi
else
body="$full_response"
fi
# 处理认证错误
if [[ "$http_code" == "401" ]]; then
echo "AUTH_ERROR_401"
return 2
fi
# 处理其他错误
if [[ "$http_code" != "200" ]]; then
echo -e "${RED}获取用户资料失败: HTTP $http_code${RESET}" >&2
echo "响应内容: $body" >&2
return 1
fi
# 显示原始响应(仅在 debug 模式)
if [[ "$DEBUG" == "true" ]]; then
local api_key_masked="${API_KEY:0:8}********"
display_raw_response "$method" "$url" "$api_key_masked" "$http_code" "$headers" "$body"
fi
echo "$body"
}
# ============================================================
# 显示余额信息
# ============================================================
display_balance_info() {
local data=$1
# 使用 jq 提取数据
local subscription_balance=$(echo "$data" | jq -r '.subscription_balance // 0')
local payg_balance=$(echo "$data" | jq -r '.pay_as_you_go_balance // 0')
local total_balance=$(echo "$data" | jq -r '.total_balance // 0')
local credit_balance=$(echo "$data" | jq -r '.credit_balance // 0')
local weekly_limit=$(echo "$data" | jq -r '.weekly_limit // 0')
local weekly_spent=$(echo "$data" | jq -r '.weekly_spent_balance // 0')
# 计算剩余额度和消费百分比
local weekly_remaining=$(echo "$weekly_limit - $weekly_spent" | bc)
if (( $(echo "$weekly_limit > 0" | bc -l) )); then
local spent_percent=$(echo "scale=1; ($weekly_spent / $weekly_limit) * 100" | bc)
local remaining_percent=$(echo "scale=1; 100 - $spent_percent" | bc)
else
local spent_percent=0
local remaining_percent=0
fi
# 显示余额信息
echo -e "${CYAN}${BOLD}💰 余额信息${RESET}"
if [[ "$subscription_balance" != "0" ]]; then
local color=$(get_balance_color "$subscription_balance")
echo -e "订阅余额:${color}\$$(printf "%.2f" $subscription_balance)${RESET}"
fi
if [[ "$payg_balance" != "0" ]]; then
local color=$(get_balance_color "$payg_balance")
echo -e "按需付费余额:${color}\$$(printf "%.2f" $payg_balance)${RESET}"
fi
if [[ "$total_balance" != "0" ]]; then
local color=$(get_balance_color "$total_balance")
echo -e "${WHITE}${BOLD}总余额:${RESET}${color}${BOLD}\$$(printf "%.2f" $total_balance)${RESET}"
fi
if (( $(echo "$credit_balance > 0" | bc -l) )); then
echo -e "信用额度:${MAGENTA}\$$(printf "%.2f" $credit_balance)${RESET}"
fi
# 显示消费信息
echo
echo -e "${CYAN}${BOLD}📊 本周消费${RESET}"
if (( $(echo "$weekly_limit > 0" | bc -l) )); then
echo -e "周限额:${CYAN}\$$(printf "%.2f" $weekly_limit)${RESET}"
local color=$(get_spending_color "$spent_percent")
echo -e "已消费:${color}\$$(printf "%.2f" $weekly_spent)${RESET} ${color}(${spent_percent}%)${RESET}"
local remaining_color=$(get_balance_color "$weekly_remaining")
echo -e "剩余额度:${remaining_color}\$$(printf "%.2f" $weekly_remaining)${RESET} ${remaining_color}(${remaining_percent}%)${RESET}"
echo -e "消费进度:$(create_progress_bar "$weekly_spent" "$weekly_limit")"
fi
}
display_providers_info() {
local data=$1
# 提取账户信息
local has_payg=$(echo "$data" | jq -r '.has_payg_balance')
local has_subscription=$(echo "$data" | jq -r '.has_subscription')
# 显示账户状态
echo -e "${CYAN}${BOLD}🔌 可用分组${RESET}"
echo
# 使用 jq 解析 providers 数组
local providers_count=$(echo "$data" | jq '.providers | length')
for ((i=0; i<$providers_count; i++)); do
local provider_data=$(echo "$data" | jq -r ".providers[$i]")
local provider_id=$(echo "$provider_data" | jq -r '.provider.id')
local display_name=$(echo "$provider_data" | jq -r '.provider.display_name')
local type=$(echo "$provider_data" | jq -r '.provider.type')
local rate=$(echo "$provider_data" | jq -r '.rate_multiplier')
local is_default=$(echo "$provider_data" | jq -r '.is_default')
local source=$(echo "$provider_data" | jq -r '.source')
# 转换 source 为中文
local source_text=""
if [[ "$source" == "subscription" ]]; then
source_text="${CYAN}订阅${RESET}"
else
source_text="${YELLOW}按需${RESET}"
fi
# 显示默认标记
local default_mark=""
if [[ "$is_default" == "true" ]]; then
default_mark=" ${GREEN}[默认]${RESET}"
fi
# 显示分组信息
echo -e "${WHITE}${BOLD}${display_name}${RESET}${default_mark}"
echo -e "ID: ${BLUE}${provider_id}${RESET} | 类型: ${type} | 费率: ${MAGENTA}×${rate}${RESET} | 来源: ${source_text}"
echo
done
}
display_provider_alternatives() {
local data=$1
local provider_id=$2
# 检查是否有数据
local data_count=$(echo "$data" | jq '.data | length')
if [[ "$data_count" == "0" || "$data_count" == "null" ]]; then
echo -e "${RED}错误: 分组不存在${RESET}"
echo
echo -e "${YELLOW}提示: 请先运行 ${WHITE}yc providers${YELLOW} 查看可用的分组 ID${RESET}"
return 1
fi
# 显示标题
echo -e "${CYAN}${BOLD}分组 ${provider_id} 可用提供商:${RESET}"
echo
# 遍历所有方案(包括原方案和替代方案)
for ((i=0; i<$data_count; i++)); do
local item=$(echo "$data" | jq -r ".data[$i]")
local is_self=$(echo "$item" | jq -r '.is_self')
local provider_id=$(echo "$item" | jq -r '.alternative.id')
local display_name=$(echo "$item" | jq -r '.alternative.display_name')
local type=$(echo "$item" | jq -r '.alternative.type')
local rate=$(echo "$item" | jq -r '.alternative.rate_multiplier')