|
- package svc
-
- import (
- "applet/app/lib/auth"
- db "code.fnuoos.com/zhimeng/model.git/src"
- "code.fnuoos.com/zhimeng/model.git/src/implement"
- "code.fnuoos.com/zhimeng/model.git/src/model"
- implement2 "code.fnuoos.com/zhimeng/model.git/src/super/implement"
- "errors"
- "github.com/gin-gonic/gin"
- "strings"
- )
-
- func GetUser(c *gin.Context) *model.Admin {
- user, _ := c.Get("admin")
- if user == nil {
- return &model.Admin{
- AdmId: 0,
- Username: "",
- Password: "",
- State: 0,
- IsSuperAdministrator: 0,
- Memo: "",
- CreateAt: "",
- UpdateAt: "",
- }
- }
- return user.(*model.Admin)
- }
-
- func CheckUser(c *gin.Context) (*model.Admin, string, error) {
- token := c.GetHeader("Authorization")
- if token == "" {
- //TODO::兼容open
- appSecret := c.GetHeader("AppSecret")
- if appSecret != "" {
- userListDb := implement2.NewUserListDb(db.Db)
- userList, err := userListDb.GetUserListByOpenAppSecret(appSecret)
- if err != nil {
- return nil, "", err
- }
- if userList != nil {
- // 获取admin
- adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
- admin, err := adminDb.GetAdmin(1001)
- if err != nil {
- return nil, "", err
- }
- c.Set("is_open", true)
- return admin, "", nil
- }
- }
- return nil, "", errors.New("token not exist")
- }
- // 按空格分割
- parts := strings.SplitN(token, " ", 2)
- if !(len(parts) == 2 && parts[0] == "Bearer") {
- return nil, "", errors.New("token format error")
- }
- // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
- mc, newToken, err := auth.ParseToken(parts[1])
- if err != nil {
- return nil, "", err
- }
-
- // 获取admin
- adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
- admin, err := adminDb.GetAdmin(mc.AdmId)
- if err != nil {
- return nil, "", err
- }
- return admin, newToken, nil
- }
|