film/worker/haha/haha_processor.go

59 lines
1.4 KiB
Go
Raw Normal View History

2023-04-09 02:11:58 +08:00
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
}