auth.go 817B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package controllers
  2. import (
  3. "strings"
  4. "wechat-conf/service/user"
  5. "wechat-conf/utils"
  6. "github.com/astaxie/beego"
  7. )
  8. const (
  9. SESSION_USER = "wechat-conf.user"
  10. )
  11. // auth 鉴权
  12. func (c *BaseController) auth() {
  13. apiPrefix := beego.AppConfig.String("ApiPrefix")
  14. path := strings.TrimPrefix(c.Ctx.Request.URL.Path, apiPrefix)
  15. method := c.Ctx.Request.Method
  16. userID, _ := c.GetSession(SESSION_USER).(string)
  17. serv := user.NewUserServ(c.Context)
  18. code, err := serv.CheckUserRoute(userID, path, method)
  19. if err != nil {
  20. c.ResponseError(err, code)
  21. }
  22. if userID != "" {
  23. userDetail, err := serv.GetUserByID(userID)
  24. if err != nil {
  25. c.ResponseError(err)
  26. }
  27. if userDetail.UserId == "" {
  28. c.ResponseError(utils.LogError("用户不存在, 请重新登录"))
  29. }
  30. c.Context.Set("user", userDetail)
  31. }
  32. }