wx_applet_service.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package backend
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "kpt-tmr-group/model"
  7. "kpt-tmr-group/pkg/logger/zaplog"
  8. "kpt-tmr-group/pkg/xerr"
  9. operationPb "kpt-tmr-group/proto/go/backend/operation"
  10. "go.uber.org/zap"
  11. )
  12. const (
  13. GRANT_TYPE = "authorization_code"
  14. OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session"
  15. )
  16. func (s *StoreEntry) GetOpenId(ctx context.Context, jsCode string) (*operationPb.WxOpenId, error) {
  17. 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)
  18. res, err := s.HttpClient.DoGet(url)
  19. if err != nil {
  20. zaplog.Error("GetOpenId", zap.Any("DoGet", err), zap.String("url", url))
  21. return nil, xerr.WithStack(err)
  22. }
  23. data := &model.JsCode{}
  24. if err = json.Unmarshal(res, data); err != nil {
  25. zaplog.Error("GetOpenId", zap.Any("Unmarshal", err), zap.String("url", url))
  26. return nil, xerr.WithStack(err)
  27. }
  28. zaplog.Info("GetOpenId", zap.Any("ok", data))
  29. return &operationPb.WxOpenId{Openid: data.OpenId}, nil
  30. }