auth.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package auth
  2. import (
  3. context2 "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/silenceper/wechat/v2/miniprogram/context"
  7. "github.com/silenceper/wechat/v2/util"
  8. )
  9. const (
  10. code2SessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
  11. checkEncryptedDataURL = "https://api.weixin.qq.com/wxa/business/checkencryptedmsg?access_token=%s"
  12. )
  13. // Auth 登录/用户信息
  14. type Auth struct {
  15. *context.Context
  16. }
  17. // NewAuth new auth
  18. func NewAuth(ctx *context.Context) *Auth {
  19. return &Auth{ctx}
  20. }
  21. // ResCode2Session 登录凭证校验的返回结果
  22. type ResCode2Session struct {
  23. util.CommonError
  24. OpenID string `json:"openid"` // 用户唯一标识
  25. SessionKey string `json:"session_key"` // 会话密钥
  26. UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
  27. }
  28. // RspCheckEncryptedData .
  29. type RspCheckEncryptedData struct {
  30. util.CommonError
  31. Vaild bool `json:"vaild"` // 是否是合法的数据
  32. CreateTime uint `json:"create_time"` // 加密数据生成的时间戳
  33. }
  34. // Code2Session 登录凭证校验。
  35. func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) {
  36. return auth.Code2SessionContext(context2.Background(), jsCode)
  37. }
  38. // Code2SessionContext 登录凭证校验。
  39. func (auth *Auth) Code2SessionContext(ctx context2.Context, jsCode string) (result ResCode2Session, err error) {
  40. var response []byte
  41. if response, err = util.HTTPGetContext(ctx, fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)); err != nil {
  42. return
  43. }
  44. if err = json.Unmarshal(response, &result); err != nil {
  45. return
  46. }
  47. if result.ErrCode != 0 {
  48. err = fmt.Errorf("Code2Session error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
  49. return
  50. }
  51. return
  52. }
  53. // GetPaidUnionID 用户支付完成后,获取该用户的 UnionId,无需用户授权
  54. func (auth *Auth) GetPaidUnionID() {
  55. // TODO
  56. }
  57. // CheckEncryptedData .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
  58. func (auth *Auth) CheckEncryptedData(encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
  59. return auth.CheckEncryptedDataContext(context2.Background(), encryptedMsgHash)
  60. }
  61. // CheckEncryptedDataContext .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
  62. func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
  63. var response []byte
  64. var (
  65. at string
  66. )
  67. if at, err = auth.GetAccessToken(); err != nil {
  68. return
  69. }
  70. if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), "encrypted_msg_hash="+encryptedMsgHash); err != nil {
  71. return
  72. }
  73. if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil {
  74. return
  75. }
  76. return
  77. }