webui功能添加 (#75)

* feat: webui添加功能

为webui添加处理request的请求
包括好友添加请求,群邀请请求,群申请加入请求

* feat: webui添加功能

为webui添加手动处理request的请求的功能
包括好友添加请求,群邀请请求,群申请加入请求

* fix: 修改request对象为指针存储
This commit is contained in:
huoxue1
2021-11-12 18:12:31 +08:00
committed by GitHub
parent 206889bdc5
commit a537306307
3 changed files with 105 additions and 1 deletions

View File

@@ -1,3 +1,7 @@
// Package webctrl
/*
* 一个用户webui的包里面包含了webui所需的所有内容
*/
package webctrl
import (
@@ -7,6 +11,7 @@ import (
"net/http"
"os"
"strconv"
"sync"
manager "github.com/FloatTech/bot-manager"
// 依赖gin监听server
@@ -28,6 +33,8 @@ var (
logConn *websocket.Conn
l logWriter
// 存储请求事件flag作为键一个request对象作为值
requestData sync.Map
)
// logWriter
@@ -36,6 +43,20 @@ var (
type logWriter struct {
}
// request
// @Description: 一个请求事件的结构体
//
type request struct {
RequestType string `json:"request_type"`
SubType string `json:"sub_type"`
Type string `json:"type"`
Comment string `json:"comment"`
GroupID int64 `json:"group_id"`
UserID int64 `json:"user_id"`
Flag string `json:"flag"`
SelfID int64 `json:"self_id"`
}
// InitGui 初始化gui
func InitGui() {
// 将日志重定向到前端hook
@@ -94,6 +115,10 @@ func controller() {
})
context.JSON(200, datas)
})
// 获取所有请求
engine.POST("/get_requests", getRequests)
// 执行一个请求事件
engine.POST("handle_request", handelRequest)
// 链接日志
engine.GET("/get_log", getLogs)
// 获取前端标签
@@ -111,6 +136,41 @@ func controller() {
}
}
// handelRequest
/**
* @Description: 处理一个请求
* @param context
*/
func handelRequest(context *gin.Context) {
var data map[string]interface{}
err := context.BindJSON(&data)
if err != nil {
context.JSON(404, nil)
return
}
r, ok := requestData.LoadAndDelete(data["flag"].(string))
if !ok {
context.JSON(404, "flag not found")
}
r2 := r.(*request)
r2.handle(data["approve"].(bool), data["reason"].(string))
context.JSON(200, "操作成功")
}
// getRequests
/**
* @Description: 获取所有的请求
* @param context
*/
func getRequests(context *gin.Context) {
var data []interface{}
requestData.Range(func(key, value interface{}) bool {
data = append(data, value)
return true
})
context.JSON(200, data)
}
// updateAllPluginStatus
/**
* @Description: 改变所有插件的状态
@@ -367,6 +427,31 @@ func messageHandle() {
}
}
})
// 直接注册一个request请求监听器优先级设置为最高设置不阻断事件传播
zero.OnRequest(func(ctx *zero.Ctx) bool {
if ctx.Event.RequestType == "friend" {
ctx.State["type_name"] = "好友添加"
} else {
if ctx.Event.SubType == "add" {
ctx.State["type_name"] = "加群请求"
} else {
ctx.State["type_name"] = "群邀请"
}
}
return true
}).SetBlock(false).FirstPriority().Handle(func(ctx *zero.Ctx) {
r := &request{
RequestType: ctx.Event.RequestType,
SubType: ctx.Event.SubType,
Type: ctx.State["type_name"].(string),
GroupID: ctx.Event.GroupID,
UserID: ctx.Event.UserID,
Flag: ctx.Event.Flag,
Comment: ctx.Event.Comment,
SelfID: ctx.Event.SelfID,
}
requestData.Store(ctx.Event.Flag, r)
})
}
// upgrade
@@ -451,6 +536,23 @@ func cors() gin.HandlerFunc {
}
}
// handle
/**
* @Description: 提交一个请求
* @receiver r
* @param approve 是否通过
* @param reason 拒绝的理由
*/
func (r *request) handle(approve bool, reason string) {
bot := zero.GetBot(r.SelfID)
if r.RequestType == "friend" {
bot.SetFriendAddRequest(r.Flag, approve, "")
} else {
bot.SetGroupAddRequest(r.Flag, r.SubType, approve, reason)
}
log.Debugln("[gui] ", "已处理", r.UserID, "的"+r.Type)
}
func (l logWriter) Write(p []byte) (n int, err error) {
if logConn != nil {
err := logConn.WriteMessage(websocket.TextMessage, p)