chore: 重新整理代码若干;撤销对 login error 的错误的错误处理

This commit is contained in:
Mmx233
2022-10-21 13:30:28 +08:00
parent 09acd88312
commit 8f8cedc0b3
3 changed files with 76 additions and 88 deletions

View File

@@ -9,7 +9,7 @@ import (
// Login 登录逻辑
func Login(localAddr net.Addr) error {
return BitSrun.Login(&BitSrun.LoginConf{
return BitSrun.Login(&BitSrun.Conf{
Https: global.Config.Settings.Basic.Https,
LoginInfo: BitSrun.LoginInfo{
Form: &global.Config.Form,

View File

@@ -2,31 +2,28 @@ package BitSrun
import (
"encoding/json"
"errors"
"github.com/Mmx233/BitSrunLoginGo/util"
log "github.com/sirupsen/logrus"
)
func Login(c *LoginConf) error {
func Login(c *Conf) error {
c.initApi()
G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta)
var ok bool
{
log.Debugln("正在检查登录状态")
res, e := c.api.GetUserInfo()
if e != nil {
return e
}
err := res["error"].(string)
err, ok := res["error"]
if err == "ok" {
log.Debugln("已登录~")
return nil
}
log.Infoln("检测到用户未登录,开始尝试登录...")
{
log.Debugln("正在获取客户端 IP")
var ip interface{}
@@ -39,13 +36,10 @@ func Login(c *LoginConf) error {
}
G.Ip = ip.(string)
log.Debugln("ip: ", G.Ip)
}
}
{
log.Debugln("正在获取 Token")
res, e := c.api.GetChallenge(G.Form.UserName, G.Ip)
res, e = c.api.GetChallenge(G.Form.UserName, G.Ip)
if e != nil {
return e
}
@@ -56,9 +50,7 @@ func Login(c *LoginConf) error {
}
G.Token = token.(string)
log.Debugln("token: ", G.Token)
}
{
log.Debugln("发送登录请求")
info, e := json.Marshal(map[string]string{
@@ -81,7 +73,7 @@ func Login(c *LoginConf) error {
chkstr += G.Token + G.EncryptedInfo
G.EncryptedChkstr = util.Sha1(chkstr)
res, e := c.api.Login(
res, e = c.api.Login(
G.Form.UserName,
G.EncryptedMd5,
G.Meta.Acid,
@@ -102,11 +94,7 @@ func Login(c *LoginConf) error {
G.LoginResult = result.(string)
if G.LoginResult == "ok" {
log.Infoln("已成功登录~")
} else {
log.Errorf("登陆失败: %s\n请开启日志 debug_level 获取更多信息", G.LoginResult)
return nil
}
return errors.New(G.LoginResult)
}
return nil

View File

@@ -25,7 +25,7 @@ type LoginInfo struct {
Meta *LoginMeta
}
type LoginConf struct {
type Conf struct {
//调用 API 时直接访问 https URL
Https bool
//登录参数,不可缺省
@@ -35,6 +35,6 @@ type LoginConf struct {
api srun.Api
}
func (a *LoginConf) initApi() {
func (a *Conf) initApi() {
a.api.Init(a.Https, a.LoginInfo.Form.Domain, a.Client)
}