film/worker/dadi.go

259 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package worker
import (
"encoding/json"
"film/base/httputil"
"film/config"
"film/model"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"gitlab.com/jiangyong27/gobase/wxapi"
"strings"
"time"
)
type Dadi struct {
Token string
citys map[string]*DadiCity
qyClient *wxapi.WxQiye
}
func (d *Dadi) Init() error {
cityUrl := "https://appapi.dadicinema.com/app-web/v1/web/cinema/cbticket/cbase/cityAndCinemaList"
params := d.getBaseParam(&DadiCinema{
Name: "大地影院(北京十里河铭泽店)",
Id: 118,
UnifiedCode: 11050621,
})
body, err := httputil.HttpGet(cityUrl, params, d.getBaseHeader())
if err != nil {
log.Errorf(" httputil.HttpGet url : %s error: %s", cityUrl, err.Error())
return err
}
result, err := d.checkError(body)
if err != nil {
return err
}
citys := make(map[string]*DadiCity)
datas := cast.ToSlice(result)
for _, d := range datas {
data := cast.ToStringMap(d)
cinemas := make([]*DadiCinema, 0)
for _, c := range cast.ToSlice(data["cinemas"]) {
cin := cast.ToStringMap(c)
cinema := new(DadiCinema)
cinema.Name = cast.ToString(cin["name"])
cinema.Address = cast.ToString(cin["address"])
cinema.Latitude = cast.ToString(cin["latitude"])
cinema.Longitude = cast.ToString(cin["longitude"])
cinema.CityId = cast.ToInt64(cin["cityId"])
cinema.UnifiedCode = cast.ToInt64(cin["unifiedCode"])
cinemas = append(cinemas, cinema)
}
cityInfo := cast.ToStringMap(data["cityInfo"])
city := new(DadiCity)
city.Name = cast.ToString(cityInfo["chName"])
city.ShortName = cast.ToString(cityInfo["shortName"])
city.CityId = cast.ToInt64(data["cityId"])
city.CityCode = cast.ToInt64(data["cityCode"])
city.Cinemas = cinemas
citys[city.ShortName] = city
}
d.citys = citys
log.Infof("load dadi citys : %d", len(d.citys))
cfg := config.GetConfig()
d.qyClient = wxapi.NewQiye(&wxapi.QiyeConfig{
Corpid: cfg.Weixin.QiyeAppid,
Secret: cfg.Weixin.Qiyesecret,
Sender: cfg.Weixin.QiyeAgent,
})
return nil
}
func (d *Dadi) UpdateOrder(order *model.Order) {
}
func (d *Dadi) NewOrder(order *model.Order) {
cinema, err := d.getCinema(order)
if err != nil {
log.Errorf("getcinema order : %s error : %s", goutil.EncodeJSON(order), err.Error())
return
}
hall, err := d.getCinemaFilm(order, cinema)
if err != nil {
log.Errorf("getCinemaFilm order : %s error : %s", goutil.EncodeJSON(order), err.Error())
return
}
dadiOrder, err := d.holdSeats(order, cinema, hall)
if err != nil {
log.Errorf("holdSeats order : %s error : %s", goutil.EncodeJSON(order), err.Error())
return
}
message := make([]string, 0)
message = append(message, "【哈哈票订单信息】")
message = append(message, fmt.Sprintf("城市:%s", order.CityName))
message = append(message, fmt.Sprintf("影院:%s", order.CinemaName))
message = append(message, fmt.Sprintf("影厅:%s", order.Ting))
message = append(message, fmt.Sprintf("影片:%s", order.MovieName))
message = append(message, fmt.Sprintf("座位:%s%s%s", order.Seats,
goutil.If(order.IsSeat == 3, "可以调座", "不可调座"),
goutil.If(order.LoverSeat == 1, "情侣座", "普通座")))
message = append(message, fmt.Sprintf("订单编号:%s", order.OrderId))
message = append(message, fmt.Sprintf("订单金额:%.2f", float64(order.TotalPrice)/100))
message = append(message, fmt.Sprintf("原价:%.2f", float64(order.MaoyanPrice)/100))
message = append(message, fmt.Sprintf("报价范围:%.2f~%.2f", float64(order.MinPrice)/100, float64(order.MaxPrice)/100))
message = append(message, fmt.Sprintf("订单时间:%s", time.Unix(order.PayTime, 0).Format("2006-01-02 15:04")))
message = append(message, fmt.Sprintf("放映时间:%s", time.Unix(order.ShowTime, 0).Format("2006-01-02 15:04")))
message = append(message, "\n")
message = append(message, "【大地订单信息】")
message = append(message, fmt.Sprintf("订单编号:%s", dadiOrder.OrderId))
if err := d.qyClient.SendText([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error : %s", err.Error())
}
}
func (d *Dadi) 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["code"]) != 200 || cast.ToBool(result["success"]) != true {
log.Errorf("code[%d] message[%s]", cast.ToInt(result["code"]), cast.ToString(result["msg"]))
return nil, fmt.Errorf("code[%d] message[%s]", cast.ToInt(result["code"]), cast.ToString(result["msg"]))
}
return result["data"], nil
}
func (d *Dadi) getBaseHeader() map[string]interface{} {
return map[string]interface{}{
"User-Agent": "apifox/1.0.0 (https://www.apifox.cn)",
}
}
func (d *Dadi) getBaseParam(cinema *DadiCinema) map[string]interface{} {
params := map[string]interface{}{
"channelCode": "SYH-DDZY-DD",
"channelName": "大地自营-大地",
"channelNo": "SYH-DDZY-DD",
"channelUid": "SYH-DDZY-DD",
"cinema": cast.ToString(cinema.UnifiedCode),
"cinemaCode": cast.ToString(cinema.UnifiedCode),
"cinemaUid": cast.ToString(cinema.Id),
"d": "iPhone14,3",
"i": "00000000-0000-0000-0000-000000000000",
"k": d.Token,
"r": "1",
"s": "iOS15.2.1",
"t": "1",
"tenantId": "321566",
"unifiedCinemaId": cast.ToString(cinema.Id),
"unifiedCinemaName": cinema.Name,
"unifiedCode": cast.ToString(cinema.UnifiedCode),
"v": "8.7.3",
}
return params
}
func (d *Dadi) getCinema(order *model.Order) (*DadiCinema, error) {
if _, ok := d.citys[order.CityName]; !ok {
log.Errorf("city[%s] not exist dadi cinema", order.CityName)
return nil, fmt.Errorf("city[%s] not exist dadi cinema", order.CityName)
}
city := d.citys[order.CityName]
for _, cinema := range city.Cinemas {
if cinema.Name != order.CinemaName {
continue
}
return cinema, nil
}
return nil, fmt.Errorf("city[%s] not[%s]", order.CityName, order.CinemaName)
}
func (d *Dadi) getCinemaFilm(order *model.Order, cinema *DadiCinema) (*DadiFilmShowHall, error) {
baseReq := d.getBaseParam(cinema)
reqUrl := "https://appapi.dadicinema.com/app-web/v1/web/film/getHitFilmAndFilmSession"
body, err := httputil.HttpGet(reqUrl, baseReq, d.getBaseHeader())
if err != nil {
log.Errorf("httpGet[%s] error : %s", reqUrl, err.Error())
return nil, err
}
result, err := d.checkError(body)
if err != nil {
return nil, err
}
orderShowDay := time.Unix(order.ShowTime, 0).Format("2006-01-02")
var hall *DadiFilmShowHall = nil
for _, f := range cast.ToSlice(cast.ToStringMap(result)["filmList"]) {
ff := cast.ToStringMap(f)
filmName := cast.ToString(ff["name"])
if filmName != order.MovieName {
continue
}
for _, s := range cast.ToSlice(ff["showList"]) {
ss := cast.ToStringMap(s)
showDay := cast.ToString(ss["dayStr"])
if showDay != orderShowDay {
continue
}
for _, p := range cast.ToSlice(ss["plist"]) {
pp := cast.ToStringMap(p)
hallName := cast.ToString(pp["hallName"])
if hallName != order.Ting {
continue
}
hall = new(DadiFilmShowHall)
hall.FilmName = filmName
hall.ShowDay = showDay
hall.HallName = cast.ToString(pp["hallName"])
hall.SessionId = cast.ToString(pp["sessionId"])
hall.StartTime = cast.ToString(pp["startTime"])
hall.EndTime = cast.ToString(pp["endTime"])
return hall, nil
}
}
}
return nil, fmt.Errorf("film[%s] day[%s] ting[%s] nohas", order.MovieName, orderShowDay, order.Ting)
}
func (d *Dadi) getCinemaSeats(order *model.Order, cinema *DadiCinema, hall *DadiFilmShowHall) error {
baseReq := d.getBaseParam(cinema)
baseReq["sessionId"] = hall.SessionId
reqUrl := "https://appapi.dadicinema.com/app-web/v1/web/cinema/cbticket/cticket/getSessionSeat"
body, err := httputil.HttpGet(reqUrl, baseReq, d.getBaseHeader())
if err != nil {
log.Errorf("http get url: %s, error: %s", reqUrl, err.Error())
return err
}
_, err = d.checkError(body)
if err != nil {
return err
}
return nil
//baseReq := d.getBaseParam(cinema)
}
func (d *Dadi) holdSeats(order *model.Order, cinema *DadiCinema, hall *DadiFilmShowHall) (*DadiFilmOrder, error) {
return &DadiFilmOrder{
OrderId: "test",
}, nil
}