2022-05-18 17:57:03 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2022-05-29 10:06:21 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/model"
|
2022-06-14 11:17:38 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/model/request"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/model/response"
|
2022-05-29 10:06:21 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/public/tools"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/service/ildap"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/service/isql"
|
2022-05-18 17:57:03 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type BaseLogic struct{}
|
|
|
|
|
|
2023-03-09 11:41:33 +08:00
|
|
|
|
// 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 重置密码
|
2022-05-18 17:57:03 +08:00
|
|
|
|
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("邮箱不存在,请检查邮箱是否正确"))
|
|
|
|
|
}
|
2023-03-09 11:41:33 +08:00
|
|
|
|
// 判断验证码是否过期
|
|
|
|
|
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("验证码错误,请检查邮箱中正确的验证码,如果点击多次发送验证码,请用最后一次生成的验证码来验证"))
|
|
|
|
|
}
|
2022-05-18 17:57:03 +08:00
|
|
|
|
|
|
|
|
|
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 {
|
2023-03-09 11:41:33 +08:00
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("邮件发送失败" + err.Error()))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新数据库密码
|
|
|
|
|
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",
|
2023-02-08 15:31:44 +08:00
|
|
|
|
Path: "#/personnel/user",
|
2022-05-18 17:57:03 +08:00
|
|
|
|
},
|
|
|
|
|
&response.DashboardList{
|
|
|
|
|
DataType: "group",
|
|
|
|
|
DataName: "分组",
|
|
|
|
|
DataCount: groupCount,
|
|
|
|
|
Icon: "peoples",
|
2023-02-08 15:31:44 +08:00
|
|
|
|
Path: "#/personnel/group",
|
2022-05-18 17:57:03 +08:00
|
|
|
|
},
|
|
|
|
|
&response.DashboardList{
|
|
|
|
|
DataType: "role",
|
|
|
|
|
DataName: "角色",
|
|
|
|
|
DataCount: roleCount,
|
|
|
|
|
Icon: "eye-open",
|
2023-02-08 15:31:44 +08:00
|
|
|
|
Path: "#/system/role",
|
2022-05-18 17:57:03 +08:00
|
|
|
|
},
|
|
|
|
|
&response.DashboardList{
|
|
|
|
|
DataType: "menu",
|
|
|
|
|
DataName: "菜单",
|
|
|
|
|
DataCount: menuCount,
|
|
|
|
|
Icon: "tree-table",
|
2023-02-08 15:31:44 +08:00
|
|
|
|
Path: "#/system/menu",
|
2022-05-18 17:57:03 +08:00
|
|
|
|
},
|
|
|
|
|
&response.DashboardList{
|
|
|
|
|
DataType: "api",
|
|
|
|
|
DataName: "接口",
|
|
|
|
|
DataCount: apiCount,
|
|
|
|
|
Icon: "tree",
|
2023-02-08 15:31:44 +08:00
|
|
|
|
Path: "#/system/api",
|
2022-05-18 17:57:03 +08:00
|
|
|
|
},
|
|
|
|
|
&response.DashboardList{
|
|
|
|
|
DataType: "log",
|
|
|
|
|
DataName: "日志",
|
|
|
|
|
DataCount: logCount,
|
|
|
|
|
Icon: "documentation",
|
2023-02-08 15:31:44 +08:00
|
|
|
|
Path: "#/log/operation-log",
|
2022-05-18 17:57:03 +08:00
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return rst, nil
|
|
|
|
|
}
|
2022-07-05 12:16:05 +08:00
|
|
|
|
|
2023-02-14 11:23:08 +08:00
|
|
|
|
// EncryptPasswd
|
|
|
|
|
func (l BaseLogic) EncryptPasswd(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.EncryptPasswdReq)
|
2022-07-05 12:16:05 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
return tools.NewGenPasswd(r.Passwd), nil
|
|
|
|
|
}
|
2023-02-14 11:23:08 +08:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|