优化了api底层封装

This commit is contained in:
Sheyiyuan 2024-12-14 17:51:09 +08:00
parent 01d3de7324
commit 11bd212a1d
9 changed files with 540 additions and 33 deletions

View File

@ -9,8 +9,10 @@ import (
type apiInfo struct{}
//一、Protocol模块
/*
关于API的说明
关于Protocol模块的说明
1.所有API请求按照OneBot11标准使用JSON格式进行数据交换api命名为由原文档中蛇形命名法改为双驼峰命名法
@ -204,7 +206,7 @@ func (a *apiInfo) SetGroupCard(groupId int64, userId int64, card string) {
}
// SetGroupName 设置群名称(可能需要群主或管理员权限)
func (a apiInfo) SetGroupName(groupId int64, groupName string) {
func (a *apiInfo) SetGroupName(groupId int64, groupName string) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_name"
messageData.Params.GroupId = groupId
@ -219,7 +221,7 @@ func (a apiInfo) SetGroupName(groupId int64, groupName string) {
}
// SetGroupLeave 退出群聊
func (a apiInfo) SetGroupLeave(groupId int64, isDismiss bool) {
func (a *apiInfo) SetGroupLeave(groupId int64, isDismiss bool) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_leave"
messageData.Params.GroupId = groupId
@ -234,7 +236,7 @@ func (a apiInfo) SetGroupLeave(groupId int64, isDismiss bool) {
}
// SetGroupSpecialTitle 设置群专属头衔(需要群主权限)
func (a apiInfo) SetGroupSpecialTitle(groupId int64, userId int64, specialTitle string, duration int32) {
func (a *apiInfo) SetGroupSpecialTitle(groupId int64, userId int64, specialTitle string, duration int32) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_special_title"
messageData.Params.GroupId = groupId
@ -251,7 +253,7 @@ func (a apiInfo) SetGroupSpecialTitle(groupId int64, userId int64, specialTitle
}
// SetFriendAddRequest 处理加好友请求
func (a apiInfo) SetFriendAddRequest(flag string, approve bool, remark string) {
func (a *apiInfo) SetFriendAddRequest(flag string, approve bool, remark string) {
var messageData wba.APIRequestInfo
messageData.Action = "set_friend_add_request"
messageData.Params.Flag = flag
@ -267,7 +269,7 @@ func (a apiInfo) SetFriendAddRequest(flag string, approve bool, remark string) {
}
// SetGroupAddRequest 处理加群请求/邀请
func (a apiInfo) SetGroupAddRequest(flag string, subType string, approve bool, reason string) {
func (a *apiInfo) SetGroupAddRequest(flag string, subType string, approve bool, reason string) {
var messageData wba.APIRequestInfo
messageData.Action = "set_group_add_request"
messageData.Params.Flag = flag
@ -283,9 +285,37 @@ func (a apiInfo) SetGroupAddRequest(flag string, subType string, approve bool, r
return
}
// SetRestart 重启
func (a *apiInfo) SetRestart(delay int32) {
var messageData wba.APIRequestInfo
messageData.Action = "set_restart"
messageData.Params.Delay = delay
_, err := wsAPI(messageData)
if err != nil {
LOG.ERROR("设置重启(SetRestart)时,执行失败: %v", err)
return
}
LOG.INFO("设置重启(SetRestart):%v", delay)
return
}
// CleanCache 清理缓存
func (a *apiInfo) CleanCache() {
var messageData wba.APIRequestInfo
messageData.Action = "clean_cache"
_, err := wsAPI(messageData)
if err != nil {
LOG.ERROR("清理缓存(CleanCache)时,执行失败: %v", err)
return
}
LOG.INFO("清理缓存(CleanCache)")
return
}
// 2.有响应API需添加echo字段
func (a apiInfo) GetLoginInfo() (Response wba.APIResponseInfo) {
// GetLoginInfo 获取登录信息
func (a *apiInfo) GetLoginInfo() (Response wba.APIResponseInfo) {
LOG.INFO("获取登录信息(GetLoginInfo)")
var messageData wba.APIRequestInfo
var err error
@ -303,6 +333,401 @@ func (a apiInfo) GetLoginInfo() (Response wba.APIResponseInfo) {
return Response
}
// GetVersionInfo 获取协议信息
func (a *apiInfo) GetVersionInfo() (Response wba.APIResponseInfo) {
LOG.INFO("获取协议信息(GetVersionInfo)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_version_info"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取协议信息(GetVersionInfo)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取登录信息(GetVersionInfo)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetMsg 获取消息
func (a *apiInfo) GetMsg(messageId int32) (Response wba.APIResponseInfo) {
LOG.INFO("获取消息(GetMsg)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_msg"
messageData.Params.MessageId = messageId
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取消息(GetMsg)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取消息(GetMsg)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetForwardMsg 获取合并转发消息
func (a *apiInfo) GetForwardMsg(id string) (Response wba.APIResponseInfo) {
LOG.INFO("获取合并转发消息(GetForwardMsg)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_forward_msg"
messageData.Params.Id = id
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取合并转发消息(GetForwardMsg)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取合并转发消息(GetForwardMsg)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetStrangerInfo 获取陌生人信息
func (a *apiInfo) GetStrangerInfo(userId int64, noCache bool) (Response wba.APIResponseInfo) {
LOG.INFO("获取陌生人信息(GetStrangerInfo)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_stranger_info"
messageData.Params.UserId = userId
messageData.Params.NoCache = noCache
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取陌生人信息(GetStrangerInfo)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取陌生人信息(GetStrangerInfo)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetFriendList 获取好友列表
func (a *apiInfo) GetFriendList() (Response wba.APIResponseInfo) {
LOG.INFO("获取好友列表(GetFriendList)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_friend_list"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取好友列表(GetFriendList)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取好友列表(GetFriendList)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupList 获取群列表
func (a *apiInfo) GetGroupList() (Response wba.APIResponseInfo) {
LOG.INFO("获取群列表(GetGroupList)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_list"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取群列表(GetGroupList)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取群列表(GetGroupList)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupInfo 获取群信息
func (a *apiInfo) GetGroupInfo(groupId int64, noCache bool) (Response wba.APIResponseInfo) {
LOG.INFO("获取群信息(GetGroupInfo)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_info"
messageData.Params.GroupId = groupId
messageData.Params.NoCache = noCache
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取群信息(GetGroupInfo)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取群信息(GetGroupInfo)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupMemberInfo 获取群成员信息
func (a *apiInfo) GetGroupMemberInfo(groupId int64, userId int64, noCache bool) (Response wba.APIResponseInfo) {
LOG.INFO("获取群成员信息(GetGroupMemberInfo)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_member_info"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Params.NoCache = noCache
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取群成员信息(GetGroupMemberInfo)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取群成员信息(GetGroupMemberInfo)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupMemberList 获取群成员列表
func (a *apiInfo) GetGroupMemberList(groupId int64) (Response wba.APIResponseInfo) {
LOG.INFO("获取群成员列表(GetGroupMemberList)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_member_list"
messageData.Params.GroupId = groupId
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取群成员列表(GetGroupMemberList)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取群成员列表(GetGroupMemberList)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetGroupHonorInfo 获取群荣誉信息
func (a *apiInfo) GetGroupHonorInfo(groupId int64, userId int64) (Response wba.APIResponseInfo) {
LOG.INFO("获取群荣誉信息(GetGroupHonorInfo)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_group_honor_info"
messageData.Params.GroupId = groupId
messageData.Params.UserId = userId
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取群荣誉信息(GetGroupHonorInfo)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取群荣誉信息(GetGroupHonorInfo)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetCookies 获取Cookies
func (a *apiInfo) GetCookies(domain string) (Response wba.APIResponseInfo) {
LOG.INFO("获取Cookies(GetCookies)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_cookies"
messageData.Params.Domain = domain
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取Cookies(GetCookies)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取Cookies(GetCookies)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetCSRFToken 获取CSRF Token
func (a *apiInfo) GetCSRFToken() (Response wba.APIResponseInfo) {
LOG.INFO("获取CSRF Token(GetCSRFToken)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_csrf_token"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取CSRF Token(GetCSRFToken)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取CSRF Token(GetCSRFToken)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetCredentials 获取登录令牌
func (a *apiInfo) GetCredentials(domain string) (Response wba.APIResponseInfo) {
LOG.INFO("获取登录令牌(GetCredentials)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_credentials"
messageData.Params.Domain = domain
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取登录令牌(GetCredentials)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取登录令牌(GetCredentials)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetRecord 获取语音
func (a *apiInfo) GetRecord(file string, outFormat string) (Response wba.APIResponseInfo) {
LOG.INFO("获取语音(GetRecord)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_record"
messageData.Params.File = file
messageData.Params.OutFormat = outFormat
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取语音(GetRecord)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取语音(GetRecord)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetImage 获取图片
func (a *apiInfo) GetImage(file string) (Response wba.APIResponseInfo) {
LOG.INFO("获取图片(GetImage)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_image"
messageData.Params.File = file
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取图片(GetImage)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取图片(GetImage)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// CanSendImage 检查是否可以发送图片
func (a *apiInfo) CanSendImage() (Response wba.APIResponseInfo) {
LOG.INFO("检查是否可以发送图片(CanSendImage)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "can_send_image"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("检查是否可以发送图片(CanSendImage)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("检查是否可以发送图片(CanSendImage)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// CanSendRecord 检查是否可以发送语音
func (a *apiInfo) CanSendRecord() (Response wba.APIResponseInfo) {
LOG.INFO("检查是否可以发送语音(CanSendRecord)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "can_send_record"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("检查是否可以发送语音(CanSendRecord)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("检查是否可以发送语音(CanSendRecord)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
// GetStatus 获取状态
func (a *apiInfo) GetStatus() (Response wba.APIResponseInfo) {
LOG.INFO("获取状态(GetStatus)")
var messageData wba.APIRequestInfo
var err error
messageData.Action = "get_status"
messageData.Echo, err = GenerateUUID()
if err != nil {
LOG.ERROR("获取状态(GetStatus)时生成UUID失败: %v", err)
return wba.APIResponseInfo{}
}
Response, err = wsAPI(messageData)
if err != nil {
LOG.ERROR("获取状态(GetStatus)时,执行失败: %v", err)
return wba.APIResponseInfo{}
}
return Response
}
//二、LOG模块
/*
关于LOG模块的说明
1.日志模块使用go-logging库日志级别分为DEBUGINFOWARNERROR
2.日志模块提供LogWith方法可以自定义日志级别调用级别为DEBUG时会打印输出调用者的文件名函数名行号
3.日志模块提供Log方法默认日志级别为INFO
*/
func (a *apiInfo) LogWith(level string, content string, args ...interface{}) {
switch level {
case "DEBUG":
LOG.DEBUG(content, args...)
return
case "WARN":
LOG.WARN(content, args...)
return
case "ERROR":
LOG.ERROR(content, args...)
return
default:
LOG.INFO(content, args...)
return
}
}
func (a *apiInfo) Log(content string, args ...interface{}) {
LOG.INFO(content, args...)
}
//database模块
//TODO: 数据库模块待实现
var AppApi apiInfo
func GenerateUUID() (string, error) {

View File

@ -39,7 +39,7 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
pluginPath := filepath.Join(appsDir, file.Name())
p, err := plugin.Open(pluginPath)
if err != nil {
LOG.ERROR("Error opening app %s: %v\n", pluginPath, err)
LOG.ERROR("Error opening app %s: %v", pluginPath, err)
return 1, 0
}

View File

@ -44,7 +44,9 @@ func WebSocketHandler(protocolAddr string, token string) error {
LOG.ERROR("Close error: %v", err)
}
}(conn)
LOG.INFO("WebSocket connection to %v established.", u.String())
LOG.INFO("已连接到WebSocket服务器: %v", u.String())
ProtocolInfo := AppApi.GetVersionInfo()
LOG.INFO("协议端信息: %v-%v", ProtocolInfo.Data.AppName, ProtocolInfo.Data.AppVersion)
logInfo := AppApi.GetLoginInfo()
LOG.INFO("连接到账号: %v%v", logInfo.Data.Nickname, logInfo.Data.UserId)

12
doc/developer_doc.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>wind开发手册</title>
</head>
<body>
<h1>wind开发手册</h1>
<p>本文档将介绍wind框架的开发规范、开发流程、开发工具、开发示例等。</p>
<p>注意:如果您只是想使用本系统搭建一个骰娘或者类似的项目,请移步至<a href="./wind_chime_doc.html">wind chime</a>手册</p>
</body>
</html>

View File

@ -1,25 +1,32 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<head>
<meta charset="UTF-8">
<meta name="keywords" content="wind,bot,dice,风铃,骰子,骰娘,wind官网">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WIND</title>
</head>
<body>
<h1>WIND</h1>
<div>WIND is not<br> Dice</div>
<hr>
<div>Hello&nbsp;World!</div>
<dl>
<dt>简单</dt>
<dd>开箱即用web界面简单易懂</dd>
<dt>快捷</dt>
<dd>app一键安装一键启动一键更新</dd>
<dt>方便</dt>
<dd>开发者友好,代码开源,可定制化</dd>
<dt>小巧</dt>
<dd>体积小,启动快,占用资源少</dd>
</dl>
<span>点击<a href="https://github.com/Sheyiyuan/ProjectWIND" target="_blank">这里</a>关注我们</span>
</body>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Wind</h1>
<p>Wind is not dice</p>
<p></p>
<a href="#" class="button">查看手册</a>
<a href="#" class="button">GitHub</a>
</header>
<div class="container">
<div class="card">
<h2>开箱即用</h2>
<p>多种部署方式,快速部署于 Windows/Linux/macOS 等主流架构平台。</p>
</div>
<div class="card">
<h2>内存轻量</h2>
<p>不依赖框架加载,不依赖 Electron内存占用低至 50-100 MB。</p>
</div>
<div class="card">
<h2>适配快速</h2>
<p>使用web ui可视化配置远程管理一键部署。</p>
</div>
</div>
</body>
</html>

43
doc/styles.css Normal file
View File

@ -0,0 +1,43 @@
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
margin: 0;
padding: 0;
}
header {
text-align: center;
padding: 20px;
}
h1 {
color: #333;
font-size: 36px;
margin: 0;
}
p {
color: #555;
font-size: 18px;
}
.container {
display: flex;
justify-content: center;
margin-top: 40px;
gap: 20px;
}
.card {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
width: 250px;
}
.button {
background-color: #ee82ee;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 5px;
text-decoration: none;
}

