package backend import ( "context" "encoding/json" "fmt" "kpt-tmr-group/model" "kpt-tmr-group/pkg/logger/zaplog" "kpt-tmr-group/pkg/xerr" operationPb "kpt-tmr-group/proto/go/backend/operation" "go.uber.org/zap" ) const ( GRANT_TYPE = "authorization_code" OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session" ) func (s *StoreEntry) GetOpenId(ctx context.Context, jsCode string) (*operationPb.WxOpenId, error) { url := fmt.Sprintf("%s?appid=%s&secret=%s&js_code=%s&grant_type=%s", OPENID_URL, s.HttpClient.AppID, s.HttpClient.Secret, jsCode, GRANT_TYPE) res, err := s.HttpClient.DoGet(url) if err != nil { zaplog.Error("GetOpenId", zap.Any("DoGet", err), zap.String("url", url)) return nil, xerr.WithStack(err) } data := &model.JsCode{} if err = json.Unmarshal(res, data); err != nil { zaplog.Error("GetOpenId", zap.Any("Unmarshal", err), zap.String("url", url)) return nil, xerr.WithStack(err) } zaplog.Info("GetOpenId", zap.Any("ok", data)) return &operationPb.WxOpenId{Openid: data.OpenId}, nil }