初始化项目
This commit is contained in:
5
trunk/center/admincenter/internal/admin.go
Normal file
5
trunk/center/admincenter/internal/admin.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
_ "admincenter/internal/admin"
|
||||
)
|
||||
34
trunk/center/admincenter/internal/admin/admin.go
Normal file
34
trunk/center/admincenter/internal/admin/admin.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//注册数据库
|
||||
connection.RegisterDBModel(&Admin{})
|
||||
}
|
||||
|
||||
type Admin 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"`
|
||||
//微信群【方便发送通知】
|
||||
WechatGroup string `gorm:"column:wechat_group;comment:微信群" json:"wechat_group"`
|
||||
//备注
|
||||
Describe string `gorm:"column:describe;comment:备注" json:"describe"`
|
||||
}
|
||||
|
||||
func (Admin) TableName() string {
|
||||
return "admin"
|
||||
}
|
||||
198
trunk/center/admincenter/internal/admin/api.go
Normal file
198
trunk/center/admincenter/internal/admin/api.go
Normal file
@@ -0,0 +1,198 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"common/remark"
|
||||
"common/resultStatus"
|
||||
"common/webServer"
|
||||
"goutil/intUtil"
|
||||
"goutil/securityUtil"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
//注册接口
|
||||
webServer.RegisterFunction(new(AdminApi))
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "AdminApi"
|
||||
desc := "管理员接口"
|
||||
author := "tangping"
|
||||
mendor := ""
|
||||
date := "2024-12-25"
|
||||
remark.RegisterModuleRemark(moduleName, desc, author, mendor, date)
|
||||
}
|
||||
|
||||
// AdminApi 管理员接口
|
||||
type AdminApi struct {
|
||||
}
|
||||
|
||||
// ---------------------------------------- 接口 --------------------------------------------------
|
||||
func init() {
|
||||
moduleName := "AdminApi"
|
||||
methodName := "Add"
|
||||
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 *AdminApi) Add(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)
|
||||
|
||||
//处理数据
|
||||
adminModel := &Admin{
|
||||
Account: account,
|
||||
Name: name,
|
||||
Password: password,
|
||||
Sex: sex,
|
||||
Birthday: birthday,
|
||||
Phone: phone,
|
||||
Email: email,
|
||||
WechatGroup: wechatGroup,
|
||||
Describe: describe,
|
||||
}
|
||||
|
||||
//添加到数据库
|
||||
AddAdmin(adminModel)
|
||||
|
||||
resultMap := make(map[string]any)
|
||||
resultMap["id"] = adminModel.ID
|
||||
responseObj.SetData(resultMap)
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "AdminApi"
|
||||
methodName := "Get"
|
||||
methodDesc := "获取管理员用户"
|
||||
methodAuthor := "tangping"
|
||||
methodMendor := ""
|
||||
methodDate := "2024-12-25 15:55:00"
|
||||
methodInParam := []string{"int64:管理员id"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
"account '类型:string'": "管理员账号",
|
||||
"name '类型:string'": "管理员名称",
|
||||
"sex '类型:int32'": "管理员性别",
|
||||
"birthday '类型:string'": "管理员生日",
|
||||
"phone '类型:int64'": "管理员手机号",
|
||||
"email '类型:string'": "管理员邮箱",
|
||||
"wechatGroup '类型:string'": "管理员微信群",
|
||||
"describe '类型:string'": "管理员备注",
|
||||
}
|
||||
}`
|
||||
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam)
|
||||
}
|
||||
func (a *AdminApi) Get(adminId int64) (responseObj *webServer.ResponseObject) {
|
||||
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
|
||||
//验证参数
|
||||
if adminId == 0 {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
|
||||
adminData, err := GetAdminByID(adminId)
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.DataError)
|
||||
return
|
||||
}
|
||||
|
||||
//adminData映射成map
|
||||
resultMap := make(map[string]any)
|
||||
resultMap["account"] = adminData.Account
|
||||
resultMap["name"] = adminData.Name
|
||||
resultMap["sex"] = adminData.Sex
|
||||
resultMap["birthday"] = adminData.Birthday
|
||||
resultMap["phone"] = adminData.Phone
|
||||
resultMap["email"] = adminData.Email
|
||||
resultMap["wechatGroup"] = adminData.WechatGroup
|
||||
resultMap["describe"] = adminData.Describe
|
||||
|
||||
responseObj.SetData(resultMap)
|
||||
return
|
||||
}
|
||||
|
||||
// 管理员登录
|
||||
func init() {
|
||||
moduleName := "AdminApi"
|
||||
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 *AdminApi) Login(account string, password string) (responseObj *webServer.ResponseObject) {
|
||||
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
|
||||
//验证参数
|
||||
if account == "" || password == "" {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
|
||||
adminData, 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: adminData.ID, Account: account})
|
||||
|
||||
//adminData映射成map
|
||||
resultMap := make(map[string]any)
|
||||
resultMap["Token"] = strconv.FormatInt(token, 10)
|
||||
|
||||
responseObj.SetData(resultMap)
|
||||
return
|
||||
}
|
||||
43
trunk/center/admincenter/internal/admin/logic.go
Normal file
43
trunk/center/admincenter/internal/admin/logic.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
)
|
||||
|
||||
// AddAdmin 添加管理员
|
||||
// AddAdmin 添加新的管理员到数据库中。
|
||||
// 参数 admin: 包含管理员信息的对象。
|
||||
// 返回值: 插入操作影响的行数和可能发生的错误。
|
||||
func AddAdmin(admin *Admin) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetAdminDB().Create(&admin) // 通过数据的指针来创建
|
||||
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加管理员失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return admin.ID, nil
|
||||
}
|
||||
|
||||
// GetAdminByID 根据管理员ID获取管理员信息
|
||||
func GetAdminByID(adminID int64) (*Admin, error) {
|
||||
var admin Admin
|
||||
result := connection.GetAdminDB().First(&admin, adminID)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &admin, nil
|
||||
}
|
||||
|
||||
// 管理员登录
|
||||
func Login(account string, password string) (*Admin, error) {
|
||||
var admin Admin
|
||||
result := connection.GetAdminDB().Where("account = ? AND password = ?", account, password).First(&admin)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &admin, nil
|
||||
}
|
||||
Reference in New Issue
Block a user