Spaces:
Running
Running
Delete main.go
Browse files
main.go
DELETED
|
@@ -1,345 +0,0 @@
|
|
| 1 |
-
package main
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"crypto/tls"
|
| 5 |
-
"embed"
|
| 6 |
-
"encoding/json"
|
| 7 |
-
"flag"
|
| 8 |
-
"fmt"
|
| 9 |
-
"io"
|
| 10 |
-
"net/http"
|
| 11 |
-
"net/url"
|
| 12 |
-
"strings"
|
| 13 |
-
"time"
|
| 14 |
-
_ "time/tzdata"
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
// 请求参数结构体
|
| 18 |
-
type RequestParams struct {
|
| 19 |
-
Title string `json:"title" form:"title"`
|
| 20 |
-
Content string `json:"content" form:"content"`
|
| 21 |
-
AppID string `json:"appid" form:"appid"`
|
| 22 |
-
Secret string `json:"secret" form:"secret"`
|
| 23 |
-
UserID string `json:"userid" form:"userid"`
|
| 24 |
-
TemplateID string `json:"template_id" form:"template_id"`
|
| 25 |
-
BaseURL string `json:"base_url" form:"base_url"`
|
| 26 |
-
Timezone string `json:"tz" form:"tz"`
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
// 全局变量用于存储命令行参数
|
| 30 |
-
var (
|
| 31 |
-
cliTitle string
|
| 32 |
-
cliContent string
|
| 33 |
-
cliAppID string
|
| 34 |
-
cliSecret string
|
| 35 |
-
cliUserID string
|
| 36 |
-
cliTemplateID string
|
| 37 |
-
cliBaseURL string
|
| 38 |
-
startPort string
|
| 39 |
-
cliTimezone string
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
// 微信AccessToken响应
|
| 43 |
-
type AccessTokenResponse struct {
|
| 44 |
-
AccessToken string `json:"access_token"`
|
| 45 |
-
ExpiresIn int `json:"expires_in"`
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
// 微信模板消息请求
|
| 49 |
-
type TemplateMessageRequest struct {
|
| 50 |
-
ToUser string `json:"touser"`
|
| 51 |
-
TemplateID string `json:"template_id"`
|
| 52 |
-
URL string `json:"url"`
|
| 53 |
-
Data map[string]interface{} `json:"data"`
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
// 微信API响应
|
| 57 |
-
type WechatAPIResponse struct {
|
| 58 |
-
Errcode int `json:"errcode"`
|
| 59 |
-
Errmsg string `json:"errmsg"`
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
func main() {
|
| 63 |
-
// 定义命令行参数
|
| 64 |
-
flag.StringVar(&cliTitle, "title", "", "消息标题")
|
| 65 |
-
flag.StringVar(&cliContent, "content", "", "消息内容")
|
| 66 |
-
flag.StringVar(&cliAppID, "appid", "", "AppID")
|
| 67 |
-
flag.StringVar(&cliSecret, "secret", "", "AppSecret")
|
| 68 |
-
flag.StringVar(&cliUserID, "userid", "", "openid")
|
| 69 |
-
flag.StringVar(&cliTemplateID, "template_id", "", "模板ID")
|
| 70 |
-
flag.StringVar(&cliBaseURL, "base_url", "", "跳转url")
|
| 71 |
-
flag.StringVar(&cliTimezone, "tz", "Asia/Shanghai", "时区,默认东八区")
|
| 72 |
-
flag.StringVar(&startPort, "port", "", "端口")
|
| 73 |
-
|
| 74 |
-
// 解析命令行参数
|
| 75 |
-
flag.Parse()
|
| 76 |
-
|
| 77 |
-
// 设置路由
|
| 78 |
-
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
| 79 |
-
w.WriteHeader(http.StatusOK)
|
| 80 |
-
fmt.Fprintf(w, `go-wxpush is running...✅`)
|
| 81 |
-
})
|
| 82 |
-
http.HandleFunc("/wxsend", handleWxSend)
|
| 83 |
-
http.HandleFunc("/detail", handleDetail)
|
| 84 |
-
|
| 85 |
-
// 启动服务器
|
| 86 |
-
//fmt.Println("Server is running on port 5566...")
|
| 87 |
-
port := "5566"
|
| 88 |
-
if startPort != "" {
|
| 89 |
-
port = startPort
|
| 90 |
-
}
|
| 91 |
-
fmt.Println("Server is running on: " + "http://127.0.0.1:" + port)
|
| 92 |
-
|
| 93 |
-
err := http.ListenAndServe(":"+port, nil)
|
| 94 |
-
|
| 95 |
-
if err != nil {
|
| 96 |
-
fmt.Printf("Error starting server: %v\n", err)
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
// 嵌入静态HTML文件
|
| 102 |
-
//
|
| 103 |
-
//go:embed msg_detail.html
|
| 104 |
-
var htmlContent embed.FS
|
| 105 |
-
|
| 106 |
-
// 处理详情页面请求
|
| 107 |
-
func handleDetail(w http.ResponseWriter, r *http.Request) {
|
| 108 |
-
// 从嵌入的资源中读取HTML内容
|
| 109 |
-
htmlData, err := htmlContent.ReadFile("msg_detail.html")
|
| 110 |
-
if err != nil {
|
| 111 |
-
w.WriteHeader(http.StatusInternalServerError)
|
| 112 |
-
fmt.Fprintf(w, `{"error": "Failed to read embedded HTML file: %v"}`, err)
|
| 113 |
-
return
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
// 设置响应头
|
| 117 |
-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
| 118 |
-
|
| 119 |
-
// 返回HTML内容
|
| 120 |
-
w.Write(htmlData)
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
func handleWxSend(w http.ResponseWriter, r *http.Request) {
|
| 124 |
-
|
| 125 |
-
// 解析参数
|
| 126 |
-
var params RequestParams
|
| 127 |
-
|
| 128 |
-
// 根据请求方法解析参数
|
| 129 |
-
if r.Method == "POST" {
|
| 130 |
-
// 解析JSON请求体
|
| 131 |
-
decoder := json.NewDecoder(r.Body)
|
| 132 |
-
err := decoder.Decode(¶ms)
|
| 133 |
-
if err != nil {
|
| 134 |
-
w.WriteHeader(http.StatusBadRequest)
|
| 135 |
-
fmt.Fprintf(w, `{"error": "Invalid JSON format: %v"}`, err)
|
| 136 |
-
return
|
| 137 |
-
}
|
| 138 |
-
} else if r.Method == "GET" {
|
| 139 |
-
// 解析GET查询参数
|
| 140 |
-
params.Title = r.URL.Query().Get("title")
|
| 141 |
-
params.Content = r.URL.Query().Get("content")
|
| 142 |
-
params.AppID = r.URL.Query().Get("appid")
|
| 143 |
-
params.Secret = r.URL.Query().Get("secret")
|
| 144 |
-
params.UserID = r.URL.Query().Get("userid")
|
| 145 |
-
params.TemplateID = r.URL.Query().Get("template_id")
|
| 146 |
-
params.BaseURL = r.URL.Query().Get("base_url")
|
| 147 |
-
params.Timezone = r.URL.Query().Get("tz")
|
| 148 |
-
} else {
|
| 149 |
-
w.WriteHeader(http.StatusMethodNotAllowed)
|
| 150 |
-
fmt.Fprintf(w, `{"error": "Method not allowed"}`)
|
| 151 |
-
return
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
// 只有当GET/POST参数为空时,才使用命令行参数
|
| 155 |
-
if params.Title == "" && cliTitle != "" {
|
| 156 |
-
params.Title = cliTitle
|
| 157 |
-
}
|
| 158 |
-
if params.Content == "" && cliContent != "" {
|
| 159 |
-
params.Content = cliContent
|
| 160 |
-
}
|
| 161 |
-
if params.AppID == "" && cliAppID != "" {
|
| 162 |
-
params.AppID = cliAppID
|
| 163 |
-
}
|
| 164 |
-
if params.Secret == "" && cliSecret != "" {
|
| 165 |
-
params.Secret = cliSecret
|
| 166 |
-
}
|
| 167 |
-
if params.UserID == "" && cliUserID != "" {
|
| 168 |
-
params.UserID = cliUserID
|
| 169 |
-
}
|
| 170 |
-
if params.TemplateID == "" && cliTemplateID != "" {
|
| 171 |
-
params.TemplateID = cliTemplateID
|
| 172 |
-
}
|
| 173 |
-
if params.BaseURL == "" && cliBaseURL != "" {
|
| 174 |
-
params.BaseURL = cliBaseURL
|
| 175 |
-
}
|
| 176 |
-
if params.Timezone == "" && cliTimezone != "" {
|
| 177 |
-
params.Timezone = cliTimezone
|
| 178 |
-
}
|
| 179 |
-
|
| 180 |
-
// 验证必要参数
|
| 181 |
-
if params.AppID == "" || params.Secret == "" || params.UserID == "" || params.TemplateID == "" {
|
| 182 |
-
w.WriteHeader(http.StatusBadRequest)
|
| 183 |
-
fmt.Fprintf(w, `{"error": "Missing required parameters"}`)
|
| 184 |
-
return
|
| 185 |
-
}
|
| 186 |
-
if params.BaseURL == "" {
|
| 187 |
-
params.BaseURL = "https://push.hzz.cool"
|
| 188 |
-
}
|
| 189 |
-
if params.Content == "" {
|
| 190 |
-
params.Content = "测试内容"
|
| 191 |
-
}
|
| 192 |
-
if params.Title == "" {
|
| 193 |
-
params.Title = "测试标题"
|
| 194 |
-
}
|
| 195 |
-
|
| 196 |
-
// 获取AccessToken
|
| 197 |
-
token, err := getAccessToken(params.AppID, params.Secret)
|
| 198 |
-
if err != nil {
|
| 199 |
-
w.WriteHeader(http.StatusInternalServerError)
|
| 200 |
-
fmt.Fprintf(w, `{"error": "Failed to get access token: %v"}`, err)
|
| 201 |
-
return
|
| 202 |
-
}
|
| 203 |
-
|
| 204 |
-
//log.Println(token)
|
| 205 |
-
// 发送模板消息
|
| 206 |
-
resp, err := sendTemplateMessage(token, params)
|
| 207 |
-
if err != nil {
|
| 208 |
-
w.WriteHeader(http.StatusInternalServerError)
|
| 209 |
-
fmt.Fprintf(w, `{"error": "Failed to send template message: %v"}`, err)
|
| 210 |
-
return
|
| 211 |
-
}
|
| 212 |
-
|
| 213 |
-
// 返回结果
|
| 214 |
-
json.NewEncoder(w).Encode(resp)
|
| 215 |
-
}
|
| 216 |
-
|
| 217 |
-
// Token请求参数结构体
|
| 218 |
-
type TokenRequestParams struct {
|
| 219 |
-
GrantType string `json:"grant_type"`
|
| 220 |
-
AppID string `json:"appid"`
|
| 221 |
-
Secret string `json:"secret"`
|
| 222 |
-
ForceRefresh bool `json:"force_refresh"`
|
| 223 |
-
}
|
| 224 |
-
|
| 225 |
-
func getAccessToken(appid, secret string) (string, error) {
|
| 226 |
-
// 构建请求参数
|
| 227 |
-
requestParams := TokenRequestParams{
|
| 228 |
-
GrantType: "client_credential",
|
| 229 |
-
AppID: appid,
|
| 230 |
-
Secret: secret,
|
| 231 |
-
ForceRefresh: false,
|
| 232 |
-
}
|
| 233 |
-
|
| 234 |
-
// 转换为JSON
|
| 235 |
-
jsonData, err := json.Marshal(requestParams)
|
| 236 |
-
if err != nil {
|
| 237 |
-
return "", err
|
| 238 |
-
}
|
| 239 |
-
|
| 240 |
-
// 忽略证书验证
|
| 241 |
-
client := &http.Client{
|
| 242 |
-
Transport: &http.Transport{
|
| 243 |
-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
| 244 |
-
},
|
| 245 |
-
}
|
| 246 |
-
|
| 247 |
-
// 发送POST请求
|
| 248 |
-
resp, err := client.Post("https://api.weixin.qq.com/cgi-bin/stable_token", "application/json", strings.NewReader(string(jsonData)))
|
| 249 |
-
if err != nil {
|
| 250 |
-
return "", err
|
| 251 |
-
}
|
| 252 |
-
defer resp.Body.Close()
|
| 253 |
-
|
| 254 |
-
// 读取响应
|
| 255 |
-
body, err := io.ReadAll(resp.Body)
|
| 256 |
-
if err != nil {
|
| 257 |
-
return "", err
|
| 258 |
-
}
|
| 259 |
-
|
| 260 |
-
//log.Println(string(body))
|
| 261 |
-
|
| 262 |
-
// 解析响应
|
| 263 |
-
var tokenResp AccessTokenResponse
|
| 264 |
-
err = json.Unmarshal(body, &tokenResp)
|
| 265 |
-
//log.Println(tokenResp)
|
| 266 |
-
|
| 267 |
-
if err != nil {
|
| 268 |
-
return "", err
|
| 269 |
-
}
|
| 270 |
-
|
| 271 |
-
return tokenResp.AccessToken, nil
|
| 272 |
-
}
|
| 273 |
-
|
| 274 |
-
func sendTemplateMessage(accessToken string, params RequestParams) (WechatAPIResponse, error) {
|
| 275 |
-
// 构建请求URL
|
| 276 |
-
apiUrl := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s", accessToken)
|
| 277 |
-
|
| 278 |
-
// 处理时区,默认东八区
|
| 279 |
-
location, err := time.LoadLocation("Asia/Shanghai")
|
| 280 |
-
if err != nil {
|
| 281 |
-
location, _ = time.LoadLocation("Asia/Shanghai") // 确保默认使用东八区
|
| 282 |
-
}
|
| 283 |
-
|
| 284 |
-
// 如果参数中有时区,则尝试使用该时区
|
| 285 |
-
if params.Timezone != "" {
|
| 286 |
-
loc, err := time.LoadLocation(params.Timezone)
|
| 287 |
-
if err == nil {
|
| 288 |
-
location = loc
|
| 289 |
-
}
|
| 290 |
-
}
|
| 291 |
-
|
| 292 |
-
// 获取当前时间
|
| 293 |
-
currentTime := time.Now().In(location)
|
| 294 |
-
timeStr := currentTime.Format("2006-01-02 15:04:05")
|
| 295 |
-
|
| 296 |
-
// 构建请求数据
|
| 297 |
-
requestData := TemplateMessageRequest{
|
| 298 |
-
ToUser: params.UserID,
|
| 299 |
-
TemplateID: params.TemplateID,
|
| 300 |
-
URL: params.BaseURL + `/detail?title=` + url.QueryEscape(params.Title) + `&message=` + url.QueryEscape(params.Content) + `&date=` + url.QueryEscape(timeStr),
|
| 301 |
-
Data: map[string]interface{}{
|
| 302 |
-
"title": map[string]string{
|
| 303 |
-
"value": params.Title,
|
| 304 |
-
},
|
| 305 |
-
"content": map[string]string{
|
| 306 |
-
"value": params.Content,
|
| 307 |
-
},
|
| 308 |
-
},
|
| 309 |
-
}
|
| 310 |
-
|
| 311 |
-
// 转换为JSON
|
| 312 |
-
jsonData, err := json.Marshal(requestData)
|
| 313 |
-
if err != nil {
|
| 314 |
-
return WechatAPIResponse{}, err
|
| 315 |
-
}
|
| 316 |
-
|
| 317 |
-
// 忽略证书验证
|
| 318 |
-
client := &http.Client{
|
| 319 |
-
Transport: &http.Transport{
|
| 320 |
-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
| 321 |
-
},
|
| 322 |
-
}
|
| 323 |
-
|
| 324 |
-
// 发送POST请求
|
| 325 |
-
resp, err := client.Post(apiUrl, "application/json", strings.NewReader(string(jsonData)))
|
| 326 |
-
if err != nil {
|
| 327 |
-
return WechatAPIResponse{}, err
|
| 328 |
-
}
|
| 329 |
-
defer resp.Body.Close()
|
| 330 |
-
|
| 331 |
-
// 读取响应
|
| 332 |
-
body, err := io.ReadAll(resp.Body)
|
| 333 |
-
if err != nil {
|
| 334 |
-
return WechatAPIResponse{}, err
|
| 335 |
-
}
|
| 336 |
-
|
| 337 |
-
// 解析响应
|
| 338 |
-
var apiResp WechatAPIResponse
|
| 339 |
-
err = json.Unmarshal(body, &apiResp)
|
| 340 |
-
if err != nil {
|
| 341 |
-
return WechatAPIResponse{}, err
|
| 342 |
-
}
|
| 343 |
-
|
| 344 |
-
return apiResp, nil
|
| 345 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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