auth.go 5.7 KB

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