Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

respData.Get("data") is empty! #58

Open
windf17 opened this issue Aug 18, 2024 · 0 comments
Open

respData.Get("data") is empty! #58

windf17 opened this issue Aug 18, 2024 · 0 comments

Comments

@windf17
Copy link

windf17 commented Aug 18, 2024

package entrance

import (
"fmt"
"robot-back/internal/consts"
"robot-back/internal/dao"
"robot-back/internal/model"
"robot-back/internal/model/entity"
"robot-back/internal/service"
"robot-back/utility"

"github.com/goflyfox/gtoken/gtoken"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/util/gconv"

)

// 登录前置
func LoginBefore(r *ghttp.Request) (string, interface{}) {
username := r.Get("username").String()
passwd := r.Get("password").String()
password, err := utility.HashPassword(passwd, username)
if err != nil {
r.Response.WriteJsonExit(g.Map{
"state": consts.ERROR,
"message": "密码加密失败!",
})
}
ctx := r.Context()
var employee = new(entity.Employee)
er := dao.Employee.Ctx(ctx).Where(g.Map{
dao.Employee.Columns().LoginName: username,
dao.Employee.Columns().PasswordHash: password,
}).Scan(&employee)
if er != nil {
r.Response.WriteJsonExit(g.Map{
"state": consts.ERROR,
"message": "数据库查询错误",
})
} else if employee == nil {
r.Response.WriteJsonExit(g.Map{
"state": consts.ERROR,
"message": "用户名或密码错误!",
})
}
var accountType = new(entity.AccountType)
er = dao.AccountType.Ctx(ctx).WherePri(employee.TypeId).Scan(&accountType)
if er != nil {
r.Response.WriteJsonExit(g.Map{
"state": consts.ERROR,
"message": "数据库查询错误",
})
} else if accountType == nil {
r.Response.WriteJsonExit(g.Map{
"state": consts.ERROR,
"message": "查询账户类型表数据错误!",
})
}
// res := model.TokenInfo{
// AccountType: *accountType,
// Employee: *employee,
// Ip: r.GetClientIp(),
// }
// fmt.Println("res:", res)
return username, "88888888"
}

// 登录成功后返回token
func LoginAfter(r *ghttp.Request, respData gtoken.Resp) {
token := respData.GetString(gtoken.KeyToken)
if respData.Success() {
userKey := respData.Get("userKey")
uuid := respData.Get("uuid")
fmt.Println("userKey:", userKey)
fmt.Println("uuid:", uuid)
service.GfToken.EncryptToken(r.Context(), userKey.String(), uuid.String())
}
fmt.Println("respData:", respData)
cacheResp := service.GfToken.DecryptToken(r.Context(), token)
fmt.Println("cacheResp:", cacheResp)
var p model.TokenInfo
err := gconv.Struct(respData.Get("data"), &p)
if err != nil {
// 如果类型转换失败,返回错误
r.Response.WriteJsonExit(g.Map{
"state": consts.ERROR,
"message": "解析Token数据失败",
})
} else {
fmt.Println("employee:", p.Employee)
fmt.Println("accountType:", p.AccountType)

	if respData.Success() {
		r.Response.WriteJsonExit(g.Map{
			"state":   consts.SUCCESS,
			"message": "登录成功!",
			"data": g.Map{
				"token":       token,
				"employee":    p.Employee,
				"accountType": p.AccountType,
			},
		})
	} else {
		r.Response.WriteJsonExit(g.Map{
			"state":   consts.ERROR,
			"message": "登录失败!",
		})
	}
}

}

// 处理退出登录的返回值
func LogoutAfter(r *ghttp.Request, respData gtoken.Resp) {
if respData.Success() {
r.Response.WriteJsonExit(g.Map{
"state": consts.SUCCESS,
"message": "退出成功!",
})
} else {
r.Response.WriteJsonExit(g.Map{
"state": consts.ERROR,
"message": "退出失败!",
})
}
}

print:

userKey: admin
uuid: 382f3750b7e53e88f5ba7d15796877a1
respData: {0 success map[token:bd9wIPz9rK3pNpim8SaPP/ZmARZBFuihI8haxREpfIw2Ttwtikd7VfXoiy+LOlwJ userKey:admin uuid:382f3750b7e53e88f5ba7d15796877a1]}
cacheResp: {0 success map[userKey:admin uuid:382f3750b7e53e88f5ba7d15796877a1]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant