Skip to content

Commit

Permalink
格式化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrnicolase committed May 8, 2020
1 parent 67419ec commit 09904fd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
28 changes: 14 additions & 14 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func toList() error {
}
log.Println("保存群发消息到服务器", string(result))

var body getui.SaveListBodyResponse
var body getui.ListBodyResponse
if err := json.Unmarshal(result, &body); nil != err {
return errors.Wrap(err, "decode bytes to SaveListBodyResponse")
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func sendTransmissionSingle() (string, error) {
msg := gt.NewMessage(getui.MsgTypeTransmission)
t, pushInfo := getui.NewTransmission(`横幅内容`, `横幅标题`, "测试透传消息, time:"+time.Now().Format(`2006-01-02 15:04:05`))
param := getui.PushToSingleParam{
Message: *msg,
Message: msg,
Transmission: t,
Cid: Cid,
PushInfo: pushInfo,
Expand All @@ -120,7 +120,7 @@ func sendNotificationSingle() (string, error) {
t.TransmissionContent = "我是一个通知"

param := getui.PushToSingleParam{
Message: *msg,
Message: msg,
Notification: t,
Cid: Cid,
}
Expand All @@ -138,7 +138,7 @@ func sendLinkSingle() (string, error) {
t := getui.NewLink("www.baidu.com", "访问百度", "百度")

param := getui.PushToSingleParam{
Message: *msg,
Message: msg,
Link: t,
Cid: Cid,
}
Expand All @@ -156,8 +156,8 @@ func saveListBody() ([]byte, error) {
t := getui.NewNotification("群推消息1", "群推1")
t.TransmissionContent = "群推透传消息"

param := getui.SaveListBodyParam{
Message: *msg,
param := getui.ListBodyParam{
Message: msg,
Notification: t,
}

Expand All @@ -177,33 +177,33 @@ func sendToList(taskid string) ([]byte, error) {
func sendSingleBatch() ([]byte, error) {
msg := gt.NewMessage(getui.MsgTypeTransmission)
t, pushInfo := getui.NewTransmission("横幅标题", "横幅内容", "Batch测试透传消息, time:"+time.Now().Format(`15:04:05`))
p := getui.NewPushToSingleParam(*msg)
p := getui.NewPushToSingleParam(msg)
p.Transmission = t
p.PushInfo = pushInfo
p.Cid = Cid

msg1 := gt.NewMessage(getui.MsgTypeNotification)
t1 := getui.NewNotification("Batch-Body", "Batch-Head")
t1.TransmissionContent = "Batch我是一个通知"
p1 := getui.NewPushToSingleParam(*msg1)
p1 := getui.NewPushToSingleParam(msg1)
p1.Notification = t1
p1.Cid = Cid

msg2 := gt.NewMessage(getui.MsgTypeLink)
t2 := getui.NewLink("www.baidu.com", "Batch访问百度", "Batch百度")
p2 := getui.NewPushToSingleParam(*msg2)
p2 := getui.NewPushToSingleParam(msg2)
p2.Link = t2
p2.Cid = Cid

var ml []getui.PushToSingleParam
ml = append(ml, *p)
ml = append(ml, *p1)
ml = append(ml, *p2)
ml = append(ml, p)
ml = append(ml, p1)
ml = append(ml, p2)

batch := getui.PushSingleBatchParam{
batch := getui.PushToBatchSingleParam{
MsgList: ml,
NeedDetail: false,
}

return gt.PushSingleBatch(batch)
return gt.PushToBatchSingle(batch)
}
8 changes: 4 additions & 4 deletions getui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package getui
import "time"

const (
// RestAPI 接口域名
RestAPI string = `https://restapi.getui.com/v1`
// APIServer 接口域名
APIServer string = `https://restapi.getui.com/v1`

// ContentTypeJSON json类型的请求
ContentTypeJSON string = `application/json`
Expand Down Expand Up @@ -85,8 +85,8 @@ type Message struct {
// NewMessage 返回消息类型
// 返回消息类型;
// 默认:is_offline = true; offline_expire_time=86400000; push_network_type = 0
func (g Getui) NewMessage(msgType MsgType) *Message {
return &Message{
func (g Getui) NewMessage(msgType MsgType) Message {
return Message{
AppKey: g.AppKey,
IsOffline: true,
OfflineExpireTime: 86400000,
Expand Down
33 changes: 16 additions & 17 deletions push.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (g *Getui) RefreshToken() error {
}

now := time.Now()
url := fmt.Sprintf(`%s/%s/auth_sign`, RestAPI, g.AppID)
url := fmt.Sprintf(`%s/%s/auth_sign`, APIServer, g.AppID)
timestamp := now.Unix() * 1000
sign := signature(g.AppKey, strconv.FormatInt(timestamp, 10), g.MasterSecret)

Expand Down Expand Up @@ -79,16 +79,15 @@ type PushToSingleParam struct {
}

// NewPushToSingleParam 返回单推消息参数
func NewPushToSingleParam(message Message) *PushToSingleParam {
return &PushToSingleParam{
func NewPushToSingleParam(message Message) PushToSingleParam {
return PushToSingleParam{
Message: message,
RequestID: strconv.FormatInt(time.Now().UnixNano(), 10),
}
}

// PushToSingle 单推
func (g *Getui) PushToSingle(p PushToSingleParam) ([]byte, error) {
// 刷新验证Token
if err := g.RefreshToken(); nil != err {
return nil, err
}
Expand All @@ -98,13 +97,13 @@ func (g *Getui) PushToSingle(p PushToSingleParam) ([]byte, error) {
if nil != err {
return nil, errors.Wrap(err, "encode push_to_single param to json bytes")
}
url := fmt.Sprintf(`%s/%s/push_single`, RestAPI, g.AppID)
url := fmt.Sprintf(`%s/%s/push_single`, APIServer, g.AppID)

return Send(url, g.Token, bytes.NewBuffer(body))
}

// SaveListBodyParam 保存消息体
type SaveListBodyParam struct {
// ListBodyParam 保存消息体
type ListBodyParam struct {
Message Message `json:"message"`
Link *Link `json:"link,omitempty"`
Notification *Notification `json:"notification,omitempty"`
Expand All @@ -113,20 +112,20 @@ type SaveListBodyParam struct {
TaskName string `json:"task_name,omitempty"`
}

// SaveListBodyResponse 保存群发消息服务器返回结构体
type SaveListBodyResponse struct {
// ListBodyResponse 保存群发消息服务器返回结构体
type ListBodyResponse struct {
Result string `json:"result"`
TaskID string `json:"taskid"`
}

// SaveListBody 将推送消息保存在服务器
// 后面可以重复调用tolist接口将保存的消息发送给不同的目标用户
func (g *Getui) SaveListBody(p SaveListBodyParam) ([]byte, error) {
func (g *Getui) SaveListBody(p ListBodyParam) ([]byte, error) {
if err := g.RefreshToken(); nil != err {
return nil, err
}

url := fmt.Sprintf(`%s/%s/save_list_body`, RestAPI, g.AppID)
url := fmt.Sprintf(`%s/%s/save_list_body`, APIServer, g.AppID)
body, err := json.Marshal(p)
if nil != err {
return nil, errors.Wrap(err, "encode save_list_body param to json bytes")
Expand All @@ -151,7 +150,7 @@ func (g *Getui) PushToList(p PushListParam) ([]byte, error) {
return nil, err
}

url := fmt.Sprintf(`%s/%s/push_list`, RestAPI, g.AppID)
url := fmt.Sprintf(`%s/%s/push_list`, APIServer, g.AppID)
body, err := json.Marshal(p)
if nil != err {
return nil, errors.Wrap(err, "encode push_list param to json bytes")
Expand All @@ -160,20 +159,20 @@ func (g *Getui) PushToList(p PushListParam) ([]byte, error) {
return Send(url, g.Token, bytes.NewBuffer(body))
}

// PushSingleBatchParam 批量单推参数
// PushToBatchSingleParam 批量单推参数
// 将一批推送个人信息合并在一个接口,一次请求进行推送
type PushSingleBatchParam struct {
type PushToBatchSingleParam struct {
MsgList []PushToSingleParam `json:"msg_list"`
NeedDetail bool `json:"need_detail,omitempty"`
}

// PushSingleBatch 批量单推
func (g *Getui) PushSingleBatch(p PushSingleBatchParam) ([]byte, error) {
// PushToBatchSingle 批量单推
func (g *Getui) PushToBatchSingle(p PushToBatchSingleParam) ([]byte, error) {
if err := g.RefreshToken(); nil != err {
return nil, err
}

url := fmt.Sprintf(`%s/%s/push_single_batch`, RestAPI, g.AppID)
url := fmt.Sprintf(`%s/%s/push_single_batch`, APIServer, g.AppID)
body, err := json.Marshal(p)
if nil != err {
return nil, errors.Wrap(err, "encode push_single_batch param to json bytes")
Expand Down
12 changes: 6 additions & 6 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ type Style struct {

// NewStyle 样式
// 默认: type=0
func NewStyle(txt, title string) *Style {
return &Style{
func NewStyle(text, title string) Style {
return Style{
Type: 0,
Text: txt,
Text: text,
Title: title,
Logo: "",
IsRing: true,
Expand Down Expand Up @@ -71,7 +71,7 @@ func (Notification) Name() string {
func NewNotification(text, title string) *Notification {
return &Notification{
TransmissionType: true,
Style: *NewStyle(text, title),
Style: NewStyle(text, title),
}
}

Expand All @@ -93,7 +93,7 @@ func (Link) Name() string {
func NewLink(url, text, title string) *Link {
return &Link{
URL: url,
Style: *NewStyle(text, title),
Style: NewStyle(text, title),
}
}

Expand Down Expand Up @@ -155,7 +155,7 @@ func (StartActivity) Name() string {
func NewStartActivity(text, title string) *StartActivity {
return &StartActivity{
TransmissionType: true,
Style: *NewStyle(text, title),
Style: NewStyle(text, title),
}
}

Expand Down

0 comments on commit 09904fd

Please sign in to comment.