1
+ // Package webctrl
2
+ /*
3
+ * 一个用户webui的包,里面包含了webui所需的所有内容
4
+ */
1
5
package webctrl
2
6
3
7
import (
@@ -7,6 +11,7 @@ import (
7
11
"net/http"
8
12
"os"
9
13
"strconv"
14
+ "sync"
10
15
11
16
manager "github.com/FloatTech/bot-manager"
12
17
// 依赖gin监听server
28
33
logConn * websocket.Conn
29
34
30
35
l logWriter
36
+ // 存储请求事件,flag作为键,一个request对象作为值
37
+ requestData sync.Map
31
38
)
32
39
33
40
// logWriter
36
43
type logWriter struct {
37
44
}
38
45
46
+ // request
47
+ // @Description: 一个请求事件的结构体
48
+ //
49
+ type request struct {
50
+ RequestType string `json:"request_type"`
51
+ SubType string `json:"sub_type"`
52
+ Type string `json:"type"`
53
+ Comment string `json:"comment"`
54
+ GroupID int64 `json:"group_id"`
55
+ UserID int64 `json:"user_id"`
56
+ Flag string `json:"flag"`
57
+ SelfID int64 `json:"self_id"`
58
+ }
59
+
39
60
// InitGui 初始化gui
40
61
func InitGui () {
41
62
// 将日志重定向到前端hook
@@ -94,6 +115,10 @@ func controller() {
94
115
})
95
116
context .JSON (200 , datas )
96
117
})
118
+ // 获取所有请求
119
+ engine .POST ("/get_requests" , getRequests )
120
+ // 执行一个请求事件
121
+ engine .POST ("handle_request" , handelRequest )
97
122
// 链接日志
98
123
engine .GET ("/get_log" , getLogs )
99
124
// 获取前端标签
@@ -111,6 +136,41 @@ func controller() {
111
136
}
112
137
}
113
138
139
+ // handelRequest
140
+ /**
141
+ * @Description: 处理一个请求
142
+ * @param context
143
+ */
144
+ func handelRequest (context * gin.Context ) {
145
+ var data map [string ]interface {}
146
+ err := context .BindJSON (& data )
147
+ if err != nil {
148
+ context .JSON (404 , nil )
149
+ return
150
+ }
151
+ r , ok := requestData .LoadAndDelete (data ["flag" ].(string ))
152
+ if ! ok {
153
+ context .JSON (404 , "flag not found" )
154
+ }
155
+ r2 := r .(* request )
156
+ r2 .handle (data ["approve" ].(bool ), data ["reason" ].(string ))
157
+ context .JSON (200 , "操作成功" )
158
+ }
159
+
160
+ // getRequests
161
+ /**
162
+ * @Description: 获取所有的请求
163
+ * @param context
164
+ */
165
+ func getRequests (context * gin.Context ) {
166
+ var data []interface {}
167
+ requestData .Range (func (key , value interface {}) bool {
168
+ data = append (data , value )
169
+ return true
170
+ })
171
+ context .JSON (200 , data )
172
+ }
173
+
114
174
// updateAllPluginStatus
115
175
/**
116
176
* @Description: 改变所有插件的状态
@@ -367,6 +427,31 @@ func messageHandle() {
367
427
}
368
428
}
369
429
})
430
+ // 直接注册一个request请求监听器,优先级设置为最高,设置不阻断事件传播
431
+ zero .OnRequest (func (ctx * zero.Ctx ) bool {
432
+ if ctx .Event .RequestType == "friend" {
433
+ ctx .State ["type_name" ] = "好友添加"
434
+ } else {
435
+ if ctx .Event .SubType == "add" {
436
+ ctx .State ["type_name" ] = "加群请求"
437
+ } else {
438
+ ctx .State ["type_name" ] = "群邀请"
439
+ }
440
+ }
441
+ return true
442
+ }).SetBlock (false ).FirstPriority ().Handle (func (ctx * zero.Ctx ) {
443
+ r := & request {
444
+ RequestType : ctx .Event .RequestType ,
445
+ SubType : ctx .Event .SubType ,
446
+ Type : ctx .State ["type_name" ].(string ),
447
+ GroupID : ctx .Event .GroupID ,
448
+ UserID : ctx .Event .UserID ,
449
+ Flag : ctx .Event .Flag ,
450
+ Comment : ctx .Event .Comment ,
451
+ SelfID : ctx .Event .SelfID ,
452
+ }
453
+ requestData .Store (ctx .Event .Flag , r )
454
+ })
370
455
}
371
456
372
457
// upgrade
@@ -451,6 +536,23 @@ func cors() gin.HandlerFunc {
451
536
}
452
537
}
453
538
539
+ // handle
540
+ /**
541
+ * @Description: 提交一个请求
542
+ * @receiver r
543
+ * @param approve 是否通过
544
+ * @param reason 拒绝的理由
545
+ */
546
+ func (r * request ) handle (approve bool , reason string ) {
547
+ bot := zero .GetBot (r .SelfID )
548
+ if r .RequestType == "friend" {
549
+ bot .SetFriendAddRequest (r .Flag , approve , "" )
550
+ } else {
551
+ bot .SetGroupAddRequest (r .Flag , r .SubType , approve , reason )
552
+ }
553
+ log .Debugln ("[gui] " , "已处理" , r .UserID , "的" + r .Type )
554
+ }
555
+
454
556
func (l logWriter ) Write (p []byte ) (n int , err error ) {
455
557
if logConn != nil {
456
558
err := logConn .WriteMessage (websocket .TextMessage , p )
0 commit comments