188 lines
5.0 KiB
Go
188 lines
5.0 KiB
Go
package logic
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"github.com/eryajf/go-ldap-admin/model"
|
||
"github.com/eryajf/go-ldap-admin/model/request"
|
||
"github.com/eryajf/go-ldap-admin/model/response"
|
||
"github.com/eryajf/go-ldap-admin/public/tools"
|
||
"github.com/eryajf/go-ldap-admin/service/ildap"
|
||
"github.com/eryajf/go-ldap-admin/service/isql"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type BaseLogic struct{}
|
||
|
||
// SendCode 发送验证码
|
||
func (l BaseLogic) SendCode(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
||
r, ok := req.(*request.BaseSendCodeReq)
|
||
if !ok {
|
||
return nil, ReqAssertErr
|
||
}
|
||
_ = c
|
||
// 判断邮箱是否正确
|
||
if !isql.User.Exist(tools.H{"mail": r.Mail}) {
|
||
return nil, tools.NewValidatorError(fmt.Errorf("邮箱不存在,请检查邮箱是否正确"))
|
||
}
|
||
|
||
err := tools.SendCode([]string{r.Mail})
|
||
if err != nil {
|
||
return nil, tools.NewLdapError(fmt.Errorf("邮件发送失败" + err.Error()))
|
||
}
|
||
|
||
return nil, nil
|
||
}
|
||
|
||
// ChangePwd 重置密码
|
||
func (l BaseLogic) ChangePwd(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
||
r, ok := req.(*request.BaseChangePwdReq)
|
||
if !ok {
|
||
return nil, ReqAssertErr
|
||
}
|
||
_ = c
|
||
// 判断邮箱是否正确
|
||
if !isql.User.Exist(tools.H{"mail": r.Mail}) {
|
||
return nil, tools.NewValidatorError(fmt.Errorf("邮箱不存在,请检查邮箱是否正确"))
|
||
}
|
||
// 判断验证码是否过期
|
||
cacheCode, ok := tools.VerificationCodeCache.Get(r.Mail)
|
||
if !ok {
|
||
return nil, tools.NewValidatorError(fmt.Errorf("对不起,该验证码已超过5分钟有效期,请重新重新密码"))
|
||
}
|
||
// 判断验证码是否正确
|
||
if cacheCode != r.Code {
|
||
return nil, tools.NewValidatorError(fmt.Errorf("验证码错误,请检查邮箱中正确的验证码,如果点击多次发送验证码,请用最后一次生成的验证码来验证"))
|
||
}
|
||
|
||
user := new(model.User)
|
||
err := isql.User.Find(tools.H{"mail": r.Mail}, user)
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("通过邮箱查询用户失败" + err.Error()))
|
||
}
|
||
|
||
newpass, err := ildap.User.NewPwd(user.Username)
|
||
if err != nil {
|
||
return nil, tools.NewLdapError(fmt.Errorf("LDAP生成新密码失败" + err.Error()))
|
||
}
|
||
|
||
err = tools.SendMail([]string{user.Mail}, newpass)
|
||
if err != nil {
|
||
return nil, tools.NewLdapError(fmt.Errorf("邮件发送失败" + err.Error()))
|
||
}
|
||
|
||
// 更新数据库密码
|
||
err = isql.User.ChangePwd(user.Username, tools.NewGenPasswd(newpass))
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("在MySQL更新密码失败: " + err.Error()))
|
||
}
|
||
|
||
return nil, nil
|
||
}
|
||
|
||
// Dashboard 仪表盘
|
||
func (l BaseLogic) Dashboard(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
||
_, ok := req.(*request.BaseDashboardReq)
|
||
if !ok {
|
||
return nil, ReqAssertErr
|
||
}
|
||
_ = c
|
||
|
||
userCount, err := isql.User.Count()
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("获取用户总数失败"))
|
||
}
|
||
groupCount, err := isql.Group.Count()
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("获取分组总数失败"))
|
||
}
|
||
roleCount, err := isql.Role.Count()
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("获取角色总数失败"))
|
||
}
|
||
menuCount, err := isql.Menu.Count()
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("获取菜单总数失败"))
|
||
}
|
||
apiCount, err := isql.Api.Count()
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("获取接口总数失败"))
|
||
}
|
||
logCount, err := isql.OperationLog.Count()
|
||
if err != nil {
|
||
return nil, tools.NewMySqlError(fmt.Errorf("获取日志总数失败"))
|
||
}
|
||
|
||
rst := make([]*response.DashboardList, 0)
|
||
|
||
rst = append(rst,
|
||
&response.DashboardList{
|
||
DataType: "user",
|
||
DataName: "用户",
|
||
DataCount: userCount,
|
||
Icon: "people",
|
||
Path: "#/personnel/user",
|
||
},
|
||
&response.DashboardList{
|
||
DataType: "group",
|
||
DataName: "分组",
|
||
DataCount: groupCount,
|
||
Icon: "peoples",
|
||
Path: "#/personnel/group",
|
||
},
|
||
&response.DashboardList{
|
||
DataType: "role",
|
||
DataName: "角色",
|
||
DataCount: roleCount,
|
||
Icon: "eye-open",
|
||
Path: "#/system/role",
|
||
},
|
||
&response.DashboardList{
|
||
DataType: "menu",
|
||
DataName: "菜单",
|
||
DataCount: menuCount,
|
||
Icon: "tree-table",
|
||
Path: "#/system/menu",
|
||
},
|
||
&response.DashboardList{
|
||
DataType: "api",
|
||
DataName: "接口",
|
||
DataCount: apiCount,
|
||
Icon: "tree",
|
||
Path: "#/system/api",
|
||
},
|
||
&response.DashboardList{
|
||
DataType: "log",
|
||
DataName: "日志",
|
||
DataCount: logCount,
|
||
Icon: "documentation",
|
||
Path: "#/log/operation-log",
|
||
},
|
||
)
|
||
|
||
return rst, nil
|
||
}
|
||
|
||
// EncryptPasswd
|
||
func (l BaseLogic) EncryptPasswd(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
||
r, ok := req.(*request.EncryptPasswdReq)
|
||
if !ok {
|
||
return nil, ReqAssertErr
|
||
}
|
||
_ = c
|
||
|
||
return tools.NewGenPasswd(r.Passwd), nil
|
||
}
|
||
|
||
// DecryptPasswd
|
||
func (l BaseLogic) DecryptPasswd(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
||
r, ok := req.(*request.DecryptPasswdReq)
|
||
if !ok {
|
||
return nil, ReqAssertErr
|
||
}
|
||
_ = c
|
||
|
||
return tools.NewParPasswd(r.Passwd), nil
|
||
}
|