Skip to content

Commit a537306

Browse files
authored
webui功能添加 (#75)
* feat: webui添加功能 为webui添加处理request的请求 包括好友添加请求,群邀请请求,群申请加入请求 * feat: webui添加功能 为webui添加手动处理request的请求的功能 包括好友添加请求,群邀请请求,群申请加入请求 * fix: 修改request对象为指针存储
1 parent 206889b commit a537306

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

control/web/gui.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Package webctrl
2+
/*
3+
* 一个用户webui的包,里面包含了webui所需的所有内容
4+
*/
15
package webctrl
26

37
import (
@@ -7,6 +11,7 @@ import (
711
"net/http"
812
"os"
913
"strconv"
14+
"sync"
1015

1116
manager "github.com/FloatTech/bot-manager"
1217
// 依赖gin监听server
@@ -28,6 +33,8 @@ var (
2833
logConn *websocket.Conn
2934

3035
l logWriter
36+
// 存储请求事件,flag作为键,一个request对象作为值
37+
requestData sync.Map
3138
)
3239

3340
// logWriter
@@ -36,6 +43,20 @@ var (
3643
type logWriter struct {
3744
}
3845

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+
3960
// InitGui 初始化gui
4061
func InitGui() {
4162
// 将日志重定向到前端hook
@@ -94,6 +115,10 @@ func controller() {
94115
})
95116
context.JSON(200, datas)
96117
})
118+
// 获取所有请求
119+
engine.POST("/get_requests", getRequests)
120+
// 执行一个请求事件
121+
engine.POST("handle_request", handelRequest)
97122
// 链接日志
98123
engine.GET("/get_log", getLogs)
99124
// 获取前端标签
@@ -111,6 +136,41 @@ func controller() {
111136
}
112137
}
113138

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+
114174
// updateAllPluginStatus
115175
/**
116176
* @Description: 改变所有插件的状态
@@ -367,6 +427,31 @@ func messageHandle() {
367427
}
368428
}
369429
})
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+
})
370455
}
371456

372457
// upgrade
@@ -451,6 +536,23 @@ func cors() gin.HandlerFunc {
451536
}
452537
}
453538

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+
454556
func (l logWriter) Write(p []byte) (n int, err error) {
455557
if logConn != nil {
456558
err := logConn.WriteMessage(websocket.TextMessage, p)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.16
55
require (
66
github.com/FloatTech/AnimeAPI v1.1.10
77
github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4
8-
github.com/FloatTech/bot-manager v1.0.0
8+
github.com/FloatTech/bot-manager v1.0.1-0.20211112011524-85b9895271ed
99
github.com/fogleman/gg v1.3.0
1010
github.com/fumiama/cron v1.3.0
1111
github.com/fumiama/go-base16384 v1.2.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4/go.mod h1:W7ag6hml1pZTNzRXKU74OMr
77
github.com/FloatTech/ZeroBot-Plugin-Timer v1.4.3/go.mod h1:MVOQQ4e6AVGFm993blXXU4Sd6bAsLY2+Zb+/HMrEeEc=
88
github.com/FloatTech/bot-manager v1.0.0 h1:d63J5htLhVBc2ITG09WBJI+qAB0ubPjYhfXl6hljBNk=
99
github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ=
10+
github.com/FloatTech/bot-manager v1.0.1-0.20211112011524-85b9895271ed h1:GEOgDVbvaxXqZxgWE/y5JOlbMXrmq7n0M+m9g3md2To=
11+
github.com/FloatTech/bot-manager v1.0.1-0.20211112011524-85b9895271ed/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ=
1012
github.com/FloatTech/imgfactory v0.1.1 h1:ooL2+fV8yrMhv1ShGGKsN0Rm/flWoKnvqXaUD+dC3DQ=
1113
github.com/FloatTech/imgfactory v0.1.1/go.mod h1:ThDALab8aOuU6KVYESVWFqmjcqtm03e0SvGlTw6s+aw=
1214
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=

0 commit comments

Comments
 (0)