feat: 移动增加扫码登录并支持换号短信登录
This commit is contained in:
@@ -134,6 +134,8 @@ func main() {
|
||||
mux.HandleFunc("GET /api/captcha", app.captcha)
|
||||
mux.HandleFunc("POST /api/sms", app.sms)
|
||||
mux.HandleFunc("POST /api/login", app.login)
|
||||
mux.HandleFunc("GET /api/qr", app.qrCode)
|
||||
mux.HandleFunc("GET /api/qr/poll", app.qrPoll)
|
||||
mux.HandleFunc("POST /api/logout", app.logout)
|
||||
mux.HandleFunc("POST /api/resume", app.resume)
|
||||
mux.HandleFunc("POST /api/interval", app.setInterval)
|
||||
@@ -366,6 +368,40 @@ func (a *App) login(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
a.acceptLogin(result)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "message": "登录成功"})
|
||||
}
|
||||
|
||||
func (a *App) qrCode(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := a.client.QRCode(r.Context())
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadGateway, map[string]any{"ok": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
|
||||
func (a *App) qrPoll(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := a.client.PollQR(r.Context())
|
||||
if err != nil {
|
||||
writeErr(w, err.Error())
|
||||
return
|
||||
}
|
||||
if status.Expired {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": false, "expired": true, "message": "二维码已失效,请刷新"})
|
||||
return
|
||||
}
|
||||
if status.Waiting || status.Result.Phone == "" {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": false, "waiting": true})
|
||||
return
|
||||
}
|
||||
a.acceptLogin(status.Result)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "message": "登录成功"})
|
||||
}
|
||||
|
||||
func (a *App) acceptLogin(result cmnet.LoginResult) {
|
||||
a.mu.Lock()
|
||||
a.loggedIn = true
|
||||
a.sessionOK = true
|
||||
@@ -379,7 +415,6 @@ func (a *App) login(w http.ResponseWriter, r *http.Request) {
|
||||
a.save()
|
||||
a.refresh()
|
||||
log.Printf("登录成功 %s", cmnet.MaskPhone(result.Phone))
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "message": "登录成功"})
|
||||
}
|
||||
|
||||
func (a *App) logout(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user