Skip to content

Commit

Permalink
fix: 修复账号密码登录的错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhuapiaoyuan committed Jan 21, 2024
1 parent fbd43e1 commit 8c424fd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
13 changes: 12 additions & 1 deletion api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func LoginPost(r *ghttp.Request) {
return
}
officialSession := record["officialSession"].String()

r.Session.Set("offical-session", officialSession)
r.Session.Set("userToken", userToken)
r.Response.RedirectTo("/")
Expand Down Expand Up @@ -149,8 +150,18 @@ func LoginPost(r *ghttp.Request) {
})
}
}

officialSession := record2["officialSession"].String()
if officialSession == "" && record2["mode"].Int() == 0 {
err = ChatgptSessionService.RefreshSession(ctx, record2)
officialSession = record2["officialSession"].String()
if err != nil {
r.Response.WriteTpl("login.html", g.Map{
"username": r.Get("username").String(),
"error": "后台登录失败,请联系管理员检查日志",
})
return
}
}
r.Session.Set("offical-session", officialSession)
r.Session.Set("userToken", user["userToken"].String())
r.Response.RedirectTo("/")
Expand Down
18 changes: 12 additions & 6 deletions api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ func Session(r *ghttp.Request) {

r.Response.WriteJsonExit(sessionJson)
} else {
refreshFlag := false
refreshCookie := ""
sessionJson := gjson.New("{}")
if record["officialSession"].String() == "" {
refreshFlag = true
} else {
sessionJson := gjson.New(record["officialSession"].String())
refreshCookie = sessionJson.Get("refreshCookie").String()
expires := sessionJson.Get("expires").Time()
refreshFlag = expires.Before(time.Now())
}

sessionJson := gjson.New(record["officialSession"].String())
getSessionUrl := config.CHATPROXY(ctx) + "/getsession"
refreshCookie := sessionJson.Get("refreshCookie").String()

expires := sessionJson.Get("expires").Time()

// 判断是否过期
if expires.Before(time.Now()) {
if refreshFlag {
g.Log().Info(ctx, "session 过期,重新获取")
sessionVar := g.Client().SetHeader("authkey", config.AUTHKEY(ctx)).PostVar(ctx, getSessionUrl, g.Map{
"username": record["email"].String(),
Expand Down
32 changes: 32 additions & 0 deletions modules/chatgpt/service/chatgpt_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ func (s *ChatgptSessionService) ModifyAfter(ctx g.Ctx, method string, param map[

}

// 写一个函数 刷新数据
func (s *ChatgptSessionService) RefreshSession(ctx g.Ctx, chatgpt_session gdb.Record) (err error) {
getSessionUrl := config.CHATPROXY(ctx) + "/getsession"
sessionVar := g.Client().SetHeader("authkey", config.AUTHKEY(ctx)).PostVar(ctx, getSessionUrl, g.Map{
"username": chatgpt_session["email"],
"password": chatgpt_session["password"],
"authkey": config.AUTHKEY(ctx),
})
sessionJson := gjson.New(sessionVar)
models := sessionJson.Get("models").Array()
_, err = cool.DBM(s.Model).Where("email=?", chatgpt_session["email"]).Update(g.Map{
"officialSession": sessionJson.String(),
"isPlus": len(models) > 1,
"status": 1,
})
chatgpt_session["officialSession"] = (g.NewVar(sessionJson.String()))
if err == nil {
g.Log().Info(ctx, "GPT账号登录成功", "邮箱:", chatgpt_session["email"])
return
}
g.Log().Error(ctx, err)
err = gerror.New("后台账号登录失败,检查登录日志")
return
}

// GetSessionByUserToken 根据userToken获取session
func (s *ChatgptSessionService) GetSessionByUserToken(ctx g.Ctx, userToken string) (record gdb.Record, expireTime string, err error) {

Expand All @@ -112,6 +137,13 @@ func (s *ChatgptSessionService) GetSessionByUserToken(ctx g.Ctx, userToken strin
return nil, "", gerror.New("没有可用的ChatGpt账号,请联系管理员")
}

officialSession := record["officialSession"].String()
if officialSession == "" && record["mode"].Int() == 0 {
// 没有登录尝试登录
err = s.RefreshSession(ctx, record)

}

record["user_username"] = user["username"]
return
}
Expand Down
12 changes: 5 additions & 7 deletions modules/chatgpt/tasks/refresh_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ func RefreshSession(ctx g.Ctx) {

getSessionUrl := config.CHATPROXY(ctx) + "/getsession"
refreshCookie := gjson.New(v["officialSession"]).Get("refreshCookie").String()
if refreshCookie == "" {
continue
}

sessionVar := g.Client().SetHeader("authkey", config.AUTHKEY(ctx)).PostVar(ctx, getSessionUrl, g.Map{
"username": v["email"],
"password": v["password"],
"authkey": config.AUTHKEY(ctx),
// "refreshCookie": refreshCookie,
"username": v["email"],
"password": v["password"],
"authkey": config.AUTHKEY(ctx),
"refreshCookie": refreshCookie,
})
g.Log().Info(ctx, "账号", v["email"], "sessionVar", sessionVar)
sessionJson := gjson.New(sessionVar)
Expand Down

0 comments on commit 8c424fd

Please sign in to comment.