|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/lib/auth"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "errors"
- "github.com/gin-gonic/gin"
- "strings"
- "time"
- )
-
- func GetUser(c *gin.Context) *model.Admin {
- admin, _ := c.Get("admin")
- if admin == nil {
- return &model.Admin{
- AdmId: 0,
- Username: "",
- Password: "",
- State: 0,
- IsSuperAdministrator: 0,
- Memo: "",
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- }
- return admin.(*model.Admin)
- }
-
- func CheckUser(c *gin.Context) (*model.Admin, error) {
- token := c.GetHeader("Authorization")
- if token == "" {
- 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")
- }
-
- mc, err := auth.ParseToken(parts[1])
- if err != nil {
- return nil, err
- }
-
-
- adminDb := implement.NewAdminDb(db.Db)
- user, err := adminDb.GetAdmin(mc.AdmId)
- if err != nil {
- return nil, err
- }
- return user, nil
- }
|