service_context.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package svc
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. "git.i2edu.net/i2/go-zero/core/stores/sqlx"
  10. "git.i2edu.net/i2/go-zero/rest/token"
  11. "git.i2edu.net/i2/go-zero/zrpc"
  12. "git.i2edu.net/i2/i2-bill-api/internal/config"
  13. "git.i2edu.net/i2/i2-bill-api/model"
  14. "git.i2edu.net/i2/i2-bill-erp/transformclient"
  15. "github.com/dgrijalva/jwt-go"
  16. _ "github.com/go-sql-driver/mysql"
  17. "github.com/xormplus/xorm"
  18. )
  19. var ServiceConfig config.Config
  20. type ServiceContext struct {
  21. Config config.Config
  22. SqlConn sqlx.SqlConn
  23. RdCli Cache
  24. Wechat *Wechat
  25. Transformer transformclient.Transform
  26. UserModel model.UserModel
  27. I2billAcquirerStudentModel model.I2billAcquirerStudentModel
  28. DB *xorm.Engine
  29. }
  30. // GetUserId defined TODO
  31. func (sc *ServiceContext) GetClaims(r *http.Request) (int64, string, error) {
  32. parser := token.NewTokenParser(token.WithResetDuration(time.Minute))
  33. tok, err := parser.ParseToken(r, sc.Config.JwtAuth.AccessSecret, "")
  34. if err != nil {
  35. return 0, "", err
  36. }
  37. userId, sessionKey := int64(0), ""
  38. m := tok.Claims.(jwt.MapClaims)
  39. switch nbf := m["userId"].(type) {
  40. case string:
  41. i, err := strconv.ParseInt(nbf, 10, 64)
  42. if err != nil {
  43. return 0, "", err
  44. }
  45. userId = i
  46. case json.Number:
  47. v, err := nbf.Int64()
  48. if err != nil {
  49. return 0, "", err
  50. }
  51. userId = v
  52. }
  53. switch nbf := m["sessionKey"].(type) {
  54. case string:
  55. sessionKey = nbf
  56. case json.Number:
  57. v, err := nbf.Int64()
  58. if err != nil {
  59. return 0, "", err
  60. }
  61. sessionKey = fmt.Sprintf("%v", v)
  62. }
  63. return userId, sessionKey, err
  64. }
  65. func (sc *ServiceContext) GetUserIdByJwt(ctx context.Context) int64 {
  66. UserIdStr := ctx.Value("userId").(json.Number)
  67. number, _ := strconv.ParseInt(UserIdStr.String(), 10, 64)
  68. return number
  69. }
  70. func NewServiceContext(c config.Config) *ServiceContext {
  71. db, err := xorm.NewEngine("mysql", c.DataSource)
  72. if err != nil {
  73. panic(err)
  74. }
  75. db.RegisterSqlMap(xorm.Xml("sqlconfig", ".xml"))
  76. db.RegisterSqlTemplate(xorm.Pongo2("sqlconfig", ".stpl"))
  77. db.RegisterSqlTemplate(xorm.Jet("sqlconfig", ".jet"))
  78. db.RegisterSqlTemplate(xorm.Default("sqlconfig", ".tpl"))
  79. db.ShowSQL(true)
  80. svc := &ServiceContext{
  81. Config: c,
  82. SqlConn: sqlx.NewMysql(c.DataSource),
  83. Transformer: transformclient.NewTransform(zrpc.MustNewClient(c.Transform)),
  84. DB: db,
  85. }
  86. svc.UserModel = model.NewUserModel(svc.SqlConn, c.Cache)
  87. svc.I2billAcquirerStudentModel = model.NewI2billAcquirerStudentModel(svc.SqlConn)
  88. svc.RdCli = svc.UserModel.CacheStorage()
  89. svc.Wechat = &Wechat{
  90. RdCli: svc.RdCli,
  91. c: c,
  92. }
  93. ServiceConfig = svc.Config
  94. return svc
  95. }