互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package controllers
  2. import (
  3. jwt2 "github.com/dgrijalva/jwt-go"
  4. "hudongzhuanjia/libs/jwt"
  5. "hudongzhuanjia/utils/code"
  6. "hudongzhuanjia/utils/define"
  7. )
  8. //执行路由方法前校验登陆态,并且解析page、pageSize
  9. type AuthorCtl struct {
  10. BaseCtl
  11. claims *jwt.Claims
  12. }
  13. func (t *AuthorCtl) Prepare() {
  14. t.BaseCtl.Prepare()
  15. skip, _ := t.GetInt64("skip")
  16. if skip != 0 {
  17. t.claims = &jwt.Claims{
  18. AccountType: "customer",
  19. AccountId: skip,
  20. CustomerId: 1,
  21. CustomerPid: 0,
  22. ActivityId: 1,
  23. AreaId: 1,
  24. StandardClaims: jwt2.StandardClaims{},
  25. }
  26. return
  27. } else {
  28. token := ""
  29. if tokenStr, ok := t.Request.SESSION[define.TOKEN]; ok {
  30. token = tokenStr
  31. } else if tokenStr, ok = t.Request.REQUEST[define.TOKEN]; ok {
  32. token = tokenStr
  33. } else if tokenStr, ok = t.Request.HEADER[define.TOKEN]; ok {
  34. token = tokenStr
  35. } else {
  36. var param = make(map[string]interface{}, 0)
  37. err := t.RequestToStruct(&param)
  38. t.CheckErr(err)
  39. if tokenStr, ok := param[define.TOKEN]; ok {
  40. token = tokenStr.(string)
  41. }
  42. }
  43. claims, err := jwt.ParseAccessToken(token)
  44. if err != nil {
  45. t.ERROR("token 失效", code.MSG_ERR_Authority)
  46. }
  47. t.claims = claims
  48. // 最后多地区:子账号的area_id = area_id, 但是主账号的area_id 需要通过activity_id 进行获取
  49. }
  50. }
  51. func (t *AuthorCtl) MustGetUID() int64 {
  52. return t.claims.AccountId
  53. }
  54. func (t *AuthorCtl) MustGetCustomerId() int64 {
  55. if t.claims.CustomerId == 0 {
  56. return t.MustGetInt64("customer_id")
  57. }
  58. return t.claims.CustomerId
  59. }
  60. func (t *AuthorCtl) MustGetAreaId() int64 {
  61. areaId, exist := t.GetInt64("area_id")
  62. if !exist {
  63. areaId = t.claims.AreaId
  64. }
  65. if areaId == 0 {
  66. t.ERROR("area_id 不能为空", code.MSG_ERR_Param)
  67. }
  68. return areaId
  69. }
  70. // 对各种角色进行不同的接口权限限定
  71. // role: main sub entry user : 主账号 子账号 录入人员 用户