auth.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. getPhoneNumber = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
  13. )
  14. // Auth 登录/用户信息
  15. type Auth struct {
  16. *context.Context
  17. }
  18. // NewAuth new auth
  19. func NewAuth(ctx *context.Context) *Auth {
  20. return &Auth{ctx}
  21. }
  22. // ResCode2Session 登录凭证校验的返回结果
  23. type ResCode2Session struct {
  24. util.CommonError
  25. OpenID string `json:"openid"` // 用户唯一标识
  26. SessionKey string `json:"session_key"` // 会话密钥
  27. UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
  28. }
  29. // RspCheckEncryptedData .
  30. type RspCheckEncryptedData struct {
  31. util.CommonError
  32. Vaild bool `json:"vaild"` // 是否是合法的数据
  33. CreateTime uint `json:"create_time"` // 加密数据生成的时间戳
  34. }
  35. // Code2Session 登录凭证校验。
  36. func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) {
  37. return auth.Code2SessionContext(context2.Background(), jsCode)
  38. }
  39. // Code2SessionContext 登录凭证校验。
  40. func (auth *Auth) Code2SessionContext(ctx context2.Context, jsCode string) (result ResCode2Session, err error) {
  41. var response []byte
  42. if response, err = util.HTTPGetContext(ctx, fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)); err != nil {
  43. return
  44. }
  45. if err = json.Unmarshal(response, &result); err != nil {
  46. return
  47. }
  48. if result.ErrCode != 0 {
  49. err = fmt.Errorf("Code2Session error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
  50. return
  51. }
  52. return
  53. }
  54. // GetPaidUnionID 用户支付完成后,获取该用户的 UnionId,无需用户授权
  55. func (auth *Auth) GetPaidUnionID() {
  56. // TODO
  57. }
  58. // CheckEncryptedData .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
  59. func (auth *Auth) CheckEncryptedData(encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
  60. return auth.CheckEncryptedDataContext(context2.Background(), encryptedMsgHash)
  61. }
  62. // CheckEncryptedDataContext .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
  63. func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
  64. var response []byte
  65. var (
  66. at string
  67. )
  68. if at, err = auth.GetAccessToken(); err != nil {
  69. return
  70. }
  71. // 由于GetPhoneNumberContext需要传入JSON,所以HTTPPostContext入参改为[]byte
  72. if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), []byte("encrypted_msg_hash="+encryptedMsgHash), nil); err != nil {
  73. return
  74. }
  75. if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil {
  76. return
  77. }
  78. return
  79. }
  80. // GetPhoneNumberResponse 新版获取用户手机号响应结构体
  81. type GetPhoneNumberResponse struct {
  82. util.CommonError
  83. PhoneInfo PhoneInfo `json:"phone_info"`
  84. }
  85. // PhoneInfo 获取用户手机号内容
  86. type PhoneInfo struct {
  87. PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号
  88. PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号
  89. CountryCode string `json:"countryCode"` // 区号
  90. WaterMark struct {
  91. Timestamp int64 `json:"timestamp"`
  92. AppID string `json:"appid"`
  93. } `json:"watermark"` // 数据水印
  94. }
  95. // GetPhoneNumberContext 小程序通过code获取用户手机号
  96. func (auth *Auth) GetPhoneNumberContext(ctx context2.Context, code string) (*GetPhoneNumberResponse, error) {
  97. var response []byte
  98. var (
  99. at string
  100. err error
  101. )
  102. if at, err = auth.GetAccessToken(); err != nil {
  103. return nil, err
  104. }
  105. body := map[string]interface{}{
  106. "code": code,
  107. }
  108. bodyBytes, err := json.Marshal(body)
  109. if err != nil {
  110. return nil, err
  111. }
  112. header := map[string]string{"Content-Type": "application/json;charset=utf-8"}
  113. if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(getPhoneNumber, at), bodyBytes, header); err != nil {
  114. return nil, err
  115. }
  116. var result GetPhoneNumberResponse
  117. if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
  118. return nil, err
  119. }
  120. return &result, nil
  121. }
  122. // GetPhoneNumber 小程序通过code获取用户手机号
  123. func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
  124. return auth.GetPhoneNumberContext(context2.Background(), code)
  125. }