We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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]}
The text was updated successfully, but these errors were encountered:
No branches or pull requests
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"
)
// 登录前置
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)
}
// 处理退出登录的返回值
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]}
The text was updated successfully, but these errors were encountered: