初始化项目
This commit is contained in:
21
trunk/center/usercenter/internal/game/game.go
Normal file
21
trunk/center/usercenter/internal/game/game.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//注册数据库
|
||||
connection.RegisterDBModel(&Game{})
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
GameID int64 `gorm:"column:game_id;primary_key;comment:用户id;autoIncrementIncrement" json:"gameid"`
|
||||
|
||||
//账号
|
||||
Account string `gorm:"column:account;comment:账号" json:"account"`
|
||||
}
|
||||
|
||||
func (Game) TableName() string {
|
||||
return "user"
|
||||
}
|
||||
77
trunk/center/usercenter/internal/game/logic.go
Normal file
77
trunk/center/usercenter/internal/game/logic.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"common/cache"
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
. "logincenter/internal/user"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 包名称(功能名称)
|
||||
var packageName = "game"
|
||||
|
||||
// 读写锁
|
||||
var lock sync.RWMutex
|
||||
|
||||
func AddGame(user *User, game *Game) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
//判断缓存是否存在
|
||||
gameMap, _ := cache.GetData[map[int64]*Game](user.Cache, packageName)
|
||||
if gameMap == nil {
|
||||
gameMap = make(map[int64]*Game)
|
||||
}
|
||||
|
||||
//添加新的缓存
|
||||
func() {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
gameMap[game.GameID] = game
|
||||
}()
|
||||
|
||||
//写入缓存
|
||||
cache.SetData[*Game](user.Cache, packageName, gameMap)
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Create(&game) // 通过数据的指针来创建
|
||||
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加用户失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return game.GameID, nil
|
||||
}
|
||||
|
||||
// 获取有些列表
|
||||
func GetGameList(user *User) (map[int64]*Game, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
//验证缓存
|
||||
gameMap, _ := cache.GetData[map[int64]*Game](user.Cache, packageName)
|
||||
if gameMap != nil {
|
||||
return gameMap, nil
|
||||
}
|
||||
|
||||
// 获取全部列表
|
||||
var gameList []*Game
|
||||
result := connection.GetUserDB().Find(&gameList) // 通过数据的指针来创建
|
||||
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加用户失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
|
||||
for _, game := range gameList {
|
||||
gameMap[game.GameID] = game
|
||||
}
|
||||
|
||||
//添加缓存
|
||||
func() {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
cache.SetData(user.Cache, packageName, gameMap)
|
||||
}()
|
||||
|
||||
return gameMap, nil
|
||||
}
|
||||
6
trunk/center/usercenter/internal/user.go
Normal file
6
trunk/center/usercenter/internal/user.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
_ "logincenter/internal/game"
|
||||
_ "logincenter/internal/user"
|
||||
)
|
||||
139
trunk/center/usercenter/internal/user/api.go
Normal file
139
trunk/center/usercenter/internal/user/api.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"common/remark"
|
||||
"common/resultStatus"
|
||||
"common/webServer"
|
||||
"goutil/intUtil"
|
||||
"goutil/securityUtil"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
//注册接口
|
||||
webServer.RegisterFunction(new(UserApi))
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "UserApi"
|
||||
desc := "用户接口"
|
||||
author := "tangping"
|
||||
mendor := ""
|
||||
date := "2024-12-25"
|
||||
remark.RegisterModuleRemark(moduleName, desc, author, mendor, date)
|
||||
}
|
||||
|
||||
// UserApi 用户接口
|
||||
type UserApi struct {
|
||||
}
|
||||
|
||||
// ---------------------------------------- 接口 --------------------------------------------------
|
||||
func init() {
|
||||
moduleName := "UserApi"
|
||||
methodName := "Register"
|
||||
methodDesc := "注册用户"
|
||||
methodAuthor := "tangping"
|
||||
methodMendor := ""
|
||||
methodDate := "2024-12-25 15:55:00"
|
||||
methodInParam := []string{"string 账号,string:用户名称,string:用户密码,string:用户性别,string:用户生日,int64:用户手机号,string:用户邮箱,string:用户微信群,string:用户备注"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
"id '类型:int'": "用户id",
|
||||
}
|
||||
}`
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam)
|
||||
}
|
||||
func (a *UserApi) Register(account string, name string, password string, sex int32, birthday string, phone int64, email string, wechatGroup string, describe string) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
|
||||
//校验参数
|
||||
if account == "" || name == "" || password == "" || sex == 0 || birthday == "" || phone == 0 || email == "" || wechatGroup == "" {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
|
||||
//密码md5加密
|
||||
password = securityUtil.Md5String(password, true)
|
||||
|
||||
//处理数据
|
||||
UserModel := &User{
|
||||
Account: account,
|
||||
Name: name,
|
||||
Password: password,
|
||||
Sex: sex,
|
||||
Birthday: birthday,
|
||||
Phone: phone,
|
||||
Email: email,
|
||||
Describe: describe,
|
||||
}
|
||||
|
||||
//添加到数据库
|
||||
AddUser(UserModel)
|
||||
|
||||
resultMap := make(map[string]any)
|
||||
resultMap["id"] = UserModel.ID
|
||||
responseObj.SetData(resultMap)
|
||||
return
|
||||
}
|
||||
|
||||
// 用户登录
|
||||
func init() {
|
||||
moduleName := "UserApi"
|
||||
methodName := "Login"
|
||||
methodDesc := "用户登录"
|
||||
methodAuthor := "tangping"
|
||||
methodMendor := ""
|
||||
methodDate := "2024-12-26 15:55:00"
|
||||
methodInParam := []string{"string:账号,string:密码"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
"Token '类型:string'": "登录令牌",
|
||||
}
|
||||
}`
|
||||
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam)
|
||||
}
|
||||
|
||||
func (a *UserApi) Login(account string, password string) (responseObj *webServer.ResponseObject) {
|
||||
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
|
||||
//验证参数
|
||||
if account == "" || password == "" {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
|
||||
userData, err := Login(account, securityUtil.Md5String(password, true))
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.DataError)
|
||||
return
|
||||
}
|
||||
|
||||
//生成一个随机token
|
||||
token, err := intUtil.ShuffleIntDigits(time.Now().UnixNano())
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.DataError)
|
||||
return
|
||||
}
|
||||
|
||||
//添加登录用户
|
||||
webServer.AddLoginUserToken(token, &webServer.TokenInfo{Id: userData.ID, Account: account})
|
||||
|
||||
//UserData映射成map
|
||||
resultMap := make(map[string]any)
|
||||
resultMap["Token"] = strconv.FormatInt(token, 10)
|
||||
|
||||
responseObj.SetData(resultMap)
|
||||
return
|
||||
}
|
||||
89
trunk/center/usercenter/internal/user/logic.go
Normal file
89
trunk/center/usercenter/internal/user/logic.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"common/cache"
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 用户缓存对象
|
||||
var userMap = make(map[int64]*User)
|
||||
var rwmu sync.RWMutex
|
||||
|
||||
// GetUserByID 根据用户id获取用户信息
|
||||
func GetUserByID(UserID int64) (*User, error) {
|
||||
|
||||
//判断缓存是否存在
|
||||
var user *User
|
||||
|
||||
func() *User {
|
||||
rwmu.RLock()
|
||||
defer rwmu.RUnlock()
|
||||
ok := true
|
||||
if user, ok = userMap[UserID]; ok {
|
||||
return user
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
if user != nil {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
result := connection.GetUserDB().First(&user, UserID)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
//添加缓存
|
||||
func() {
|
||||
rwmu.Lock()
|
||||
defer rwmu.Unlock()
|
||||
user.Cache = cache.NewCache()
|
||||
userMap[user.ID] = user
|
||||
}()
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// AddUserCache 添加用户缓存
|
||||
func AddUserCache(user *User) {
|
||||
rwmu.Lock()
|
||||
defer rwmu.Unlock()
|
||||
user.Cache = cache.NewCache()
|
||||
userMap[user.ID] = user
|
||||
}
|
||||
|
||||
// AddUser 添加用户
|
||||
// AddUser 添加新的用户到数据库中。
|
||||
// 参数 User: 包含用户信息的对象。
|
||||
// 返回值: 插入操作影响的行数和可能发生的错误。
|
||||
func AddUser(User *User) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
//写入缓存
|
||||
AddUserCache(User)
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Create(&User) // 通过数据的指针来创建
|
||||
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加用户失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return User.ID, nil
|
||||
}
|
||||
|
||||
// Login 用户登录
|
||||
func Login(account string, password string) (*User, error) {
|
||||
|
||||
//这里优先验证缓存数据
|
||||
|
||||
var User User
|
||||
result := connection.GetUserDB().Where("account = ? AND password = ?", account, password).First(&User)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &User, nil
|
||||
}
|
||||
36
trunk/center/usercenter/internal/user/user.go
Normal file
36
trunk/center/usercenter/internal/user/user.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"common/cache"
|
||||
"common/connection"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//注册数据库
|
||||
connection.RegisterDBModel(&User{})
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int64 `gorm:"column:id;primary_key;comment:用户id;autoIncrementIncrement" json:"id"`
|
||||
//账号
|
||||
Account string `gorm:"column:account;comment:账号" json:"account"`
|
||||
Name string `gorm:"column:name;comment:用户名称" json:"name"`
|
||||
Password string `gorm:"column:password;comment:用户密码" json:"password"`
|
||||
//性别
|
||||
Sex int32 `gorm:"column:sex;comment:性别" json:"sex"`
|
||||
//生日
|
||||
Birthday string `gorm:"column:birthday;comment:生日" json:"birthday"`
|
||||
//手机
|
||||
Phone int64 `gorm:"column:phone;comment:手机" json:"phone"`
|
||||
//邮箱
|
||||
Email string `gorm:"column:email;comment:邮箱" json:"email"`
|
||||
//备注
|
||||
Describe string `gorm:"column:describe;comment:备注" json:"describe"`
|
||||
|
||||
//玩家的缓存对象
|
||||
Cache *cache.Cache `gorm:"-"`
|
||||
}
|
||||
|
||||
func (User) TableName() string {
|
||||
return "user"
|
||||
}
|
||||
Reference in New Issue
Block a user