Skip to content

Commit 6312ce9

Browse files
committed
feat: prepare v1.1.0 with activity cards and portable Windows startup
1 parent fc6f95d commit 6312ce9

14 files changed

Lines changed: 464 additions & 29 deletions

File tree

‎.github/workflows/cd.yml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ jobs:
4747
binary=zcode2api.exe
4848
fi
4949
mkdir -p "dist/$package"
50-
go build -trimpath -ldflags="-s -w" -o "dist/$package/$binary" .
51-
cp LICENSE README.md SECURITY.md config.example.json "dist/$package/"
50+
go build -trimpath -ldflags="-s -w -X main.version=${GITHUB_REF_NAME}" -o "dist/$package/$binary" .
51+
cp LICENSE README.md SECURITY.md CHANGELOG.md config.example.json "dist/$package/"
5252
if [ "$GOOS" = windows ]; then
53+
mkdir -p "dist/$package/scripts"
54+
cp scripts/start.ps1 scripts/stop.ps1 "dist/$package/scripts/"
5355
(cd dist && zip -r "$package.zip" "$package")
5456
else
5557
tar -czf "dist/$package.tar.gz" -C dist "$package"
@@ -102,6 +104,7 @@ jobs:
102104
with:
103105
context: .
104106
platforms: linux/amd64,linux/arm64
107+
build-args: VERSION=${{ github.ref_name }}
105108
push: true
106109
tags: ${{ steps.meta.outputs.tags }}
107110
labels: ${{ steps.meta.outputs.labels }}
@@ -137,4 +140,4 @@ jobs:
137140
if [[ "$TAG" == *-* ]]; then
138141
flags+=(--prerelease)
139142
fi
140-
gh release create "$TAG" dist/* --verify-tag --title "$TAG" --generate-notes "${flags[@]}"
143+
gh release create "$TAG" dist/* --verify-tag --title "zcode2api $TAG" --generate-notes "${flags[@]}"

‎.github/workflows/ci.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,13 @@ jobs:
5959
- name: Test frontend
6060
run: node --test test/*.test.mjs
6161

62+
- name: Test Windows startup scripts
63+
if: runner.os == 'Windows'
64+
shell: pwsh
65+
run: |
66+
go build -trimpath -o bin/zcode2api-smoke.exe .
67+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
68+
./test/startup.test.ps1 -BinaryPath ./bin/zcode2api-smoke.exe
69+
6270
- name: Build
6371
run: go build -trimpath ./...

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 更新记录
2+
3+
## v1.1.0 — 2026-10-30
4+
5+
- 修复周末等活动卡未显示、自动领卡遗漏的问题,合并活动投放与旧版额度卡来源,并按 `plan_id` 去重。
6+
- 区分暂无可领、部分同步和查询失败,避免将上游缺少数据的响应当作空清单;单个来源失败不影响已发现的卡片。
7+
- 将可领卡改为紧凑横条,奖励任务和进度条铺满容器,保留窄屏适配。
8+
- Windows Release 包加入启停脚本,解压后无需 Go 即可后台启动;修复从其他目录调用脚本时相对数据目录不一致的问题。
9+
- 增加 `--version`,预编译程序和 Docker 镜像写入对应发布版本。
10+
- 增加领卡回归测试,以及源码、免 Go 发行包、重复启停和自定义数据目录的 Windows 启动测试。

‎Dockerfile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
FROM golang:1.26-bookworm AS build
2+
ARG VERSION=dev
23
WORKDIR /src
34
COPY go.mod go.sum ./
45
RUN go mod download
56
COPY main.go ./
67
COPY internal ./internal
78
COPY src/web ./src/web
8-
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/zcode2api .
9+
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /out/zcode2api .
910

1011
FROM debian:bookworm-slim
1112
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates tzdata && rm -rf /var/lib/apt/lists/*

‎README.md‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,32 @@ zcode2api 是一个使用 Go 编写的 ZCode 多账号额度管理与 HTTP 推
4747
| --- | --- |
4848
| 操作系统 | Windows、Linux 或 macOS |
4949
| 源码构建 | Go **1.26 或更高版本** |
50-
| Windows 启停脚本 | PowerShell,且 `go` 命令可用 |
50+
| Windows 启停脚本 | PowerShell;源码启动还需 `go` 命令,Release 压缩包无需 Go |
5151
| Docker 部署 | Docker Engine / Docker Desktop 与 Docker Compose |
5252
| 账号 | 本人拥有或获得明确授权,且具备相应 ZCode 权益的账号 |
5353

5454
### 安装与启动
5555

56-
下载源码后,进入项目根目录,选择一种方式启动。
56+
可使用预编译 Release 压缩包、源码或 Docker 启动。程序版本可通过 `zcode2api --version` 查看;设置里的 `app_version` 是上游 API 版本,二者独立。
57+
58+
**预编译 Release 压缩包(无需 Go):**
59+
60+
从 [Releases](https://github.com/do-whilefor/zcode2api/releases) 下载对应操作系统和架构的压缩包,完整解压后进入该目录。
61+
62+
Windows 在 PowerShell 中运行 `./scripts/start.ps1`,会直接使用压缩包内的 `zcode2api.exe` 在后台启动;停止时运行 `./scripts/stop.ps1`。也可以运行 `./zcode2api.exe --root .` 在当前终端启动。
63+
64+
Linux / macOS 运行:
65+
66+
```bash
67+
chmod +x ./zcode2api
68+
./zcode2api --root .
69+
```
70+
71+
首次管理密码保存在 `data/admin-password.txt`。Windows 启停脚本中的相对 `ZCODE2API_DATA_DIR` 以项目或解压目录为基准。
72+
73+
**从源码启动:**
74+
75+
下载源码后,进入项目根目录,选择下列方式。
5776

5877
**Windows PowerShell:**
5978

@@ -102,7 +121,7 @@ docker compose up -d --build
102121
4. 进入「领卡」查看可领取卡、待生效额度与领取记录,可手动领取单张卡或全部可领卡。
103122
5. 在「设置 → 自动领卡」中确认开关和检查周期。默认启用,检查周期为 **240 分钟**,服务需保持运行。
104123

105-
自动领卡只领取上游实际提供、账号符合条件的额度卡。需要先完成的奖励任务仍需用户在 ZCode 中完成;领取成功也可能有生效时间,待生效额度不会计入当前可用余额。
124+
自动领卡合并查询活动投放接口和旧版额度卡接口,只领取上游明确提供领取动作或列入可领清单的卡片。同一卡片按 `plan_id` 去重;部分来源异常时保留已查询到的卡片并提示“部分同步”,全部失败时提示查询失败。需要先完成的奖励任务仍需用户在 ZCode 中完成;领取成功也可能有生效时间,待生效额度不会计入当前可用余额。
106125

107126
网页 Cookie 导入仅用于受支持的平台资料等接口,**不能用于推理或余额查询**。推理和领卡需要账号具备有效的 ZCode 凭据。
108127

@@ -128,6 +147,8 @@ Windows 停止服务:
128147

129148
更新源码后,先停止服务,再运行 `start.ps1` 重新构建并启动。Linux / macOS 前台运行时按 `Ctrl+C` 停止,更新源码后重新构建。
130149

150+
使用 Release 压缩包时,先停止旧程序,再用新版本替换程序、启停脚本等发行文件,保留原来的 `data` 目录和自定义 `config.json`,然后重新启动。
151+
131152
Docker 更新源码后,重新构建并启动:
132153

133154
```bash

‎internal/server/claims.go‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,104 @@ import (
44
"context"
55
"fmt"
66
"net/url"
7+
"strings"
78
)
89

910
func (a *App) preview(ctx context.Context, r M) (M, error) {
11+
// New campaigns are delivered through marketing/touch; billing/preview
12+
// remains a separate source for older manually claimable plans.
13+
plans, warnings := []any{}, []string{}
14+
seen := map[string]bool{}
15+
for _, source := range []struct {
16+
name string
17+
load func(context.Context, M) (M, error)
18+
}{{"活动卡", a.marketingPreview}, {"额度卡", a.billingPreview}} {
19+
p, e := source.load(ctx, r)
20+
if e != nil {
21+
warnings = append(warnings, source.name+": "+e.Error())
22+
continue
23+
}
24+
for _, v := range arr(p["plans"]) {
25+
plan := obj(v)
26+
id := strings.TrimSpace(str(plan["plan_id"]))
27+
if id == "" || seen[id] {
28+
continue
29+
}
30+
seen[id] = true
31+
plan["plan_id"] = id
32+
plans = append(plans, plan)
33+
}
34+
}
35+
out := M{"plans": plans}
36+
if len(warnings) == 2 {
37+
return out, fmt.Errorf("%s", strings.Join(warnings, ";"))
38+
}
39+
if len(warnings) > 0 {
40+
out["warning"] = strings.Join(warnings, ";")
41+
}
42+
return out, nil
43+
}
44+
45+
func (a *App) billingPreview(ctx context.Context, r M) (M, error) {
1046
res, e := a.api(ctx, r, "GET", "/api/v1/zcode-plan/billing/preview?app_version="+url.QueryEscape(str(a.config()["app_version"]))+"&platform="+url.QueryEscape(platform()), str(a.tokens(str(r["id"]))["zcode_jwt"]), nil, M{"X-Device-Mid": r["device_mid"]}, a.jar(str(r["id"])))
1147
if e != nil {
1248
return nil, e
1349
}
1450
if !res.OK || !codeIs(res.JSON["code"], 0) {
1551
return nil, apiFailure(res, "preview 失败")
1652
}
17-
return obj(res.JSON["data"]), nil
53+
d, ok := res.JSON["data"].(map[string]any)
54+
if !ok || d == nil {
55+
return nil, fmt.Errorf("preview 响应缺少 data,无法确认可领取清单")
56+
}
57+
if d["plans"] != nil {
58+
if _, ok := d["plans"].([]any); !ok {
59+
return nil, fmt.Errorf("preview 响应的 plans 格式无效")
60+
}
61+
}
62+
return d, nil
63+
}
64+
65+
func (a *App) marketingPreview(ctx context.Context, r M) (M, error) {
66+
res, e := a.api(ctx, r, "GET", "/api/v1/marketing/touch", str(a.tokens(str(r["id"]))["zcode_jwt"]), nil, M{"X-Device-Mid": r["device_mid"]}, a.jar(str(r["id"])))
67+
if e != nil {
68+
return nil, e
69+
}
70+
if !res.OK || !codeIs(res.JSON["code"], 0) {
71+
return nil, apiFailure(res, "活动卡查询失败")
72+
}
73+
deliveries, ok := obj(res.JSON["data"])["deliveries"].([]any)
74+
if !ok {
75+
return nil, fmt.Errorf("活动卡响应缺少 deliveries 清单或格式无效")
76+
}
77+
plans := []any{}
78+
for _, v := range deliveries {
79+
delivery := obj(v)
80+
position := str(delivery["resource_position"])
81+
if position != "banner" && position != "popup" {
82+
continue
83+
}
84+
content := obj(delivery[position])
85+
for _, v := range arr(content["buttons"]) {
86+
action := obj(obj(v)["action"])
87+
id := strings.TrimSpace(str(obj(action["args"])["plan_id"]))
88+
if str(action["type"]) != "claim_zcode_plan" || id == "" {
89+
continue
90+
}
91+
// Only explicit claim actions establish availability. Metadata in
92+
// success popups or referral rewards does not make a plan claimable.
93+
plan := M{"plan_id": id, "name": id, "entitlements": []any{}}
94+
for _, key := range []string{"background", "hero"} {
95+
details := obj(obj(obj(content[key])["args"])["zcode_plan"])
96+
if strings.TrimSpace(str(details["plan_id"])) == id {
97+
plan = merge(details, M{"plan_id": id, "name": fallback(details["name"], id), "entitlements": arr(details["entitlements"])})
98+
break
99+
}
100+
}
101+
plans = append(plans, plan)
102+
}
103+
}
104+
return M{"plans": plans}, nil
18105
}
19106
func (a *App) incentives(ctx context.Context, r M) (M, error) {
20107
res, e := a.api(ctx, r, "GET", "/api/v1/incentive/referral/summary", str(a.tokens(str(r["id"]))["zcode_jwt"]), nil, nil, nil)
@@ -65,6 +152,9 @@ func (a *App) discover(ctx context.Context, id string) M {
65152
m := M{"account_id": r["id"], "label": r["label"], "plans": []any{}}
66153
if p, e := a.preview(ctx, r); e == nil {
67154
m["plans"] = arr(p["plans"])
155+
if p["warning"] != nil {
156+
m["warning"] = p["warning"]
157+
}
68158
} else {
69159
m["error"] = e.Error()
70160
}
@@ -148,6 +238,9 @@ func (a *App) autoClaim(ctx context.Context, dry bool) (M, error) {
148238
results = append(results, M{"account": id, "error": e.Error()})
149239
continue
150240
}
241+
if p["warning"] != nil {
242+
results = append(results, M{"account": id, "warning": p["warning"]})
243+
}
151244
seen := map[string]bool{}
152245
for _, v := range arr(p["plans"]) {
153246
plan := str(obj(v)["plan_id"])

‎internal/server/claims_test.go‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package server
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"strings"
7+
"sync/atomic"
8+
"testing"
9+
)
10+
11+
func marketingClaimDelivery(position, id string) M {
12+
visual := "background"
13+
if position == "popup" {
14+
visual = "hero"
15+
}
16+
return M{"resource_position": position, position: M{
17+
"buttons": []any{M{"action": M{"type": "close"}}, M{"action": M{"type": "claim_zcode_plan", "args": M{"plan_id": id}}}},
18+
visual: M{"args": M{"zcode_plan": M{
19+
"plan_id": id, "name": "Weekend Build", "starts_at": 100, "ends_at": 300,
20+
"entitlements": []any{M{"show_name": "GLM-5.3-Flash", "grant_units": 300000000, "unit_type": "token", "effective_at": 200}},
21+
}}},
22+
"success_popup": M{"buttons": []any{M{"action": M{"type": "claim_zcode_plan", "args": M{"plan_id": "not-available"}}}}},
23+
}}
24+
}
25+
26+
func TestMarketingClaimsDiscoveryAndAutoClaim(t *testing.T) {
27+
a := testApp(t)
28+
_, _, accounts := testPool(t, a, 1)
29+
id := str(accounts[0]["id"])
30+
const planID = "weekend-card"
31+
var claims atomic.Int32
32+
installUpstream(t, a, func(w http.ResponseWriter, r *http.Request) {
33+
if r.Header.Get("Authorization") != "Bearer synthetic-0" || r.Method == "GET" && r.URL.Path == "/api/v1/marketing/touch" && r.Header.Get("X-Device-Mid") != str(accounts[0]["device_mid"]) {
34+
t.Error("missing account authorization or device identity")
35+
}
36+
switch r.URL.Path {
37+
case "/api/v1/marketing/touch":
38+
writeJSON(w, 200, M{"code": 0, "data": M{"deliveries": []any{
39+
marketingClaimDelivery("banner", planID), marketingClaimDelivery("popup", planID),
40+
}}})
41+
case "/api/v1/zcode-plan/billing/preview":
42+
// Reproduce the legacy endpoint's observed success without data.
43+
writeJSON(w, 200, M{"code": 0, "msg": ""})
44+
case "/api/v1/incentive/referral/summary":
45+
writeJSON(w, 200, M{"code": 0, "data": M{"gameplay": M{"status": "online"}, "inviter": M{"rewards": []any{
46+
M{"state": "in_progress", "plan": M{"plan_id": "unearned-referral-card"}},
47+
}}}})
48+
case "/api/v1/zcode-plan/billing/claim":
49+
claims.Add(1)
50+
equal(t, r.Method, "POST")
51+
body, e := readBody(w, r)
52+
must(t, e)
53+
equal(t, body["plan_id"], planID)
54+
writeJSON(w, 200, M{"code": 0, "data": M{"plan": M{"plan_id": planID, "user_plan_id": "claimed"}}})
55+
case "/api/v1/zcode-plan/billing/balance":
56+
writeJSON(w, 200, M{"code": 0, "data": M{"plans": []any{}, "balances": []any{}}})
57+
default:
58+
t.Errorf("unexpected request: %s", r.URL.Path)
59+
w.WriteHeader(404)
60+
}
61+
})
62+
63+
discovery := a.discover(context.Background(), id)
64+
equal(t, len(arr(discovery["previews"])), 1)
65+
preview := obj(arr(discovery["previews"])[0])
66+
equal(t, preview["error"], nil)
67+
if !strings.Contains(str(preview["warning"]), "缺少 data") {
68+
t.Fatalf("missing partial-source warning: %s", js(preview))
69+
}
70+
plans := arr(preview["plans"])
71+
equal(t, len(plans), 1)
72+
plan := obj(plans[0])
73+
equal(t, plan["plan_id"], planID)
74+
equal(t, plan["name"], "Weekend Build")
75+
equal(t, plan["ends_at"], 300)
76+
entitlement := obj(arr(plan["entitlements"])[0])
77+
equal(t, entitlement["grant_units"], 300000000)
78+
equal(t, entitlement["effective_at"], 200)
79+
80+
dry, e := a.autoClaim(context.Background(), true)
81+
must(t, e)
82+
jobs := []any{}
83+
for _, v := range arr(dry["results"]) {
84+
if truth(obj(v)["would_claim"]) {
85+
jobs = append(jobs, v)
86+
}
87+
}
88+
equal(t, jobs, []any{M{"account": id, "plan_id": planID, "would_claim": true}})
89+
equal(t, claims.Load(), 0)
90+
91+
result, e := a.autoClaim(context.Background(), false)
92+
must(t, e)
93+
equal(t, result["claimed"], 1)
94+
equal(t, claims.Load(), 1)
95+
equal(t, a.rows("SELECT plan_id,trigger,ok FROM claim_log"), []any{M{"plan_id": planID, "trigger": "auto", "ok": 1}})
96+
}
97+
98+
func TestMarketingPreviewRequiresExplicitClaimAction(t *testing.T) {
99+
a := testApp(t)
100+
_, _, accounts := testPool(t, a, 1)
101+
delivery := marketingClaimDelivery("popup", "action-plan")
102+
content := obj(delivery["popup"])
103+
obj(obj(obj(content["hero"])["args"])["zcode_plan"])["plan_id"] = "different-metadata-plan"
104+
metadataOnly := marketingClaimDelivery("banner", "metadata-only")
105+
obj(metadataOnly["banner"])["buttons"] = []any{M{"action": M{"type": "open_url", "args": M{"plan_id": "not-a-claim"}}}}
106+
installUpstream(t, a, func(w http.ResponseWriter, r *http.Request) {
107+
writeJSON(w, 200, M{"code": 0, "data": M{"deliveries": []any{metadataOnly, delivery}}})
108+
})
109+
preview, e := a.marketingPreview(context.Background(), accounts[0])
110+
must(t, e)
111+
equal(t, preview["plans"], []any{M{"plan_id": "action-plan", "name": "action-plan", "entitlements": []any{}}})
112+
}
113+
114+
func TestClaimPreviewSourcesAndValidation(t *testing.T) {
115+
emptyBilling := M{"code": 0, "data": M{"plans": []any{}}}
116+
emptyMarketing := M{"code": 0, "data": M{"deliveries": []any{}}}
117+
billingCard := M{"code": 0, "data": M{"plans": []any{M{"plan_id": "legacy-card"}}}}
118+
marketingCard := M{"code": 0, "data": M{"deliveries": []any{marketingClaimDelivery("banner", "legacy-card")}}}
119+
for _, tc := range []struct {
120+
name string
121+
billing, marketing M
122+
count int
123+
warning, failure bool
124+
}{
125+
{"empty lists", emptyBilling, emptyMarketing, 0, false, false},
126+
{"legacy fallback", billingCard, M{"code": 500, "msg": "unavailable"}, 1, true, false},
127+
{"deduplicate sources", billingCard, marketingCard, 1, false, false},
128+
{"missing billing data", M{"code": 0}, emptyMarketing, 0, true, false},
129+
{"invalid billing plans", M{"code": 0, "data": M{"plans": M{}}}, emptyMarketing, 0, true, false},
130+
{"missing deliveries", emptyBilling, M{"code": 0, "data": M{}}, 0, true, false},
131+
{"both unavailable", M{"code": 0}, M{"code": 0}, 0, false, true},
132+
} {
133+
t.Run(tc.name, func(t *testing.T) {
134+
a := testApp(t)
135+
_, _, accounts := testPool(t, a, 1)
136+
installUpstream(t, a, func(w http.ResponseWriter, r *http.Request) {
137+
if r.URL.Path == "/api/v1/marketing/touch" {
138+
writeJSON(w, 200, tc.marketing)
139+
} else {
140+
writeJSON(w, 200, tc.billing)
141+
}
142+
})
143+
preview, e := a.preview(context.Background(), accounts[0])
144+
equal(t, e != nil, tc.failure)
145+
equal(t, truth(preview["warning"]), tc.warning)
146+
equal(t, len(arr(preview["plans"])), tc.count)
147+
})
148+
}
149+
}

0 commit comments

Comments
 (0)