forked from lxh/go-wxhelper
🎉 基础框架搭建完成
This commit is contained in:
commit
735d4d1f27
21
.editorconfig
Normal file
21
.editorconfig
Normal file
@ -0,0 +1,21 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
insert_final_newline = true
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
|
||||
[*.md]
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
eclint_indent_style = unset
|
||||
|
||||
[Dockerfile]
|
||||
indent_size = 4
|
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
.idea
|
||||
vendor
|
||||
logs
|
||||
*build*
|
||||
debug
|
||||
dist
|
||||
|
||||
*_deleted*
|
||||
|
||||
config.yaml
|
18
Dockerfile
Normal file
18
Dockerfile
Normal file
@ -0,0 +1,18 @@
|
||||
FROM golang:alpine as builder
|
||||
|
||||
WORKDIR /builder
|
||||
COPY . .
|
||||
RUN go mod download
|
||||
RUN go build -o app
|
||||
RUN ls -lh && chmod +x ./app
|
||||
|
||||
FROM code.hyxc1.com/open/alpine:3.16.0 as runner
|
||||
LABEL org.opencontainers.image.authors="lxh@cxh.cn"
|
||||
|
||||
# 定义一下版本号
|
||||
ARG APP_VER
|
||||
ENV APP_VER=${APP_VER}
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /builder/app ./app
|
||||
CMD ./app
|
34
api/admin/login/captcha.go
Normal file
34
api/admin/login/captcha.go
Normal file
@ -0,0 +1,34 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"wechat-robot/model/vo/login"
|
||||
"wechat-robot/pkg/captcha"
|
||||
"wechat-robot/pkg/response"
|
||||
)
|
||||
|
||||
// GetImgCaptcha
|
||||
// @description: 获取图片验证码
|
||||
// @param ctx
|
||||
func GetImgCaptcha(ctx *gin.Context) {
|
||||
store := new(captcha.RedisStore)
|
||||
math := base64Captcha.DriverMath{
|
||||
Height: 60,
|
||||
Width: 240,
|
||||
Fonts: []string{"ApothecaryFont.ttf"},
|
||||
}
|
||||
|
||||
driver := math.ConvertFonts()
|
||||
|
||||
// 验证码生成器
|
||||
c := base64Captcha.NewCaptcha(driver, store)
|
||||
id, imgStr, _, err := c.Generate()
|
||||
if err != nil {
|
||||
response.New(ctx).SetMsg("验证码生成失败").Fail()
|
||||
return
|
||||
}
|
||||
// 数据返回
|
||||
result := login.CaptchaCode{Id: id, Img: imgStr}
|
||||
response.New(ctx).SetData(result).Success()
|
||||
}
|
120
api/admin/login/login.go
Normal file
120
api/admin/login/login.go
Normal file
@ -0,0 +1,120 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"wechat-robot/internal/redis"
|
||||
"wechat-robot/model/param/login"
|
||||
"wechat-robot/pkg/auth"
|
||||
"wechat-robot/pkg/captcha"
|
||||
"wechat-robot/pkg/response"
|
||||
userService "wechat-robot/service/adminuser"
|
||||
)
|
||||
|
||||
// Login
|
||||
// @description: 登录
|
||||
// @param ctx
|
||||
// @return err
|
||||
func Login(ctx *gin.Context) {
|
||||
log.Debugf("收到登录请求")
|
||||
var p login.GetTokenWithPassword
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
response.New(ctx).SetMsg("参数错误").SetError(err).Fail()
|
||||
return
|
||||
}
|
||||
|
||||
// 验证验证码是否正确
|
||||
if !new(captcha.RedisStore).Verify(p.VerifyId, p.VerifyCode, true) {
|
||||
response.New(ctx).SetMsg("验证码错误").Fail()
|
||||
return
|
||||
}
|
||||
|
||||
// 重写参数
|
||||
ctx.Request.Form = url.Values{
|
||||
"username": {p.Username},
|
||||
"password": {p.Password},
|
||||
"scope": {"ALL"},
|
||||
"grant_type": {"password"},
|
||||
}
|
||||
// 参数解析成功,进行登录
|
||||
if err := auth.OAuthServer.HandleTokenRequest(ctx.Writer, ctx.Request); err != nil {
|
||||
log.Errorf("登录失败:%s", err.Error())
|
||||
response.New(ctx).SetMsg("系统错误").SetError(err).Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.Writer.Status() == http.StatusOK {
|
||||
go userService.UpdateLastLoginInfo(p.Username, ctx.ClientIP())
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh
|
||||
// @description: 刷新Token
|
||||
// @param ctx
|
||||
func Refresh(ctx *gin.Context) {
|
||||
var p login.RefreshToken
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
response.New(ctx).SetMsg("参数错误").SetError(err).Fail()
|
||||
return
|
||||
}
|
||||
|
||||
// 取出用户Id
|
||||
userId := auth.GetUserIdWithRefreshToken(p.RefreshToken)
|
||||
|
||||
// 重写参数
|
||||
ctx.Request.Form = url.Values{
|
||||
"refresh_token": {p.RefreshToken},
|
||||
"grant_type": {"refresh_token"},
|
||||
}
|
||||
|
||||
// 刷新Token
|
||||
if err := auth.OAuthServer.HandleTokenRequest(ctx.Writer, ctx.Request); err != nil {
|
||||
log.Errorf("Token数据返回失败: %v", err.Error())
|
||||
response.New(ctx).SetMsg("系统错误").Fail()
|
||||
}
|
||||
|
||||
// 登录成功才更新登录时间
|
||||
if ctx.Writer.Status() == http.StatusOK {
|
||||
// 登录成功,更新登录时间和IP
|
||||
go userService.UpdateLastLoginInfo(userId, ctx.ClientIP())
|
||||
}
|
||||
}
|
||||
|
||||
// Logout
|
||||
// @description: 退出登录
|
||||
// @param ctx
|
||||
func Logout(ctx *gin.Context) {
|
||||
log.Debug("退出登录啦")
|
||||
// Token字符串前缀
|
||||
const bearerSchema string = "Bearer "
|
||||
// 取出Token
|
||||
tokenHeader := ctx.GetHeader("Authorization")
|
||||
tokenStr := tokenHeader[len(bearerSchema):]
|
||||
// 取出原始RedisKey
|
||||
baseDataId, err := redis.Client.Get(context.Background(), "oauth:token:"+tokenStr).Result()
|
||||
if err != nil {
|
||||
response.New(ctx).SetMsg("Token信息获取失败").Fail()
|
||||
return
|
||||
}
|
||||
baseDataStr, err := redis.Client.Get(context.Background(), "oauth:token:"+baseDataId).Result()
|
||||
if err != nil {
|
||||
response.New(ctx).SetMsg("Token信息获取失败").Fail()
|
||||
return
|
||||
}
|
||||
// 转换数据为Map
|
||||
tokenData := make(map[string]interface{})
|
||||
if err = json.Unmarshal([]byte(baseDataStr), &tokenData); err != nil {
|
||||
response.New(ctx).SetMsg("系统错误").SetError(err).Fail()
|
||||
return
|
||||
}
|
||||
// 删除Redis缓存的数据
|
||||
redis.Client.Del(context.Background(), "oauth:token:"+baseDataId)
|
||||
redis.Client.Del(context.Background(), "oauth:token:"+tokenData["Access"].(string))
|
||||
redis.Client.Del(context.Background(), "oauth:token:"+tokenData["Refresh"].(string))
|
||||
|
||||
response.New(ctx).Success()
|
||||
}
|
1
api/admin/menu/menu.go
Normal file
1
api/admin/menu/menu.go
Normal file
@ -0,0 +1 @@
|
||||
package menu
|
23
api/admin/menu/user.go
Normal file
23
api/admin/menu/user.go
Normal file
@ -0,0 +1,23 @@
|
||||
package menu
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"wechat-robot/pkg/response"
|
||||
"wechat-robot/service/menu"
|
||||
)
|
||||
|
||||
// GetUserMenuTree
|
||||
// @description: 获取用户权限内的菜单树
|
||||
// @param ctx
|
||||
func GetUserMenuTree(ctx *gin.Context) {
|
||||
// 获取用户Id
|
||||
userId := ctx.GetString("userId")
|
||||
// 获取菜单树
|
||||
tree, err := menu.GetUserMenus(userId)
|
||||
if err != nil {
|
||||
response.New(ctx).SetMsg("系统错误").SetError(err).Fail()
|
||||
return
|
||||
}
|
||||
// 返回数据
|
||||
response.New(ctx).SetData(tree).Success()
|
||||
}
|
1
api/admin/role/role.go
Normal file
1
api/admin/role/role.go
Normal file
@ -0,0 +1 @@
|
||||
package role
|
18
api/callback/hook.go
Normal file
18
api/callback/hook.go
Normal file
@ -0,0 +1,18 @@
|
||||
package callback
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"wechat-robot/model/param/callback"
|
||||
"wechat-robot/pkg/response"
|
||||
)
|
||||
|
||||
// RobotHookNotify
|
||||
// @description: 机器人HOOK后回调
|
||||
// @param ctx
|
||||
func RobotHookNotify(ctx *gin.Context) {
|
||||
var param callback.RobotHook
|
||||
if err := ctx.ShouldBind(¶m); err != nil {
|
||||
response.New(ctx).SetError(err).Fail()
|
||||
return
|
||||
}
|
||||
}
|
24
config.yaml.example
Normal file
24
config.yaml.example
Normal file
@ -0,0 +1,24 @@
|
||||
# 数据库配置
|
||||
database:
|
||||
type: mysql # 使用的数据库类型,可选 mysql | postgresql
|
||||
host: mysql # 数据库地址(使用docker-compose启动可以不用改)
|
||||
port: 3306 # 数据库端口
|
||||
username: pixiu # 数据库用户名
|
||||
password: pixiu # 数据库密码
|
||||
database: pixiu # 数据库名
|
||||
|
||||
# Redis配置
|
||||
redis:
|
||||
host: redis # Redis地址(使用docker-compose启动可以不用改)
|
||||
port: 6379 # Redis端口
|
||||
password: mNhgeSk32fUf69C6
|
||||
db: 0
|
||||
|
||||
# AI回复
|
||||
ai:
|
||||
# 模型,不填默认gpt-3.5-turbo-0613
|
||||
model: gpt-3.5-turbo-0613
|
||||
# 接口代理域名,不填默认ChatGPT官方地址
|
||||
baseUrl: https://sxxx
|
||||
# 人设
|
||||
personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。
|
9
config/ai.go
Normal file
9
config/ai.go
Normal file
@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
// ai
|
||||
// @description: AI配置
|
||||
type ai struct {
|
||||
Model string `json:"model" yaml:"model"` // 模型
|
||||
BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址
|
||||
Personality string `json:"personality" yaml:"personality"` // 默认人设
|
||||
}
|
12
config/config.go
Normal file
12
config/config.go
Normal file
@ -0,0 +1,12 @@
|
||||
package config
|
||||
|
||||
// Conf 配置
|
||||
var Conf conf
|
||||
|
||||
// Config
|
||||
// @description: 配置
|
||||
type conf struct {
|
||||
Database db `json:"database" yaml:"database"` // 数据库 配置
|
||||
Redis redis `json:"redis" yaml:"redis"` // Redis 配置
|
||||
Ai ai `json:"ai" yaml:"ai"` // AI配置
|
||||
}
|
32
config/db.go
Normal file
32
config/db.go
Normal file
@ -0,0 +1,32 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
// db
|
||||
// @description: 数据库配置
|
||||
type db struct {
|
||||
Type string `json:"type" yaml:"type" mapstructure:"type"` // 数据库类型
|
||||
Host string `json:"host" yaml:"host" mapstructure:"host"` // 数据库地址
|
||||
Port int `json:"port" yaml:"port" mapstructure:"port"` // 数据库端口
|
||||
Username string `json:"username" yaml:"username" mapstructure:"username"` // 数据库用户名
|
||||
Password string `json:"password" yaml:"password" mapstructure:"password"` // 数据库密码
|
||||
Database string `json:"database" yaml:"database" mapstructure:"database"` // 数据库名称
|
||||
}
|
||||
|
||||
// GetMysqlDSN
|
||||
// @description: 获取MySQL连接DSN
|
||||
// @receiver d
|
||||
// @return string
|
||||
func (d db) GetMysqlDSN() string {
|
||||
return fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
d.Username, d.Password, d.Host, d.Port, d.Database)
|
||||
}
|
||||
|
||||
// GetPostgreSQLDSN
|
||||
// @description: 获取PostgreSQL连接DSN
|
||||
// @receiver d
|
||||
// @return string
|
||||
func (d db) GetPostgreSQLDSN() string {
|
||||
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
|
||||
d.Host, d.Username, d.Password, d.Database, d.Port)
|
||||
}
|
15
config/redis.go
Normal file
15
config/redis.go
Normal file
@ -0,0 +1,15 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Redis配置
|
||||
type redis struct {
|
||||
Host string `mapstructure:"host" yaml:"host"` // 主机
|
||||
Port int `mapstructure:"port" yaml:"port"` // 端口
|
||||
Password string `mapstructure:"password" yaml:"password"` // 密码
|
||||
Db int `mapstructure:"db" yaml:"db"` // 数据库名称
|
||||
}
|
||||
|
||||
func (r redis) GetDSN() string {
|
||||
return fmt.Sprintf("%s:%v", r.Host, r.Port)
|
||||
}
|
110
go.mod
Normal file
110
go.mod
Normal file
@ -0,0 +1,110 @@
|
||||
module wechat-robot
|
||||
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
gitee.ltd/lxh/logger v1.0.15
|
||||
github.com/fsnotify/fsnotify v1.7.0
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/go-co-op/gocron v1.37.0
|
||||
github.com/go-oauth2/oauth2/v4 v4.5.2
|
||||
github.com/go-oauth2/redis/v4 v4.1.1
|
||||
github.com/go-playground/locales v0.14.1
|
||||
github.com/go-playground/universal-translator v0.18.1
|
||||
github.com/go-playground/validator/v10 v10.17.0
|
||||
github.com/go-redis/redis/v8 v8.0.0-beta.5
|
||||
github.com/google/uuid v1.5.0
|
||||
github.com/mojocn/base64Captcha v1.3.6
|
||||
github.com/spf13/viper v1.18.2
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1
|
||||
golang.org/x/crypto v0.16.0
|
||||
gorm.io/driver/mysql v1.5.2
|
||||
gorm.io/driver/postgres v1.5.4
|
||||
gorm.io/gorm v1.25.5
|
||||
gorm.io/plugin/soft_delete v1.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bytedance/sonic v1.9.1 // indirect
|
||||
github.com/caarlos0/env/v6 v6.10.1 // indirect
|
||||
github.com/cespare/xxhash v1.1.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200609043717-5ab96a526299 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-kit/kit v0.12.0 // indirect
|
||||
github.com/go-kit/log v0.2.1 // indirect
|
||||
github.com/go-logfmt/logfmt v0.5.1 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/jpillora/backoff v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/lixh00/loki-client-go v1.0.1 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/prometheus/client_golang v1.13.0 // indirect
|
||||
github.com/prometheus/client_model v0.3.0 // indirect
|
||||
github.com/prometheus/common v0.37.0 // indirect
|
||||
github.com/prometheus/procfs v0.8.0 // indirect
|
||||
github.com/prometheus/prometheus v1.8.2-0.20201028100903-3245b3267b24 // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/cast v1.6.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/tidwall/btree v0.0.0-20191029221954-400434d76274 // indirect
|
||||
github.com/tidwall/buntdb v1.1.2 // indirect
|
||||
github.com/tidwall/gjson v1.12.1 // indirect
|
||||
github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e // indirect
|
||||
github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
go.opentelemetry.io/otel v0.6.0 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
go.uber.org/zap v1.23.0 // indirect
|
||||
golang.org/x/arch v0.3.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/image v0.13.0 // indirect
|
||||
golang.org/x/net v0.19.0 // indirect
|
||||
golang.org/x/oauth2 v0.15.0 // indirect
|
||||
golang.org/x/sys v0.15.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
|
||||
google.golang.org/grpc v1.59.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
49
internal/database/database.go
Normal file
49
internal/database/database.go
Normal file
@ -0,0 +1,49 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"strconv"
|
||||
"wechat-robot/config"
|
||||
)
|
||||
|
||||
// Client 客户端
|
||||
var Client *gorm.DB
|
||||
|
||||
// Init
|
||||
// @description: 初始化数据库连接
|
||||
func Init() {
|
||||
var dialector gorm.Dialector
|
||||
switch config.Conf.Database.Type {
|
||||
case "mysql":
|
||||
// MySQL
|
||||
dialector = mysql.Open(config.Conf.Database.GetMysqlDSN())
|
||||
case "postgresql":
|
||||
// PostgreSQL
|
||||
dialector = postgres.Open(config.Conf.Database.GetPostgreSQLDSN())
|
||||
default:
|
||||
log.Panic("未配置数据库或数据库类型不支持")
|
||||
}
|
||||
|
||||
// gorm 配置
|
||||
gormConfig := gorm.Config{
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
}
|
||||
// 是否开启调试模式
|
||||
if flag, _ := strconv.ParseBool(os.Getenv("GORM_DEBUG")); flag {
|
||||
gormConfig.Logger = logger.DefaultGormLogger()
|
||||
}
|
||||
|
||||
// 初始化连接
|
||||
conn, err := gorm.Open(dialector, &gormConfig)
|
||||
if err != nil {
|
||||
log.Panicf("数据库连接初始化失败: %v", err)
|
||||
} else {
|
||||
log.Debug("数据库连接初始化成功")
|
||||
}
|
||||
Client = conn
|
||||
}
|
72
internal/initialize/adminuser.go
Normal file
72
internal/initialize/adminuser.go
Normal file
@ -0,0 +1,72 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"gorm.io/gorm"
|
||||
"wechat-robot/internal/database"
|
||||
"wechat-robot/model/entity"
|
||||
"wechat-robot/utils"
|
||||
)
|
||||
|
||||
// initDefaultAdminUser
|
||||
// @description: 初始化默认后台用户
|
||||
func initDefaultAdminUser() {
|
||||
// 如果数据库没有有效用户,初始化一个默认账号
|
||||
var count int64
|
||||
if err := database.Client.Model(entity.AdminUser{}).Count(&count).Error; err != nil {
|
||||
log.Panicf("初始化默认账号失败: %s", err.Error())
|
||||
}
|
||||
if count > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
var err error
|
||||
tx := database.Client.Begin()
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
tx.Rollback()
|
||||
} else {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
} else {
|
||||
tx.Commit()
|
||||
}
|
||||
}
|
||||
}()
|
||||
// 判断角色是否存在
|
||||
var role entity.Role
|
||||
if err = tx.Where("code = ?", "admin").First(&role).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Panicf("初始化默认账号失败: %s", err.Error())
|
||||
return
|
||||
}
|
||||
// 如果角色不存在,创建角色
|
||||
if role.Id == "" {
|
||||
role.Name = "超级管理员"
|
||||
role.Code = "admin"
|
||||
role.Describe = "系统默认超级管理员"
|
||||
if err = tx.Create(&role).Error; err != nil {
|
||||
log.Panicf("初始化默认账号失败: %s", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
var adminUser entity.AdminUser
|
||||
adminUser.Username = "admin"
|
||||
adminUser.Password = "admin123"
|
||||
utils.PasswordUtils().HashPassword(&adminUser.Password)
|
||||
if err = tx.Create(&adminUser).Error; err != nil {
|
||||
log.Panicf("初始化默认账号失败: %s", err.Error())
|
||||
return
|
||||
}
|
||||
// 绑定角色
|
||||
var userRole entity.AdminUserRole
|
||||
userRole.RoleId = role.Id
|
||||
userRole.UserId = adminUser.Id
|
||||
if err = tx.Create(&userRole).Error; err != nil {
|
||||
log.Panicf("初始化默认账号失败: %s", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
47
internal/initialize/config.go
Normal file
47
internal/initialize/config.go
Normal file
@ -0,0 +1,47 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
"wechat-robot/config"
|
||||
"wechat-robot/internal/database"
|
||||
"wechat-robot/internal/redis"
|
||||
)
|
||||
|
||||
// 配置管理工具
|
||||
var vp *viper.Viper
|
||||
|
||||
// Config
|
||||
// @description: 初始化配置
|
||||
func initConfig() {
|
||||
vp = viper.New()
|
||||
vp.AddConfigPath(".") // 设置配置文件路径
|
||||
vp.SetConfigName("config") // 设置配置文件名
|
||||
vp.SetConfigType("yaml") // 设置配置文件类型
|
||||
// 读取配置文件
|
||||
if err := vp.ReadInConfig(); err != nil {
|
||||
log.Panicf("读取配置文件失败: %v", err)
|
||||
}
|
||||
// 绑定配置文件
|
||||
if err := vp.Unmarshal(&config.Conf); err != nil {
|
||||
log.Panicf("配置文件解析失败: %v", err)
|
||||
}
|
||||
log.Debugf("配置文件解析完成: %+v", config.Conf)
|
||||
// 初始化数据库连接
|
||||
database.Init()
|
||||
redis.Init()
|
||||
|
||||
// 下面的代码是配置变动之后自动刷新的
|
||||
vp.WatchConfig()
|
||||
vp.OnConfigChange(func(e fsnotify.Event) {
|
||||
// 绑定配置文件
|
||||
if err := vp.Unmarshal(&config.Conf); err != nil {
|
||||
log.Errorf("配置文件更新失败: %v", err)
|
||||
} else {
|
||||
// 初始化数据库连接
|
||||
database.Init()
|
||||
redis.Init()
|
||||
}
|
||||
})
|
||||
}
|
25
internal/initialize/datatable.go
Normal file
25
internal/initialize/datatable.go
Normal file
@ -0,0 +1,25 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"wechat-robot/internal/database"
|
||||
"wechat-robot/model/entity"
|
||||
)
|
||||
|
||||
// databaseTable
|
||||
// @description: 初始化数据库表
|
||||
func databaseTable() {
|
||||
tables := []any{
|
||||
new(entity.AdminUser), // 用户表
|
||||
new(entity.Menu), // 菜单表
|
||||
new(entity.Role), // 角色表
|
||||
new(entity.RoleMenu), // 角色菜单表
|
||||
new(entity.AdminUserRole), // 用户角色表
|
||||
new(entity.Robot), // 机器人表
|
||||
}
|
||||
|
||||
// 同步表结构
|
||||
if err := database.Client.AutoMigrate(tables...); err != nil {
|
||||
log.Panicf("初始化数据库表失败: %v", err)
|
||||
}
|
||||
}
|
16
internal/initialize/init.go
Normal file
16
internal/initialize/init.go
Normal file
@ -0,0 +1,16 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"wechat-robot/internal/tasks"
|
||||
"wechat-robot/pkg/auth"
|
||||
)
|
||||
|
||||
// InitSystem
|
||||
// @description: 初始化系统
|
||||
func InitSystem() {
|
||||
initConfig() // 初始化配置
|
||||
databaseTable() // 初始化数据库表
|
||||
initDefaultAdminUser() // 初始化默认管理员用户
|
||||
auth.InitOAuth2Server() // 初始化OAuth2服务
|
||||
tasks.StartScheduled() // 启动定时任务
|
||||
}
|
30
internal/orm/page.go
Normal file
30
internal/orm/page.go
Normal file
@ -0,0 +1,30 @@
|
||||
package orm
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Page
|
||||
// @description: 分页组件
|
||||
// @param current
|
||||
// @param size
|
||||
// @return func(db *gorm.DB) *gorm.DB
|
||||
func Page(current, size int) func(db *gorm.DB) *gorm.DB {
|
||||
// 如果页码是-1,就不分页
|
||||
if current == -1 {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
return db
|
||||
}
|
||||
}
|
||||
// 分页
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if current == 0 {
|
||||
current = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
// 计算偏移量
|
||||
offset := (current - 1) * size
|
||||
// 返回组装结果
|
||||
return db.Offset(offset).Limit(size)
|
||||
}
|
||||
}
|
28
internal/redis/redis.go
Normal file
28
internal/redis/redis.go
Normal file
@ -0,0 +1,28 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"wechat-robot/config"
|
||||
)
|
||||
|
||||
var Client *redis.Client
|
||||
|
||||
// Init
|
||||
// @description: 初始化redis客户端
|
||||
func Init() {
|
||||
conf := config.Conf.Redis
|
||||
// 初始化连接
|
||||
conn := redis.NewClient(&redis.Options{
|
||||
Addr: conf.GetDSN(),
|
||||
Password: conf.Password,
|
||||
DB: conf.Db,
|
||||
})
|
||||
if err := conn.Ping(context.Background()).Err(); err != nil {
|
||||
log.Panicf("Redis连接初始化失败: %v", err)
|
||||
} else {
|
||||
log.Debug("Redis连接初始化成功")
|
||||
}
|
||||
Client = conn
|
||||
}
|
20
internal/tasks/tasks.go
Normal file
20
internal/tasks/tasks.go
Normal file
@ -0,0 +1,20 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"github.com/go-co-op/gocron"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Scheduler *gocron.Scheduler
|
||||
|
||||
// StartScheduled
|
||||
// @description: 启动定时任务
|
||||
func StartScheduled() {
|
||||
// 定时任务发送消息
|
||||
Scheduler = gocron.NewScheduler(time.Local)
|
||||
|
||||
// 开启定时任务
|
||||
Scheduler.StartAsync()
|
||||
log.Println("定时任务初始化成功")
|
||||
}
|
50
main.go
Normal file
50
main.go
Normal file
@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"wechat-robot/internal/initialize"
|
||||
"wechat-robot/pkg/validator"
|
||||
"wechat-robot/router/admin"
|
||||
"wechat-robot/router/callback"
|
||||
"wechat-robot/router/middleware"
|
||||
"wechat-robot/tcpserver"
|
||||
)
|
||||
|
||||
// init
|
||||
// @description: 初始系统
|
||||
func init() {
|
||||
// 初始化日志工具
|
||||
logger.InitLogger(logger.LogConfig{Mode: logger.Dev, LokiEnable: false, FileEnable: true})
|
||||
// 初始化系统
|
||||
initialize.InitSystem()
|
||||
}
|
||||
|
||||
// main
|
||||
// @description: 启动入口
|
||||
func main() {
|
||||
// 启动TCP服务
|
||||
go tcpserver.Start()
|
||||
|
||||
// 注册参数绑定错误信息翻译器
|
||||
validator.Init()
|
||||
app := gin.Default()
|
||||
|
||||
// 开启自定义请求方式不允许处理函数
|
||||
app.HandleMethodNotAllowed = true
|
||||
// 处理请求方式不对
|
||||
app.NoMethod(middleware.NoMethodHandler())
|
||||
// 404返回数据
|
||||
app.NoRoute(middleware.NoRouteHandler())
|
||||
|
||||
// 初始化接口路由
|
||||
admin.InitRoute(app.Group("/admin/v1")) // 后台接口
|
||||
callback.InitRoute(app.Group("/callback")) // 回调接口
|
||||
|
||||
// 启动服务
|
||||
if err := app.Run(":8080"); err != nil {
|
||||
log.Errorf("服务启动失败:%v", err)
|
||||
return
|
||||
}
|
||||
}
|
23
model/entity/adminuser.go
Normal file
23
model/entity/adminuser.go
Normal file
@ -0,0 +1,23 @@
|
||||
package entity
|
||||
|
||||
import "wechat-robot/pkg/types"
|
||||
|
||||
// AdminUser
|
||||
// @description: 用户表
|
||||
type AdminUser struct {
|
||||
types.BaseDbModel
|
||||
Username string `json:"username" gorm:"index:deleted,unique;type:varchar(255); not null; comment:'登录账号'"`
|
||||
Password string `json:"password" gorm:"type:varchar(255); comment:'密码'"`
|
||||
Email *string `json:"email" gorm:"type:varchar(255); comment:'邮箱'"`
|
||||
IsVerified bool `json:"isVerified" gorm:"type:tinyint(1); not null; default:0; comment:'是否验证邮箱'"`
|
||||
LastLoginAt *types.DateTime `json:"lastLoginAt" gorm:"comment:'最后登录时间'"`
|
||||
LastLoginIp *string `json:"lastLoginIp" gorm:"type:varchar(255); comment:'最后登录ip'"`
|
||||
}
|
||||
|
||||
// TableName
|
||||
// @description: 表名
|
||||
// @receiver User
|
||||
// @return string
|
||||
func (AdminUser) TableName() string {
|
||||
return "t_admin_user"
|
||||
}
|
23
model/entity/menu.go
Normal file
23
model/entity/menu.go
Normal file
@ -0,0 +1,23 @@
|
||||
package entity
|
||||
|
||||
import "wechat-robot/pkg/types"
|
||||
|
||||
// Menu
|
||||
// @description: 菜单表
|
||||
type Menu struct {
|
||||
types.BaseDbModel
|
||||
Type types.MenuType `json:"type" gorm:"type:enum('MENU','BUTTON'); default:'MENU'; not null; comment:'菜单类型(菜单或按钮)'"`
|
||||
Name string `json:"name" gorm:"type:varchar(255);not null;comment:'菜单名称'"`
|
||||
Path string `json:"path" gorm:"type:varchar(255);comment:'路径'"`
|
||||
Icon string `json:"icon" gorm:"type:varchar(255);comment:'菜单图标'"`
|
||||
Sort int `json:"sort" gorm:"type:int(3);not null;comment:'排序值(数字越小约靠前)'"`
|
||||
ParentId *string `json:"parentId" gorm:"type:varchar(32);comment:'父级菜单ID(0表示顶级菜单)'"`
|
||||
}
|
||||
|
||||
// TableName
|
||||
// @description: 表名
|
||||
// @receiver Menu
|
||||
// @return string
|
||||
func (Menu) TableName() string {
|
||||
return "t_menu"
|
||||
}
|
26
model/entity/robot.go
Normal file
26
model/entity/robot.go
Normal file
@ -0,0 +1,26 @@
|
||||
package entity
|
||||
|
||||
import "wechat-robot/pkg/types"
|
||||
|
||||
// Robot
|
||||
// @description: 机器人信息
|
||||
type Robot struct {
|
||||
types.BaseDbModel
|
||||
WxId string `json:"wxid" gorm:"index:deleted,unique;column:wxid;type:varchar(255);not null;comment:'微信Id'"` // 微信Id
|
||||
Account string `json:"account" gorm:"type:varchar(255);comment:'微信号'"` // 微信号
|
||||
Nickname string `json:"name" gorm:"type:varchar(255);comment:'昵称'"` // 昵称
|
||||
Avatar string `json:"avatar" gorm:"type:varchar(255);comment:'头像'"` // 头像
|
||||
Mobile string `json:"mobile" gorm:"type:varchar(255);comment:'手机号'"` // 手机
|
||||
CurrentDataPath string `json:"currentDataPath" gorm:"type:varchar(255);comment:'当前数据目录'"` // 当前数据目录,登录的账号目录
|
||||
DataSavePath string `json:"dataSavePath" gorm:"type:varchar(255);comment:'微信保存目录'"` // 微信保存目录
|
||||
DbKey string `json:"dbKey" gorm:"type:varchar(255);comment:'数据库的SQLCipher的加密key'"` // 数据库的SQLCipher的加密key,可以使用该key配合decrypt.py解密数据库
|
||||
HookApi string `json:"hookApi" gorm:"type:varchar(255);comment:'hook接口地址'"` // hook接口地址
|
||||
}
|
||||
|
||||
// TableName
|
||||
// @description: 表名
|
||||
// @receiver Robot
|
||||
// @return string
|
||||
func (Robot) TableName() string {
|
||||
return "t_robot"
|
||||
}
|
56
model/entity/role.go
Normal file
56
model/entity/role.go
Normal file
@ -0,0 +1,56 @@
|
||||
package entity
|
||||
|
||||
import "wechat-robot/pkg/types"
|
||||
|
||||
// Role
|
||||
// @description: 角色表
|
||||
type Role struct {
|
||||
types.BaseDbModel
|
||||
Name string `json:"name" gorm:"type:varchar(20) not null comment '角色名称'"`
|
||||
Code string `json:"code" gorm:"type:varchar(20) not null comment '角色代码'"`
|
||||
Describe string `json:"describe" gorm:"type:varchar(20) not null comment '描述'"`
|
||||
}
|
||||
|
||||
// TableName
|
||||
// @description: 表名
|
||||
// @receiver Role
|
||||
// @return string
|
||||
func (Role) TableName() string {
|
||||
return "t_role"
|
||||
}
|
||||
|
||||
// =====================================================================================================================
|
||||
|
||||
// RoleMenu
|
||||
// @description: 角色菜单表
|
||||
type RoleMenu struct {
|
||||
types.BaseDbModelWithReal
|
||||
RoleId string `json:"roleId" gorm:"index:idx_rm_only,unique;type:varchar(32);not null;comment:'角色id'"`
|
||||
MenuId string `json:"menuId" gorm:"index:idx_rm_only,unique;type:varchar(32);not null;comment:'菜单id'"`
|
||||
}
|
||||
|
||||
// TableName
|
||||
// @description: 表名
|
||||
// @receiver RoleMenu
|
||||
// @return string
|
||||
func (RoleMenu) TableName() string {
|
||||
return "t_role_menu"
|
||||
}
|
||||
|
||||
// =====================================================================================================================
|
||||
|
||||
// AdminUserRole
|
||||
// @description: 管理员角色表
|
||||
type AdminUserRole struct {
|
||||
types.BaseDbModelWithReal
|
||||
RoleId string `json:"roleId" gorm:"index:idx_aur_only,unique;type:varchar(32);not null;comment:'角色id'"`
|
||||
UserId string `json:"menuId" gorm:"index:idx_aur_only,unique;type:varchar(32);not null;comment:'管理员id'"`
|
||||
}
|
||||
|
||||
// TableName
|
||||
// @description: 表名
|
||||
// @receiver AdminUserRole
|
||||
// @return string
|
||||
func (AdminUserRole) TableName() string {
|
||||
return "t_admin_user_role"
|
||||
}
|
8
model/param/callback/hook.go
Normal file
8
model/param/callback/hook.go
Normal file
@ -0,0 +1,8 @@
|
||||
package callback
|
||||
|
||||
// RobotHook
|
||||
// @description: 机器人HOOK后回调
|
||||
type RobotHook struct {
|
||||
Robot string `json:"robot" form:"robot"` // 机器人
|
||||
Status string `json:"status" form:"status"` // 状态
|
||||
}
|
24
model/param/login/login.go
Normal file
24
model/param/login/login.go
Normal file
@ -0,0 +1,24 @@
|
||||
package login
|
||||
|
||||
// GetTokenWithPassword
|
||||
// @description: 账号密码登录
|
||||
type GetTokenWithPassword struct {
|
||||
VerifyId string `json:"verifyId" form:"verifyId" binding:"required"` // 验证Id
|
||||
VerifyCode string `json:"verifyCode" form:"verifyCode" binding:"required"` // 验证码
|
||||
Username string `json:"username" form:"username" binding:"required"` // 用户名
|
||||
Password string `json:"password" form:"password" binding:"required"` // 密码
|
||||
}
|
||||
|
||||
// RefreshToken
|
||||
// @description: 刷新Token入参
|
||||
type RefreshToken struct {
|
||||
RefreshToken string `json:"refresh_token" form:"refresh_token" binding:"required"` // 刷新Token
|
||||
}
|
||||
|
||||
// Register
|
||||
// @description: 注册账号
|
||||
type Register struct {
|
||||
Username string `json:"username" form:"username" binding:"required"` // 用户名
|
||||
Password string `json:"password" form:"password" binding:"required"` // 密码
|
||||
InvitationCode string `json:"invitationCode" form:"invitationCode"` // 邀请码
|
||||
}
|
159
model/robot/message.go
Normal file
159
model/robot/message.go
Normal file
@ -0,0 +1,159 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"wechat-robot/pkg/types"
|
||||
)
|
||||
|
||||
// Message
|
||||
// @description: 消息
|
||||
type Message struct {
|
||||
MsgId int64 `json:"msgId"`
|
||||
CreateTime int `json:"createTime"`
|
||||
Content string `json:"content"`
|
||||
DisplayFullContent string `json:"displayFullContent"`
|
||||
FromUser string `json:"fromUser"`
|
||||
GroupUser string `json:"-"`
|
||||
MsgSequence int `json:"msgSequence"`
|
||||
Pid int `json:"pid"`
|
||||
Signature string `json:"signature"`
|
||||
ToUser string `json:"toUser"`
|
||||
Type types.MessageType `json:"type"`
|
||||
Raw string `json:"raw"`
|
||||
}
|
||||
|
||||
// systemMsgDataXml
|
||||
// @description: 微信系统消息的xml结构
|
||||
type systemMsgDataXml struct {
|
||||
SysMsg sysMsg `xml:"sysmsg"`
|
||||
Type string `xml:"type,attr"`
|
||||
}
|
||||
|
||||
// atMsgDataXml
|
||||
// @description: 微信@消息的xml结构
|
||||
type atMsgDataXml struct {
|
||||
XMLName xml.Name `xml:"msgsource"`
|
||||
Text string `xml:",chardata"`
|
||||
AtUserList string `xml:"atuserlist"`
|
||||
Silence string `xml:"silence"`
|
||||
MemberCount string `xml:"membercount"`
|
||||
Signature string `xml:"signature"`
|
||||
TmpNode struct {
|
||||
Text string `xml:",chardata"`
|
||||
PublisherID string `xml:"publisher-id"`
|
||||
} `xml:"tmp_node"`
|
||||
}
|
||||
|
||||
// sysMsg
|
||||
// @description: 消息主体
|
||||
type sysMsg struct{}
|
||||
|
||||
func (m Message) IsGroup() bool {
|
||||
return strings.HasSuffix(m.FromUser, "@chatroom")
|
||||
}
|
||||
|
||||
// IsPat
|
||||
// @description: 是否是拍一拍消息
|
||||
// @receiver m
|
||||
// @return bool
|
||||
func (m Message) IsPat() bool {
|
||||
// 解析xml
|
||||
var d systemMsgDataXml
|
||||
if err := xml.Unmarshal([]byte(m.Content), &d); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return m.Type == types.MsgTypeRecalled && d.Type == "pat"
|
||||
}
|
||||
|
||||
// IsRevokeMsg
|
||||
// @description: 是否是撤回消息
|
||||
// @receiver m
|
||||
// @return bool
|
||||
func (m Message) IsRevokeMsg() bool {
|
||||
// 解析xml
|
||||
var d systemMsgDataXml
|
||||
if err := xml.Unmarshal([]byte(m.Content), &d); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return m.Type == types.MsgTypeRecalled && d.Type == "revokemsg"
|
||||
}
|
||||
|
||||
// IsNewUserJoin
|
||||
// @description: 是否是新人入群
|
||||
// @receiver m
|
||||
// @return bool
|
||||
func (m Message) IsNewUserJoin() bool {
|
||||
sysFlag := m.Type == types.MsgTypeSys && strings.Contains(m.Content, "\"邀请\"") && strings.Contains(m.Content, "\"加入了群聊")
|
||||
if sysFlag {
|
||||
return true
|
||||
}
|
||||
// 解析另一种情况
|
||||
var d systemMsgDataXml
|
||||
if err := xml.Unmarshal([]byte(m.Content), &d); err != nil {
|
||||
return false
|
||||
}
|
||||
return m.Type == types.MsgTypeSys && d.Type == "delchatroommember"
|
||||
}
|
||||
|
||||
// IsAt
|
||||
// @description: 是否是At机器人的消息
|
||||
// @receiver m
|
||||
// @return bool
|
||||
func (m Message) IsAt() bool {
|
||||
return strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你")
|
||||
}
|
||||
|
||||
// IsAtAll
|
||||
// @description: 是否是At所有人的消息
|
||||
// @receiver m
|
||||
// @return bool
|
||||
func (m Message) IsAtAll() bool {
|
||||
// 解析raw里面的xml
|
||||
var d atMsgDataXml
|
||||
if err := xml.Unmarshal([]byte(m.Signature), &d); err != nil {
|
||||
return false
|
||||
}
|
||||
// 转换@用户列表为数组
|
||||
atUserList := strings.Split(d.AtUserList, ",")
|
||||
// 判断是否包含@所有人
|
||||
return slices.Contains(atUserList, "notify@all")
|
||||
}
|
||||
|
||||
// IsPrivateText
|
||||
// @description: 是否是私聊消息
|
||||
// @receiver m
|
||||
// @return bool
|
||||
func (m Message) IsPrivateText() bool {
|
||||
// 发信人不以@chatroom结尾且消息类型为文本
|
||||
return !strings.HasSuffix(m.FromUser, "chatroom") && m.Type == types.MsgTypeText
|
||||
}
|
||||
|
||||
// CleanContentStartWith
|
||||
// @description: 判断是否包含指定消息前缀
|
||||
// @receiver m
|
||||
// @param prefix
|
||||
// @return bool
|
||||
func (m Message) CleanContentStartWith(prefix string) bool {
|
||||
content := m.Content
|
||||
|
||||
// 如果是@消息,过滤掉@的内容
|
||||
if m.IsAt() {
|
||||
re := regexp.MustCompile(`@([^ | ]+)`)
|
||||
matches := re.FindStringSubmatch(content)
|
||||
if len(matches) > 0 {
|
||||
// 过滤掉第一个匹配到的
|
||||
content = strings.Replace(content, matches[0], "", 1)
|
||||
}
|
||||
}
|
||||
|
||||
// 去掉最前面的空格
|
||||
content = strings.TrimLeft(content, " ")
|
||||
content = strings.TrimLeft(content, " ")
|
||||
|
||||
return strings.HasPrefix(content, prefix)
|
||||
}
|
8
model/vo/login/captcha.go
Normal file
8
model/vo/login/captcha.go
Normal file
@ -0,0 +1,8 @@
|
||||
package login
|
||||
|
||||
// CaptchaCode
|
||||
// @description: 获取图形验证码返回结果
|
||||
type CaptchaCode struct {
|
||||
Id string `json:"id"` // 验证ID
|
||||
Img string `json:"img"` // 图片Base64字符串
|
||||
}
|
20
model/vo/menu/menu.go
Normal file
20
model/vo/menu/menu.go
Normal file
@ -0,0 +1,20 @@
|
||||
package menu
|
||||
|
||||
// Item
|
||||
// @description:
|
||||
type Item struct {
|
||||
Id string `json:"id"` // 菜单Id
|
||||
Path string `json:"path"` // 访问路径
|
||||
Meta ItemMeta `json:"meta"` // 元数据
|
||||
Children []Item `json:"children"` // 子菜单
|
||||
}
|
||||
|
||||
// ItemMeta
|
||||
// @description: 菜单元数据
|
||||
type ItemMeta struct {
|
||||
Title string `json:"title"` // 标题
|
||||
Icon string `json:"icon"` // 图标
|
||||
Rank int `json:"rank"` // 排序
|
||||
Roles []string `json:"roles"` // 当前菜单所属的角色代码
|
||||
Auths []string `json:"auths"` // 当前菜单包含的按钮,如果传入了用户Id,返回的将会是权限内所有的按钮
|
||||
}
|
60
pkg/auth/auth.go
Normal file
60
pkg/auth/auth.go
Normal file
@ -0,0 +1,60 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"github.com/go-oauth2/oauth2/v4"
|
||||
"github.com/go-oauth2/oauth2/v4/manage"
|
||||
"github.com/go-oauth2/oauth2/v4/models"
|
||||
"github.com/go-oauth2/oauth2/v4/server"
|
||||
"github.com/go-oauth2/oauth2/v4/store"
|
||||
od "github.com/go-oauth2/redis/v4"
|
||||
"time"
|
||||
"wechat-robot/internal/redis"
|
||||
"wechat-robot/pkg/auth/handle"
|
||||
)
|
||||
|
||||
var (
|
||||
OAuthServer *server.Server // 后台OAuth2服务
|
||||
)
|
||||
|
||||
// InitOAuth2Server
|
||||
// @description: 初始化后台OAuth2服务
|
||||
func InitOAuth2Server() {
|
||||
manager := manage.NewDefaultManager()
|
||||
// 配置信息
|
||||
cfg := &manage.Config{
|
||||
AccessTokenExp: time.Hour * 24, // 访问令牌过期时间
|
||||
RefreshTokenExp: time.Hour * 24 * 30, // 更新令牌过期时间
|
||||
IsGenerateRefresh: true, // 是否生成新的更新令牌
|
||||
}
|
||||
// 设置密码模式的配置参数
|
||||
manager.SetPasswordTokenCfg(cfg)
|
||||
|
||||
manager.MapTokenStorage(od.NewRedisStoreWithCli(redis.Client, "oauth:token:"))
|
||||
// 生成Token方式
|
||||
manager.MapAccessGenerate(handle.NewAccessGenerate())
|
||||
|
||||
// 配置客户端
|
||||
// 配置客户端
|
||||
clientStore := store.NewClientStore()
|
||||
_ = clientStore.Set("admin", &models.Client{
|
||||
ID: "admin",
|
||||
Secret: "kycgfS4sBjx2rPVD",
|
||||
})
|
||||
manager.MapClientStorage(clientStore)
|
||||
|
||||
srv := server.NewServer(server.NewConfig(), manager)
|
||||
// 设置密码登录模式处理逻辑
|
||||
srv.SetPasswordAuthorizationHandler(handle.LoginWithPassword)
|
||||
// 允许密码模式、刷新Token
|
||||
srv.SetAllowedGrantType(oauth2.PasswordCredentials, oauth2.Refreshing)
|
||||
// 客户端ID和授权模式检查
|
||||
//srv.SetClientAuthorizedHandler(handle.CheckClient)
|
||||
// 自定义响应Token的扩展字段
|
||||
srv.SetExtensionFieldsHandler(handle.ExtensionFields)
|
||||
// 自定义返回数据接口
|
||||
srv.SetResponseTokenHandler(handle.ResponseToken)
|
||||
// 自定义内部错误处理
|
||||
srv.SetInternalErrorHandler(handle.InternalErrorHandler)
|
||||
|
||||
OAuthServer = srv
|
||||
}
|
137
pkg/auth/handle/oauth2.go
Normal file
137
pkg/auth/handle/oauth2.go
Normal file
@ -0,0 +1,137 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/go-oauth2/oauth2/v4"
|
||||
"github.com/go-oauth2/oauth2/v4/errors"
|
||||
"net/http"
|
||||
"time"
|
||||
"wechat-robot/internal/database"
|
||||
"wechat-robot/model/entity"
|
||||
userService "wechat-robot/service/adminuser"
|
||||
"wechat-robot/utils"
|
||||
)
|
||||
|
||||
// LoginWithPassword
|
||||
// @description: 账号密码登录模式
|
||||
// @param _
|
||||
// @param clientId
|
||||
// @param userId
|
||||
// @param password
|
||||
// @return userID
|
||||
// @return err
|
||||
func LoginWithPassword(_ context.Context, clientId, username, password string) (userId string, err error) {
|
||||
log.Debugf("[%v]处理登录请求,账号:%s --> %s", clientId, username, password)
|
||||
|
||||
// 取出用户信息
|
||||
var userInfo entity.AdminUser
|
||||
userInfo, err = userService.GetUserWithLogin(username)
|
||||
userId = userInfo.Id
|
||||
if err != nil {
|
||||
log.Errorf("获取用户信息失败: %v", err.Error())
|
||||
err = errors.New("账号不存在")
|
||||
return
|
||||
}
|
||||
// 校验密码
|
||||
if !utils.PasswordUtils().ComparePassword(userInfo.Password, password) {
|
||||
err = errors.New("密码错误")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ExtensionFields
|
||||
// @description: 自定义响应Token的扩展字段
|
||||
// @param _
|
||||
// @return fieldsValue
|
||||
func ExtensionFields(ti oauth2.TokenInfo) (fieldsValue map[string]any) {
|
||||
fieldsValue = map[string]any{}
|
||||
fieldsValue["license"] = "Made By Lixunhuan"
|
||||
fieldsValue["developer"] = "https://lxh.io"
|
||||
fieldsValue["expires"] = time.Now().Local().Add(ti.GetAccessExpiresIn()).Format("2006/01/02 15:04:05")
|
||||
|
||||
// 缓存用户的accessToken,为后续删除做准备
|
||||
//key := fmt.Sprintf("oauth:token:token:%s", ti.GetUserID())
|
||||
//if err := redis.Client.Set(context.Background(), key, ti.GetAccess(), time.Hour*24*30).Err(); err != nil {
|
||||
// log.Errorf("缓存用户的accessToken失败: %v", err.Error())
|
||||
//}
|
||||
|
||||
// 填充用户信息
|
||||
var ui entity.AdminUser
|
||||
_ = database.Client.Take(&ui, "id = ?", ti.GetUserID()).Error
|
||||
fieldsValue["username"] = ui.Username
|
||||
fieldsValue["id"] = ui.Id
|
||||
//fieldsValue["roles"] = strings.Split(ui.Role, ",")
|
||||
return
|
||||
}
|
||||
|
||||
// ResponseToken
|
||||
// @description: 返回Token生成结果
|
||||
// @param w http.ResponseWriter 写入响应
|
||||
// @param data map[string]any 响应数据
|
||||
// @param header http.Header 响应头
|
||||
// @param statusCode ...int 响应状态
|
||||
// @return error 错误信息
|
||||
func ResponseToken(w http.ResponseWriter, data map[string]any, header http.Header, statusCode ...int) error {
|
||||
log.Debugf("返回Token原始数据: %+v", data)
|
||||
type response struct {
|
||||
Code int `json:"code"`
|
||||
Data map[string]any `json:"data"`
|
||||
Msg string `json:"message"`
|
||||
}
|
||||
|
||||
status := http.StatusOK
|
||||
msg := "login success"
|
||||
if len(statusCode) > 0 && statusCode[0] > 0 {
|
||||
status = statusCode[0]
|
||||
msg = fmt.Sprintf("%v", data["error_description"])
|
||||
// 处理特殊返回 - 刷新Token到期了
|
||||
switch data["error"] {
|
||||
case "invalid_grant":
|
||||
msg = "登录已过期,请重新授权登录"
|
||||
case "invalid_request":
|
||||
msg = "登录参数错误"
|
||||
case "invalid_client":
|
||||
msg = "客户端验证失败"
|
||||
default:
|
||||
log.Errorf("收到未定义的登录错误: %v", data["error_description"])
|
||||
}
|
||||
data = nil
|
||||
}
|
||||
|
||||
res := response{
|
||||
Code: status,
|
||||
Msg: msg,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
|
||||
for key := range header {
|
||||
w.Header().Set(key, header.Get(key))
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
_, err = w.Write(jsonBytes)
|
||||
if err != nil {
|
||||
log.Errorf("返回Token失败: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// InternalErrorHandler 自定义内部错误处理
|
||||
func InternalErrorHandler(err error) (re *errors.Response) {
|
||||
re = errors.NewResponse(err, http.StatusUnauthorized)
|
||||
re.Description = err.Error()
|
||||
return
|
||||
}
|
78
pkg/auth/handle/token_gen.go
Normal file
78
pkg/auth/handle/token_gen.go
Normal file
@ -0,0 +1,78 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/go-oauth2/oauth2/v4"
|
||||
"github.com/google/uuid"
|
||||
"strings"
|
||||
"wechat-robot/internal/redis"
|
||||
)
|
||||
|
||||
func NewAccessGenerate() *AccessGenerate {
|
||||
return &AccessGenerate{}
|
||||
}
|
||||
|
||||
type AccessGenerate struct {
|
||||
}
|
||||
|
||||
// Token
|
||||
// @description: 手动实现Token生成,直接生成UUID,替换掉自带的那个憨得一批的长长的字符串
|
||||
// @receiver ag
|
||||
// @param ctx context.Context 上下文
|
||||
// @param data *oauth2.GenerateBasic 生成基础数据
|
||||
// @param isGenRefresh bool 是否生成RefreshToken
|
||||
// @return string AccessToken 令牌
|
||||
// @return string RefreshToken 刷新令牌
|
||||
// @return error 错误信息
|
||||
func (ag *AccessGenerate) Token(ctx context.Context, data *oauth2.GenerateBasic, isGenRefresh bool) (string, string, error) {
|
||||
u, _ := uuid.NewUUID()
|
||||
access := strings.ReplaceAll(u.String(), "-", "")
|
||||
|
||||
refresh := ""
|
||||
if isGenRefresh {
|
||||
u, _ = uuid.NewUUID()
|
||||
refresh = strings.ReplaceAll(u.String(), "-", "")
|
||||
}
|
||||
// 生成新的,清理掉旧的
|
||||
ag.clearOldToken(ctx, data.UserID)
|
||||
// 返回结果
|
||||
return access, refresh, nil
|
||||
}
|
||||
|
||||
// clearOldToken
|
||||
// @description: 清理掉旧的Token和RefreshToken
|
||||
// @receiver ag
|
||||
// @param ctx context.Context 上下文
|
||||
// @param userId string 用户ID
|
||||
func (ag *AccessGenerate) clearOldToken(ctx context.Context, userId string) {
|
||||
key := fmt.Sprintf("oauth:token:token:%s", userId)
|
||||
accessToken, err := redis.Client.Get(context.Background(), key).Result()
|
||||
if err != nil {
|
||||
//log.Errorf("获取缓存用户的accessToken失败: %v", err.Error())
|
||||
return
|
||||
}
|
||||
if accessToken != "" {
|
||||
// 老的Token
|
||||
var baseKey string
|
||||
baseKey, err = redis.Client.Get(ctx, "oauth:token:"+accessToken).Result()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 老Token详细数据
|
||||
var dataStr string
|
||||
dataStr, err = redis.Client.Get(ctx, "oauth:token:"+baseKey).Result()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err = json.Unmarshal([]byte(dataStr), &m); err != nil {
|
||||
return
|
||||
}
|
||||
// 删除AccessToken等信息
|
||||
redis.Client.Del(ctx, fmt.Sprintf("oauth:token:%v", m["Access"]))
|
||||
redis.Client.Del(ctx, fmt.Sprintf("oauth:token:%v", m["Refresh"]))
|
||||
redis.Client.Del(ctx, "oauth:token:"+baseKey)
|
||||
}
|
||||
}
|
39
pkg/auth/utils.go
Normal file
39
pkg/auth/utils.go
Normal file
@ -0,0 +1,39 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/go-oauth2/oauth2/v4/models"
|
||||
"wechat-robot/internal/redis"
|
||||
)
|
||||
|
||||
// GetUserIdWithRefreshToken
|
||||
// @description: 根据refreshToken获取userId
|
||||
// @param refreshToken string refreshToken
|
||||
// @return userId string userId
|
||||
func GetUserIdWithRefreshToken(refreshToken string) (userId string) {
|
||||
// 取出真实保存token信息的key
|
||||
realKey, err := redis.Client.Get(context.Background(), "oauth:token:"+refreshToken).Result()
|
||||
if err != nil {
|
||||
log.Errorf("根据refreshToken获取realKey失败: %v", err)
|
||||
return
|
||||
}
|
||||
// 取出缓存信息
|
||||
var (
|
||||
tiStr string // 保存的原始字符串
|
||||
tokenInfo models.Token // Token结构体
|
||||
)
|
||||
tiStr, err = redis.Client.Get(context.Background(), "oauth:token:"+realKey).Result()
|
||||
if err != nil {
|
||||
log.Errorf("获取tokenInfo失败: %v", err)
|
||||
return
|
||||
}
|
||||
// 反序列化
|
||||
if err = json.Unmarshal([]byte(tiStr), &tokenInfo); err != nil {
|
||||
log.Errorf("tokenInfo反序列化失败: %v", err)
|
||||
return
|
||||
}
|
||||
// 返回userId
|
||||
return tokenInfo.GetUserID()
|
||||
}
|
66
pkg/captcha/captcha.go
Normal file
66
pkg/captcha/captcha.go
Normal file
@ -0,0 +1,66 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"time"
|
||||
"wechat-robot/internal/redis"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
type RedisStore struct{}
|
||||
|
||||
// 验证码缓存前缀
|
||||
const cachePrefix = "captcha:img:"
|
||||
|
||||
// Set
|
||||
// @description: 缓存验证码信息
|
||||
// @receiver r
|
||||
// @param id string 验证码ID
|
||||
// @param value string 验证码答案
|
||||
// @return error 错误信息
|
||||
func (r RedisStore) Set(id string, value string) error {
|
||||
key := cachePrefix + id
|
||||
err := redis.Client.Set(ctx, key, value, time.Minute).Err()
|
||||
if err != nil {
|
||||
log.Errorf("写入缓存验证码失败: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get
|
||||
// @description: 获取验证码信息
|
||||
// @receiver r
|
||||
// @param id string 验证码ID
|
||||
// @param clear
|
||||
// @return string
|
||||
func (r RedisStore) Get(id string, clear bool) string {
|
||||
key := cachePrefix + id
|
||||
val, err := redis.Client.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
log.Errorf("读取缓存验证码失败: %v", err.Error())
|
||||
return ""
|
||||
}
|
||||
if clear {
|
||||
err = redis.Client.Del(ctx, key).Err()
|
||||
if err != nil {
|
||||
log.Errorf("读取缓存验证码失败: %v", err.Error())
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// Verify
|
||||
// @description: 验证验证码
|
||||
// @receiver r
|
||||
// @param id string 验证码ID
|
||||
// @param answer string 验证码答案
|
||||
// @param clear bool 是否清除验证码
|
||||
// @return bool 是否验证成功
|
||||
func (r RedisStore) Verify(id, answer string, clear bool) bool {
|
||||
v := r.Get(id, clear)
|
||||
return v == answer
|
||||
}
|
16
pkg/response/fail.go
Normal file
16
pkg/response/fail.go
Normal 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
pkg/response/page.go
Normal file
43
pkg/response/page.go
Normal 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
|
||||
}
|
77
pkg/response/response.go
Normal file
77
pkg/response/response.go
Normal file
@ -0,0 +1,77 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"wechat-robot/pkg/validator"
|
||||
)
|
||||
|
||||
// 定义状态码
|
||||
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 {
|
||||
err = validator.Translate(err)
|
||||
r.errMsg = err.Error()
|
||||
}
|
||||
return r
|
||||
}
|
27
pkg/response/result.go
Normal file
27
pkg/response/result.go
Normal 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)
|
||||
}
|
16
pkg/response/success.go
Normal file
16
pkg/response/success.go
Normal 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()
|
||||
}
|
144
pkg/types/date.go
Normal file
144
pkg/types/date.go
Normal file
@ -0,0 +1,144 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 默认时间格式
|
||||
const dateFormat = "2006-01-02"
|
||||
|
||||
// Date 自定义时间类型
|
||||
type Date time.Time
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (dt *Date) Scan(value any) error {
|
||||
// mysql 内部日期的格式可能是 2006-01-02 15:04:05 +0800 CST 格式,所以检出的时候还需要进行一次格式化
|
||||
tTime, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", value.(time.Time).String())
|
||||
*dt = Date(tTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (dt Date) Value() (driver.Value, error) {
|
||||
// 0001-01-01 00:00:00 属于空值,遇到空值解析成 null 即可
|
||||
if dt.String() == "0001-01-01" {
|
||||
return nil, nil
|
||||
}
|
||||
return []byte(dt.Format(dateFormat)), nil
|
||||
}
|
||||
|
||||
// 用于 fmt.Println 和后续验证场景
|
||||
func (dt Date) String() string {
|
||||
return dt.Format(dateFormat)
|
||||
}
|
||||
|
||||
// Format 格式化
|
||||
func (dt Date) Format(fm string) string {
|
||||
return time.Time(dt).Format(fm)
|
||||
}
|
||||
|
||||
// After 时间比较
|
||||
func (dt *Date) After(now time.Time) bool {
|
||||
return time.Time(*dt).After(now)
|
||||
}
|
||||
|
||||
// Before 时间比较
|
||||
func (dt *Date) Before(now time.Time) bool {
|
||||
return time.Time(*dt).Before(now)
|
||||
}
|
||||
|
||||
// IBefore 时间比较
|
||||
func (dt *Date) IBefore(now Date) bool {
|
||||
return dt.Before(time.Time(now))
|
||||
}
|
||||
|
||||
// SubTime 对比
|
||||
func (dt Date) SubTime(t time.Time) time.Duration {
|
||||
return dt.ToTime().Sub(t)
|
||||
}
|
||||
|
||||
// Sub 对比
|
||||
func (dt Date) Sub(t Date) time.Duration {
|
||||
return dt.ToTime().Sub(t.ToTime())
|
||||
}
|
||||
|
||||
// ToTime 转换为golang的时间类型
|
||||
func (dt Date) ToTime() time.Time {
|
||||
return time.Time(dt)
|
||||
}
|
||||
|
||||
// IsNil 是否为空值
|
||||
func (dt Date) IsNil() bool {
|
||||
return dt.Format(dateFormat) == "0001-01-01"
|
||||
}
|
||||
|
||||
// Unix 实现Unix函数
|
||||
func (dt Date) Unix() int64 {
|
||||
return dt.ToTime().Unix()
|
||||
}
|
||||
|
||||
// ======== 序列化 JSON ========
|
||||
|
||||
// MarshalJSON 时间到字符串
|
||||
func (dt Date) MarshalJSON() ([]byte, error) {
|
||||
// 过滤掉空数据
|
||||
if dt.IsNil() {
|
||||
return []byte("\"\""), nil
|
||||
}
|
||||
output := fmt.Sprintf(`"%s"`, dt.Format(dateFormat))
|
||||
return []byte(output), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON 字符串到时间
|
||||
func (dt *Date) UnmarshalJSON(b []byte) error {
|
||||
if len(b) == 2 {
|
||||
*dt = Date{}
|
||||
return nil
|
||||
}
|
||||
// 解析指定的格式
|
||||
var now time.Time
|
||||
var err error
|
||||
if strings.HasPrefix(string(b), "\"") {
|
||||
now, err = time.ParseInLocation(`"`+dateFormat+`"`, string(b), time.Local)
|
||||
} else {
|
||||
now, err = time.ParseInLocation(dateFormat, string(b), time.Local)
|
||||
}
|
||||
*dt = Date(now)
|
||||
return err
|
||||
}
|
||||
|
||||
// ======== 序列化 RPCX ========
|
||||
|
||||
// EncodeMsgpack
|
||||
// @description: 序列化(由于Msgpack不支持时区,所以这里序列化成字符串)
|
||||
// @receiver dt
|
||||
// @param enc
|
||||
// @return error
|
||||
func (dt Date) EncodeMsgpack(enc *msgpack.Encoder) error {
|
||||
return enc.Encode(dt.Format("2006-01-02"))
|
||||
}
|
||||
|
||||
// DecodeMsgpack
|
||||
// @description: 反序列化(由于Msgpack不支持时区,所以这里从字符串反序列化)
|
||||
// @receiver dt
|
||||
// @param dec
|
||||
// @return error
|
||||
func (dt *Date) DecodeMsgpack(dec *msgpack.Decoder) error {
|
||||
var dtStr string
|
||||
err := dec.Decode(&dtStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var tm time.Time
|
||||
tm, err = time.ParseInLocation("2006-01-02", dtStr, time.Local)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*dt = Date(tm)
|
||||
return nil
|
||||
}
|
156
pkg/types/datetime.go
Normal file
156
pkg/types/datetime.go
Normal file
@ -0,0 +1,156 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 默认时间格式
|
||||
const dateTimeFormat = "2006-01-02 15:04:05.000"
|
||||
|
||||
// 可能包含的时间格式
|
||||
var formatMap = map[string]string{
|
||||
"yyyy-mm-dd hh:mm:ss": "2006-01-02 15:04:05",
|
||||
"yyyy-mm-dd hh:mm": "2006-01-02 15:04",
|
||||
"yyyy-mm-dd hh": "2006-01-02 15:04",
|
||||
"yyyy-mm-dd": "2006-01-02",
|
||||
"yyyy-mm": "2006-01",
|
||||
"mm-dd": "01-02",
|
||||
"dd-mm-yy hh:mm:ss": "02-01-06 15:04:05",
|
||||
"yyyy/mm/dd hh:mm:ss": "2006/01/02 15:04:05",
|
||||
"yyyy/mm/dd hh:mm": "2006/01/02 15:04",
|
||||
"yyyy/mm/dd hh": "2006/01/02 15",
|
||||
"yyyy/mm/dd": "2006/01/02",
|
||||
"yyyy/mm": "2006/01",
|
||||
"mm/dd": "01/02",
|
||||
"dd/mm/yy hh:mm:ss": "02/01/06 15:04:05",
|
||||
"yyyy": "2006",
|
||||
"mm": "01",
|
||||
"hh:mm:ss": "15:04:05",
|
||||
"mm:ss": "04:05",
|
||||
}
|
||||
|
||||
// DateTime 自定义时间类型
|
||||
type DateTime time.Time
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (dt *DateTime) Scan(value interface{}) error {
|
||||
// mysql 内部日期的格式可能是 2006-01-02 15:04:05 +0800 CST 格式,所以检出的时候还需要进行一次格式化
|
||||
tTime, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", value.(time.Time).String())
|
||||
*dt = DateTime(tTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (dt DateTime) Value() (dv driver.Value, err error) {
|
||||
// 0001-01-01 00:00:00 属于空值,遇到空值解析成 null 即可
|
||||
if dt.String() == "0001-01-01 00:00:00.000" {
|
||||
return nil, nil
|
||||
}
|
||||
dv, err = []byte(dt.Format(dateTimeFormat)), nil
|
||||
return
|
||||
}
|
||||
|
||||
// 用于 fmt.Println 和后续验证场景
|
||||
func (dt DateTime) String() string {
|
||||
return dt.Format(dateTimeFormat)
|
||||
}
|
||||
|
||||
// Format 格式化
|
||||
func (dt *DateTime) Format(fm string) string {
|
||||
return time.Time(*dt).Format(fm)
|
||||
}
|
||||
|
||||
// AutoParse 假装是个自动解析时间的函数
|
||||
func (dt DateTime) AutoParse(timeStr string) (t time.Time, err error) {
|
||||
// 循环匹配预设的时间格式
|
||||
for _, v := range formatMap {
|
||||
// 尝试解析,没报错就是解析成功了
|
||||
t, err = time.ParseInLocation(v, timeStr, time.Local)
|
||||
if err == nil {
|
||||
// 错误为空,表示匹配上了
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// After 时间比较
|
||||
func (dt *DateTime) After(now time.Time) bool {
|
||||
return time.Time(*dt).After(now)
|
||||
}
|
||||
|
||||
// Before 时间比较
|
||||
func (dt *DateTime) Before(now time.Time) bool {
|
||||
return time.Time(*dt).Before(now)
|
||||
}
|
||||
|
||||
// IBefore 时间比较
|
||||
func (dt *DateTime) IBefore(now DateTime) bool {
|
||||
return dt.Before(time.Time(now))
|
||||
}
|
||||
|
||||
// SubTime 对比
|
||||
func (dt DateTime) SubTime(t time.Time) time.Duration {
|
||||
return dt.ToTime().Sub(t)
|
||||
}
|
||||
|
||||
// Sub 对比
|
||||
func (dt DateTime) Sub(t DateTime) time.Duration {
|
||||
return dt.ToTime().Sub(t.ToTime())
|
||||
}
|
||||
|
||||
// ToTime 转换为golang的时间类型
|
||||
func (dt DateTime) ToTime() time.Time {
|
||||
return time.Time(dt)
|
||||
}
|
||||
|
||||
// IsNil 是否为空值
|
||||
func (dt DateTime) IsNil() bool {
|
||||
return dt.Format(dateTimeFormat) == "0001-01-01 00:00:00.000"
|
||||
}
|
||||
|
||||
// Unix 实现Unix函数
|
||||
func (dt DateTime) Unix() int64 {
|
||||
return dt.ToTime().Unix()
|
||||
}
|
||||
|
||||
// EndOfCentury 获取本世纪最后时间
|
||||
func (dt DateTime) EndOfCentury() DateTime {
|
||||
yearEnd := time.Now().Local().Year()/100*100 + 99
|
||||
return DateTime(time.Date(yearEnd, 12, 31, 23, 59, 59, 999999999, time.Local))
|
||||
}
|
||||
|
||||
// ======== 序列化 JSON ========
|
||||
|
||||
// MarshalJSON 时间到字符串
|
||||
func (dt DateTime) MarshalJSON() ([]byte, error) {
|
||||
// 过滤掉空数据
|
||||
if dt.IsNil() {
|
||||
return []byte("\"\""), nil
|
||||
}
|
||||
output := fmt.Sprintf(`"%s"`, dt.Format("2006-01-02 15:04:05"))
|
||||
return []byte(output), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON 字符串到时间
|
||||
func (dt *DateTime) UnmarshalJSON(b []byte) (err error) {
|
||||
if len(b) == 2 {
|
||||
*dt = DateTime{}
|
||||
return
|
||||
}
|
||||
// 解析指定的格式
|
||||
var now time.Time
|
||||
if strings.HasPrefix(string(b), "\"") {
|
||||
now, err = dt.AutoParse(string(b)[1 : len(b)-1])
|
||||
} else {
|
||||
now, err = dt.AutoParse(string(b))
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
*dt = DateTime(now)
|
||||
return
|
||||
}
|
22
pkg/types/menu.go
Normal file
22
pkg/types/menu.go
Normal file
@ -0,0 +1,22 @@
|
||||
package types
|
||||
|
||||
type MenuType string
|
||||
|
||||
const (
|
||||
MenuTypeMenu = MenuType("MENU") // 菜单
|
||||
MenuTypeButton = MenuType("BUTTON") // 按钮
|
||||
)
|
||||
|
||||
// 状态对应的描述
|
||||
var menuTypeMap = map[MenuType]string{
|
||||
MenuTypeMenu: "菜单",
|
||||
MenuTypeButton: "按钮",
|
||||
}
|
||||
|
||||
// 处理为看得懂的状态
|
||||
func (s MenuType) String() string {
|
||||
if str, ok := menuTypeMap[s]; ok {
|
||||
return str
|
||||
}
|
||||
return string(s)
|
||||
}
|
51
pkg/types/message.go
Normal file
51
pkg/types/message.go
Normal file
@ -0,0 +1,51 @@
|
||||
package types
|
||||
|
||||
import "fmt"
|
||||
|
||||
type MessageType int
|
||||
|
||||
// 微信定义的消息类型
|
||||
const (
|
||||
MsgTypeText MessageType = 1 // 文本消息
|
||||
MsgTypeImage MessageType = 3 // 图片消息
|
||||
MsgTypeVoice MessageType = 34 // 语音消息
|
||||
MsgTypeVerify MessageType = 37 // 认证消息
|
||||
MsgTypePossibleFriend MessageType = 40 // 好友推荐消息
|
||||
MsgTypeShareCard MessageType = 42 // 名片消息
|
||||
MsgTypeVideo MessageType = 43 // 视频消息
|
||||
MsgTypeEmoticon MessageType = 47 // 表情消息
|
||||
MsgTypeLocation MessageType = 48 // 地理位置消息
|
||||
MsgTypeApp MessageType = 49 // APP消息
|
||||
MsgTypeVoip MessageType = 50 // VOIP消息
|
||||
MsgTypeVoipNotify MessageType = 52 // VOIP结束消息
|
||||
MsgTypeVoipInvite MessageType = 53 // VOIP邀请
|
||||
MsgTypeMicroVideo MessageType = 62 // 小视频消息
|
||||
MsgTypeSys MessageType = 10000 // 系统消息
|
||||
MsgTypeRecalled MessageType = 10002 // 消息撤回
|
||||
)
|
||||
|
||||
var MessageTypeMap = map[MessageType]string{
|
||||
MsgTypeText: "文本消息",
|
||||
MsgTypeImage: "图片消息",
|
||||
MsgTypeVoice: "语音消息",
|
||||
MsgTypeVerify: "认证消息",
|
||||
MsgTypePossibleFriend: "好友推荐消息",
|
||||
MsgTypeShareCard: "名片消息",
|
||||
MsgTypeVideo: "视频消息",
|
||||
MsgTypeEmoticon: "表情消息",
|
||||
MsgTypeLocation: "地理位置消息",
|
||||
MsgTypeApp: "APP消息",
|
||||
MsgTypeVoip: "VOIP消息",
|
||||
MsgTypeVoipNotify: "VOIP结束消息",
|
||||
MsgTypeVoipInvite: "VOIP邀请",
|
||||
MsgTypeMicroVideo: "小视频消息",
|
||||
MsgTypeSys: "系统消息",
|
||||
MsgTypeRecalled: "消息撤回",
|
||||
}
|
||||
|
||||
func (mt MessageType) String() string {
|
||||
if msg, ok := MessageTypeMap[mt]; ok {
|
||||
return msg
|
||||
}
|
||||
return fmt.Sprintf("未知消息类型(%d)", mt)
|
||||
}
|
42
pkg/types/model.go
Normal file
42
pkg/types/model.go
Normal file
@ -0,0 +1,42 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/plugin/soft_delete"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// BaseDbModel
|
||||
// @description: 带逻辑删除的数据库通用字段
|
||||
type BaseDbModel struct {
|
||||
Id string `json:"id" gorm:"type:varchar(32);primarykey"`
|
||||
CreatedAt DateTime `json:"createdAt"`
|
||||
UpdatedAt DateTime `json:"updatedAt"`
|
||||
DeletedAt int64 `json:"-" gorm:"index:deleted; default:0"`
|
||||
IsDel soft_delete.DeletedAt `json:"-" gorm:"softDelete:flag,DeletedAtField:DeletedAt; index:deleted; default:0; type:tinyint(1)"`
|
||||
}
|
||||
|
||||
// BeforeCreate 创建数据库对象之前生成UUID
|
||||
func (m *BaseDbModel) BeforeCreate(*gorm.DB) (err error) {
|
||||
if m.Id == "" {
|
||||
m.Id = strings.ReplaceAll(uuid.New().String(), "-", "")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// =====================================================================================================================
|
||||
|
||||
// BaseDbModelWithReal
|
||||
// @description: 不带逻辑删除的数据库通用字段
|
||||
type BaseDbModelWithReal struct {
|
||||
Id string `json:"id" gorm:"type:varchar(32);primarykey"`
|
||||
CreatedAt DateTime `json:"createdAt"`
|
||||
UpdatedAt DateTime `json:"updatedAt"`
|
||||
}
|
||||
|
||||
// BeforeCreate 创建数据库对象之前生成UUID
|
||||
func (m *BaseDbModelWithReal) BeforeCreate(*gorm.DB) (err error) {
|
||||
m.Id = strings.ReplaceAll(uuid.New().String(), "-", "")
|
||||
return
|
||||
}
|
55
pkg/validator/validator.go
Normal file
55
pkg/validator/validator.go
Normal file
@ -0,0 +1,55 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"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"
|
||||
"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)
|
||||
|
||||
ves, ok := err.(validator.ValidationErrors)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range ves {
|
||||
errorMsg = append(errorMsg, e.Translate(trans))
|
||||
}
|
||||
|
||||
return errors.New(strings.Join(errorMsg, ";"))
|
||||
}
|
18
router/admin/login.go
Normal file
18
router/admin/login.go
Normal file
@ -0,0 +1,18 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
loginApi "wechat-robot/api/admin/login"
|
||||
"wechat-robot/router/middleware"
|
||||
)
|
||||
|
||||
// 登录相关
|
||||
func login(g *gin.RouterGroup) {
|
||||
cg := g.Group("/captcha") // 验证码相关接口
|
||||
cg.GET("/img", loginApi.GetImgCaptcha) // 获取图形验证码
|
||||
|
||||
t := g.Group("/token") // 登录相关接口
|
||||
t.POST("", loginApi.Login) // 登录
|
||||
t.POST("/refresh", loginApi.Refresh) // 刷新Token
|
||||
t.POST("/logout", middleware.AuthorizeToken(), loginApi.Logout) // 退出登录
|
||||
}
|
13
router/admin/menu.go
Normal file
13
router/admin/menu.go
Normal file
@ -0,0 +1,13 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
menuApi "wechat-robot/api/admin/menu"
|
||||
)
|
||||
|
||||
// menu
|
||||
// @description: 菜单相关
|
||||
// @param g
|
||||
func menu(g *gin.RouterGroup) {
|
||||
g.GET("/self", menuApi.GetUserMenuTree)
|
||||
}
|
16
router/admin/route.go
Normal file
16
router/admin/route.go
Normal file
@ -0,0 +1,16 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"wechat-robot/router/middleware"
|
||||
)
|
||||
|
||||
// InitRoute
|
||||
// @description: 初始化路由
|
||||
// @param g
|
||||
func InitRoute(g *gin.RouterGroup) {
|
||||
login(g) // 登录相关路由
|
||||
|
||||
g.Use(middleware.AuthorizeToken())
|
||||
menu(g.Group("/menu")) // 菜单相关
|
||||
}
|
13
router/callback/route.go
Normal file
13
router/callback/route.go
Normal file
@ -0,0 +1,13 @@
|
||||
package callback
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"wechat-robot/api/callback"
|
||||
)
|
||||
|
||||
// InitRoute
|
||||
// @description: 初始化路由
|
||||
// @param g
|
||||
func InitRoute(g *gin.RouterGroup) {
|
||||
g.GET("/hook", callback.RobotHookNotify)
|
||||
}
|
36
router/middleware/auth.go
Normal file
36
router/middleware/auth.go
Normal file
@ -0,0 +1,36 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
"wechat-robot/pkg/auth"
|
||||
"wechat-robot/pkg/response"
|
||||
)
|
||||
|
||||
// AuthorizeToken
|
||||
// @description: 验证OAuth2生成的Token
|
||||
// @return gin.HandlerFunc
|
||||
func AuthorizeToken() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
// 判断有无token
|
||||
tokenStr := ctx.GetHeader("Authorization")
|
||||
if tokenStr == "" || !strings.HasPrefix(tokenStr, "Bearer ") {
|
||||
response.New(ctx).SetMsg("请先登录").Fail()
|
||||
ctx.Abort()
|
||||
return
|
||||
}
|
||||
// 先取出用户Token
|
||||
token, err := auth.OAuthServer.ValidationBearerToken(ctx.Request)
|
||||
if err != nil {
|
||||
log.Errorf("获取Token失败,错误:%s", err.Error())
|
||||
response.New(ctx).SetCode(http.StatusUnauthorized).SetMsg("登录已失效或已在其他地方登录").Fail()
|
||||
ctx.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 判断通过,允许放行
|
||||
ctx.Set("userId", token.GetUserID())
|
||||
}
|
||||
}
|
33
router/middleware/request.go
Normal file
33
router/middleware/request.go
Normal file
@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"wechat-robot/pkg/response"
|
||||
)
|
||||
|
||||
// NoMethodHandler
|
||||
// @description: 405错误处理
|
||||
// @return gin.HandlerFunc
|
||||
func NoMethodHandler() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
response.New(ctx).
|
||||
SetCode(http.StatusMethodNotAllowed).
|
||||
SetMsg(fmt.Sprintf("不支持%v请求", ctx.Request.Method)).
|
||||
Fail()
|
||||
ctx.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
// NoRouteHandler
|
||||
// @description: 404错误处理
|
||||
// @return gin.HandlerFunc
|
||||
func NoRouteHandler() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
response.New(ctx).SetCode(http.StatusNotFound).
|
||||
SetMsg("请求接口不存在").
|
||||
Result()
|
||||
ctx.Abort()
|
||||
}
|
||||
}
|
24
service/adminuser/save.go
Normal file
24
service/adminuser/save.go
Normal file
@ -0,0 +1,24 @@
|
||||
package adminuser
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"time"
|
||||
"wechat-robot/internal/database"
|
||||
"wechat-robot/model/entity"
|
||||
)
|
||||
|
||||
// UpdateLastLoginInfo
|
||||
// @description: 更新最后登录时间和IP
|
||||
// @param username string 登录账号
|
||||
// @param ip string IP地址
|
||||
func UpdateLastLoginInfo(username, ip string) {
|
||||
err := database.Client.Model(&entity.AdminUser{}).
|
||||
Where("username = ? OR email = ?", username, username).
|
||||
Updates(map[string]any{
|
||||
"last_login_at": time.Now(),
|
||||
"last_login_ip": ip,
|
||||
}).Error
|
||||
if err != nil {
|
||||
log.Errorf("更新管理员用户最后登录时间和IP失败: %v", err)
|
||||
}
|
||||
}
|
19
service/adminuser/select.go
Normal file
19
service/adminuser/select.go
Normal file
@ -0,0 +1,19 @@
|
||||
package adminuser
|
||||
|
||||
import (
|
||||
"wechat-robot/internal/database"
|
||||
"wechat-robot/model/entity"
|
||||
)
|
||||
|
||||
// GetUserWithLogin
|
||||
// @description: 根据账号获取用户信息
|
||||
// @param account
|
||||
// @return record
|
||||
// @return err
|
||||
func GetUserWithLogin(account string) (record entity.AdminUser, err error) {
|
||||
err = database.Client.
|
||||
Where("username = ?", account).
|
||||
Or("email = ? AND is_verified IS TRUE", account).
|
||||
First(&record).Error
|
||||
return
|
||||
}
|
1
service/friend/select.go
Normal file
1
service/friend/select.go
Normal file
@ -0,0 +1 @@
|
||||
package friend
|
1
service/group/select.go
Normal file
1
service/group/select.go
Normal file
@ -0,0 +1 @@
|
||||
package group
|
88
service/menu/select.go
Normal file
88
service/menu/select.go
Normal file
@ -0,0 +1,88 @@
|
||||
package menu
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"wechat-robot/internal/database"
|
||||
"wechat-robot/model/vo/menu"
|
||||
"wechat-robot/pkg/types"
|
||||
"wechat-robot/service/role"
|
||||
)
|
||||
|
||||
type menuRecordItem struct {
|
||||
Id string
|
||||
Type types.MenuType
|
||||
Name string
|
||||
Path string
|
||||
Icon string
|
||||
Sort int
|
||||
ParentId *string
|
||||
RoleCode string
|
||||
AuthCode string
|
||||
}
|
||||
|
||||
// GetUserMenus
|
||||
// @description: 获取用户权限内的菜单列表
|
||||
// @param userId string 用户Id
|
||||
// @return records []entity.Menu 菜单列表
|
||||
// @return err error 错误信息
|
||||
func GetUserMenus(userId string) (records []menu.Item, err error) {
|
||||
// 检查是否是超级管理员用户
|
||||
isSuperAdminUser := role.CheckIsSuperAdminUser(userId)
|
||||
|
||||
// 取出用户包含的所有菜单
|
||||
var menus []menuRecordItem
|
||||
tx := database.Client.Table("t_menu AS tm").
|
||||
Joins("LEFT JOIN t_role_menu AS trm ON trm.menu_id = tm.id").
|
||||
Joins("LEFT JOIN t_role AS tr ON tr.id = trm.role_id AND tr.is_del IS FALSE").
|
||||
Select("tm.*", "tr.code AS role_code").
|
||||
Where("tm.is_del IS FALSE")
|
||||
if !isSuperAdminUser {
|
||||
// 不是超级管理员,只能获取自己的菜单
|
||||
tx.Joins("LEFT JOIN t_admin_user_role AS tur ON tur.role_id = tr.id").
|
||||
Where("tur.user_id = ?", userId)
|
||||
}
|
||||
err = tx.Find(&menus).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 处理出菜单Id对应的角色代码,然后去重
|
||||
var items []menuRecordItem
|
||||
roleCodesMap := make(map[string][]string) // key: menuId, value: roleCode Array
|
||||
authMap := make(map[string][]string) // key: menuId, value: 类型为按钮的path
|
||||
for _, m := range menus {
|
||||
if _, ok := roleCodesMap[m.Id]; !ok {
|
||||
if m.RoleCode != "" {
|
||||
roleCodesMap[m.Id] = []string{m.RoleCode}
|
||||
}
|
||||
// 将菜单填充到items里
|
||||
if m.Type == types.MenuTypeMenu {
|
||||
items = append(items, m)
|
||||
}
|
||||
} else {
|
||||
roleCodesMap[m.Id] = append(roleCodesMap[m.Id], m.RoleCode)
|
||||
}
|
||||
|
||||
// 如果是按钮,且有父级,就把按钮的path放到父级的authMap里
|
||||
if m.Type == types.MenuTypeButton && m.ParentId != nil {
|
||||
authMap[*m.ParentId] = append(authMap[*m.ParentId], m.Path)
|
||||
}
|
||||
}
|
||||
// 再次循环数据,把角色代码们拼接起来
|
||||
for i, m := range items {
|
||||
roleCodes := roleCodesMap[m.Id]
|
||||
authCodes := authMap[m.Id]
|
||||
if isSuperAdminUser {
|
||||
// 固定填充一个admin角色代码进去
|
||||
roleCodes = append(roleCodes, "admin")
|
||||
}
|
||||
|
||||
items[i].RoleCode = strings.Join(slices.Compact(roleCodes), ",")
|
||||
items[i].AuthCode = strings.Join(slices.Compact(authCodes), ",")
|
||||
}
|
||||
|
||||
// 处理成树形结构
|
||||
records = toTree(items, "")
|
||||
return
|
||||
}
|
54
service/menu/utils.go
Normal file
54
service/menu/utils.go
Normal file
@ -0,0 +1,54 @@
|
||||
package menu
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"wechat-robot/model/vo/menu"
|
||||
)
|
||||
|
||||
// toTree
|
||||
// @description: 递归生成菜单树
|
||||
// @param records
|
||||
// @param pid
|
||||
// @return tree
|
||||
func toTree(records []menuRecordItem, pid string) (tree []menu.Item) {
|
||||
for _, record := range records {
|
||||
// 判断数据父级Id和传入的父级Id是否相等,相等就处理出来
|
||||
var deal bool
|
||||
if pid == "" {
|
||||
deal = record.ParentId == nil
|
||||
} else {
|
||||
deal = record.ParentId != nil && *record.ParentId == pid
|
||||
}
|
||||
|
||||
if deal {
|
||||
var node menu.Item
|
||||
node.Id = record.Id
|
||||
node.Path = record.Path
|
||||
|
||||
var meta menu.ItemMeta
|
||||
meta.Title = record.Name
|
||||
meta.Icon = record.Icon
|
||||
meta.Rank = record.Sort
|
||||
if record.RoleCode != "" {
|
||||
meta.Roles = strings.Split(record.RoleCode, ",")
|
||||
}
|
||||
if record.AuthCode != "" {
|
||||
meta.Auths = strings.Split(record.AuthCode, ",")
|
||||
}
|
||||
node.Meta = meta
|
||||
|
||||
// 处理子级
|
||||
node.Children = toTree(records, record.Id)
|
||||
// 往数组塞数据
|
||||
tree = append(tree, node)
|
||||
}
|
||||
}
|
||||
|
||||
// 排序
|
||||
sort.SliceStable(tree, func(i, j int) bool {
|
||||
return tree[i].Meta.Rank < tree[j].Meta.Rank
|
||||
})
|
||||
|
||||
return
|
||||
}
|
1
service/robot/select.go
Normal file
1
service/robot/select.go
Normal file
@ -0,0 +1 @@
|
||||
package robot
|
22
service/role/select.go
Normal file
22
service/role/select.go
Normal file
@ -0,0 +1,22 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"wechat-robot/internal/database"
|
||||
)
|
||||
|
||||
// CheckIsSuperAdminUser
|
||||
// @description: 检查是否是超级管理员用户
|
||||
// @param userId string 用户Id
|
||||
// @return flag bool 是否是超级管理员用户
|
||||
func CheckIsSuperAdminUser(userId string) (flag bool) {
|
||||
var codes []string
|
||||
// 获取用户角色代码
|
||||
database.Client.Table("t_role AS tr").
|
||||
Joins("LEFT JOIN t_admin_user_role AS tur ON tur.role_id = tr.id").
|
||||
Select("tr.code").
|
||||
Where("tur.user_id = ?", userId).
|
||||
Where("tr.is_del IS FALSE").
|
||||
Take(&codes)
|
||||
return slices.Contains(codes, "admin")
|
||||
}
|
19
tcpserver/forward.go
Normal file
19
tcpserver/forward.go
Normal file
@ -0,0 +1,19 @@
|
||||
package tcpserver
|
||||
|
||||
// forward
|
||||
// @description: 转发消息
|
||||
func forward(msg []byte) {
|
||||
// 使用socket转发消息
|
||||
//for _, s := range config.Conf.Wechat.Forward {
|
||||
// conn, err := net.Dial("tcp", s)
|
||||
// if err != nil {
|
||||
// log.Printf("转发消息失败,错误信息: %v", err)
|
||||
// continue
|
||||
// }
|
||||
// _, err = conn.Write(msg)
|
||||
// if err != nil {
|
||||
// log.Printf("转发消息失败,错误信息: %v", err)
|
||||
// }
|
||||
// _ = conn.Close()
|
||||
//}
|
||||
}
|
35
tcpserver/handle.go
Normal file
35
tcpserver/handle.go
Normal file
@ -0,0 +1,35 @@
|
||||
package tcpserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
// process
|
||||
// @description: 处理连接
|
||||
// @param conn
|
||||
func process(conn net.Conn) {
|
||||
// 处理完关闭连接
|
||||
defer func() {
|
||||
log.Printf("处理完成: -> %s", conn.RemoteAddr())
|
||||
_ = conn.Close()
|
||||
}()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err := io.Copy(&buf, conn); err != nil {
|
||||
log.Printf("[%s]返回数据失败,错误信息: %v", conn.RemoteAddr(), err)
|
||||
}
|
||||
log.Printf("[%s]数据长度: %d", conn.RemoteAddr(), buf.Len())
|
||||
go parse(conn.RemoteAddr(), buf.Bytes())
|
||||
|
||||
// 转发到其他地方去
|
||||
//if len(config.Conf.Wechat.Forward) > 0 {
|
||||
// go forward(buf.Bytes())
|
||||
//}
|
||||
// 将接受到的数据返回给客户端
|
||||
if _, err := conn.Write([]byte("200 OK")); err != nil {
|
||||
log.Printf("[%s]返回数据失败,错误信息: %v", conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
45
tcpserver/parse.go
Normal file
45
tcpserver/parse.go
Normal file
@ -0,0 +1,45 @@
|
||||
package tcpserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
model "wechat-robot/model/robot"
|
||||
"wechat-robot/pkg/types"
|
||||
)
|
||||
|
||||
// parse
|
||||
// @description: 解析消息
|
||||
// @param msg
|
||||
func parse(remoteAddr net.Addr, msg []byte) {
|
||||
var m model.Message
|
||||
if err := json.Unmarshal(msg, &m); err != nil {
|
||||
log.Printf("[%s]消息解析失败: %v", remoteAddr, err)
|
||||
log.Printf("[%s]消息内容: %d -> %v", remoteAddr, len(msg), string(msg))
|
||||
return
|
||||
}
|
||||
// 记录原始数据
|
||||
m.Raw = string(msg)
|
||||
|
||||
// 提取出群成员信息
|
||||
// Sys类型的消息正文不包含微信 Id,所以不需要处理
|
||||
if m.IsGroup() && m.Type != types.MsgTypeSys {
|
||||
// 群消息,处理一下消息和发信人
|
||||
groupUser := strings.Split(m.Content, "\n")[0]
|
||||
groupUser = strings.ReplaceAll(groupUser, ":", "")
|
||||
// 如果两个id一致,说明是系统发的
|
||||
if m.FromUser != groupUser {
|
||||
m.GroupUser = groupUser
|
||||
}
|
||||
// 用户的操作单独提出来处理一下
|
||||
m.Content = strings.Join(strings.Split(m.Content, "\n")[1:], "\n")
|
||||
}
|
||||
log.Printf("%s\n消息来源: %s\n群成员: %s\n消息类型: %v\n消息内容: %s", remoteAddr, m.FromUser, m.GroupUser, m.Type, m.Content)
|
||||
|
||||
// 插件不为空,开始执行
|
||||
//if p := current.GetRobotMessageHandler(); p != nil {
|
||||
// p(&m)
|
||||
//}
|
||||
|
||||
}
|
29
tcpserver/server.go
Normal file
29
tcpserver/server.go
Normal file
@ -0,0 +1,29 @@
|
||||
package tcpserver
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"net"
|
||||
)
|
||||
|
||||
// Start
|
||||
// @description: 启动服务
|
||||
func Start() {
|
||||
// 建立 tcp 服务
|
||||
listen, err := net.Listen("tcp", "0.0.0.0:8081")
|
||||
if err != nil {
|
||||
log.Errorf("TCP服务启动失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
// 等待客户端建立连接
|
||||
var conn net.Conn
|
||||
conn, err = listen.Accept()
|
||||
if err != nil {
|
||||
log.Errorf("客户端连接失败: %v", err)
|
||||
continue
|
||||
}
|
||||
// 启动一个单独的 goroutine 去处理连接
|
||||
go process(conn)
|
||||
}
|
||||
}
|
61
utils/password.go
Normal file
61
utils/password.go
Normal file
@ -0,0 +1,61 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Password
|
||||
// @description: 密码工具包
|
||||
type Password interface {
|
||||
CheckPasswordRule(password string) bool // 检查是否合规
|
||||
HashPassword(password *string) // 加密密码
|
||||
ComparePassword(dbPass, pass string) bool // 校验密码
|
||||
}
|
||||
|
||||
type password struct{}
|
||||
|
||||
// PasswordUtils
|
||||
// @description: 密码工具包
|
||||
// @return Password
|
||||
func PasswordUtils() Password {
|
||||
return &password{}
|
||||
}
|
||||
|
||||
// CheckPasswordRule
|
||||
// @description: TODO 检查密码规则是否健全
|
||||
// @receiver password
|
||||
// @param password
|
||||
// @return bool
|
||||
func (password) CheckPasswordRule(password string) bool {
|
||||
// 正则判断密码必须包含数字、字母大小写
|
||||
//reg := regexp.MustCompile(`^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$`)
|
||||
////return regexp.MustCompile(`^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*]).{6,32}$`).MatchString(password)
|
||||
//matched, err := regexp.MatchString(`^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,32}$`, password)
|
||||
//if err != nil {
|
||||
// log.Errorf("正则匹配错误: %v", err.Error())
|
||||
// return false
|
||||
//}
|
||||
//return matched
|
||||
return utf8.RuneCountInString(password) >= 8 && utf8.RuneCountInString(password) <= 32
|
||||
}
|
||||
|
||||
// HashPassword
|
||||
// @description: 密码加密
|
||||
// @receiver password
|
||||
// @param pass
|
||||
func (password) HashPassword(pass *string) {
|
||||
bytePass := []byte(*pass)
|
||||
hPass, _ := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost)
|
||||
*pass = string(hPass)
|
||||
}
|
||||
|
||||
// ComparePassword
|
||||
// @description: 密码比较
|
||||
// @receiver password
|
||||
// @param dbPass
|
||||
// @param pass
|
||||
// @return bool
|
||||
func (password) ComparePassword(dbPass, pass string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(dbPass), []byte(pass)) == nil
|
||||
}
|
110
utils/random.go
Normal file
110
utils/random.go
Normal file
@ -0,0 +1,110 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// Random
|
||||
// @description: 随机数工具接口
|
||||
type Random interface {
|
||||
GetRandomInt(len int) string // 获取指定长度的随机数字字符串
|
||||
GetRandomString(len int) string // 生成随机字符串
|
||||
GetRandomStringLower(len int) string // 生成小写随机字符串
|
||||
GetRandomStringMini(len int) string // 获取去掉了iI0O1的随机字符串
|
||||
GetRandomNumber(min, max int) int // 获取指定范围内的一个随机数
|
||||
}
|
||||
|
||||
type random struct{}
|
||||
|
||||
// RandomUtils
|
||||
// @description: 随机数工具
|
||||
// @return Random
|
||||
func RandomUtils() Random {
|
||||
return &random{}
|
||||
}
|
||||
|
||||
// GetRandomInt
|
||||
// @description: 获取指定长度的随机数字字符串
|
||||
// @receiver random
|
||||
// @param len
|
||||
// @return string
|
||||
func (random) GetRandomInt(len int) string {
|
||||
var numbers = []byte{0, 1, 2, 3, 4, 5, 7, 8, 9}
|
||||
var container string
|
||||
length := bytes.NewReader(numbers).Len()
|
||||
|
||||
for i := 1; i <= len; i++ {
|
||||
rd, _ := rand.Int(rand.Reader, big.NewInt(int64(length)))
|
||||
container += fmt.Sprintf("%d", numbers[rd.Int64()])
|
||||
}
|
||||
return container
|
||||
}
|
||||
|
||||
// GetRandomString
|
||||
// @description: 生成随机字符串
|
||||
// @receiver random
|
||||
// @param len
|
||||
// @return string
|
||||
func (random) GetRandomString(len int) string {
|
||||
var container string
|
||||
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||
b := bytes.NewBufferString(str)
|
||||
length := b.Len()
|
||||
bigInt := big.NewInt(int64(length))
|
||||
for i := 0; i < len; i++ {
|
||||
randomInt, _ := rand.Int(rand.Reader, bigInt)
|
||||
container += string(str[randomInt.Int64()])
|
||||
}
|
||||
return container
|
||||
}
|
||||
|
||||
// GetRandomStringLower
|
||||
// @description: 生成小写的随机字符串
|
||||
// @receiver r
|
||||
// @param len
|
||||
// @return string
|
||||
func (r random) GetRandomStringLower(len int) string {
|
||||
var container string
|
||||
var str = "abcdefghijklmnopqrstuvwxyz1234567890"
|
||||
b := bytes.NewBufferString(str)
|
||||
length := b.Len()
|
||||
bigInt := big.NewInt(int64(length))
|
||||
for i := 0; i < len; i++ {
|
||||
randomInt, _ := rand.Int(rand.Reader, bigInt)
|
||||
container += string(str[randomInt.Int64()])
|
||||
}
|
||||
return container
|
||||
}
|
||||
|
||||
// GetRandomStringMini
|
||||
// @description: 获取去掉了iI0O1的随机字符串
|
||||
// @receiver random
|
||||
// @param len
|
||||
// @return string
|
||||
func (random) GetRandomStringMini(len int) string {
|
||||
var container string
|
||||
var str = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"
|
||||
b := bytes.NewBufferString(str)
|
||||
length := b.Len()
|
||||
bigInt := big.NewInt(int64(length))
|
||||
for i := 0; i < len; i++ {
|
||||
randomInt, _ := rand.Int(rand.Reader, bigInt)
|
||||
container += string(str[randomInt.Int64()])
|
||||
}
|
||||
return container
|
||||
}
|
||||
|
||||
// GetRandomNumber
|
||||
// @description: 获取指定范围内的一个随机数
|
||||
// @receiver random
|
||||
// @param min
|
||||
// @param max
|
||||
// @return int
|
||||
func (random) GetRandomNumber(min, max int) int {
|
||||
en, _ := rand.Int(rand.Reader, big.NewInt(int64(max-min)))
|
||||
rn := int(en.Int64()) + min
|
||||
return rn
|
||||
}
|
Loading…
Reference in New Issue
Block a user