-
Notifications
You must be signed in to change notification settings - Fork 9
/
eventSend.go
100 lines (93 loc) · 2.84 KB
/
eventSend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package api
import (
"encoding/json"
"github.com/XiaoMengXinX/Music163Api-Go/types"
"github.com/XiaoMengXinX/Music163Api-Go/utils"
"github.com/google/uuid"
"strings"
)
// SendEventAPI 发送动态 API
const SendEventAPI = "/api/share/friends/resource"
// SendEventReq SendEvent API 的 body json
type SendEventReq struct {
Msg string `json:"msg"`
Type string `json:"type"`
UUID string `json:"uuid"`
Pics string `json:"pics"`
AddComment string `json:"addComment"`
Header string `json:"header"`
ER string `json:"e_r"`
}
// EventPic 用于发送动态的图片数据
type EventPic struct {
OriginId int `json:"originId"`
SquareId int `json:"squareId"`
RectangleId int `json:"rectangleId"`
Format string `json:"format"`
}
// CreateEventReqJson 创建请求 body json
func CreateEventReqJson(text, pics string) string {
UUID := uuid.New()
shareConfig := SendEventReq{
Msg: text,
Pics: pics,
Type: "noresource",
UUID: strings.Replace(UUID.String(), "-", "", -1),
AddComment: "false",
Header: "{}",
ER: "true",
}
reqBodyJson, _ := json.Marshal(shareConfig)
return string(reqBodyJson)
}
// CreateEventPicsJson 创建动态图片数据 json
func CreateEventPicsJson(picData []types.UploadEventImgData) string {
var eventPicData []EventPic
for i := 0; i < len(picData); i++ {
eventPicData = append(eventPicData, EventPic{
OriginId: picData[i].PicInfo.OriginId,
SquareId: picData[i].PicInfo.SquareId,
RectangleId: picData[i].PicInfo.RectangleId,
Format: picData[i].PicSubtype,
})
}
resultJson, _ := json.Marshal(eventPicData)
return string(resultJson)
}
// SendEvent 发送动态(可以带图片)
func SendEvent(data utils.RequestData, text string, picPath []string) (result types.SendEventData, err error) {
var options utils.EapiOption
options.Path = SendEventAPI
options.Url = "https://music.163.com/eapi/share/friends/resource"
if len(picPath) != 0 {
var picData []types.UploadEventImgData
for i := 0; i < len(picPath); i++ {
nosToken, file, err := GetNosToken(data, picPath[i])
if err != nil {
return result, err
}
_, err = UploadFile(data, file, nosToken)
if err != nil {
return result, err
}
_, picSubtype := utils.DetectFileType(file[:32])
uploadResult, err := UploadEventImg(data, nosToken.Result.DocId, picSubtype)
if err != nil {
return result, err
}
uploadResult.PicSubtype = picSubtype
picData = append(picData, uploadResult)
}
picDataJson := CreateEventPicsJson(picData)
options.Json = CreateEventReqJson(text, picDataJson)
} else {
options.Json = CreateEventReqJson(text, "[]")
}
resBody, _, err := utils.EapiRequest(options, data)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
return result, err
}