2023-04-06 13:46:19 +08:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"film/model"
|
|
|
|
"fmt"
|
|
|
|
"github.com/go-co-op/gocron"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Worker struct {
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) Init() error {
|
|
|
|
timezone, _ := time.LoadLocation("Asia/Shanghai")
|
|
|
|
cron := gocron.NewScheduler(timezone)
|
|
|
|
|
|
|
|
cron.Every(30).Seconds().Do(func() {
|
|
|
|
w.syncOrder()
|
|
|
|
})
|
|
|
|
|
|
|
|
cron.StartAsync()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) httpPost(requestUrl string) ([]byte, error) {
|
|
|
|
client := &http.Client{Timeout: 20 * time.Second}
|
|
|
|
//忽略https的证书
|
|
|
|
client.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
p := url.Values{}
|
|
|
|
u, _ := url.Parse(requestUrl)
|
|
|
|
|
|
|
|
u.RawQuery = p.Encode()
|
|
|
|
req, err := http.NewRequest("POST", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("token", w.Token)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("%d:%s", resp.StatusCode, resp.Status)
|
|
|
|
}
|
|
|
|
return io.ReadAll(resp.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) syncOrder() {
|
|
|
|
orderUrl := "https://hahapiao.cn/api/Synchro/toList"
|
|
|
|
body, err := w.httpPost(orderUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("syncOrder error : %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
|
|
log.Errorf("json.unmarshal [%s] error : %s", string(body), err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cast.ToInt(result["status"]) != 200 || cast.ToInt(result["code"]) != 200 {
|
|
|
|
log.Errorf("status[%d] code[%d] message[%s]", cast.ToInt(result["status"]), cast.ToInt(result["code"]), cast.ToString(result["msg"]))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
datas := cast.ToSlice(result["data"])
|
|
|
|
|
|
|
|
for _, d := range datas {
|
|
|
|
data := cast.ToStringMap(d)
|
|
|
|
id := cast.ToInt64(data["id"])
|
|
|
|
order, err := model.GetOrder(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("get order id : %d error :%s", id, err.Error())
|
|
|
|
}
|
|
|
|
isAdd := false
|
|
|
|
|
|
|
|
if order == nil {
|
|
|
|
order = new(model.Order)
|
|
|
|
order.Id = id
|
|
|
|
isAdd = true
|
|
|
|
}
|
|
|
|
order.OrderId = cast.ToString(data["order_id"])
|
|
|
|
order.PayPrice = int64(cast.ToFloat64(data["payPrice"]) * 100)
|
|
|
|
order.TotalPrice = int64(cast.ToFloat64(data["total_price"]) * 100)
|
|
|
|
order.MaoyanPrice = int64(cast.ToFloat64(data["maoyan_price"]) * 100)
|
|
|
|
order.StartTime = cast.ToString(data["startTime"])
|
|
|
|
order.StartDate = cast.ToString(data["invalidateDate"])
|
|
|
|
order.SeatNum = cast.ToInt(data["seat_num"])
|
|
|
|
order.Seats = cast.ToString(data["seats"])
|
|
|
|
order.ShowTime = cast.ToInt64(data["show_time"])
|
|
|
|
order.PayTime = cast.ToInt64(data["pay_time"])
|
|
|
|
order.CityName = cast.ToString(data["cityName"])
|
|
|
|
order.ProvinceName = cast.ToString(data["provinceName"])
|
|
|
|
order.MovieName = cast.ToString(data["movieName"])
|
|
|
|
order.Address = cast.ToString(data["address"])
|
|
|
|
order.Ting = cast.ToString(data["ting"])
|
2023-04-06 13:59:46 +08:00
|
|
|
order.CinemaName = cast.ToString(data["cinemaName"])
|
2023-04-06 13:46:19 +08:00
|
|
|
|
|
|
|
if isAdd {
|
|
|
|
model.AddOrder(order)
|
|
|
|
} else {
|
|
|
|
model.UpdateOrder(order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|