package svc import ( "context" "encoding/json" "errors" "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/config" "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/model" "github.com/zeromicro/go-zero/core/stores/sqlx" ) type ServiceContext struct { Config config.Config AppUser model.AppUserModel GameScore model.GameScoreModel AppAccount model.AppAccountModel } func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ Config: c, AppUser: model.NewAppUserModel(sqlx.NewMysql(c.DB.DataSource), c.Cache), GameScore: model.NewGameScoreModel(sqlx.NewMysql(c.DB.DataSource), c.Cache), AppAccount: model.NewAppAccountModel(sqlx.NewMysql(c.DB.DataSource), c.Cache), } } type AccessToken struct { AppId uint64 `json:"appId"` UserId uint64 `json:"userId"` AppIdStr string `json:"appIdStr"` OpenId string `json:"openId"` } func UnmarshalAccessToken(d any) (ac AccessToken, err error) { m, ok := d.(map[string]interface{}) if !ok { err = errors.New("invalid access token") return } appId, _ := m["appId"].(json.Number).Int64() ac.AppId = uint64(appId) userId, _ := m["userId"].(json.Number).Int64() ac.UserId = uint64(userId) ac.AppIdStr = m["appIdStr"].(string) ac.OpenId = m["openId"].(string) return } func GetCtxToken(ctx context.Context) (ac AccessToken, err error) { return UnmarshalAccessToken(ctx.Value("payload")) }