forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package crossSer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"stat/common/crypt"
|
||||
"stat/statg"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InnerCryptPost(requestURL string, postData []byte) (*http.Response, error) {
|
||||
key := statg.Conf.InnerKey
|
||||
enData, err := crypt.CoreAesEncryptEx(postData, 12, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bodyReader := bytes.NewReader(enData)
|
||||
req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
timeout := 30 * time.Second
|
||||
if strings.Contains(requestURL, "botMonthStat") {
|
||||
timeout = 15 * time.Minute
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
client := http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enData, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deData, err := crypt.CoreAesDecryptEx(enData, 12, key)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
resp.Body = io.NopCloser(bytes.NewBuffer(deData))
|
||||
resp.ContentLength = int64(len(deData))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func InnerPlainPost(requestURL string, postData []byte) (*http.Response, error) {
|
||||
bodyReader := bytes.NewReader(postData)
|
||||
req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
timeout := 8 * time.Second
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
client := http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
func InnerRequest(url string, req interface{}, bind interface{}, crypt bool) error {
|
||||
data, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var resp *http.Response
|
||||
if crypt {
|
||||
resp, err = InnerCryptPost(url, data)
|
||||
} else {
|
||||
resp, err = InnerPlainPost(url, data)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
return fmt.Errorf("error status %s, http code %d", resp.Status, resp.StatusCode)
|
||||
}
|
||||
data, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, bind)
|
||||
}
|
||||
|
||||
func GetDateDesc(moveBackDays int) string {
|
||||
if moveBackDays < 0 {
|
||||
moveBackDays = moveBackDays * -1
|
||||
}
|
||||
switch moveBackDays {
|
||||
case 0:
|
||||
return "今天"
|
||||
case 1:
|
||||
return "昨天"
|
||||
case 2:
|
||||
return "前天"
|
||||
default:
|
||||
return fmt.Sprintf("前%d天", moveBackDays)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user