film/worker/haha/haha_processor.go

131 lines
2.9 KiB
Go

package haha
import (
"encoding/json"
"film/base/httputil"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
type ProcessorConfig struct {
Token string
}
type Processor struct {
token string
}
func NewProcessor(cfg *ProcessorConfig) *Processor {
return &Processor{
token: cfg.Token,
}
}
func (p *Processor) checkError(body []byte) (interface{}, error) {
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 nil, err
}
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 nil, fmt.Errorf("%d:%s", cast.ToInt(result["code"]), cast.ToString(result["msg"]))
}
return result["data"], nil
}
func (p *Processor) BidPrice(id int64, price int64) error {
reqUrl := "https://hahapiao.cn/api/Synchro/toPrice"
params := map[string]interface{}{
"id": cast.ToString(id),
"price": fmt.Sprintf("%.2f", float64(price)/100),
}
header := map[string]interface{}{
"token": p.token,
}
body, err := httputil.HttpPostForm(reqUrl, header, params)
if err != nil {
log.Error("httpPost error : %s", err.Error())
return err
}
_, err = p.checkError(body)
if err != nil {
return err
}
return nil
}
func (p *Processor) queryBid(params map[string]interface{}) ([]*BidOrder, error) {
reqUrl := "https://hahapiao.cn/api/Synchro/getOrderList"
header := map[string]interface{}{
"token": p.token,
}
body, err := httputil.HttpPostForm(reqUrl, header, params)
if err != nil {
log.Error("httpPost error : %s", err.Error())
return nil, err
}
datas, err := p.checkError(body)
if err != nil {
return nil, err
}
result := make([]*BidOrder, 0)
for _, d := range cast.ToSlice(datas) {
data := cast.ToStringMap(d)
o := new(BidOrder)
o.OrderId = cast.ToInt64(data["id"])
o.OrderNo = cast.ToString(data["orderNumber"])
o.Status = cast.ToString(data["status"])
o.BidTime = cast.ToInt64(data["bindd_time"])
o.BidPrice = int64(cast.ToFloat64(data["price"]) * 100)
result = append(result, o)
}
return result, nil
}
func (p *Processor) QueryBidding() ([]*BidOrder, error) {
bidOrder, err := p.queryBid(map[string]interface{}{
"tab": 1,
"type": 1,
})
if err != nil {
return nil, err
}
return bidOrder, nil
}
func (p *Processor) QueryBidWin() ([]*BidOrder, error) {
bidOrder, err := p.queryBid(map[string]interface{}{
"pageSize": 10,
"current": 1,
"total": 0,
"tab": 0,
"type": 1,
"mold": 1,
})
if err != nil {
return nil, err
}
return bidOrder, nil
}
func (p *Processor) QueryBidComplete() ([]*BidOrder, error) {
bidOrder, err := p.queryBid(map[string]interface{}{
"tab": 0,
"type": 2,
"form": 1,
})
if err != nil {
return nil, err
}
return bidOrder, nil
}