auth.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), "encrypted_msg_hash="+encryptedMsgHash); err != nil {
  72. return
  73. }
  74. if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil {
  75. return
  76. }
  77. return
  78. }
  79. // GetPhoneNumberResponse 新版获取用户手机号响应结构体
  80. type GetPhoneNumberResponse struct {
  81. util.CommonError
  82. PhoneInfo PhoneInfo `json:"phone_info"`
  83. }
  84. // PhoneInfo 获取用户手机号内容
  85. type PhoneInfo struct {
  86. PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号
  87. PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号
  88. CountryCode string `json:"countryCode"` // 区号
  89. WaterMark struct {
  90. Timestamp int64 `json:"timestamp"`
  91. AppID string `json:"appid"`
  92. } `json:"watermark"` // 数据水印
  93. }
  94. // GetPhoneNumber 小程序通过code获取用户手机号
  95. func (auth *Auth) GetPhoneNumber(code string) (result GetPhoneNumberResponse, err error) {
  96. var response []byte
  97. var (
  98. at string
  99. )
  100. if at, err = auth.GetAccessToken(); err != nil {
  101. return
  102. }
  103. body := map[string]interface{}{
  104. "code": code,
  105. }
  106. if response, err = util.PostJSON(fmt.Sprintf(getPhoneNumber, at), body); err != nil {
  107. return
  108. }
  109. if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
  110. return
  111. }
  112. return
  113. }