-
Notifications
You must be signed in to change notification settings - Fork 19
/
api.go
162 lines (152 loc) · 4.45 KB
/
api.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package wxpusher
import (
"bytes"
"encoding/json"
"errors"
"github.com/wxpusher/wxpusher-sdk-go/model"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
// URLBase 接口域名
const URLBase = "http://wxpusher.zjiecode.com"
// URLSendMessage 发送消息
const URLSendMessage = URLBase + "/api/send/message"
// URLMessageStatus 查询发送状态
const URLMessageStatus = URLBase + "/api/send/query/${messageID}"
// URLCreateQrcode 创建参数二维码
const URLCreateQrcode = URLBase + "/api/fun/create/qrcode"
// URLQueryWxUser 查询App的关注用户
const URLQueryWxUser = URLBase + "/api/fun/wxuser"
// SendMessage 发送消息
func SendMessage(message *model.Message) ([]model.SendMsgResult, error) {
msgResults := make([]model.SendMsgResult, 0)
// 校验消息内容
err := message.Check()
if err != nil {
return msgResults, err
}
// 参数转json
requestBody, _ := json.Marshal(message)
resp, err := http.Post(URLSendMessage, "application/json", bytes.NewReader(requestBody))
if err != nil {
return msgResults, model.NewSDKError(err)
}
defer func() { _ = resp.Body.Close() }()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return msgResults, model.NewSDKError(err)
}
result := model.Result{}
err = json.Unmarshal(respBody, &result)
if err != nil {
return msgResults, model.NewSDKError(err)
}
if !result.Success() {
return msgResults, model.NewError(result.Code, errors.New(result.Msg))
}
// result.Data 转为[]model.SendMsgResult
byteData, err := json.Marshal(result.Data)
if err != nil {
return msgResults, model.NewSDKError(err)
}
err = json.Unmarshal(byteData, &msgResults)
if err != nil {
return msgResults, model.NewSDKError(err)
}
return msgResults, nil
}
// QueryMessageStatus 查询消息发送状态
func QueryMessageStatus(messageID int) (model.Result, error) {
var result model.Result
url := strings.ReplaceAll(URLMessageStatus, "${messageID}", strconv.Itoa(messageID))
resp, err := http.Get(url)
if err != nil {
return result, model.NewSDKError(err)
}
defer func() { _ = resp.Body.Close() }()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return result, model.NewSDKError(err)
}
err = json.Unmarshal(respBody, &result)
if err != nil {
return result, model.NewSDKError(err)
}
if !result.Success() {
return result, model.NewError(result.Code, errors.New(result.Msg))
}
return result, nil
}
// CreateQrcode 创建参数二维码
func CreateQrcode(qrcode *model.Qrcode) (model.CreateQrcodeResult, error) {
var qrResult model.CreateQrcodeResult
err := qrcode.Check()
if err != nil {
return qrResult, err
}
requestBody, _ := json.Marshal(qrcode)
resp, err := http.Post(URLCreateQrcode, "application/json", bytes.NewReader(requestBody))
if err != nil {
return qrResult, model.NewSDKError(err)
}
defer func() { _ = resp.Body.Close() }()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return qrResult, model.NewSDKError(err)
}
result := model.Result{}
err = json.Unmarshal(respBody, &result)
if err != nil {
return qrResult, model.NewSDKError(err)
}
if !result.Success() {
return qrResult, model.NewError(result.Code, errors.New(result.Msg))
}
// result.Data 转为model.CreateQrcodeResult
byteData, err := json.Marshal(result.Data)
if err != nil {
return qrResult, model.NewSDKError(err)
}
err = json.Unmarshal(byteData, &qrResult)
if err != nil {
return qrResult, model.NewSDKError(err)
}
return qrResult, nil
}
// QueryWxUser 查询App的关注用户
func QueryWxUser(appToken string, page, pageSize int) (model.QueryWxUserResult, error) {
var queryResult model.QueryWxUserResult
req, _ := http.NewRequest("GET", URLQueryWxUser, nil)
q := req.URL.Query()
q.Add("appToken", appToken)
q.Add("page", strconv.Itoa(page))
q.Add("pageSize", strconv.Itoa(pageSize))
req.URL.RawQuery = q.Encode()
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return queryResult, model.NewSDKError(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return queryResult, model.NewSDKError(err)
}
defer func() { _ = resp.Body.Close() }()
result := model.Result{}
err = json.Unmarshal(body, &result)
if err != nil {
return queryResult, model.NewSDKError(err)
}
// result.Data 转为model.QueryWxUserResult
byteData, err := json.Marshal(result.Data)
if err != nil {
return queryResult, model.NewSDKError(err)
}
err = json.Unmarshal(byteData, &queryResult)
if err != nil {
return queryResult, model.NewSDKError(err)
}
return queryResult, nil
}