feat: client 增删改查

This commit is contained in:
Blizzard
2025-05-08 23:03:00 +08:00
parent 75e9157e5e
commit ab51ba91bc
41 changed files with 1093 additions and 57 deletions
+76
View File
@@ -0,0 +1,76 @@
package system
import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"go.uber.org/zap"
"sundynix-go/global"
"sundynix-go/model/commom/response"
"sundynix-go/model/system"
systemReq "sundynix-go/model/system/request"
systemRes "sundynix-go/model/system/response"
"sundynix-go/utils"
)
var store = base64Captcha.DefaultMemStore
type AuthApi struct{}
// Login api
func (a *AuthApi) Login(c *gin.Context) {
var l systemReq.Login
err := c.ShouldBindJSON(&l)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
if l.CaptchaId != "" && l.Captcha != "" && store.Verify(l.CaptchaId, l.Captcha, true) {
u := &system.User{Account: l.Account, Password: l.Password}
user, err := UserService.Login(u)
if err != nil {
global.Logger.Error("登录失败! 用户名不存在或者密码错误!", zap.Error(err))
response.FailWithMsg("用户名不存在或者密码错误", c)
return
}
a.GetToken(c, *user)
return
}
response.FailWithMsg("验证码错误", c)
}
// Captcha api 生成验证码
func (u *AuthApi) Captcha(c *gin.Context) {
var driver = base64Captcha.DriverString{
Height: 80,
Width: 240,
NoiseCount: 2,
ShowLineOptions: 4,
Length: 6,
Source: "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",
}
cp := base64Captcha.NewCaptcha(&driver, store)
id, b64s, _, err := cp.Generate()
if err != nil {
global.Logger.Error("GenerateCaptcha err", zap.Error(err))
response.FailWithMsg("GenerateCaptcha err", c)
return
}
response.OkWithData(systemRes.CaptchaRes{
CaptchaId: id,
Captcha: b64s,
}, c)
}
func (a *AuthApi) GetToken(c *gin.Context, user system.User) {
token, claims, err := utils.GetLoginToken(&user)
if err != nil {
global.Logger.Error("GetToken err", zap.Error(err))
response.FailWithMsg("GetToken err", c)
}
response.OkWithData(systemRes.LoginResponse{
ExpiresAt: claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
Token: token,
User: user,
}, c)
}
+9 -1
View File
@@ -1,6 +1,14 @@
package system
import "sundynix-go/service"
type ApiGroup struct {
LoginApi
AuthApi
UserApi
ClientApi
}
var (
UserService = service.ServiceGroupApp.SystemServiceGroup.UserService
ClientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
)
-15
View File
@@ -1,15 +0,0 @@
package system
import "github.com/gin-gonic/gin"
type LoginApi struct{}
// Login
func (u LoginApi) Login(c *gin.Context) {
c.String(200, "Hello Admin")
}
// Captcha
func (u LoginApi) Captcha(c *gin.Context) {
c.String(200, "Hello Admin")
}
+100
View File
@@ -0,0 +1,100 @@
package system
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"sundynix-go/global"
"sundynix-go/model/commom/request"
"sundynix-go/model/commom/response"
"sundynix-go/model/system"
systemReq "sundynix-go/model/system/request"
)
type ClientApi struct {
}
func (s *ClientApi) SaveClient(c *gin.Context) {
var client system.Client
err := c.ShouldBindJSON(&client)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = ClientService.SaveClient(client)
if err != nil {
global.Logger.Error("保存客户端失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("保存客户端成功!", c)
}
func (s *ClientApi) UpdateClient(c *gin.Context) {
var client system.Client
err := c.ShouldBindJSON(&client)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = ClientService.UpdateClient(client)
if err != nil {
global.Logger.Error("更新客户端失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("更新客户端成功!", c)
}
func (s *ClientApi) GetClientList(c *gin.Context) {
var pageInfo systemReq.GetClientList
err := c.ShouldBindJSON(&pageInfo)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
list, total, err := ClientService.GetClientList(pageInfo)
if err != nil {
global.Logger.Error("获取客户端列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Current,
PageSize: pageInfo.PageSize,
}, c)
}
func (s *ClientApi) Delete(c *gin.Context) {
var ids request.IdsReq
err := c.ShouldBindJSON(&ids)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = ClientService.DeleteClientByIds(ids)
if err != nil {
global.Logger.Error("删除客户端失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("删除客户端成功!", c)
}
func (s *ClientApi) Detail(c *gin.Context) {
var idInfo request.GetById
err := c.ShouldBindJSON(&idInfo)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
client, err := ClientService.GetClientById(idInfo.ID)
if err != nil {
global.Logger.Error("获取客户端详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(client, c)
}
+29
View File
@@ -1,4 +1,33 @@
package system
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"sundynix-go/global"
"sundynix-go/model/commom/response"
systemReq "sundynix-go/model/system/request"
)
type UserApi struct {
}
func (u *UserApi) GetUserList(c *gin.Context) {
var pageInfo systemReq.GetUserList
err := c.ShouldBindJSON(&pageInfo)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
list, total, err := UserService.GetUserList(pageInfo)
if err != nil {
global.Logger.Error("获取用户列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Current,
PageSize: pageInfo.PageSize,
}, c)
}