Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
WorldEpcho
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ford
WorldEpcho
Commits
fb5e23a9
Commit
fb5e23a9
authored
Oct 29, 2024
by
Ford
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加了用户画像的创建和查询相关世界的用户画像
parent
44ca2d74
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
515 additions
and
66 deletions
+515
-66
config.json
+17
-1
src/config/config.go
+25
-7
src/controllers/NpcController.go
+318
-0
src/controllers/WorldController.go
+9
-50
src/main.go
+9
-6
src/models/NPCRole.go
+85
-0
src/routers/routers.go
+5
-1
src/service/WorldChat_HighConcurrency.go
+1
-1
src/utils/utils.go
+46
-0
src/worldEpcho
+0
-0
No files found.
config.json
View file @
fb5e23a9
...
...
@@ -208,7 +208,23 @@
"adaptability"
:
"应变能力"
,
"communicationSkills"
:
"沟通能力"
,
"socialEtiquette"
:
"社交礼仪"
}
},
"npc_role_detail_translations"
:
{
"roleName"
:
"角色名称"
,
"introduction"
:
"简介"
,
"consumptionExpectations"
:
"消费心理预期"
,
"psychologicalTraits"
:
"心理特征"
,
"appearance"
:
"外观"
,
"personality"
:
"性格"
,
"decisionMakingStyle"
:
"决策风格"
,
"riskPreference"
:
"风险偏好"
,
"attitudeTowardsUsers"
:
"对用户的态度"
,
"educationLevel"
:
"教育水平"
,
"professionalBackground"
:
"职业背景"
,
"purpose"
:
"目的性"
,
"knownInformation"
:
"已知信息"
}
...
...
src/config/config.go
View file @
fb5e23a9
...
...
@@ -62,9 +62,13 @@ type AppConfig struct {
JwtSecret
string
`json:"jwt_secret"`
MiGuAuthId
string
`json:"miGuAuthId"`
// 增加 BusinessManagerAssessmentConfigTranslations 字段
// 增加
业务经理考核系统世界配置信息
BusinessManagerAssessmentConfigTranslations 字段
BusinessManagerAssessmentConfigTranslations
map
[
string
]
string
`json:"business_manager_assessment_config_translations"`
//英文转中文
ReverseTranslations
map
[
string
]
string
// 反向映射,中文转英文,不需要从JSON解码
BusinessManagerReverseTranslations
map
[
string
]
string
// 反向映射,中文转英文,不需要从JSON解码
// 用户画像角色详情信息 NpcRoleDetail
NpcRoleDetailTranslations
map
[
string
]
string
`json:"npc_role_detail_translations"`
//英文对照中文
NpcRoleDetailReverseTranslations
map
[
string
]
string
// 反向映射,中文转英文,不需要从JSON解码
SoulUrl
string
`json:"soul_url"`
SoulAuth
string
`json:"soul_auth"`
...
...
@@ -120,6 +124,15 @@ func GetModelPathsByName(name string) (string, string, bool) {
return
""
,
""
,
false
}
// 用于反转映射的辅助函数
func
ReverseMap
(
m
map
[
string
]
string
)
map
[
string
]
string
{
reversed
:=
make
(
map
[
string
]
string
)
for
k
,
v
:=
range
m
{
reversed
[
v
]
=
k
}
return
reversed
}
func
InitConfig
()
*
AppConfig
{
fmt
.
Println
(
" 读取配置文件... "
)
file
,
err
:=
os
.
Open
(
"./config.json"
)
...
...
@@ -149,12 +162,17 @@ func InitConfig() *AppConfig {
}
// 创建世界配置信息json反向映射字典
reverseTranslations
:=
make
(
map
[
string
]
string
)
for
k
,
v
:=
range
conf
.
BusinessManagerAssessmentConfigTranslations
{
reverseTranslations
[
v
]
=
k
}
//reverseTranslations := make(map[string]string)
//for k, v := range conf.BusinessManagerAssessmentConfigTranslations {
// reverseTranslations[v] = k
//}
reverseTranslations
:=
ReverseMap
(
conf
.
BusinessManagerAssessmentConfigTranslations
)
// 将反向映射存储为全局变量,或添加到配置结构中
conf
.
ReverseTranslations
=
reverseTranslations
// 假设你在AppConfig中添加了这个字段
conf
.
BusinessManagerReverseTranslations
=
reverseTranslations
// 假设你在AppConfig中添加了这个字段
//将用户画像的角色详情信息反向中文转英文映射
NpcRoleDetailReverseMap
:=
ReverseMap
(
conf
.
NpcRoleDetailTranslations
)
conf
.
NpcRoleDetailReverseTranslations
=
NpcRoleDetailReverseMap
Conf
=
&
conf
return
&
conf
...
...
src/controllers/NpcController.go
0 → 100644
View file @
fb5e23a9
package
controllers
import
(
"WorldEpcho/src/config/e"
database
"WorldEpcho/src/datasource"
"WorldEpcho/src/models"
"WorldEpcho/src/service"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"strings"
"time"
)
// 定义英文-中文对照字典
/*var NpcRoleDetail = map[string]string{
"roleName": "角色名称",
"introduction": "简介",
"consumptionExpectations": "消费心理预期",
"psychologicalTraits": "心理特征",
"appearance": "外观",
"personality": "性格",
"decisionMakingStyle": "决策风格",
"riskPreference": "风险偏好",
"attitudeTowardsUsers": "对用户的态度",
"educationLevel": "教育水平",
"professionalBackground": "职业背景",
"purpose": "目的性",
"knownInformation": "已知信息",
}
*/
//创建角色画像的请求体
type
NpcRoleRequest
struct
{
Name
string
`json:"name"`
// 名称
Details
Details
`json:"details"`
//详情
Gender
string
`json:"gender"`
//性别
Tags
string
`json:"tags"`
//世界标签集
RelatedWorlds
string
`json:"relatedWorlds"`
//相关世界
UpdateTime
int64
`json:"updateTime"`
//更新时间
CreateTime
int64
`json:"createTime"`
//构造时间
ServiceProviderId
string
`json:"authId,omitempty"`
// ServiceProviderId
}
// Detail 用于解析详细信息中的 JSON 字符串
type
Details
struct
{
RoleName
string
`json:"roleName"`
//角色名称
Introduction
Introduction
`json:"introduction"`
//简介
ConsumptionExpectations
[]
string
`json:"consumptionExpectations"`
//消费心理预期
PsychologicalTraits
[]
string
`json:"psychologicalTraits"`
//心理特征
Appearance
string
`json:"appearance"`
//外观
Personality
string
`json:"personality"`
//性格
DecisionMakingStyle
string
`json:"decisionMakingStyle"`
//决策风格
RiskPreference
string
`json:"riskPreference"`
//风险偏好
AttitudeTowardsUsers
string
`json:"attitudeTowardsUsers"`
//对用户的态度
}
//个人传记 Personal profile
type
Introduction
struct
{
EducationLevel
string
`json:"educationLevel"`
//教育水平
ProfessionalBackground
string
`json:"professionalBackground"`
//职业背景
Purpose
string
`json:"purpose"`
//目的性
KnownInformation
string
`json:"knownInformation"`
//已知信息
}
//创建Npc用户画像的接口
func
CreateNpcUserPortrait
(
ctx
*
gin
.
Context
)
{
var
NpcRequest
NpcRoleRequest
if
err
:=
ctx
.
ShouldBindJSON
(
&
NpcRequest
);
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
InvalidParams
,
"data"
:
nil
,
"message"
:
"请求数据不正确"
})
return
}
// 从请求头 Authorization 中提取令牌
tokenString
:=
ctx
.
GetHeader
(
"Token"
)
if
tokenString
!=
""
{
// 解析并验证 JWT 令牌
isValid
,
err
:=
service
.
IsValidMiGuToken
(
tokenString
)
if
err
!=
nil
||
!
isValid
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
InvalidToken
,
"data"
:
nil
,
"message"
:
"无效或已过期的令牌"
})
return
}
}
else
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
UnauthorizedStatus
,
"data"
:
nil
,
"message"
:
"请求头中无token,或未授权的token访问"
})
return
}
if
NpcRequest
.
Name
==
""
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
EmptyParamsError
,
"data"
:
nil
,
"message"
:
"用户角色名称不能为空"
})
return
}
if
NpcRequest
.
ServiceProviderId
==
""
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
EmptyParamsError
,
"data"
:
nil
,
"message"
:
"服务商id不能为空"
})
return
}
sp
,
err
:=
models
.
GetServiceProviderById
(
NpcRequest
.
ServiceProviderId
)
if
err
!=
nil
||
sp
==
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
NotFound
,
"data"
:
nil
,
"message"
:
"根据服务商id查询服务商出错或服务商不存在"
,
"error"
:
err
.
Error
()})
return
}
//可以访问的标签
// 创建一个空切片存储匹配的标签
matchingTags
:=
[]
string
{}
// 分割字符串,得到单独的字符串
tagsSplit
:=
strings
.
Split
(
NpcRequest
.
Tags
,
","
)
for
_
,
tagStr
:=
range
tagsSplit
{
//如果包含服务商可访问的世界标签,则遍历将包含的标签放入数组
// 检查每个用户标签是否被服务商允许
if
ContainsString
(
sp
.
AccessibleTags
,
tagStr
)
{
matchingTags
=
append
(
matchingTags
,
tagStr
)
}
}
// 如果没有任何匹配的标签,则返回错误信息
if
len
(
matchingTags
)
==
0
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
NotFound
,
"data"
:
nil
,
"message"
:
"不包含该服务商可访问的世界标签!"
})
fmt
.
Println
(
"不包含该服务商可访问的世界标签"
)
return
}
// 序列化 Details 为 JSON 字符串
detailsJSON
,
err
:=
json
.
Marshal
(
NpcRequest
.
Details
)
if
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
InternalError
,
"data"
:
nil
,
"message"
:
"详情信息序列化失败"
})
return
}
if
string
(
detailsJSON
)
==
""
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
EmptyParamsError
,
"data"
:
nil
,
"message"
:
"用户详情信息不能为空"
})
return
}
/* configData 英文键转换为中文 */
/*
var data map[string]interface{}
if err := json.Unmarshal([]byte(string(detailsJSON)), &data); err != nil {
ctx.JSON(http.StatusOK, gin.H{"code": e.ParamParseError, "data": nil, "message": "解析用户画像详细信息数据出错"})
return
}
// 转换键名 => 英文转中文
NpcRoleDetailTranslations := config.Conf.NpcRoleDetailTranslations
translatedData := utils.TranslateKeysEnglishToChinese(NpcRoleDetailTranslations, data)
translatedJSON, err := json.MarshalIndent(translatedData, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
ctx.JSON(http.StatusOK, gin.H{"code": e.ParamParseError, "data": nil, "message": "解析用户画像详细信息数据出错"})
return
}
fmt.Println(" Details translatedJSON ==> ", string(translatedJSON))
*/
// 创建一个空切片存储相关世界
relatedWorldIds
:=
[]
int64
{}
// 分割字符串,得到单独的字符串
RelatedWorldIdsSplit
:=
strings
.
Split
(
NpcRequest
.
RelatedWorlds
,
","
)
for
_
,
RelatedWorldId
:=
range
RelatedWorldIdsSplit
{
worldId
,
err
:=
strconv
.
ParseInt
(
RelatedWorldId
,
10
,
64
)
if
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
ParamParseError
,
"data"
:
nil
,
"message"
:
"相关世界ID类型转换错误"
})
return
}
relatedWorldIds
=
append
(
relatedWorldIds
,
worldId
)
}
//开启事务
session
:=
database
.
Engine
.
NewSession
()
defer
session
.
Close
()
if
err
:=
session
.
Begin
();
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
InternalError
,
"data"
:
nil
,
"message"
:
"开启事务失败"
})
return
}
//用户画像详情信息
// 将Details结构体转换为 JSON
jsonData
,
err
:=
json
.
Marshal
(
NpcRequest
.
Details
)
if
err
!=
nil
{
fmt
.
Println
(
"Error:"
,
err
)
return
}
npcUserRole
:=
&
models
.
NpcRole
{
Name
:
NpcRequest
.
Name
,
Details
:
string
(
jsonData
),
Gender
:
NpcRequest
.
Gender
,
Tags
:
matchingTags
,
RelatedWorlds
:
relatedWorldIds
,
CreateTime
:
time
.
Now
()
.
Unix
(),
UpdateTime
:
time
.
Now
()
.
Unix
(),
}
var
newNpcRole
*
models
.
NpcRole
if
newNpcRole
,
err
=
models
.
CreateNpcRole
(
session
,
npcUserRole
);
err
!=
nil
{
session
.
Rollback
()
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
ErrorDatabase
,
"data"
:
nil
,
"message"
:
"创建用户画像信息出错"
})
return
}
if
err
:=
session
.
Commit
();
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
InternalError
,
"data"
:
nil
,
"message"
:
"事务提交失败"
})
return
}
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
SUCCESS
,
"data"
:
gin
.
H
{
"newNpcRoleInfo"
:
newNpcRole
},
"message"
:
"创建用户画像成功"
})
}
// 定义查询业务经理考核系统世界信息的请求参数的结构体
type
NpcRoleRequestParams
struct
{
WorldId
int64
`json:"worldId"`
Tags
string
`json:"tags"`
ServiceProviderId
string
`json:"authId,omitempty"`
// ServiceProviderId
}
// 查询业务经理考核相关的世界信息接口2
func
QueryNpcRoleInfo
(
ctx
*
gin
.
Context
)
{
var
params
NpcRoleRequestParams
// 从请求体解析 JSON 参数
if
err
:=
ctx
.
BindJSON
(
&
params
);
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
ParamParseError
,
"data"
:
nil
,
"message"
:
"参数解析错误"
})
return
}
// 从请求头 Authorization 中提取令牌
tokenString
:=
ctx
.
GetHeader
(
"Token"
)
if
tokenString
==
""
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
UnauthorizedStatus
,
"data"
:
nil
,
"message"
:
"请求头中无token,或未授权的token访问"
})
return
}
// 解析并验证 JWT 令牌
isValid
,
err
:=
service
.
IsValidMiGuToken
(
tokenString
)
if
err
!=
nil
||
!
isValid
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
InvalidToken
,
"data"
:
nil
,
"message"
:
"无效或已过期的令牌"
})
return
}
if
params
.
ServiceProviderId
==
""
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
EmptyParamsError
,
"data"
:
nil
,
"message"
:
"服务商id不能为空"
})
return
}
sp
,
err
:=
models
.
GetServiceProviderById
(
params
.
ServiceProviderId
)
if
err
!=
nil
||
sp
==
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
NotFound
,
"data"
:
nil
,
"message"
:
"根据服务商id查询服务商出错或服务商不存在"
,
"error"
:
err
.
Error
()})
return
}
//可以访问的标签
// 创建一个空切片存储匹配的标签
matchingTags
:=
[]
string
{}
// 分割字符串,得到单独的字符串
tagsSplit
:=
strings
.
Split
(
params
.
Tags
,
","
)
for
_
,
tagStr
:=
range
tagsSplit
{
//如果包含服务商可访问的世界标签,则遍历将包含的标签放入数组
// 检查每个用户标签是否被服务商允许
if
ContainsString
(
sp
.
AccessibleTags
,
tagStr
)
{
matchingTags
=
append
(
matchingTags
,
tagStr
)
}
}
// 如果没有任何匹配的标签,则返回错误信息
if
len
(
matchingTags
)
==
0
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
NotFound
,
"data"
:
nil
,
"message"
:
"不包含该服务商可访问的世界标签!"
})
fmt
.
Println
(
"不包含该服务商可访问的世界标签"
)
return
}
/*
worldInfo, err := models.FindWorlds2(params.WorldName, tagsArr)
if err != nil {
ctx.JSON(http.StatusOK, gin.H{"code": e.ErrorDatabase, "data": nil, "message": "查询世界信息失败"})
return
}*/
//开启事务
session
:=
database
.
Engine
.
NewSession
()
defer
session
.
Close
()
//延迟关闭session
err1
:=
session
.
Begin
()
if
err1
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
0
,
"message"
:
"开启事务失败!"
})
fmt
.
Println
(
"开启事务失败"
)
return
}
//查询包含指定related_worlds ID的NPC角色数组
npcRoles
,
err
:=
models
.
FindNpcRolesByWorldID
(
session
,
params
.
WorldId
)
if
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
ErrorDatabase
,
"data"
:
nil
,
"message"
:
"根据相关世界ID查询用户画像信息出错!"
})
fmt
.
Println
(
"根据相关世界ID查询用户画像信息出错"
)
return
}
//提交事务
err
=
session
.
Commit
()
if
err
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
0
,
"message"
:
"提交事务失败"
})
return
}
if
npcRoles
!=
nil
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
SUCCESS
,
"data"
:
gin
.
H
{
"npcRoles"
:
npcRoles
},
"message"
:
"查询相关世界的用户画像信息成功"
,
})
}
else
{
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
e
.
NotFound
,
"data"
:
nil
,
"message"
:
"未查相关世界的用户画像信息"
,
})
}
}
src/controllers/WorldController.go
View file @
fb5e23a9
...
...
@@ -67,52 +67,6 @@ func init() {
}
}*/
// translateKeys 是递归函数,用于转换JSON对象的键从中文到英文
// 中文到英文
func
TranslateKeysChineseToEnglish
(
data
interface
{})
interface
{}
{
ReverseTranslations
:=
config
.
Conf
.
ReverseTranslations
switch
obj
:=
data
.
(
type
)
{
case
map
[
string
]
interface
{}
:
newMap
:=
make
(
map
[
string
]
interface
{})
for
k
,
v
:=
range
obj
{
newKey
:=
k
if
val
,
ok
:=
ReverseTranslations
[
k
];
ok
{
newKey
=
val
}
newMap
[
newKey
]
=
TranslateKeysChineseToEnglish
(
v
)
}
return
newMap
case
[]
interface
{}
:
for
i
,
v
:=
range
obj
{
obj
[
i
]
=
TranslateKeysChineseToEnglish
(
v
)
}
}
return
data
}
// translateKeys 是递归函数,用于转换JSON对象的键
// 英文转中文
func
TranslateKeysEnglishToChinese
(
data
interface
{})
interface
{}
{
BusinessManagerAssessmentTranslations
:=
config
.
Conf
.
BusinessManagerAssessmentConfigTranslations
switch
obj
:=
data
.
(
type
)
{
case
map
[
string
]
interface
{}
:
newMap
:=
make
(
map
[
string
]
interface
{})
for
k
,
v
:=
range
obj
{
newKey
:=
k
if
val
,
ok
:=
BusinessManagerAssessmentTranslations
[
k
];
ok
{
newKey
=
val
}
newMap
[
newKey
]
=
TranslateKeysEnglishToChinese
(
v
)
}
return
newMap
case
[]
interface
{}
:
for
i
,
v
:=
range
obj
{
obj
[
i
]
=
TranslateKeysEnglishToChinese
(
v
)
}
}
return
data
}
//查询用户与那些世界进行过聊天会话
func
QueryUserToWorldConversation
(
ctx
*
gin
.
Context
)
{
/*
...
...
@@ -988,7 +942,8 @@ func MiGuCreateBusinessManagerAssessmentSystem1(ctx *gin.Context) {
}
// 转换键名
translatedData
:=
TranslateKeysEnglishToChinese
(
data
)
BusinessManagerAssessmentTranslations
:=
config
.
Conf
.
BusinessManagerAssessmentConfigTranslations
translatedData
:=
utils
.
TranslateKeysEnglishToChinese
(
BusinessManagerAssessmentTranslations
,
data
)
translatedJSON
,
err
:=
json
.
MarshalIndent
(
translatedData
,
""
,
" "
)
if
err
!=
nil
{
fmt
.
Println
(
"Error marshalling JSON:"
,
err
)
...
...
@@ -1169,7 +1124,8 @@ func MiGuCreateBusinessManagerAssessmentSystem(ctx *gin.Context) {
return
}
// 转换键名
translatedData
:=
TranslateKeysEnglishToChinese
(
data
)
BusinessManagerAssessmentTranslations
:=
config
.
Conf
.
BusinessManagerAssessmentConfigTranslations
translatedData
:=
utils
.
TranslateKeysEnglishToChinese
(
BusinessManagerAssessmentTranslations
,
data
)
translatedJSON
,
err
:=
json
.
MarshalIndent
(
translatedData
,
""
,
" "
)
if
err
!=
nil
{
fmt
.
Println
(
"Error marshalling JSON:"
,
err
)
...
...
@@ -1372,7 +1328,8 @@ func EditBusinessManagerAssessmentSystem(ctx *gin.Context) {
return
}
/* var panel models.AssessmentCustomizationPanel
/*
var panel models.AssessmentCustomizationPanel
if err := ctx.BindJSON(&panel); err != nil {
ctx.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
...
...
@@ -1700,7 +1657,9 @@ func MiGuQueryBusinessManagerWorldInfo(ctx *gin.Context) {
return
}
// 转换键名
translatedData
:=
TranslateKeysChineseToEnglish
(
data
)
// 中文转英文
ReverseTranslations
:=
config
.
Conf
.
BusinessManagerReverseTranslations
translatedData
:=
utils
.
TranslateKeysChineseToEnglish
(
ReverseTranslations
,
data
)
translatedJSON
,
err
:=
json
.
MarshalIndent
(
translatedData
,
""
,
" "
)
if
err
!=
nil
{
fmt
.
Println
(
"Error marshalling JSON:"
,
err
)
...
...
src/main.go
View file @
fb5e23a9
...
...
@@ -3,12 +3,15 @@ package main
import
(
"WorldEpcho/src/config"
"WorldEpcho/src/datasource"
"WorldEpcho/src/https_auth"
"WorldEpcho/src/models"
"WorldEpcho/src/routers"
"WorldEpcho/src/service"
"WorldEpcho/src/utils"
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
...
...
@@ -22,10 +25,10 @@ func main() {
config
.
RandInit
()
datasource
.
InitMysql
()
// 同步结构体与数据库表
/* err := datasource.Engine.Sync2(new(models.WorldDeductionResult
))
err
:=
datasource
.
Engine
.
Sync2
(
new
(
models
.
NpcRole
))
if
err
!=
nil
{
log.Fatal
("创建表失败:", err)
}*/
fmt
.
Printf
(
"创建表失败:"
,
err
)
}
//生成一个9位的邀请码
//println("生成一个9位的邀请码: ", utils.GenerateInviteCode(1))
...
...
@@ -99,7 +102,7 @@ func main() {
//service.ExampleSend()
/*
router.GET("/https_test", func(c *gin.Context) {
router
.
GET
(
"/https_test"
,
func
(
c
*
gin
.
Context
)
{
fmt
.
Println
(
c
.
Request
.
Host
)
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
http
.
StatusOK
,
...
...
@@ -108,11 +111,11 @@ func main() {
})
router
.
Use
(
https_auth
.
HttpsHandler
())
//https对应的中间件
path
:=
"https_auth/CA/"
//证书的路径
router.RunTLS(":8099", path+"admin.mindepoch.com.pem", path+"admin.mindepoch.com.key") //开启HTTPS服务*/
router
.
RunTLS
(
":8099"
,
path
+
"admin.mindepoch.com.pem"
,
path
+
"admin.mindepoch.com.key"
)
//开启HTTPS服务
//初始化redis配置
//utils.InitRedisStore()
//Run("里面不指定端口号默认为8080")
router
.
Run
(
":8099"
)
//
router.Run(":8099")
}
src/models/NPCRole.go
0 → 100644
View file @
fb5e23a9
package
models
import
(
"WorldEpcho/src/datasource"
"fmt"
"github.com/go-xorm/xorm"
"strconv"
)
// NPCRole 表示一个 NPC 角色的全部信息
type
NpcRole
struct
{
ID
int64
`xorm:"'id' pk autoincr" json:"id"`
//NPC角色ID
Name
string
`xorm:"'name'" json:"name"`
// 名称
Details
string
`xorm:"'details' TEXT" json:"details"`
//详情 JSON string
Gender
string
`xorm:"'gender'" json:"gender"`
//性别
Tags
[]
string
`xorm:"'tags' json" json:"tags"`
//世界标签集
RelatedWorlds
[]
int64
`xorm:"'related_worlds' json" json:"related_worlds"`
//相关世界
UpdateTime
int64
`xorm:"updated" json:"update_time"`
//更新时间
CreateTime
int64
`xorm:"created" json:"create_time"`
//构造时间
ServiceProviderId
string
`xorm:"-" json:"authId,omitempty"`
// ServiceProviderId 在数据库中忽略
}
// 增加一个新的 NPC 角色
func
CreateNpcRole
(
session
*
xorm
.
Session
,
npcRole
*
NpcRole
)
(
*
NpcRole
,
error
)
{
_
,
err
:=
session
.
Insert
(
npcRole
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"插入NPC角色失败: %v"
,
err
)
}
return
npcRole
,
nil
}
// FindNpcRolesByWorldID 查询包含指定related_worlds ID的NPC角色数组
func
FindNpcRolesByWorldID
(
session
*
xorm
.
Session
,
worldID
int64
)
([]
NpcRole
,
error
)
{
var
npcRoles
[]
NpcRole
// 使用LIKE操作符来搜索包含特定world ID的文本字段
worldIDStr
:=
strconv
.
FormatInt
(
worldID
,
10
)
// 将参与者ID转换为字符串
query
:=
fmt
.
Sprintf
(
"[%s]"
,
worldIDStr
)
err
:=
session
.
Where
(
"JSON_CONTAINS(related_worlds, ?)"
,
query
)
.
Find
(
&
npcRoles
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"根据相关世界ID查询画像失败: %v"
,
err
)
}
return
npcRoles
,
nil
}
// 根据 ID 删除 NPC 角色
func
DeleteNpcRole
(
id
int
)
error
{
npcRole
:=
new
(
NpcRole
)
_
,
err
:=
datasource
.
Engine
.
ID
(
id
)
.
Delete
(
npcRole
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to delete npc role: %v"
,
err
)
}
return
nil
}
// 更新 NPC 角色信息
func
UpdateNpcRole
(
npcRole
*
NpcRole
)
error
{
_
,
err
:=
datasource
.
Engine
.
ID
(
npcRole
.
ID
)
.
Update
(
npcRole
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to update npc role: %v"
,
err
)
}
return
nil
}
// 根据 ID 查询单个 NPC 角色
func
GetNpcRoleByID
(
id
int
)
(
*
NpcRole
,
error
)
{
npcRole
:=
new
(
NpcRole
)
has
,
err
:=
datasource
.
Engine
.
ID
(
id
)
.
Get
(
npcRole
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to get npc role: %v"
,
err
)
}
if
!
has
{
return
nil
,
fmt
.
Errorf
(
"npc role not found"
)
}
return
npcRole
,
nil
}
// 查询所有 NPC 角色
func
GetAllNpcRoles
()
([]
NpcRole
,
error
)
{
var
npcRoles
[]
NpcRole
err
:=
datasource
.
Engine
.
Find
(
&
npcRoles
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to get all npc roles: %v"
,
err
)
}
return
npcRoles
,
nil
}
src/routers/routers.go
View file @
fb5e23a9
...
...
@@ -61,8 +61,12 @@ func InitRouter() *gin.Engine {
router
.
POST
(
"/MiGuQueryBusinessManagerWorldInfo"
,
controllers
.
MiGuQueryBusinessManagerWorldInfo
)
//语音识别
router
.
POST
(
"/MiGuSpeechConvertor"
,
service
.
MiGuTranscribeAudio
)
// 创建用户画像
router
.
POST
(
"/MiGuCreateNpcUserPortrait"
,
controllers
.
CreateNpcUserPortrait
)
// 查询用户画像
router
.
POST
(
"/MiGuQueryNpcRoleInfo"
,
controllers
.
QueryNpcRoleInfo
)
//
======================================================================
//
//
======================================================================
//
//语音识别路由
router
.
POST
(
"/speechConvertor"
,
service
.
TranscribeAudio
)
//AI接口绘制漫画头像
...
...
src/service/WorldChat_HighConcurrency.go
View file @
fb5e23a9
...
...
@@ -45,7 +45,7 @@ type WorldReplyMsg struct {
// 转发AI接口意识流响应给前端
type
WorldSoulReplyMsg
struct
{
Code
int
`json:"code"`
// 5 ==>
Code
int
`json:"code"`
WObj
map
[
string
]
interface
{}
`json:"WObj"`
ISLIU
string
`json:"ISLIU"`
WorldName
string
`json:"WorldName"`
...
...
src/utils/utils.go
View file @
fb5e23a9
...
...
@@ -1008,3 +1008,49 @@ func SendMessage_HUAWEICloud(templateParas map[string]string, phones, smsTemplat
return
nil
}
// translateKeys 是递归函数,用于转换JSON对象的键从中文到英文
// 中文到英文
func
TranslateKeysChineseToEnglish
(
translateKeysMap
map
[
string
]
string
,
data
interface
{})
interface
{}
{
//ReverseTranslations := config.Conf.BusinessManagerReverseTranslations
switch
obj
:=
data
.
(
type
)
{
case
map
[
string
]
interface
{}
:
newMap
:=
make
(
map
[
string
]
interface
{})
for
k
,
v
:=
range
obj
{
newKey
:=
k
if
val
,
ok
:=
translateKeysMap
[
k
];
ok
{
newKey
=
val
}
newMap
[
newKey
]
=
TranslateKeysChineseToEnglish
(
translateKeysMap
,
v
)
}
return
newMap
case
[]
interface
{}
:
for
i
,
v
:=
range
obj
{
obj
[
i
]
=
TranslateKeysChineseToEnglish
(
translateKeysMap
,
v
)
}
}
return
data
}
// translateKeys 是递归函数,用于转换JSON对象的键
// 英文转中文
func
TranslateKeysEnglishToChinese
(
translateKeysMap
map
[
string
]
string
,
data
interface
{})
interface
{}
{
//BusinessManagerAssessmentTranslations := config.Conf.BusinessManagerAssessmentConfigTranslations
switch
obj
:=
data
.
(
type
)
{
case
map
[
string
]
interface
{}
:
newMap
:=
make
(
map
[
string
]
interface
{})
for
k
,
v
:=
range
obj
{
newKey
:=
k
if
val
,
ok
:=
translateKeysMap
[
k
];
ok
{
newKey
=
val
}
newMap
[
newKey
]
=
TranslateKeysEnglishToChinese
(
translateKeysMap
,
v
)
}
return
newMap
case
[]
interface
{}
:
for
i
,
v
:=
range
obj
{
obj
[
i
]
=
TranslateKeysEnglishToChinese
(
translateKeysMap
,
v
)
}
}
return
data
}
src/worldEpcho
View file @
fb5e23a9
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment