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

153 lines
3.5 KiB

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