context.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package model
  2. import (
  3. "dashoo.cn/micro/app/dao"
  4. "github.com/gogf/gf/frame/g"
  5. "github.com/gogf/gf/net/ghttp"
  6. )
  7. const (
  8. // CtxKey 上下文变量存储键名,前后端系统共享
  9. CtxKey = "GFastContext"
  10. )
  11. // Context 请求上下文结构
  12. type Context struct {
  13. Session *ghttp.Session // 当前Session管理对象
  14. User *CtxUser // 上下文用户信息
  15. Data g.Map // 自定KV变量,业务模块根据需要设置,不固定
  16. }
  17. // CtxUser 请求上下文中的用户信息
  18. type CtxUser struct {
  19. Id uint64 `json:"id"` // 用户id
  20. UserName string `json:"userName"` // 用户名
  21. DeptId uint64 `json:"deptId"` // 部门id
  22. UserNickname string `json:"userNickname"` // 用户昵称
  23. UserStatus uint `json:"userStatus"` // 用户状态;0:禁用,1:正常,2:未验证
  24. IsAdmin int `json:"isAdmin"` // 是否后台管理员 1 是 0 否
  25. Avatar string `json:"avatar"` //头像
  26. }
  27. // GetUserId 获取登录用户id
  28. func (ctxUser *CtxUser) GetUserId() (id uint64) {
  29. return ctxUser.Id
  30. }
  31. // GetDept 获取登录用户所属部门
  32. func (ctxUser *CtxUser) GetDept() (err error, dept *SysDept) {
  33. err = g.DB().Model(dao.SysDept.Table).Fields(dao.SysDept.Columns.Id, dao.SysDept.Columns.DeptName).WherePri(ctxUser.DeptId).Scan(&dept)
  34. if dept == nil {
  35. dept = &SysDept{}
  36. }
  37. return
  38. }