104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package jiguang
|
||
|
||
import (
|
||
"encoding/base64"
|
||
"fmt"
|
||
"github.com/go-resty/resty/v2"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/os/gctx"
|
||
"golang.org/x/net/context"
|
||
)
|
||
|
||
var appKey = "9bbb84675085d124417ceb27"
|
||
var masterSecret = "1a7dff8f3ce58df231481fce"
|
||
|
||
// @Title Notification 2024/7/25 15:45:00
|
||
// @Description 通知
|
||
// @Auth Cory
|
||
type Notification struct {
|
||
Platform interface{} `json:"platform" dc:"推送平台设置"`
|
||
Audience interface{} `json:"audience" dc:"推送设备指定"`
|
||
Notification struct {
|
||
Android struct {
|
||
Title string `json:"title" dc:"通知标题"`
|
||
Alert string `json:"alert" dc:"通知内容"`
|
||
} `json:"android" dc:"Android 平台上的通知"`
|
||
} `json:"notification" dc:"通知内容体,是被推送到客户端的内容。"`
|
||
}
|
||
|
||
// @Title SendAppClockingNotice 2024/7/25 15:35:00
|
||
// @Description 发送APP的打卡通知信息 (需注意:registration_id一次最多只能发送1000)
|
||
// @Auth Cory
|
||
// @param 输入参数名 ---> "参数解释"
|
||
// @Return error ---> "错误信息"
|
||
func SendAppClockingNotice(ctx context.Context, deviceIds []string, dataStr string, flagStr string) {
|
||
//1、发送位置及authorization
|
||
url := "https://api.jpush.cn/v3/push"
|
||
authorization := base64.StdEncoding.EncodeToString([]byte(appKey + ":" + masterSecret))
|
||
|
||
//2、创建 Resty 客户端对象
|
||
client := resty.New()
|
||
|
||
//3、POST 请求示例
|
||
splitIds := splitDeviceIds(deviceIds)
|
||
for _, ids := range splitIds {
|
||
//reqBody := map[string]interface{}{
|
||
// "platform": []string{"android"},
|
||
// "audience": map[string]interface{}{"registration_id": []string{"", ""}},
|
||
// "notification": Notification{Android: AndroidEntity(struct {
|
||
// Title string
|
||
// Alert string
|
||
// }{Title: "上下班打卡提醒", Alert: "即将进入打卡时间,请立即前往【定位考勤】页面进行打卡(如若已打卡或休息请忽略)"})},
|
||
//}
|
||
notification := Notification{
|
||
Platform: []string{"android"},
|
||
Audience: map[string]interface{}{"registration_id": ids},
|
||
Notification: struct {
|
||
Android struct {
|
||
Title string `json:"title" dc:"通知标题"`
|
||
Alert string `json:"alert" dc:"通知内容"`
|
||
} `json:"android" dc:"Android 平台上的通知"`
|
||
}(struct {
|
||
Android struct {
|
||
Title string
|
||
Alert string
|
||
}
|
||
}{
|
||
Android: struct {
|
||
Title string
|
||
Alert string
|
||
}{
|
||
Title: "中煤" + dataStr + flagStr + "打卡提醒",
|
||
Alert: "即将进入打卡时间,请立即前往【定位考勤】页面进行打卡(如若已打卡或休息请忽略)",
|
||
},
|
||
}),
|
||
}
|
||
//4、发送post请求
|
||
_, err := client.R().
|
||
SetHeader("Content-Type", "application/json").
|
||
SetHeader("Authorization", "Basic "+authorization).
|
||
SetBody(notification).
|
||
Post(url)
|
||
if err != nil {
|
||
fmt.Println("发送 POST 请求失败:", err)
|
||
g.Log().Error(gctx.New(), err)
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
// @Title splitDeviceIds 2024/7/26 15:56:00
|
||
// @Description 数据切分(900一条)
|
||
// @Auth Cory
|
||
func splitDeviceIds(deviceIds []string) [][]string {
|
||
var result [][]string
|
||
for i := 0; i < len(deviceIds); i += 900 {
|
||
end := i + 900
|
||
if end > len(deviceIds) {
|
||
end = len(deviceIds)
|
||
}
|
||
result = append(result, deviceIds[i:end])
|
||
}
|
||
return result
|
||
}
|