🎨 新增设置用户vip接口,优化机器人查询接口&修改跨域配置

This commit is contained in:
2025-09-03 02:25:29 +08:00
parent 5496bdaa94
commit 09b22856a5
10 changed files with 163 additions and 27 deletions

View File

@@ -48,6 +48,21 @@ func (*UserApi) SetBalance(ctx *gin.Context) {
r.OkWithMessage("设置用户余额成功", ctx)
}
// SetUserVip 设置用户会员
func (*UserApi) SetUserVip(ctx *gin.Context) {
var p request.SetUserVipReq
if err := ctx.ShouldBind(&p); err != nil {
r.FailWithMessage(err.Error(), ctx)
global.GVA_LOG.Error("参数错误,设置用户会员失败", zap.Error(err))
return
}
if err := userService.SetUserVip(p); err != nil {
r.FailWithMessage("设置用户会员失败", ctx)
return
}
r.OkWithMessage("设置用户会员成功", ctx)
}
// Register 注册-后台用
func (*UserApi) Register(ctx *gin.Context) {
var p request.RegisterReq
@@ -95,6 +110,7 @@ func (*UserApi) GetUserById(ctx *gin.Context) {
r.OkWithDetailed(user, "获取用户信息成功", ctx)
}
// GetTeachers 获取教师列表
func (a *UserApi) GetTeachers(context *gin.Context) {
var p common.PageInfo
if err := context.ShouldBind(&p); err != nil {

View File

@@ -265,7 +265,7 @@ disk-list:
# 跨域配置
# 需要配合 server/initialize/router.go -> `Router.Use(middleware.CorsByRules())` 使用
cors:
mode: strict-whitelist # 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
mode: allow-all # 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
whitelist:
- allow-origin: example1.com
allow-headers: Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id

View File

@@ -1,19 +0,0 @@
//go:build !windows
// +build !windows
package core
import (
"time"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
)
func initServer(address string, router *gin.Engine) server {
s := endless.NewServer(address, router)
s.ReadHeaderTimeout = 10 * time.Minute
s.WriteTimeout = 10 * time.Minute
s.MaxHeaderBytes = 1 << 20
return s
}

View File

@@ -1,6 +1,13 @@
package initialize
import (
"git.echol.cn/loser/lckt/model/app"
"git.echol.cn/loser/lckt/model/article"
"git.echol.cn/loser/lckt/model/bot"
"git.echol.cn/loser/lckt/model/category"
"git.echol.cn/loser/lckt/model/notice"
"git.echol.cn/loser/lckt/model/user"
"git.echol.cn/loser/lckt/model/vip"
"os"
"git.echol.cn/loser/lckt/global"
@@ -63,6 +70,16 @@ func RegisterTables() {
example.ExaFileChunk{},
example.ExaFileUploadAndDownload{},
example.ExaAttachmentCategory{},
app.TeacherApply{},
app.Order{},
app.Banner{},
bot.Bot{},
user.User{},
article.Article{},
category.Category{},
notice.Notice{},
vip.Vip{},
)
if err != nil {
global.GVA_LOG.Error("register table failed", zap.Error(err))

View File

@@ -64,7 +64,7 @@ func Routers() *gin.Engine {
Router.StaticFS(global.GVA_CONFIG.Local.StorePath, justFilesFilesystem{http.Dir(global.GVA_CONFIG.Local.StorePath)}) // Router.Use(middleware.LoadTls()) // 如果需要使用https 请打开此中间件 然后前往 core/server.go 将启动模式 更变为 Router.RunTLS("端口","你的cre/pem文件","你的key文件")
// 跨域,如需跨域可以打开下面的注释
// Router.Use(middleware.Cors()) // 直接放行全部跨域请求
Router.Use(middleware.Cors()) // 直接放行全部跨域请求
// Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
// global.GVA_LOG.Info("use middleware cors")
docs.SwaggerInfo.BasePath = global.GVA_CONFIG.System.RouterPrefix
@@ -73,13 +73,12 @@ func Routers() *gin.Engine {
// 方便统一添加路由组前缀 多服务器上线使用
PublicGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
PublicGroup.Use(middleware.Cors()) // 直接放行全部跨域请求
PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler()).Use(middleware.Cors())
PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
AppAuthGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
//AppAuthGroup.Use(middleware.AllCors())
AppAuthGroup.Use(middleware.UserJWTAuth()).Use(middleware.Cors())
AppAuthGroup.Use(middleware.UserJWTAuth())
{
// 健康监测
PublicGroup.GET("/health", func(c *gin.Context) {

View File

@@ -12,6 +12,9 @@ type Bot struct {
Content *string `json:"content" form:"content" gorm:"column:content;comment:内容;type:text;" binding:"required"` //内容
// 关联词
RelatedWords *string `json:"related_words" form:"related_words" gorm:"column:related_words;comment:关联词;type:text;"` //关联词
//查询次数
SearchCount int `json:"search_count" form:"search_count" gorm:"column:search_count;comment:查询次数;"` //查询次数
CreateBy string `json:"create_by" form:"create_by" gorm:"column:create_by;comment:创建人;"` //创建人
}
// TableName 机器人 Bot自定义表名 bots

View File

@@ -82,3 +82,9 @@ type GetTeacherApplyListReq struct {
Phone string `json:"phone" form:"phone"`
Nickname string `json:"nickname" form:"nickname"`
}
type SetUserVipReq struct {
Id int `json:"id" form:"id" vd:"@:len($)>0; msg:'用户ID不能为空'"`
VipExpireTime string `json:"vip_expire_time" form:"vip_expire_time" vd:"@:len($)>0; msg:'会员过期时间不能为空'"`
UserLabel int64 `json:"user_label" form:"user_label" vd:"@:len($)>0; msg:'用户标签不能为空'"` // 1 普通用户 2 Vip 3 Svip
}

View File

@@ -13,6 +13,7 @@ func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup, PublicRouter *gin.R
{
userRouter.GET("list", userApi.GetUserList) // 获取用户列表
userRouter.PUT("setBalance", userApi.SetBalance) // 更新用户余额
userRouter.PUT("setUserVip", userApi.SetUserVip) // 设置用户会员
userRouter.POST("register", userApi.Register) // 注册
userRouter.PUT("status/:id", userApi.SetUserStatus) // 更新用户状态
userRouter.GET(":id", userApi.GetUserById) // 获取用户信息

View File

@@ -5,6 +5,8 @@ import (
"git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/bot"
botReq "git.echol.cn/loser/lckt/model/bot/request"
"gorm.io/gorm"
"strings"
)
type BotService struct{}
@@ -68,7 +70,98 @@ func (btService *BotService) GetBotInfoList(ctx context.Context, info botReq.Bot
err = db.Find(&bts).Error
return bts, total, err
}
// GetBotPublic 模糊搜索公开机器人
func (btService *BotService) GetBotPublic(keyWord botReq.FindKey) (bt bot.Bot, err error) {
err = global.GVA_DB.Where("keyword Like ?", "%"+keyWord.KeyWord+"%").First(&bt).Error
userInput := strings.TrimSpace(keyWord.KeyWord)
// 生成用户输入的所有变体
variants := btService.generateKeywordVariants(userInput)
var conditions []string
var args []interface{}
// 对每个变体进行多种匹配策略
for _, variant := range variants {
// 1. 精确匹配(逗号分隔的关键词)
conditions = append(conditions, "FIND_IN_SET(?, keyword)")
args = append(args, variant)
// 2. 包含匹配
conditions = append(conditions, "keyword LIKE ?")
args = append(args, "%"+variant+"%")
// 3. 反向包含匹配(关键词包含用户输入)
conditions = append(conditions, "? LIKE CONCAT('%', keyword, '%')")
args = append(args, variant)
}
// 4. 特殊处理:如果用户输入较短,尝试作为关键词的子串匹配
if len([]rune(userInput)) <= 3 {
conditions = append(conditions, "keyword LIKE ?")
args = append(args, userInput+"%")
conditions = append(conditions, "keyword LIKE ?")
args = append(args, "%"+userInput)
}
whereClause := strings.Join(conditions, " OR ")
err = global.GVA_DB.Where(whereClause, args...).First(&bt).Error
go func() {
// 更新查询次数
if err == nil && bt.ID != 0 {
global.GVA_DB.Model(&bot.Bot{}).Where("id = ?", bt.ID).UpdateColumn("search_count", gorm.Expr("search_count + ?", 1))
}
}()
return
}
// generateKeywordVariants 生成关键词的所有可能变体
func (btService *BotService) generateKeywordVariants(input string) []string {
variants := make(map[string]bool)
variants[input] = true
// 中文数字到阿拉伯数字的映射
chineseToNum := map[string]string{
"一": "1", "二": "2", "三": "3", "四": "4", "五": "5",
"六": "6", "七": "7", "八": "8", "九": "9", "十": "10",
"零": "0",
}
// 阿拉伯数字到中文数字的映射
numToChinese := map[string]string{
"1": "一", "2": "二", "3": "三", "4": "四", "5": "五",
"6": "六", "7": "七", "8": "八", "9": "九", "10": "十",
"0": "零",
}
// 生成数字转换变体
current := input
// 中文数字 -> 阿拉伯数字
for cn, num := range chineseToNum {
if strings.Contains(current, cn) {
numVariant := strings.ReplaceAll(current, cn, num)
variants[numVariant] = true
}
}
// 阿拉伯数字 -> 中文数字
for num, cn := range numToChinese {
if strings.Contains(current, num) {
cnVariant := strings.ReplaceAll(current, num, cn)
variants[cnVariant] = true
}
}
// 转换为切片返回
result := make([]string, 0, len(variants))
for v := range variants {
if v != "" && len(strings.TrimSpace(v)) > 0 {
result = append(result, v)
}
}
return result
}

View File

@@ -228,3 +228,23 @@ func (u *UserService) UpdateTeacherApplyStatus(p app.TeacherApply) (err error) {
}
return
}
func (u *UserService) SetUserVip(p request.SetUserVipReq) error {
var user user.User
err := global.GVA_DB.Model(&user).Where("id = ?", p.Id).First(&user).Error
if err != nil {
global.GVA_LOG.Error("查询用户信息失败", zap.Error(err))
return err
}
user.IsVip = 1
user.UserLabel = p.UserLabel
user.VipExpireTime = p.VipExpireTime
err = global.GVA_DB.Save(&user).Error
if err != nil {
global.GVA_LOG.Error("设置用户VIP失败", zap.Error(err))
return err
}
return nil
}