/
notify.go
62 lines (55 loc) · 1.36 KB
/
notify.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
package ws
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/qauzy/mynahs/internal/controller"
"io"
"net/http"
)
type NotifyController struct {
controller.Api
}
func NewNotifyController() *NotifyController {
return &NotifyController{}
}
// HelloWorld hello world
func (api *NotifyController) Notify(c *gin.Context) {
str, ok := c.GetQuery("msg")
if !ok {
WebsocketManager.SendAll([]byte("收到非法消息"))
}
_, ok = c.GetQuery("weather")
if ok {
res, err := http.Get("http://www.weather.com.cn/data/sk/101281201.html")
if err == nil {
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err == nil {
var au AutoGenerated
err = json.Unmarshal(b, &au)
if err == nil {
str += "现在温度是" + au.Weatherinfo.Temp + "度,相对湿度" + au.Weatherinfo.SD
}
}
}
}
WebsocketManager.SendAll([]byte(str))
api.Success(c, "success")
}
type AutoGenerated struct {
Weatherinfo struct {
City string `json:"city"`
Cityid string `json:"cityid"`
Temp string `json:"temp"`
WD string `json:"WD"`
WS string `json:"WS"`
SD string `json:"SD"`
AP string `json:"AP"`
Njd string `json:"njd"`
WSE string `json:"WSE"`
Time string `json:"time"`
Sm string `json:"sm"`
IsRadar string `json:"isRadar"`
Radar string `json:"Radar"`
} `json:"weatherinfo"`
}