This commit is contained in:
jiangyong27 2024-06-25 16:50:22 +08:00
parent 53957e8d9c
commit 6126eceb5b
2 changed files with 82 additions and 4 deletions

View File

@ -94,7 +94,7 @@ func (g *Getui) Token() string {
params["appkey"] = g.config.AppKey
params["sign"] = util.Sha256(signStr)
reqBody, _ := json.Marshal(params)
rspBody, err := util.HttpPostJson(reqUrl, nil, reqBody)
rspBody, err := HttpPostJson(reqUrl, nil, reqBody)
if err != nil {
log.Errorf("http post [%s] error :%s", string(reqBody), err.Error())
return ""
@ -170,7 +170,7 @@ func (g *Getui) Push(req *GetuiMessage) (*GetuiPushRsp, error) {
return nil, err
}
rspBody, err := util.HttpPostJson(reqUrl, map[string]string{"token": g.Token()}, reqBody)
rspBody, err := HttpPostJson(reqUrl, map[string]string{"token": g.Token()}, reqBody)
if err != nil {
log.Errorf("goutil http post error :%s", err.Error())
return nil, err
@ -194,7 +194,7 @@ func (g *Getui) Push(req *GetuiMessage) (*GetuiPushRsp, error) {
func (g *Getui) GetUserStatus(cid string) (*GetuiStatus, error) {
reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/user/status/%s", g.config.AppId, cid)
rspBody, err := util.HttpGet(reqUrl, map[string]string{
rspBody, err := HttpGet(reqUrl, map[string]string{
"token": g.Token(),
})
@ -221,7 +221,7 @@ func (g *Getui) GetUserStatus(cid string) (*GetuiStatus, error) {
func (g *Getui) GetUserDetail(cid string) (*GetuiDetail, error) {
reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/user/detail/%s", g.config.AppId, cid)
rspBody, err := util.HttpGet(reqUrl, map[string]string{
rspBody, err := HttpGet(reqUrl, map[string]string{
"token": g.Token(),
})

78
push/http.go Normal file
View File

@ -0,0 +1,78 @@
package push
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"net/http"
"time"
)
// Get 请求 link请求url
func HttpGet(link string, header map[string]string) ([]byte, error) {
client := &http.Client{Timeout: 20 * time.Second}
//忽略https的证书
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, err
}
if header != nil {
for k, v := range header {
req.Header.Add(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized {
return io.ReadAll(resp.Body)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%d:%s", resp.StatusCode, resp.Status)
}
return io.ReadAll(resp.Body)
}
// PostJson 请求
func HttpPostJson(link string, header map[string]string, json []byte) ([]byte, error) {
client := &http.Client{Timeout: 20 * time.Second}
//忽略https的证书
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
req, err := http.NewRequest("POST", link, bytes.NewBuffer(json))
if err != nil {
return nil, err
}
if header != nil {
for k, v := range header {
req.Header.Add(k, v)
}
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized {
return io.ReadAll(resp.Body)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%d:%s", resp.StatusCode, resp.Status)
}
return io.ReadAll(resp.Body)
}