1
0
forked from lxh/go-wxhelper

🔥 改为前后端分离形式

This commit is contained in:
李寻欢 2024-07-04 14:10:46 +08:00
parent f39f46bfbf
commit b806ade655
49 changed files with 387 additions and 62 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ log
dist
*.log
blacklist.txt
frontend.*

View File

@ -2,6 +2,9 @@ package app
import (
"github.com/gin-gonic/gin"
"go-wechat/common/response"
"go-wechat/config"
"go-wechat/service"
)
// SaveAssistant
@ -12,3 +15,22 @@ func SaveAssistant(ctx *gin.Context) {
//ctx.String(http.StatusOK, "操作成功")
ctx.Redirect(302, "/assistant.html")
}
// GetAssistants
// @description: 获取AI助手列表
// @param ctx
func GetAssistants(ctx *gin.Context) {
records, err := service.GetAllAiAssistant()
if err != nil {
response.New(ctx).SetMsg("系统错误").SetError(err).Fail()
return
}
response.New(ctx).SetData(records).Success()
}
// GetAiModels
// @description: 获取AI模型列表
// @param ctx
func GetAiModels(ctx *gin.Context) {
response.New(ctx).SetData(config.Conf.Ai.Models).Success()
}

33
app/contact.go Normal file
View File

@ -0,0 +1,33 @@
package app
import (
"github.com/gin-gonic/gin"
"go-wechat/common/response"
"go-wechat/service"
)
// GetFriends
// @description: 获取好友列表
// @param ctx
func GetFriends(ctx *gin.Context) {
// 取出所有好友列表
friends, _, err := service.GetAllFriend()
if err != nil {
response.New(ctx).SetMsg("系统错误").SetError(err).Fail()
}
response.New(ctx).SetData(friends).Success()
}
// GetGroups
// @description: 获取群列表
// @param ctx
func GetGroups(ctx *gin.Context) {
// 取出所有好友列表
_, groups, err := service.GetAllFriend()
if err != nil {
response.New(ctx).SetMsg("系统错误").SetError(err).Fail()
}
response.New(ctx).SetData(groups).Success()
}

View File

