This commit is contained in:
2024-08-05 12:06:31 +08:00
commit 4f17eb8bf6
10 changed files with 276 additions and 0 deletions

62
core/crontab.go Normal file
View File

@@ -0,0 +1,62 @@
package core
import (
"fmt"
"github.com/robfig/cron"
)
var CronInfo CronServiceInfo
type CronServiceInfo struct {
Cron []string
Func []func()
}
func (c *CronServiceInfo) Register(cron string, f func()) {
c.Cron = append(c.Cron, cron)
c.Func = append(c.Func, f)
}
func CronRun() {
c := cron.New()
for k, v := range CronInfo.Cron {
err := c.AddFunc(v, wrap(CronInfo.Func[k]))
if err != nil {
fmt.Println("定时任务添加失败")
}
}
c.Start()
}
func wrap(f func()) func() {
return func() {
//defer CronRecovery(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name())
f()
}
}
func CronRegister(rule string, f func()) {
CronInfo.Register(rule, f)
}
//func CronRecovery(name string){
// if err := recover(); err != nil {
// var code int
// var msg interface{}
// if h, ok := err.(*Exception); ok {
// code = h.Code
// msg = h.Msg
// } else if _, ok = err.(error); ok {
// msg = "未知错误"
// code = UNKNOW_ERROR
// } else {
// msg = "服务器错误"
// code = SERVER_ERROR
// }
// Log.WithFields(map[string]interface{}{
// "code":code,
// "model":"task",
// "func":name,
// }).Error(msg)
// }
//}

85
core/http.go Normal file
View File

@@ -0,0 +1,85 @@
package core
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
var ApiUrl = "https://www.shuote.top:8082/flash/activity/applyActivity"
var Headers = map[string]string{
"Host": "www.shuote.top:8082",
"Connection": "keep-alive",
"Request-Channel": "MP-WEIXIN",
"X-Access-Token": "",
"Accept-Encoding": "gzip,compress,br,deflate",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x18003137) NetType/WIFI Language/zh_CN",
"Referer": "https://servicewechat.com/wxbb50595cab69d719/403/page-frame.html",
}
// PostStruct 结构
type PostStruct struct {
PayAmount float64 `json:"payAmount"`
ActivityID int `json:"activityId"`
MaleCount int `json:"maleCount"`
FemaleCount int `json:"femaleCount"`
PayType int `json:"payType"`
GroupID int `json:"groupId"`
XbSex any `json:"xbSex"`
CardType int `json:"cardType"`
}
func ApiRequest(url string, customHeaders map[string]string, postData *PostStruct) (string, error) {
if url == "" {
return "", fmt.Errorf("URL is empty")
}
jsonData, err := json.Marshal(postData)
if err != nil {
return "", err
}
client := &http.Client{
Timeout: 30 * time.Second,
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return "", err
}
// 设置默认请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(jsonData)))
// 如果有自定义请求头,则设置请求头
for key, value := range customHeaders {
req.Header.Set(key, value)
}
// 设置Accept-Encoding以接收压缩响应
req.Header.Set("Accept-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(resp.Body)
if err != nil {
return "", err
}
defer reader.Close()
default:
reader = resp.Body
}
responseBytes, err := io.ReadAll(reader)
if err != nil {
return "", err
}
return string(responseBytes), nil
}

13
core/json.go Normal file
View File

@@ -0,0 +1,13 @@
package core
import (
"encoding/json"
)
func JsonToString(postData interface{}) string {
jsons, errs := json.Marshal(postData) //转换成JSON返回的是byte[]
if errs != nil {
return ""
}
return string(jsons)
}