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

151 lines
3.5 KiB

5 years ago
  1. package wx
  2. import (
  3. "fmt"
  4. "github.com/kirinlabs/HttpRequest"
  5. "github.com/ouxuanserver/osmanthuswine/src/helper"
  6. "net/url"
  7. )
  8. func GetCodeUrl() string {
  9. uid := helper.CreateUUID()
  10. return fmt.Sprintf(CodeUrl, Appid, url.PathEscape(RedirectUri), uid)
  11. }
  12. func GetAccessTokenUrl(code string) string {
  13. return fmt.Sprintf(AccessTokenUrl, Appid, Secret, code)
  14. }
  15. func GetRefreshTokenUrl(refreshToken string) string {
  16. return fmt.Sprintf(RefreshTokenUrl, Appid, refreshToken)
  17. }
  18. func GetAuthUrl(accessToken string, openid string) string {
  19. return fmt.Sprintf(AuthUrl, accessToken, openid)
  20. }
  21. func GetUserInfoUrl(accessToken string, openid string) string {
  22. return fmt.Sprintf(UserInfoUrl, accessToken, openid)
  23. }
  24. func GetTicketUrl(ticketToken string) string {
  25. return fmt.Sprintf(TicketUrl, ticketToken)
  26. }
  27. func GetTicketTokenUrl() string {
  28. return fmt.Sprintf(TicketTokenUrl, Appid, Secret)
  29. }
  30. ///
  31. type Token struct {
  32. AccessToken string `json:"access_token"`
  33. ExpiresIn int64 `json:"expires_in"`
  34. RefreshToken string `json:"refresh_token"`
  35. Openid string `json:"openid"`
  36. Scope string `json:"scope"`
  37. Unionid string `json:"unionid"`
  38. }
  39. func GetToken(code string) (*Token, error) {
  40. tokenUrl := GetAccessTokenUrl(code)
  41. resp, err := HttpRequest.Get(tokenUrl)
  42. if err != nil {
  43. return nil, err
  44. }
  45. token := new(Token)
  46. err = resp.Json(token)
  47. return token, err
  48. }
  49. func RefreshToken(refreshToken string) (*Token, error) {
  50. refreshUrl := GetRefreshTokenUrl(refreshToken)
  51. resp, err := HttpRequest.Get(refreshUrl)
  52. if err != nil {
  53. return nil, err
  54. }
  55. token := new(Token)
  56. err = resp.Json(token)
  57. return token, err
  58. }
  59. type ValidMessage struct {
  60. ErrCode int `json:"err_code"`
  61. ErrMsg string `json:"err_msg"`
  62. }
  63. func ValidToken(accessToken, openid string) bool {
  64. authUrl := GetAuthUrl(accessToken, openid)
  65. resp, err := HttpRequest.Get(authUrl)
  66. if err != nil {
  67. return false
  68. }
  69. msg := new(ValidMessage)
  70. err = resp.Json(msg)
  71. if err != nil {
  72. return false
  73. }
  74. if msg.ErrCode != 0 {
  75. return false
  76. }
  77. return true
  78. }
  79. type UserInfo struct {
  80. Openid string `json:"openid"`
  81. Nickname string `json:"nickname"`
  82. Sex int `json:"sex"`
  83. Province string `json:"province"`
  84. City string `json:"city"`
  85. Country string `json:"country"`
  86. Headimgurl string `json:"headimgurl"`
  87. Privilege []string `json:"privilege"`
  88. Unionid string `json:"unionid"`
  89. }
  90. func GetUserInfo(accessToken, openid string) (*UserInfo, error) {
  91. userInfoUrl := GetUserInfoUrl(accessToken, openid)
  92. resp, err := HttpRequest.Get(userInfoUrl)
  93. if err != nil {
  94. return nil, err
  95. }
  96. userInfo := new(UserInfo)
  97. err = resp.Json(userInfo)
  98. return userInfo, err
  99. }
  100. type TicketToken struct {
  101. AccessToken string `json:"access_token"`
  102. ExpiresIn int64 `json:"expires_in"`
  103. ErrCode int `json:"err_code"`
  104. ErrMsg string `json:"err_msg"`
  105. }
  106. func GetTicketToken() (*TicketToken, error) {
  107. tokenUrl := GetTicketTokenUrl()
  108. resp, err := HttpRequest.Debug(true).Get(tokenUrl)
  109. if err != nil {
  110. return nil, err
  111. }
  112. token := new(TicketToken)
  113. err = resp.Json(token)
  114. return token, err
  115. }
  116. type ApiTicket struct {
  117. ErrCode int `json:"err_code"`
  118. ErrMsg string `json:"err_msg"`
  119. Ticket string `json:"ticket"`
  120. ExpiresIn int64 `json:"expires_in"`
  121. }
  122. func GetTicket(ticketToken string) (*ApiTicket, error) {
  123. ticketUrl := GetTicketUrl(ticketToken)
  124. resp, err := HttpRequest.Debug(true).Get(ticketUrl)
  125. if err != nil {
  126. return nil, err
  127. }
  128. ticket := new(ApiTicket)
  129. err = resp.Json(ticket)
  130. return ticket, err
  131. }