gosdk/push/getui.go

194 lines
5.1 KiB
Go

package push
import (
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"gitlab.batiao8.com/open/gosdk/util"
"time"
)
type GetuiConfig struct {
AppId string
AppKey string
MasterSecret string
}
type GetuiStatus struct {
Status string
LastLoginTime int64
}
type GetuiDetail struct {
ClientAppId string
PackageName string
DeviceToken string
PhoneType int
PhoneModel string
NotificationSwitch bool
CreateTime int64
LoginFreq int
Brand string
}
type GetuiMessage struct {
Cid string
Title string
Message string
}
type Getui struct {
config *GetuiConfig
token string
tokenExpire int64
}
func NewGetui(c *GetuiConfig) *Getui {
return &Getui{
config: c,
tokenExpire: 0,
}
}
func (g *Getui) getResult(rspBody []byte) (map[string]interface{}, error) {
result := make(map[string]interface{})
if err := json.Unmarshal(rspBody, &result); err != nil {
log.Errorf("json[%s] error :%s", string(rspBody), err.Error())
return nil, err
}
code := cast.ToInt(result["code"])
if code != 0 {
log.Errorf("json[%s] rsp error", string(rspBody))
return nil, errors.New(string(rspBody))
}
data := cast.ToStringMap(result["data"])
return data, nil
}
func (g *Getui) Token() string {
nowTime := time.Now().Unix()
if nowTime < g.tokenExpire {
return g.token
}
timestamp := cast.ToString(time.Now().UnixMilli())
signStr := fmt.Sprintf("%s%s%s", g.config.AppKey, timestamp, g.config.MasterSecret)
reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/auth", g.config.AppId)
params := make(map[string]string)
params["timestamp"] = timestamp
params["appkey"] = g.config.AppKey
params["sign"] = util.Sha256(signStr)
reqBody, _ := json.Marshal(params)
rspBody, err := util.HttpPostJson(reqUrl, nil, reqBody)
if err != nil {
log.Errorf("goutil http post error :%s", err.Error())
return ""
}
result, err := g.getResult(rspBody)
if err != nil {
log.Errorf("get Result error :%s", err.Error())
return ""
}
token := cast.ToString(result["token"])
tokenExpire := cast.ToInt64(result["expire_time"])
g.token = token
g.tokenExpire = tokenExpire
return g.token
}
func (g *Getui) Push(req *GetuiMessage) error {
reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/push/single/cid", g.config.AppId)
params := make(map[string]interface{})
params["request_id"] = time.Now().Format("20060102150405") + "_" + goutil.RandomStr(6)
params["settings"] = map[string]interface{}{
"ttl": 2 * 3600 * 24 * 1000,
}
params["audience"] = map[string]interface{}{
"cid": []string{req.Cid},
}
params["push_message"] = map[string]interface{}{
"notification": map[string]interface{}{
"title": req.Title,
"body": req.Message,
"channel_level": 4,
"click_type": "startapp",
},
}
reqBody, _ := json.Marshal(params)
_, err := util.HttpPostJson(reqUrl, map[string]string{"token": g.Token()}, reqBody)
if err != nil {
log.Errorf("goutil http post error :%s", err.Error())
return err
}
return nil
}
func (g *Getui) GetUserStatus(cid string) (*GetuiStatus, error) {
reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/user/status/%s", g.config.AppId, cid)
rspBody, err := util.HttpGet(reqUrl, map[string]string{
"token": g.Token(),
})
if err != nil {
log.Errorf("goutil http get error :%s", err.Error())
return nil, err
}
result, err := g.getResult(rspBody)
if err != nil {
log.Errorf("get result[%s] error :%s", string(rspBody), err.Error())
return nil, err
}
if _, ok := result[cid]; !ok {
return nil, errors.New("not cid exist")
}
status := new(GetuiStatus)
cidResult := cast.ToStringMap(result[cid])
status.Status = cast.ToString(cidResult["status"])
status.Status = cast.ToString(cidResult["status"])
status.LastLoginTime = cast.ToInt64(cidResult["last_login_time"]) / 1000
return status, nil
}
func (g *Getui) GetUserDetail(cid string) (*GetuiDetail, error) {
reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/user/detail/%s", g.config.AppId, cid)
rspBody, err := util.HttpGet(reqUrl, map[string]string{
"token": g.Token(),
})
if err != nil {
log.Errorf("goutil http get error :%s", err.Error())
return nil, err
}
result, err := g.getResult(rspBody)
if err != nil {
log.Errorf("get result[%s] error :%s", string(rspBody), err.Error())
return nil, err
}
validCids := cast.ToStringMap(result["validCids"])
if _, ok := validCids[cid]; !ok {
return nil, errors.New("not cid exist")
}
cidDetail := cast.ToStringMap(validCids[cid])
detail := new(GetuiDetail)
detail.ClientAppId = cast.ToString(cidDetail["client_app_id"])
detail.PackageName = cast.ToString(cidDetail["package_name"])
detail.DeviceToken = cast.ToString(cidDetail["device_token"])
detail.PhoneModel = cast.ToString(cidDetail["phone_model"])
detail.Brand = cast.ToString(cidDetail["brand"])
detail.PhoneType = cast.ToInt(cidDetail["phone_type"])
detail.LoginFreq = cast.ToInt(cidDetail["login_freq"])
detail.NotificationSwitch = cast.ToBool(cidDetail["notification_switch"])
return detail, nil
}