mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
✏️ 解耦 web gui
This commit is contained in:
parent
3b3dd3df99
commit
8c9ced0bda
@ -49,8 +49,8 @@ func newctrl(service string, o *Options) *Control {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// enable enables a group to pass the Manager.
|
// Enable enables a group to pass the Manager.
|
||||||
func (m *Control) enable(groupID int64) {
|
func (m *Control) Enable(groupID int64) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
err := db.Insert(m.service, &grpcfg{groupID, 0})
|
err := db.Insert(m.service, &grpcfg{groupID, 0})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -59,8 +59,8 @@ func (m *Control) enable(groupID int64) {
|
|||||||
m.Unlock()
|
m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// disable disables a group to pass the Manager.
|
// Disable disables a group to pass the Manager.
|
||||||
func (m *Control) disable(groupID int64) {
|
func (m *Control) Disable(groupID int64) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
err := db.Insert(m.service, &grpcfg{groupID, 1})
|
err := db.Insert(m.service, &grpcfg{groupID, 1})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -69,7 +69,7 @@ func (m *Control) disable(groupID int64) {
|
|||||||
m.Unlock()
|
m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Control) isEnabledIn(gid int64) bool {
|
func (m *Control) IsEnabledIn(gid int64) bool {
|
||||||
m.RLock()
|
m.RLock()
|
||||||
var c grpcfg
|
var c grpcfg
|
||||||
err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10))
|
err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10))
|
||||||
@ -81,9 +81,9 @@ func (m *Control) isEnabledIn(gid int64) bool {
|
|||||||
logrus.Errorf("[control] %v", err)
|
logrus.Errorf("[control] %v", err)
|
||||||
m.RUnlock()
|
m.RUnlock()
|
||||||
if m.options.DisableOnDefault {
|
if m.options.DisableOnDefault {
|
||||||
m.disable(gid)
|
m.Disable(gid)
|
||||||
} else {
|
} else {
|
||||||
m.enable(gid)
|
m.Enable(gid)
|
||||||
}
|
}
|
||||||
return !m.options.DisableOnDefault
|
return !m.options.DisableOnDefault
|
||||||
}
|
}
|
||||||
@ -92,21 +92,21 @@ func (m *Control) isEnabledIn(gid int64) bool {
|
|||||||
func (m *Control) Handler() zero.Rule {
|
func (m *Control) Handler() zero.Rule {
|
||||||
return func(ctx *zero.Ctx) bool {
|
return func(ctx *zero.Ctx) bool {
|
||||||
ctx.State["manager"] = m
|
ctx.State["manager"] = m
|
||||||
return m.isEnabledIn(ctx.Event.GroupID)
|
return m.IsEnabledIn(ctx.Event.GroupID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookup returns a Manager by the service name, if
|
// Lookup returns a Manager by the service name, if
|
||||||
// not exist, it will returns nil.
|
// not exist, it will returns nil.
|
||||||
func lookup(service string) (*Control, bool) {
|
func Lookup(service string) (*Control, bool) {
|
||||||
mu.RLock()
|
mu.RLock()
|
||||||
defer mu.RUnlock()
|
defer mu.RUnlock()
|
||||||
m, ok := managers[service]
|
m, ok := managers[service]
|
||||||
return m, ok
|
return m, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// forEach iterates through managers.
|
// ForEach iterates through managers.
|
||||||
func forEach(iterator func(key string, manager *Control) bool) {
|
func ForEach(iterator func(key string, manager *Control) bool) {
|
||||||
mu.RLock()
|
mu.RLock()
|
||||||
m := copyMap(managers)
|
m := copyMap(managers)
|
||||||
mu.RUnlock()
|
mu.RUnlock()
|
||||||
@ -138,11 +138,11 @@ func init() {
|
|||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
model := extension.CommandModel{}
|
model := extension.CommandModel{}
|
||||||
_ = ctx.Parse(&model)
|
_ = ctx.Parse(&model)
|
||||||
service, ok := lookup(model.Args)
|
service, ok := Lookup(model.Args)
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.Send("没有找到指定服务!")
|
ctx.Send("没有找到指定服务!")
|
||||||
}
|
}
|
||||||
service.enable(ctx.Event.GroupID)
|
service.Enable(ctx.Event.GroupID)
|
||||||
ctx.Send(message.Text("已启用服务: " + model.Args))
|
ctx.Send(message.Text("已启用服务: " + model.Args))
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -150,11 +150,11 @@ func init() {
|
|||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
model := extension.CommandModel{}
|
model := extension.CommandModel{}
|
||||||
_ = ctx.Parse(&model)
|
_ = ctx.Parse(&model)
|
||||||
service, ok := lookup(model.Args)
|
service, ok := Lookup(model.Args)
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.Send("没有找到指定服务!")
|
ctx.Send("没有找到指定服务!")
|
||||||
}
|
}
|
||||||
service.disable(ctx.Event.GroupID)
|
service.Disable(ctx.Event.GroupID)
|
||||||
ctx.Send(message.Text("已关闭服务: " + model.Args))
|
ctx.Send(message.Text("已关闭服务: " + model.Args))
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ func init() {
|
|||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
model := extension.CommandModel{}
|
model := extension.CommandModel{}
|
||||||
_ = ctx.Parse(&model)
|
_ = ctx.Parse(&model)
|
||||||
service, ok := lookup(model.Args)
|
service, ok := Lookup(model.Args)
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.Send("没有找到指定服务!")
|
ctx.Send("没有找到指定服务!")
|
||||||
}
|
}
|
||||||
@ -177,10 +177,10 @@ func init() {
|
|||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
msg := `---服务列表---`
|
msg := `---服务列表---`
|
||||||
i := 0
|
i := 0
|
||||||
forEach(func(key string, manager *Control) bool {
|
ForEach(func(key string, manager *Control) bool {
|
||||||
i++
|
i++
|
||||||
msg += "\n" + strconv.Itoa(i) + `: `
|
msg += "\n" + strconv.Itoa(i) + `: `
|
||||||
if manager.isEnabledIn(ctx.Event.GroupID) {
|
if manager.IsEnabledIn(ctx.Event.GroupID) {
|
||||||
msg += "●" + key
|
msg += "●" + key
|
||||||
} else {
|
} else {
|
||||||
msg += "○" + key
|
msg += "○" + key
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package control
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
// 前端静态文件
|
// 前端静态文件
|
||||||
|
ctrl "github.com/FloatTech/ZeroBot-Plugin/control"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
@ -34,8 +35,8 @@ var (
|
|||||||
type logWriter struct {
|
type logWriter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitGui 初始化gui
|
// initGui 初始化gui
|
||||||
func InitGui() {
|
func initGui() {
|
||||||
// 将日志重定向到前端hook
|
// 将日志重定向到前端hook
|
||||||
writer := io.MultiWriter(l, os.Stderr)
|
writer := io.MultiWriter(l, os.Stderr)
|
||||||
log.SetOutput(writer)
|
log.SetOutput(writer)
|
||||||
@ -86,8 +87,8 @@ func controller() {
|
|||||||
// 获取插件列表
|
// 获取插件列表
|
||||||
engine.POST("/get_plugins", func(context *gin.Context) {
|
engine.POST("/get_plugins", func(context *gin.Context) {
|
||||||
var datas []map[string]interface{}
|
var datas []map[string]interface{}
|
||||||
forEach(func(key string, manager *Control) bool {
|
ctrl.ForEach(func(key string, manager *ctrl.Control) bool {
|
||||||
datas = append(datas, map[string]interface{}{"id": 1, "handle_type": "", "name": key, "enable": manager.isEnabledIn(0)})
|
datas = append(datas, map[string]interface{}{"id": 1, "handle_type": "", "name": key, "enable": manager.IsEnabledIn(0)})
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
context.JSON(200, datas)
|
context.JSON(200, datas)
|
||||||
@ -134,14 +135,14 @@ func updateAllPluginStatus(context *gin.Context) {
|
|||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
forEach(func(key string, manager *Control) bool {
|
ctrl.ForEach(func(key string, manager *ctrl.Control) bool {
|
||||||
if enable {
|
if enable {
|
||||||
for _, group := range groups {
|
for _, group := range groups {
|
||||||
manager.enable(group)
|
manager.Enable(group)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, group := range groups {
|
for _, group := range groups {
|
||||||
manager.disable(group)
|
manager.Disable(group)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -168,7 +169,7 @@ func updatePluginAllGroupStatus(context *gin.Context) {
|
|||||||
name = parse["name"].(string)
|
name = parse["name"].(string)
|
||||||
enable = parse["enable"].(bool)
|
enable = parse["enable"].(bool)
|
||||||
}
|
}
|
||||||
control, b := lookup(name)
|
control, b := ctrl.Lookup(name)
|
||||||
if !b {
|
if !b {
|
||||||
context.JSON(404, nil)
|
context.JSON(404, nil)
|
||||||
return
|
return
|
||||||
@ -176,9 +177,9 @@ func updatePluginAllGroupStatus(context *gin.Context) {
|
|||||||
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
|
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
|
||||||
for _, group := range ctx.GetGroupList().Array() {
|
for _, group := range ctx.GetGroupList().Array() {
|
||||||
if enable {
|
if enable {
|
||||||
control.enable(group.Get("group_id").Int())
|
control.Enable(group.Get("group_id").Int())
|
||||||
} else {
|
} else {
|
||||||
control.disable(group.Get("group_id").Int())
|
control.Disable(group.Get("group_id").Int())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,15 +206,15 @@ func updatePluginStatus(context *gin.Context) {
|
|||||||
name := parse["name"].(string)
|
name := parse["name"].(string)
|
||||||
enable := parse["enable"].(bool)
|
enable := parse["enable"].(bool)
|
||||||
fmt.Println(name)
|
fmt.Println(name)
|
||||||
control, b := lookup(name)
|
control, b := ctrl.Lookup(name)
|
||||||
if !b {
|
if !b {
|
||||||
context.JSON(404, "服务不存在")
|
context.JSON(404, "服务不存在")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if enable {
|
if enable {
|
||||||
control.enable(groupID)
|
control.Enable(groupID)
|
||||||
} else {
|
} else {
|
||||||
control.disable(groupID)
|
control.Disable(groupID)
|
||||||
}
|
}
|
||||||
context.JSON(200, nil)
|
context.JSON(200, nil)
|
||||||
}
|
}
|
||||||
@ -237,12 +238,12 @@ func getPluginStatus(context *gin.Context) {
|
|||||||
groupID = int64(parse["group_id"].(float64))
|
groupID = int64(parse["group_id"].(float64))
|
||||||
name = parse["name"].(string)
|
name = parse["name"].(string)
|
||||||
}
|
}
|
||||||
control, b := lookup(name)
|
control, b := ctrl.Lookup(name)
|
||||||
if !b {
|
if !b {
|
||||||
context.JSON(404, "服务不存在")
|
context.JSON(404, "服务不存在")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
context.JSON(200, gin.H{"enable": control.isEnabledIn(groupID)})
|
context.JSON(200, gin.H{"enable": control.IsEnabledIn(groupID)})
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPluginsStatus
|
// getPluginsStatus
|
||||||
@ -263,8 +264,8 @@ func getPluginsStatus(context *gin.Context) {
|
|||||||
groupID = int64(parse["group_id"].(float64))
|
groupID = int64(parse["group_id"].(float64))
|
||||||
}
|
}
|
||||||
var datas []map[string]interface{}
|
var datas []map[string]interface{}
|
||||||
forEach(func(key string, manager *Control) bool {
|
ctrl.ForEach(func(key string, manager *ctrl.Control) bool {
|
||||||
enable := manager.isEnabledIn(groupID)
|
enable := manager.IsEnabledIn(groupID)
|
||||||
datas = append(datas, map[string]interface{}{"name": key, "enable": enable})
|
datas = append(datas, map[string]interface{}{"name": key, "enable": enable})
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
13
control/web/init.go
Normal file
13
control/web/init.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Package web 网页管理后端
|
||||||
|
package web
|
||||||
|
|
||||||
|
import "flag"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var en bool
|
||||||
|
// 解析命令行参数,输入 `-g` 即可启用 gui
|
||||||
|
flag.BoolVar(&en, "g", false, "Enable web gui.")
|
||||||
|
if en {
|
||||||
|
initGui()
|
||||||
|
}
|
||||||
|
}
|
||||||
14
main.go
14
main.go
@ -7,9 +7,9 @@ import (
|
|||||||
|
|
||||||
// 注:以下插件均可通过前面加 // 注释,注释后停用并不加载插件
|
// 注:以下插件均可通过前面加 // 注释,注释后停用并不加载插件
|
||||||
// 下列插件可与 wdvxdr1123/ZeroBot v1.1.2 以上配合单独使用
|
// 下列插件可与 wdvxdr1123/ZeroBot v1.1.2 以上配合单独使用
|
||||||
|
// 插件控制
|
||||||
|
_ "github.com/FloatTech/ZeroBot-Plugin/control/web" // web 后端控制
|
||||||
// 词库类
|
// 词库类
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_atri" // ATRI词库
|
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_atri" // ATRI词库
|
||||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat" // 基础词库
|
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat" // 基础词库
|
||||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_qingyunke" // 青云客
|
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_qingyunke" // 青云客
|
||||||
@ -45,11 +45,9 @@ import (
|
|||||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_setutime" // 来份涩图
|
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_setutime" // 来份涩图
|
||||||
|
|
||||||
// 以下为内置依赖,勿动
|
// 以下为内置依赖,勿动
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/driver"
|
"github.com/wdvxdr1123/ZeroBot/driver"
|
||||||
|
|
||||||
"github.com/FloatTech/ZeroBot-Plugin/control"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -63,16 +61,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var en bool
|
|
||||||
var debg bool
|
var debg bool
|
||||||
|
/* 注释处已移动至 control/web
|
||||||
// 解析命令行参数,输入 `-g` 即可启用 gui
|
// 解析命令行参数,输入 `-g` 即可启用 gui
|
||||||
flag.BoolVar(&en, "g", false, "Enable web gui.")
|
flag.BoolVar(&en, "g", false, "Enable web gui.")
|
||||||
|
*/
|
||||||
// 解析命令行参数,输入 `-d` 即可开启 debug log
|
// 解析命令行参数,输入 `-d` 即可开启 debug log
|
||||||
flag.BoolVar(&debg, "d", false, "Enable debug log.")
|
flag.BoolVar(&debg, "d", false, "Enable debug log.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if en {
|
|
||||||
control.InitGui()
|
|
||||||
}
|
|
||||||
if debg {
|
if debg {
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user