12
doc/wind_chime_doc.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wind Chime 使用说明</title>
</head>
<body>
<h1>风铃(Wind Chime)TRPG辅助系统使用手册</h1>
<h2>什么是风铃?</h2>
<p>风铃是一款TRPG辅助系统它可以帮助你在线上社交平台完成跑团</p>
</body>
</html>

View File

@ -297,9 +297,9 @@ func startProtocol() {
func AutoSave() {
for {
time.Sleep(time.Second * 60)
LOG.INFO("自动保存")
//TODO: 这里要添加自动保存的代码
time.Sleep(time.Second * 60)
}
}

View File

@ -28,6 +28,9 @@ type WindAPI interface {
SetFriendAddRequest(flag string, approve bool, remark string)
SetGroupAddRequest(flag string, subType string, approve bool, reason string)
GetLoginInfo() APIResponseInfo
GetVersionInfo() APIResponseInfo
LogWith(level string, log string, args ...interface{})
Log(log string, args ...interface{})
}
type AppInfo struct {
@ -279,6 +282,9 @@ type ParamsInfo struct {
NoCache bool `json:"no_cache,omitempty"`
File string `json:"file,omitempty"`
Times int `json:"times,omitempty"`
Domain string `json:"domain,omitempty"`
OutFormat string `json:"out_format,omitempty"`
Delay int32 `json:"delay,omitempty"`
}
type APIRequestInfo struct {