parent
9a2935a7eb
commit
4ff2078f17
@ -0,0 +1,40 @@
|
||||
package gitee
|
||||
|
||||
import (
|
||||
"go.dtapp.net/library/utils/golog"
|
||||
"go.dtapp.net/library/utils/gorequest"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ConfigClient struct {
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
RedirectUri string
|
||||
PgsqlDb *gorm.DB // 日志数据库
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
client *gorequest.App // 请求客户端
|
||||
log *golog.ApiClient // 日志服务
|
||||
config *ConfigClient // 配置
|
||||
}
|
||||
|
||||
func NewClient(config *ConfigClient) (*Client, error) {
|
||||
|
||||
var err error
|
||||
c := &Client{config: config}
|
||||
|
||||
c.client = gorequest.NewHttp()
|
||||
|
||||
if c.config.PgsqlDb != nil {
|
||||
c.log, err = golog.NewApiClient(
|
||||
golog.WithGormClient(c.config.PgsqlDb),
|
||||
golog.WithTableName(logTable),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package gitee
|
||||
|
||||
const (
|
||||
apiUrl = "https://gitee.com"
|
||||
)
|
||||
|
||||
const (
|
||||
logTable = "gitee"
|
||||
)
|
@ -0,0 +1,10 @@
|
||||
package gitee
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// OauthAuthorize 获取登录地址
|
||||
func (c *Client) OauthAuthorize() string {
|
||||
return fmt.Sprintf(apiUrl+"/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code", c.config.ClientID, c.config.RedirectUri)
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package gitee
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go.dtapp.net/library/utils/gorequest"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type OauthTokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
type OauthTokenResult struct {
|
||||
Result OauthTokenResponse // 结果
|
||||
Body []byte // 内容
|
||||
Http gorequest.Response // 请求
|
||||
Err error // 错误
|
||||
}
|
||||
|
||||
func newOauthTokenResult(result OauthTokenResponse, body []byte, http gorequest.Response, err error) *OauthTokenResult {
|
||||
return &OauthTokenResult{Result: result, Body: body, Http: http, Err: err}
|
||||
}
|
||||
|
||||
// OauthToken OAuth2 获取 AccessToken 认证步骤
|
||||
// https://gitee.com/api/v5/oauth_doc#/list-item-2
|
||||
func (c *Client) OauthToken(code string) *OauthTokenResult {
|
||||
// 参数
|
||||
params := gorequest.NewParamsWith()
|
||||
// 请求
|
||||
request, err := c.request(apiUrl+fmt.Sprintf("/oauth/token?grant_type=authorization_code&code=%s&client_id=%s&redirect_uri=%s&client_secret=%s", code, c.config.ClientID, c.config.RedirectUri, c.config.ClientSecret), params, http.MethodPost)
|
||||
// 定义
|
||||
var response OauthTokenResponse
|
||||
err = json.Unmarshal(request.ResponseBody, &response)
|
||||
return newOauthTokenResult(response, request.ResponseBody, request, err)
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package gitee
|
||||
|
||||
import "go.dtapp.net/library/utils/gorequest"
|
||||
|
||||
func (c *Client) request(url string, params map[string]interface{}, method string) (gorequest.Response, error) {
|
||||
|
||||
// 创建请求
|
||||
client := c.client
|
||||
|
||||
// 设置请求地址
|
||||
client.SetUri(url)
|
||||
|
||||
// 设置方式
|
||||
client.SetMethod(method)
|
||||
|
||||
// 设置格式
|
||||
client.SetContentTypeJson()
|
||||
|
||||
// 设置参数
|
||||
client.SetParams(params)
|
||||
|
||||
// 发起请求
|
||||
request, err := client.Request()
|
||||
if err != nil {
|
||||
return gorequest.Response{}, err
|
||||
}
|
||||
|
||||
// 日志
|
||||
if c.config.PgsqlDb != nil {
|
||||
go c.log.GormMiddleware(request)
|
||||
}
|
||||
|
||||
return request, err
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package gitee
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GetAccessToken 请求参数
|
||||
type GetAccessToken struct {
|
||||
Code string
|
||||
}
|
||||
|
||||
// GetAccessTokenResult 返回参数
|
||||
type GetAccessTokenResult struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
// GetAccessToken OAuth2 获取 AccessToken 认证步骤 https://gitee.com/api/v5/oauth_doc#/list-item-2
|
||||
func (app *App) GetAccessToken(param GetAccessToken) (result GetAccessTokenResult, err error) {
|
||||
url := fmt.Sprintf("https://gitee.com/oauth/token?grant_type=authorization_code&code=%s&client_id=%s&redirect_uri=%s&client_secret=%s", param.Code, app.ClientID, app.RedirectUri, app.ClientSecret)
|
||||
|
||||
// api params
|
||||
params := map[string]interface{}{}
|
||||
|
||||
// common params
|
||||
|
||||
// request
|
||||
body, err := app.request(url, params, "POST")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package gitee
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
|
||||
"net/http"
|
||||
// "strconv"
|
||||
)
|
||||
|
||||
// App 基本配置
|
||||
type App struct {
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
RedirectUri string
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func (app *App) request(url string, params map[string]interface{}, method string) (resp []byte, err error) {
|
||||
// 请求参数
|
||||
marshal, _ := json.Marshal(params)
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest(method, url, bytes.NewReader(marshal))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpClient := &http.Client{}
|
||||
var response *http.Response
|
||||
response, err = httpClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 处理成功
|
||||
defer response.Body.Close()
|
||||
resp, err = ioutil.ReadAll(response.Body)
|
||||
|
||||
return resp, err
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package gitee
|
||||
|
||||
import "fmt"
|
||||
|
||||
// GetRedirectUri 获取登录地址
|
||||
func (app *App) GetRedirectUri() string {
|
||||
return fmt.Sprintf("https://gitee.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code", app.ClientID, app.RedirectUri)
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package gitee
|
||||
|
||||
const (
|
||||
GiteeOAuth2AuthorizeServer = "https://gitee.com/oauth/authorize"
|
||||
GiteeOAuth2TokenServer = "https://gitee.com/oauth/token"
|
||||
)
|
@ -1,29 +0,0 @@
|
||||
package workwechat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// AuthAccessTokenResult 返回参数
|
||||
type AuthAccessTokenResult struct {
|
||||
Errcode int `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
// AuthAccessToken 获取access_token https://open.work.weixin.qq.com/api/doc/90000/90135/91039
|
||||
func (app *App) AuthAccessToken() (result AuthAccessTokenResult, err error) {
|
||||
url := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", app.AppID, app.Secret)
|
||||
|
||||
// request
|
||||
body, err := app.request(url, map[string]interface{}{}, "GET")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package workwechat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// App 基本配置
|
||||
type App struct {
|
||||
AppID string
|
||||
AgentID int
|
||||
Secret string
|
||||
RedirectUri string
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func (app *App) request(url string, params map[string]interface{}, method string) (resp []byte, err error) {
|
||||
// 请求参数
|
||||
marshal, _ := json.Marshal(params)
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest(method, url, bytes.NewReader(marshal))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpClient := &http.Client{}
|
||||
var response *http.Response
|
||||
response, err = httpClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 处理成功
|
||||
defer response.Body.Close()
|
||||
resp, err = ioutil.ReadAll(response.Body)
|
||||
|
||||
return resp, err
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package workwechat
|
||||
|
||||
import "fmt"
|
||||
|
||||
// GetRedirectUri 构造网页授权链接 https://work.weixin.qq.com/api/doc/90000/90135/91022
|
||||
func (app *App) GetRedirectUri() string {
|
||||
return fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", app.AppID, app.RedirectUri)
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package workwechat
|
||||
|
||||
import "fmt"
|
||||
|
||||
// QrConnect 构造独立窗口登录二维码 https://open.work.weixin.qq.com/api/doc/90000/90135/91019
|
||||
func (app *App) QrConnect() string {
|
||||
return fmt.Sprintf("https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=%s&agentid=%d&redirect_uri=%s&state=STATE&lang=zh", app.AppID, app.AgentID, app.RedirectUri)
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package wechatqy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go.dtapp.net/library/utils/gorequest"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CgiBinGetTokenResponse struct {
|
||||
Errcode int `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
type CgiBinGetTokenResult struct {
|
||||
Result CgiBinGetTokenResponse // 结果
|
||||
Body []byte // 内容
|
||||
Http gorequest.Response // 请求
|
||||
Err error // 错误
|
||||
}
|
||||
|
||||
func newCgiBinGetTokenResult(result CgiBinGetTokenResponse, body []byte, http gorequest.Response, err error) *CgiBinGetTokenResult {
|
||||
return &CgiBinGetTokenResult{Result: result, Body: body, Http: http, Err: err}
|
||||
}
|
||||
|
||||
// CgiBinGetToken 获取access_token
|
||||
// https://open.work.weixin.qq.com/api/doc/90000/90135/91039
|
||||
func (c *Client) CgiBinGetToken(notMustParams ...gorequest.Params) *CgiBinGetTokenResult {
|
||||
// 参数
|
||||
params := gorequest.NewParamsWith(notMustParams...)
|
||||
// 请求
|
||||
request, err := c.request(apiUrl+fmt.Sprintf("/cgi-bin/gettoken?corpid=%s&corpsecret=%s", c.config.AppID, c.config.Secret), params, http.MethodGet)
|
||||
// 定义
|
||||
var response CgiBinGetTokenResponse
|
||||
err = json.Unmarshal(request.ResponseBody, &response)
|
||||
return newCgiBinGetTokenResult(response, request.ResponseBody, request, err)
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package wechatqy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ConnectOauth2Authorize 构造网页授权链接
|
||||
// https://work.weixin.qq.com/api/doc/90000/90135/91022
|
||||
func (c *Client) ConnectOauth2Authorize() string {
|
||||
return fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", c.config.AppID, c.config.RedirectUri)
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package wechatqy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// WwOpenSsoQrConnect 构造独立窗口登录二维码
|
||||
// https://open.work.weixin.qq.com/api/doc/90000/90135/91019
|
||||
func (c *Client) WwOpenSsoQrConnect() string {
|
||||
return fmt.Sprintf("https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=%s&agentid=%d&redirect_uri=%s&state=STATE&lang=zh", c.config.AppID, c.config.AgentID, c.config.RedirectUri)
|
||||
}
|
Loading…
Reference in new issue