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.
152 lines
3.5 KiB
152 lines
3.5 KiB
package wx
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/kirinlabs/HttpRequest"
|
|
"github.com/ouxuanserver/osmanthuswine/src/helper"
|
|
"hudongzhuanjia/utils/define"
|
|
"net/url"
|
|
)
|
|
|
|
func GetCodeUrl() string {
|
|
uid := helper.CreateUUID()
|
|
return fmt.Sprintf(CodeUrl, define.WxAppId, url.PathEscape(RedirectUri), uid)
|
|
}
|
|
|
|
func GetAccessTokenUrl(code string) string {
|
|
return fmt.Sprintf(AccessTokenUrl, define.WxAppId, define.Secret, code)
|
|
}
|
|
|
|
func GetRefreshTokenUrl(refreshToken string) string {
|
|
return fmt.Sprintf(RefreshTokenUrl, define.WxAppId, refreshToken)
|
|
}
|
|
|
|
func GetAuthUrl(accessToken string, openid string) string {
|
|
return fmt.Sprintf(AuthUrl, accessToken, openid)
|
|
}
|
|
|
|
func GetUserInfoUrl(accessToken string, openid string) string {
|
|
return fmt.Sprintf(UserInfoUrl, accessToken, openid)
|
|
}
|
|
|
|
func GetTicketUrl(ticketToken string) string {
|
|
return fmt.Sprintf(TicketUrl, ticketToken)
|
|
}
|
|
|
|
func GetTicketTokenUrl() string {
|
|
return fmt.Sprintf(TicketTokenUrl, define.WxAppId, define.Secret)
|
|
}
|
|
|
|
///
|
|
|
|
type Token struct {
|
|
AccessToken string `json:"access_token"`
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
Openid string `json:"openid"`
|
|
Scope string `json:"scope"`
|
|
Unionid string `json:"unionid"`
|
|
}
|
|
|
|
func GetToken(code string) (*Token, error) {
|
|
tokenUrl := GetAccessTokenUrl(code)
|
|
resp, err := HttpRequest.Get(tokenUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
token := new(Token)
|
|
err = resp.Json(token)
|
|
return token, err
|
|
}
|
|
|
|
func RefreshToken(refreshToken string) (*Token, error) {
|
|
refreshUrl := GetRefreshTokenUrl(refreshToken)
|
|
resp, err := HttpRequest.Get(refreshUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
token := new(Token)
|
|
err = resp.Json(token)
|
|
return token, err
|
|
}
|
|
|
|
type ValidMessage struct {
|
|
ErrCode int `json:"err_code"`
|
|
ErrMsg string `json:"err_msg"`
|
|
}
|
|
|
|
func ValidToken(accessToken, openid string) bool {
|
|
authUrl := GetAuthUrl(accessToken, openid)
|
|
resp, err := HttpRequest.Get(authUrl)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
msg := new(ValidMessage)
|
|
err = resp.Json(msg)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if msg.ErrCode != 0 {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
type UserInfo struct {
|
|
Openid string `json:"openid"`
|
|
Nickname string `json:"nickname"`
|
|
Sex int `json:"sex"`
|
|
Province string `json:"province"`
|
|
City string `json:"city"`
|
|
Country string `json:"country"`
|
|
Headimgurl string `json:"headimgurl"`
|
|
Privilege []string `json:"privilege"`
|
|
Unionid string `json:"unionid"`
|
|
}
|
|
|
|
func GetUserInfo(accessToken, openid string) (*UserInfo, error) {
|
|
userInfoUrl := GetUserInfoUrl(accessToken, openid)
|
|
resp, err := HttpRequest.Get(userInfoUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
userInfo := new(UserInfo)
|
|
err = resp.Json(userInfo)
|
|
return userInfo, err
|
|
}
|
|
|
|
type TicketToken struct {
|
|
AccessToken string `json:"access_token"`
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
ErrCode int `json:"err_code"`
|
|
ErrMsg string `json:"err_msg"`
|
|
}
|
|
|
|
func GetTicketToken() (*TicketToken, error) {
|
|
tokenUrl := GetTicketTokenUrl()
|
|
resp, err := HttpRequest.Debug(true).Get(tokenUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
token := new(TicketToken)
|
|
err = resp.Json(token)
|
|
return token, err
|
|
}
|
|
|
|
type ApiTicket struct {
|
|
ErrCode int `json:"err_code"`
|
|
ErrMsg string `json:"err_msg"`
|
|
Ticket string `json:"ticket"`
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
}
|
|
|
|
func GetTicket(ticketToken string) (*ApiTicket, error) {
|
|
ticketUrl := GetTicketUrl(ticketToken)
|
|
resp, err := HttpRequest.Debug(true).Get(ticketUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ticket := new(ApiTicket)
|
|
err = resp.Json(ticket)
|
|
return ticket, err
|
|
}
|