@ -3,7 +3,7 @@ package app
import (
"github.com/gin-gonic/gin"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/model/entity"
"gorm.io/gorm"
"log"
"net/http"

View File

@ -1,7 +1,7 @@
package current
import (
"go-wechat/model"
"go-wechat/model/model"
plugin "go-wechat/plugin"
)

16
common/response/fail.go Normal file
View File

@ -0,0 +1,16 @@
package response
// Fail
// @description: 失败响应
// @receiver r
// @param data
// @return err
func (r *Response) Fail() {
if r.msg == "" {
r.msg = "系统错误"
}
if r.code == 0 {
r.code = fail
}
r.Result()
}

43
common/response/page.go Normal file
View File

@ -0,0 +1,43 @@
package response
// PageData
// @description: 分页数据通用结构体
type PageData[T any] struct {
Current int `json:"current"` // 当前页码
Size int `json:"size"` // 每页数量
Total int64 `json:"total"` // 总数
TotalPage int `json:"totalPage"` // 总页数
Records T `json:"records"` // 返回数据
}
// NewPageData
// @description: 创建分页数据
// @param records any 数据列表
// @param total int64 总数
// @param current int 页码
// @param size int 页数量
// @return data PageData[any] 分页数据
func NewPageData(records any, total int64, current, size int) (data PageData[any]) {
// 处理一下页码、页数量
if current == -1 {
current = 1
size = int(total)
}
// 计算总页码
totalPage := 0
if total > 0 {
upPage := 0
if int(total)%size > 0 {
upPage = 1
}
totalPage = (int(total) / size) + upPage
}
data = PageData[any]{
Current: current,
Size: size,
Total: total,
TotalPage: totalPage,
Records: records,
}
return
}

View File

@ -0,0 +1,81 @@
package response
import (
"github.com/gin-gonic/gin"
"go-wechat/common/validator"
"net/http"
)
// 定义状态码
const (
fail = http.StatusInternalServerError
success = http.StatusOK
)
// Response
// @description: 返回结果
type Response struct {
ctx *gin.Context
code int
data any
msg string
errMsg string
}
// New
// @description: 返回结果实现
// @param ctx
// @return Response
func New(ctx *gin.Context) *Response {
var r Response
r.ctx = ctx
return &r
}
// SetCode
// @description: 设置状态码
// @receiver r
// @param code
// @return *Response
func (r *Response) SetCode(code int) *Response {
r.code = code
return r
}
// SetData
// @description: 设置返回数据
// @receiver r
// @param data
// @return *Response
func (r *Response) SetData(data any) *Response {
r.data = data
return r
}
// SetMsg
// @description: 设置返回消息
// @receiver r
// @param msg
// @return *Response
func (r *Response) SetMsg(msg string) *Response {
r.msg = msg
return r
}
// SetError
// @description: 设置错误信息
// @receiver r
// @param err
// @return *Response
func (r *Response) SetError(err error) *Response {
if err != nil {
ne := validator.Translate(err)
if ne != nil {
r.errMsg = ne.Error()
} else {
r.errMsg = err.Error()
}
}
return r
}

27
common/response/result.go Normal file
View File

@ -0,0 +1,27 @@
package response
// Result
// @description: 响应
// @receiver r
// @param code int 状态码
// @param data any 数据
// @param msg string 消息
// @param err string 错误信息
// @return err error 返回数据错误
func (r *Response) Result() {
type resp struct {
Code int `json:"code"`
Data any `json:"data"`
Msg string `json:"message"`
ErrMsg string `json:"errMsg,omitempty"`
}
rd := resp{
r.code,
r.data,
r.msg,
r.errMsg,
}
// 返回数据
r.ctx.JSON(r.code, rd)
}

View File

@ -0,0 +1,16 @@
package response
// Success
// @description: 成功响应
// @receiver r
// @param data
// @return err
func (r *Response) Success() {
if r.msg == "" {
r.msg = "success"
}
if r.code == 0 {
r.code = success
}
r.Result()
}

View File

@ -0,0 +1,56 @@
package validator
import (
"errors"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
"log"
"strings"
)
var (
uni *ut.UniversalTranslator
validate *validator.Validate
trans ut.Translator
)
// Init
// @description: 初始化验证器
func Init() {
//注册翻译器
zhTranslator := zh.New()
uni = ut.New(zhTranslator, zhTranslator)
trans, _ = uni.GetTranslator("zh")
//获取gin的校验器
validate = binding.Validator.Engine().(*validator.Validate)
//注册翻译器
err := zhTranslations.RegisterDefaultTranslations(validate, trans)
if err != nil {
log.Panicf("注册翻译器失败:%v", err)
}
}
// Translate
// @description: 翻译错误信息
// @param err
// @return error
func Translate(err error) error {
errorMsg := make([]string, 0)
var ves validator.ValidationErrors
ok := errors.As(err, &ves)
if !ok {
return err
}
for _, e := range ves {
errorMsg = append(errorMsg, e.Translate(trans))
}
return errors.New(strings.Join(errorMsg, ""))
}

View File

@ -20,7 +20,7 @@ system:
# 微信HOOK配置
wechat:
# 微信HOOK接口地址
host: 10.0.0.73:19088
host: 10.0.0.79:19088
# 微信容器映射出来的vnc页面地址没有就不填
# vncUrl: http://192.168.1.175:19087/vnc_lite.html
# 是否在启动的时候自动设置hook服务的回调

View File

@ -2,7 +2,7 @@ package initialization
import (
"go-wechat/common/current"
"go-wechat/model"
"go-wechat/model/model"
plugin "go-wechat/plugin"
"go-wechat/plugin/plugins"
"go-wechat/service"

View File

@ -4,7 +4,7 @@ import (
"github.com/go-resty/resty/v2"
"go-wechat/common/current"
"go-wechat/config"
"go-wechat/model"
model2 "go-wechat/model/model"
"log"
)
@ -12,7 +12,7 @@ import (
// @description: 初始化微信机器人信息
func InitWechatRobotInfo() {
// 获取数据
var base model.Response[model.RobotUserInfo]
var base model2.Response[model2.RobotUserInfo]
_, err := resty.New().R().
SetHeader("Content-Type", "application/json;chartset=utf-8").
SetResult(&base).

View File

@ -163,3 +163,15 @@ func (m Message) CleanContentStartWith(prefix string) bool {
return strings.HasPrefix(content, prefix)
}
// IsInvitationJoinGroup
// @description: 是否是邀请入群消息
// @receiver m
// @return bool
func (m Message) IsInvitationJoinGroup() bool {
if m.Type == types.MsgTypeApp {
// 解析xml
}
return false
}

View File

@ -3,7 +3,7 @@ package mq
import (
"encoding/json"
"go-wechat/common/current"
"go-wechat/model"
"go-wechat/model/model"
"go-wechat/types"
"log"
"strings"

View File

@ -1,7 +1,7 @@
package plugin
import (
"go-wechat/model"
"go-wechat/model/model"
)
// MessageHandler 消息处理函数

View File

@ -8,7 +8,7 @@ import (
"go-wechat/client"
"go-wechat/common/current"
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/plugin"
"go-wechat/service"
"go-wechat/types"

View File

@ -3,7 +3,7 @@ package command
import (
"fmt"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/utils"
"log"
"strings"

View File

@ -5,10 +5,10 @@ import (
"errors"
"fmt"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/model"
"go-wechat/model/entity"
"go-wechat/model/model"
"go-wechat/model/vo"
"go-wechat/utils"
"go-wechat/vo"
"gorm.io/gorm"
"log"
"strings"

View File

@ -1,7 +1,7 @@
package plugins
import (
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/plugin"
"go-wechat/service"
"time"

View File

@ -3,7 +3,7 @@ package plugins
import (
"go-wechat/client"
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/plugin"
"go-wechat/utils"
)

View File

@ -9,31 +9,49 @@ import (
// @description: 初始化路由
// @param g
func Init(g *gin.Engine) {
g.GET("/", func(ctx *gin.Context) {
// 重定向到index.html
ctx.Redirect(302, "/index.html")
})
//g.GET("/", func(ctx *gin.Context) {
// // 重定向到index.html
// ctx.Redirect(302, "/index.html")
//})
g.GET("/index.html", app.Index) // 首页
g.GET("/friend.html", app.Friend) // 好友列表
g.GET("/group.html", app.Group) // 群组列表
g.GET("/assistant.html", app.Assistant) // AI角色
g.GET("/404.html", app.PageNotFound) // 群组列表
//g.GET("/404.html", app.PageNotFound) // 群组列表
// 接口
api := g.Group("/api")
api.PUT("/ai/status", app.ChangeEnableAiStatus) // 修改是否开启AI状态
api.POST("/ai/model", app.ChangeUseAiModel) // 修改使用的AI模型
api.POST("/ai/assistant", app.ChangeUseAiAssistant) // 修改使用的AI助手
api.PUT("/welcome/status", app.ChangeEnableWelcomeStatus) // 修改是否开启迎新状态
api.PUT("/command/status", app.ChangeEnableCommandStatus) // 修改是否开启指令状态
api.PUT("/news/status", app.ChangeEnableNewsStatus) // 修改是否开启早报状态
api.PUT("/grouprank/status", app.ChangeEnableGroupRankStatus) // 修改是否开启水群排行榜状态
api.PUT("/grouprank/skip", app.ChangeSkipGroupRankStatus) // 修改是否跳过水群排行榜状态
api.GET("/group/users", app.GetGroupUsers) // 获取群成员列表
api.PUT("/summary/status", app.ChangeEnableSummaryStatus) // 修改是否开启群聊总结状态
api.PUT("/clearmembers", app.AutoClearMembers) // 自动清理群成员
api.POST("/assistant", app.SaveAssistant) // 保存AI助手
contact := api.Group("/contact") // 通讯录
{
contact.GET("/friend", app.GetFriends) // 获取好友列表
contact.GET("/group", app.GetGroups) // 获取群组列表
profile := contact.Group("/profile") // 配置
{
profile.PUT("/ai/status", app.ChangeEnableAiStatus) // 修改是否开启AI状态
profile.POST("/ai/model", app.ChangeUseAiModel) // 修改使用的AI模型
profile.POST("/ai/assistant", app.ChangeUseAiAssistant) // 修改使用的AI助手
profile.PUT("/welcome/status", app.ChangeEnableWelcomeStatus) // 修改是否开启迎新
profile.PUT("/command/status", app.ChangeEnableCommandStatus) // 修改是否开启指令
profile.PUT("/news/status", app.ChangeEnableNewsStatus) // 修改是否开启早报
profile.PUT("/grouprank/status", app.ChangeEnableGroupRankStatus) // 修改是否开启水群排行榜
profile.PUT("/summary/status", app.ChangeEnableSummaryStatus) // 修改是否开启群聊总结
profile.PUT("/clearmembers", app.AutoClearMembers) // 自动清理群成员
}
group := contact.Group("/group") // 群组
{
group.GET("/users", app.GetGroupUsers) // 获取群成员列表
group.PUT("/grouprank/skip", app.ChangeSkipGroupRankStatus) // 修改是否跳过水群排行榜状态
}
}
system := api.Group("/system") // 系统设置
{
system.GET("/assistant", app.GetAssistants) // 获取AI助手列表
system.POST("/assistant", app.SaveAssistant) // 保存AI助手
system.GET("/models", app.GetAiModels) // 获取AI模型列表
}
}

View File

@ -2,7 +2,7 @@ package service
import (
"go-wechat/client"
"go-wechat/entity"
"go-wechat/model/entity"
)
// GetAllAiAssistant

View File

@ -2,8 +2,8 @@ package service
import (
"go-wechat/client"
"go-wechat/entity"
"go-wechat/vo"
"go-wechat/model/entity"
"go-wechat/model/vo"
"log"
"strings"
)

View File

@ -2,7 +2,7 @@ package service
import (
"go-wechat/client"
"go-wechat/vo"
"go-wechat/model/vo"
)
// GetGroupUsersByGroupId

View File

@ -2,8 +2,8 @@ package service
import (
"go-wechat/client"
"go-wechat/entity"
"go-wechat/vo"
"go-wechat/model/entity"
"go-wechat/model/vo"
"log"
"os"
"strconv"

View File

@ -3,7 +3,7 @@ package cleargroupuser
import (
"fmt"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/service"
"go-wechat/utils"
"log"

View File

@ -6,8 +6,8 @@ import (
"go-wechat/client"
"go-wechat/common/constant"
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model"
"go-wechat/model/entity"
model2 "go-wechat/model/model"
"go-wechat/utils"
"gorm.io/gorm"
"log"
@ -24,7 +24,7 @@ var hc = resty.New()
// Sync
// @description: 同步好友列表
func Sync() {
var base model.Response[[]model.FriendItem]
var base model2.Response[[]model2.FriendItem]
resp, err := hc.R().
SetHeader("Content-Type", "application/json;chartset=utf-8").
@ -155,7 +155,7 @@ func Sync() {
// @description: 同步群成员
// @param gid
func syncGroupUsers(tx *gorm.DB, gid string) {
var baseResp model.Response[model.GroupUser]
var baseResp model2.Response[model2.GroupUser]
// 组装参数
param := map[string]any{
@ -242,8 +242,8 @@ func syncGroupUsers(tx *gorm.DB, gid string) {
// @param wxid
// @return ent
// @return err
func getContactProfile(wxid string) (ent model.ContactProfile, err error) {
var baseResp model.Response[model.ContactProfile]
func getContactProfile(wxid string) (ent model2.ContactProfile, err error) {
var baseResp model2.Response[model2.ContactProfile]
// 组装参数
param := map[string]any{

View File

@ -5,9 +5,9 @@ import (
"fmt"
"github.com/sashabaranov/go-openai"
"go-wechat/config"
"go-wechat/model/vo"
"go-wechat/service"
"go-wechat/utils"
"go-wechat/vo"
"log"
"strings"
)

View File

@ -4,7 +4,7 @@ import (
"fmt"
"go-wechat/client"
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/service"
"go-wechat/utils"
"log"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"go-wechat/client"
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/service"
"go-wechat/utils"
"log"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"go-wechat/client"
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/service"
"go-wechat/utils"
"log"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"go-wechat/client"
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model/entity"
"go-wechat/service"
"go-wechat/utils"
"log"

View File

@ -3,7 +3,7 @@ package tcpserver
import (
"encoding/json"
"go-wechat/common/current"
"go-wechat/model"
"go-wechat/model/model"
"go-wechat/types"
"log"
"net"

View File

@ -6,16 +6,16 @@ import (
"errors"
"fmt"
"github.com/go-resty/resty/v2"
"go-wechat/model"
model2 "go-wechat/model/model"
"log"
)
// LeiGod
// @description: 雷神加速器相关接口
type LeiGod interface {
Login() error // 登录
Info() (model.LeiGodUserInfoResp, error) // 获取用户信息
Pause() error // 暂停加速
Login() error // 登录
Info() (model2.LeiGodUserInfoResp, error) // 获取用户信息
Pause() error // 暂停加速
}
type leiGod struct {
@ -59,7 +59,7 @@ func (l *leiGod) Login() (err error) {
}
pbs, _ := json.Marshal(param)
var loginResp model.Response[any]
var loginResp model2.Response[any]
var resp *resty.Response
res := resty.New()
@ -84,7 +84,7 @@ func (l *leiGod) Login() (err error) {
return
}
var loginInfo model.LeiGodLoginResp
var loginInfo model2.LeiGodLoginResp
if err = json.Unmarshal(bs, &loginInfo); err != nil {
return
}
@ -100,7 +100,7 @@ func (l *leiGod) Login() (err error) {
// @description: 获取用户信息
// @receiver l
// @return string
func (l *leiGod) Info() (ui model.LeiGodUserInfoResp, err error) {
func (l *leiGod) Info() (ui model2.LeiGodUserInfoResp, err error) {
// 组装参数
param := map[string]any{
"account_token": l.token,
@ -109,7 +109,7 @@ func (l *leiGod) Info() (ui model.LeiGodUserInfoResp, err error) {
}
pbs, _ := json.Marshal(param)
var userInfoResp model.Response[model.LeiGodUserInfoResp]
var userInfoResp model2.Response[model2.LeiGodUserInfoResp]
var resp *resty.Response
res := resty.New()
@ -145,7 +145,7 @@ func (l *leiGod) Pause() (err error) {
}
pbs, _ := json.Marshal(param)
var pauseResp model.Response[any]
var pauseResp model2.Response[any]
var resp *resty.Response
res := resty.New()

View File

@ -2,7 +2,7 @@ package utils
import (
"github.com/go-resty/resty/v2"
"go-wechat/model"
"go-wechat/model/model"
"log"
)