fix: 退出后不保留登录状态,调试输出改为总开关

This commit is contained in:
Nixevol
2026-10-16 15:39:05 +08:00
parent 543fcce134
commit d5dcd7086d
7 changed files with 253 additions and 51 deletions
+27 -27
View File
@@ -69,6 +69,7 @@ type overview struct {
Error string `json:"error,omitempty"`
Stale bool `json:"stale"`
HasData bool `json:"hasData"`
LoginEpoch int `json:"loginEpoch"`
cmnet.Snapshot
}
@@ -95,6 +96,7 @@ type App struct {
lastError string
stale bool
refreshing bool
loginEpoch int
}
func main() {
@@ -239,7 +241,15 @@ func (a *App) refresh() bool {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
real, plan, err := a.client.Query(ctx, phone, prov)
cmnet.Debugf("刷新 phone=%s prov=%s err=%v", phone, prov, err)
if errors.Is(err, cmnet.ErrNeedLogin) {
if a.client.SessionAlive(ctx) {
a.mu.Lock()
a.stale = true
a.lastError = "账户数据暂时没有刷新"
a.mu.Unlock()
return true
}
a.markLoggedOut("会话已失效,请重新登录")
return true
}
@@ -272,6 +282,7 @@ func (a *App) refresh() bool {
}
func (a *App) markLoggedOut(msg string) {
cmnet.Debugf("清除登录状态 %s", msg)
a.client.Reset()
a.mu.Lock()
a.loggedIn = false
@@ -282,7 +293,9 @@ func (a *App) markLoggedOut(msg string) {
a.refreshedAt = time.Time{}
a.lastError = msg
a.nextAt = time.Time{}
a.loginEpoch++
a.mu.Unlock()
a.save()
}
func (a *App) page(w http.ResponseWriter, r *http.Request) {
@@ -418,10 +431,18 @@ func (a *App) acceptLogin(result cmnet.LoginResult) {
}
func (a *App) logout(w http.ResponseWriter, r *http.Request) {
cmnet.Debugf("用户退出,清除登录状态")
a.client.Reset()
a.mu.Lock()
a.loggedIn = false
a.sessionOK = false
a.snap = emptySnap()
a.realRaw, a.planRaw = nil, nil
a.stale = false
a.refreshedAt = time.Time{}
a.lastError = ""
a.nextAt = time.Time{}
a.loginEpoch++
a.mu.Unlock()
a.wakeLoop()
a.save()
@@ -532,6 +553,7 @@ func (a *App) view() overview {
Error: a.lastError,
Stale: a.loggedIn && a.stale,
HasData: a.loggedIn && hasData(snap),
LoginEpoch: a.loginEpoch,
Snapshot: snap,
}
if a.loggedIn && !a.refreshedAt.IsZero() {
@@ -552,32 +574,18 @@ func emptySnap() cmnet.Snapshot {
}
func (a *App) load() {
a.phone = a.cfgPhone
b, err := os.ReadFile(statePath)
if err != nil {
a.phone = a.cfgPhone
return
}
var st savedState
if json.Unmarshal(b, &st) != nil {
a.phone = a.cfgPhone
return
}
a.phone = strings.TrimSpace(st.Phone)
if a.phone == "" {
a.phone = a.cfgPhone
}
a.provCode = st.ProvCode
if st.DeviceID != "" {
a.deviceID = st.DeviceID
}
a.sessionOK = st.SessionOK && a.phone != "" && len(st.Cookies) > 0
a.loggedIn = st.LoggedIn && a.sessionOK
a.snap = st.Snapshot
a.realRaw = st.RealRaw
a.planRaw = st.PlanRaw
a.refreshedAt = st.RefreshedAt
a.lastError = st.LastError
a.stale = st.Stale
if st.PollMinutes >= 1 && st.PollMinutes <= 1440 {
a.interval = time.Duration(st.PollMinutes) * time.Minute
a.pollText = humanInterval(a.interval)
@@ -585,13 +593,7 @@ func (a *App) load() {
if st.JitterMinutes >= 0 && st.JitterMinutes <= 1440 {
a.jitter = time.Duration(st.JitterMinutes) * time.Minute
}
a.client.ImportCookies(st.Cookies)
if len(a.planRaw) > 0 || len(a.realRaw) > 0 {
a.snap = cmnet.BuildSnapshot(a.phone, a.snap.Region, a.realRaw, a.planRaw)
}
if a.loggedIn {
log.Printf("已恢复会话 %s", cmnet.MaskPhone(a.phone))
}
a.save()
}
func (a *App) save() {
@@ -599,13 +601,11 @@ func (a *App) save() {
defer a.saveMu.Unlock()
a.mu.Lock()
st := savedState{
Phone: a.phone, ProvCode: a.provCode, DeviceID: a.deviceID, LoggedIn: a.loggedIn, SessionOK: a.sessionOK,
Snapshot: a.snap, RealRaw: a.realRaw, PlanRaw: a.planRaw, RefreshedAt: a.refreshedAt,
LastError: a.lastError, Stale: a.stale,
PollMinutes: int(a.interval / time.Minute), JitterMinutes: int(a.jitter / time.Minute),
DeviceID: a.deviceID,
PollMinutes: int(a.interval / time.Minute),
JitterMinutes: int(a.jitter / time.Minute),
}
a.mu.Unlock()
st.Cookies = a.client.ExportCookies()
buf, err := json.MarshalIndent(st, "", " ")
if err != nil {
log.Printf("保存会话失败: %v", err)