互动
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.

139 lines
3.7 KiB

5 years ago
  1. package qq
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/ouxuanserver/osmanthuswine/src/helper"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. )
  10. var (
  11. Appid = ""
  12. AppKey = ""
  13. RedirectUri = ""
  14. DefaultScope = "get_user_info"
  15. CodeUrl = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s&scope=%s"
  16. TokenUrl = "https://graph.qq.com/oauth2.0/authorize?grant_type=authorization_code&client_id=%s&client_serect=%s&code=%s&redirect_uri=%s"
  17. RefreshUrl = "https://graph.qq.com/oauth2.0/token?grant_type=refresh_token&client_id=%s&client_serect=%s&refresh_token=%s"
  18. PcOpenidUrl = "https://graph.qq.com/oauth2.0/me?access_token=%s"
  19. WapOpenidUrl = "https://graph.z.qq.com/moc2/me?access_token=%s"
  20. UserInfoUrl = "https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s&format=json"
  21. )
  22. func GetCodeUrl() string {
  23. uid := helper.CreateUUID()
  24. return fmt.Sprintf(CodeUrl, Appid, url.PathEscape(RedirectUri), uid, DefaultScope)
  25. }
  26. func GetTokenUrl(code string) string {
  27. return fmt.Sprintf(TokenUrl, Appid, AppKey, code, RedirectUri)
  28. }
  29. func GetRefreshUrl(refreshToken string) string {
  30. return fmt.Sprintf(RefreshUrl, Appid, AppKey, refreshToken)
  31. }
  32. func GetPcOpenidUrl(accessToken string) string {
  33. return fmt.Sprintf(PcOpenidUrl, accessToken)
  34. }
  35. func GetWapOpenidUrl(accessToken string) string {
  36. return fmt.Sprintf(WapOpenidUrl, accessToken)
  37. }
  38. func GetUserInfoUrl(accessToken, openid string) string {
  39. return fmt.Sprintf(UserInfoUrl, accessToken, Appid, openid)
  40. }
  41. type Token struct {
  42. AccessToken string `json:"access_token"`
  43. ExpiresIn int64 `json:"expires_in"`
  44. RefreshToken string `json:"refresh_token"`
  45. }
  46. func GetToken(code string) (*Token, error) {
  47. tokenUrl := GetTokenUrl(code)
  48. resp, err := http.Get(tokenUrl)
  49. if err != nil {
  50. return nil, err
  51. }
  52. body, err := ioutil.ReadAll(resp.Body)
  53. defer resp.Body.Close()
  54. if err != nil {
  55. return nil, err
  56. }
  57. token := new(Token)
  58. err = json.Unmarshal(body, token)
  59. return token, err
  60. }
  61. func RefreshToken(refreshToken string) (*Token, error) {
  62. refreshUrl := GetRefreshUrl(refreshToken)
  63. resp, err := http.Get(refreshUrl)
  64. if err != nil {
  65. return nil, err
  66. }
  67. body, err := ioutil.ReadAll(resp.Body)
  68. defer resp.Body.Close()
  69. if err != nil {
  70. return nil, err
  71. }
  72. token := new(Token)
  73. err = json.Unmarshal(body, token)
  74. return token, err
  75. }
  76. type Open struct {
  77. Openid string `json:"openid"`
  78. ClientId string `json:"client_id"`
  79. }
  80. func GetPcOpenid(accessToken string) (*Open, error) {
  81. pcOpenidUrl := GetPcOpenidUrl(accessToken)
  82. resp, err := http.Get(pcOpenidUrl)
  83. if err != nil {
  84. return nil, err
  85. }
  86. body, err := ioutil.ReadAll(resp.Body)
  87. defer resp.Body.Close()
  88. if err != nil {
  89. return nil, err
  90. }
  91. open := new(Open)
  92. err = json.Unmarshal(body, open)
  93. return open, err
  94. }
  95. type UserInfo struct {
  96. Ret int `json:"ret"`
  97. Msg string `json:"msg"`
  98. Nickname string `json:"nickname"`
  99. Figureurl string `json:"figureurl"`
  100. Figureurl1 string `json:"figureurl_1"`
  101. Figureurl2 string `json:"figureurl_2"`
  102. FigureurlQq1 string `json:"figureurl_qq_1"`
  103. FigureurlQq2 string `json:"figureurl_qq_2"`
  104. Gender string `json:"gender"`
  105. IsYellowVip string `json:"is_yellow_vip"`
  106. Vip string `json:"vip"`
  107. YellowVipLevel string `json:"yellow_vip_level"`
  108. Level string `json:"level"`
  109. IsYellowYearVip string `json:"is_yellow_year_vip"`
  110. }
  111. func GetUserInfo(accessToken, openid string) (*UserInfo, error) {
  112. userInfoUrl := GetUserInfoUrl(accessToken, openid)
  113. resp, err := http.Get(userInfoUrl)
  114. if err != nil {
  115. return nil, err
  116. }
  117. body, err := ioutil.ReadAll(resp.Body)
  118. if err != nil {
  119. return nil, err
  120. }
  121. userInfo := new(UserInfo)
  122. err = json.Unmarshal(body, userInfo)
  123. return userInfo, err
  124. }