golang 的 rabbitmq 消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.go 4.9 KiB

6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package taobao
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "strings"
  8. "applet/app/utils/logx"
  9. "github.com/tidwall/gjson"
  10. )
  11. type AuthRes struct {
  12. AccessToken string `json:"access_token"` // Access token
  13. TokenType string `json:"token_type"` // Access token的类型目前只支持bearer
  14. ExpiresIn int `json:"expires_in"` // Access token过期时间
  15. RefreshToken string `json:"refresh_token"` // Refresh token,可用来刷新access_token
  16. ReExpiresIn int `json:"re_expires_in"` // Refresh token过期时间
  17. RefreshTokenValidTime int64 `json:"refresh_token_valid_time"` // Refresh token可用时间
  18. ExpireTime int64 `json:"expire_time"`
  19. W1ExpiresIn int `json:"w1_expires_in"`
  20. W1Valid int64 `json:"w1_valid"`
  21. W2ExpiresIn int `json:"w2_expires_in"`
  22. W2Valid int64 `json:"w2_valid"`
  23. R1ExpiresIn int `json:"r1_expires_in"` // r1级别API或字段的访问过期时间
  24. R2ExpiresIn int `json:"r2_expires_in"` // r2级别API或字段的访问过期时间;
  25. R1Valid int64 `json:"r1_valid"`
  26. R2Valid int64 `json:"r2_valid"`
  27. TaobaoOpenUID string `json:"taobao_open_uid"`
  28. TaobaoUserNick string `json:"taobao_user_nick"`
  29. TaobaoUserID string `json:"taobao_user_id"`
  30. }
  31. // 获取淘宝授权url
  32. func (t *TB) AuthUrl(callbackUrl string) string {
  33. baseUrl := "https://oauth.taobao.com/authorize?response_type=code&client_id=%v&redirect_uri=%v"
  34. baseUrl = fmt.Sprintf(baseUrl, t.Svc_AK, url.QueryEscape(callbackUrl))
  35. return baseUrl
  36. }
  37. // 授权token
  38. func (t *TB) AuthGetTokenByCode(code string) (*AuthRes, error) {
  39. //taobao.tbk.sc.publisher.info.save
  40. args := map[string]string{"code": code}
  41. method := "taobao.top.auth.token.create"
  42. resp, err := send(args, method, t.Svc_AK, t.Svc_SK)
  43. if err != nil {
  44. return nil, logx.Warn(err)
  45. }
  46. var tmp struct {
  47. TopAuthTokenCreateResponse struct {
  48. TokenResult string `json:"token_result"`
  49. } `json:"top_auth_token_create_response"`
  50. }
  51. if err = json.Unmarshal(resp, &tmp); err != nil {
  52. return nil, logx.Warn("[resp]:" + string(resp) + ", [err]: " + err.Error())
  53. }
  54. if tmp.TopAuthTokenCreateResponse.TokenResult == "" {
  55. return nil, logx.Warn("[resp]:" + string(resp))
  56. }
  57. res := &AuthRes{}
  58. if err = json.Unmarshal([]byte(tmp.TopAuthTokenCreateResponse.TokenResult), res); err != nil {
  59. return nil, nil
  60. }
  61. return res, nil
  62. }
  63. // 刷新token
  64. func (t *TB) AuthTokenRefresh(token string) (*AuthRes, error) {
  65. args := map[string]string{"refresh_token": token}
  66. method := "taobao.top.auth.token.refresh"
  67. resp, err := send(args, method, t.Svc_AK, t.Svc_SK)
  68. if err != nil {
  69. return nil, logx.Warn(err)
  70. }
  71. tmp := struct {
  72. TopAuthTokenCreateResponse struct {
  73. TokenResult string `json:"token_result"`
  74. } `json:"top_auth_token_create_response"`
  75. }{}
  76. if err = json.Unmarshal(resp, &tmp); err != nil {
  77. return nil, logx.Warnf("[resp]:" + string(resp) + ", [err]: " + err.Error())
  78. }
  79. res := new(AuthRes)
  80. if err = json.Unmarshal([]byte(tmp.TopAuthTokenCreateResponse.TokenResult), res); err != nil {
  81. return nil, logx.Warnf("[resp]:" + tmp.TopAuthTokenCreateResponse.TokenResult + ", [err]: " + err.Error())
  82. }
  83. return res, nil
  84. }
  85. // 用户备案, 获取淘宝relationId, 与specialId,用于跟踪客户订单
  86. func (t *TB) AuthSave(sid, inviteCode string) (int64, error) {
  87. method := "taobao.tbk.sc.publisher.info.save"
  88. args := map[string]string{
  89. "session": sid, // 这里是用户的邀请码
  90. "inviter_code": inviteCode,
  91. "info_type": "1",
  92. "offline_scene": "4",
  93. "online_scene": "3",
  94. }
  95. resp, err := send(args, method, t.Svc_AK, t.Svc_SK)
  96. errResp := gjson.GetBytes(resp, "error_response").String()
  97. logx.Warn(errResp)
  98. if errResp != "" {
  99. msg := gjson.GetBytes(resp, "error_response.sub_msg").String()
  100. if strings.Contains(string(resp), "isv.permission-api-package-limit") {
  101. msg = "请到联盟申请API权限"
  102. }
  103. if strings.Contains(string(resp), "ip") {
  104. msg = "请到联盟设置IP白名单"
  105. }
  106. return 0, errors.New(msg)
  107. }
  108. var tmp struct {
  109. TbkScPublisherInfoSaveResponse struct {
  110. Data struct {
  111. AccountName string `json:"account_name"`
  112. Desc string `json:"desc"`
  113. RelationID int64 `json:"relation_id"`
  114. SpecialID int64 `json:"special_id"`
  115. } `json:"data"`
  116. RequestID string `json:"request_id"`
  117. } `json:"tbk_sc_publisher_info_save_response"`
  118. }
  119. if err = json.Unmarshal(resp, &tmp); err != nil {
  120. return 0, logx.Warnf("[resp]:" + string(resp) + ", [err]: " + err.Error())
  121. }
  122. if tmp.TbkScPublisherInfoSaveResponse.Data.RelationID != 0 {
  123. return tmp.TbkScPublisherInfoSaveResponse.Data.RelationID, nil
  124. }
  125. if tmp.TbkScPublisherInfoSaveResponse.Data.SpecialID != 0 {
  126. return tmp.TbkScPublisherInfoSaveResponse.Data.SpecialID, nil
  127. }
  128. return 0, logx.Warnf("[resp]:" + string(resp) + ", [err]: " + err.Error())
  129. }