2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-22 20:51:46 +08:00
|
|
|
|
"mayfly-go/internal/common/utils"
|
2023-07-03 21:42:04 +08:00
|
|
|
|
msgapp "mayfly-go/internal/msg/application"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/api/form"
|
|
|
|
|
|
"mayfly-go/internal/sys/api/vo"
|
|
|
|
|
|
"mayfly-go/internal/sys/application"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
|
"mayfly-go/pkg/biz"
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"mayfly-go/pkg/contextx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/ginx"
|
2022-07-14 11:39:12 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-01-14 16:29:52 +08:00
|
|
|
|
"mayfly-go/pkg/req"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/cryptox"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
const (
|
|
|
|
|
|
OtpStatusNone = -1 // 未启用otp校验
|
|
|
|
|
|
OtpStatusReg = 1 // 用户otp secret已注册
|
|
|
|
|
|
OtpStatusNoReg = 2 // 用户otp secret未注册
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
type Account struct {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
AccountApp application.Account
|
|
|
|
|
|
ResourceApp application.Resource
|
|
|
|
|
|
RoleApp application.Role
|
2023-07-03 21:42:04 +08:00
|
|
|
|
MsgApp msgapp.Msg
|
2022-08-26 20:15:36 +08:00
|
|
|
|
ConfigApp application.Config
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
// 获取当前登录用户的菜单与权限码
|
2023-04-13 20:11:22 +08:00
|
|
|
|
func (a *Account) GetPermissions(rc *req.Ctx) {
|
2023-11-07 21:05:21 +08:00
|
|
|
|
account := rc.GetLoginAccount()
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
var resources vo.AccountResourceVOList
|
|
|
|
|
|
// 获取账号菜单资源
|
2023-10-26 17:15:49 +08:00
|
|
|
|
biz.ErrIsNil(a.ResourceApp.GetAccountResources(account.Id, &resources))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 菜单树与权限code数组
|
|
|
|
|
|
var menus vo.AccountResourceVOList
|
|
|
|
|
|
var permissions []string
|
|
|
|
|
|
for _, v := range resources {
|
|
|
|
|
|
if v.Type == entity.ResourceTypeMenu {
|
|
|
|
|
|
menus = append(menus, v)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
permissions = append(permissions, *v.Code)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-09 16:58:57 +08:00
|
|
|
|
// 保存该账号的权限codes
|
2023-01-14 16:29:52 +08:00
|
|
|
|
req.SavePermissionCodes(account.Id, permissions)
|
2023-10-12 12:14:56 +08:00
|
|
|
|
rc.ResData = collx.M{
|
2023-04-13 20:11:22 +08:00
|
|
|
|
"menus": menus.ToTrees(0),
|
|
|
|
|
|
"permissions": permissions,
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) ChangePassword(rc *req.Ctx) {
|
2022-07-18 20:36:31 +08:00
|
|
|
|
form := new(form.AccountChangePasswordForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(rc.GinCtx, form)
|
|
|
|
|
|
|
2023-07-21 17:07:04 +08:00
|
|
|
|
originOldPwd, err := cryptox.DefaultRsaDecrypt(form.OldPassword, true)
|
2022-07-18 20:36:31 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
|
|
|
|
|
|
|
2022-08-02 21:44:01 +08:00
|
|
|
|
account := &entity.Account{Username: form.Username}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
err = a.AccountApp.GetBy(account, "Id", "Username", "Password", "Status")
|
2022-08-02 21:44:01 +08:00
|
|
|
|
biz.ErrIsNil(err, "旧密码错误")
|
2023-07-21 17:07:04 +08:00
|
|
|
|
biz.IsTrue(cryptox.CheckPwdHash(originOldPwd, account.Password), "旧密码错误")
|
2022-08-02 21:44:01 +08:00
|
|
|
|
biz.IsTrue(account.IsEnable(), "该账号不可用")
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
2023-07-21 17:07:04 +08:00
|
|
|
|
originNewPwd, err := cryptox.DefaultRsaDecrypt(form.NewPassword, true)
|
2022-07-18 20:36:31 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "解密新密码错误: %s")
|
2023-07-22 20:51:46 +08:00
|
|
|
|
biz.IsTrue(utils.CheckAccountPasswordLever(originNewPwd), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
|
|
|
|
|
updateAccount := new(entity.Account)
|
|
|
|
|
|
updateAccount.Id = account.Id
|
2023-07-21 17:07:04 +08:00
|
|
|
|
updateAccount.Password = cryptox.PwdHash(originNewPwd)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, updateAccount), "更新账号密码失败")
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
|
2023-11-07 21:05:21 +08:00
|
|
|
|
contextx.WithLoginAccount(rc.MetaCtx, &model.LoginAccount{
|
|
|
|
|
|
Id: account.Id,
|
|
|
|
|
|
Username: account.Username,
|
|
|
|
|
|
})
|
2022-07-18 20:36:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-11 14:04:09 +08:00
|
|
|
|
// 获取个人账号信息
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) AccountInfo(rc *req.Ctx) {
|
2021-09-11 14:04:09 +08:00
|
|
|
|
ap := new(vo.AccountPersonVO)
|
|
|
|
|
|
// 角色信息
|
|
|
|
|
|
roles := new([]vo.AccountRoleVO)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
a.RoleApp.GetAccountRoles(rc.GetLoginAccount().Id, roles)
|
2021-09-11 14:04:09 +08:00
|
|
|
|
|
|
|
|
|
|
ap.Roles = *roles
|
|
|
|
|
|
rc.ResData = ap
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新个人账号信息
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) UpdateAccount(rc *req.Ctx) {
|
2023-07-08 20:05:55 +08:00
|
|
|
|
updateAccount := ginx.BindJsonAndCopyTo[*entity.Account](rc.GinCtx, new(form.AccountUpdateForm), new(entity.Account))
|
2021-09-11 14:04:09 +08:00
|
|
|
|
// 账号id为登录者账号
|
2023-11-07 21:05:21 +08:00
|
|
|
|
updateAccount.Id = rc.GetLoginAccount().Id
|
2021-09-11 14:04:09 +08:00
|
|
|
|
|
|
|
|
|
|
if updateAccount.Password != "" {
|
2023-07-22 20:51:46 +08:00
|
|
|
|
biz.IsTrue(utils.CheckAccountPasswordLever(updateAccount.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
|
2023-07-21 17:07:04 +08:00
|
|
|
|
updateAccount.Password = cryptox.PwdHash(updateAccount.Password)
|
2021-09-11 14:04:09 +08:00
|
|
|
|
}
|
2023-07-24 22:36:07 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
oldAcc, err := a.AccountApp.GetById(new(entity.Account), updateAccount.Id)
|
|
|
|
|
|
biz.ErrIsNil(err, "账号信息不存在")
|
2023-07-24 22:36:07 +08:00
|
|
|
|
// 账号创建十分钟内允许修改用户名(兼容oauth2首次登录修改用户名),否则不允许修改
|
|
|
|
|
|
if oldAcc.CreateTime.Add(10 * time.Minute).Before(time.Now()) {
|
|
|
|
|
|
// 禁止更新用户名,防止误传被更新
|
|
|
|
|
|
updateAccount.Username = ""
|
|
|
|
|
|
}
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, updateAccount))
|
2021-09-11 14:04:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 后台账号操作 **/
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// @router /accounts [get]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) Accounts(rc *req.Ctx) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
condition := &entity.Account{}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
condition.Username = rc.GinCtx.Query("username")
|
2023-10-26 17:15:49 +08:00
|
|
|
|
res, err := a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
|
|
|
|
|
|
biz.ErrIsNil(err)
|
|
|
|
|
|
rc.ResData = res
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-26 20:15:36 +08:00
|
|
|
|
// @router /accounts
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) SaveAccount(rc *req.Ctx) {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
form := &form.AccountCreateForm{}
|
2023-07-08 20:05:55 +08:00
|
|
|
|
account := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Account))
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-07-08 20:05:55 +08:00
|
|
|
|
form.Password = "*****"
|
|
|
|
|
|
rc.ReqParam = form
|
2022-08-26 20:15:36 +08:00
|
|
|
|
|
|
|
|
|
|
if account.Id == 0 {
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Create(rc.MetaCtx, account))
|
2022-08-26 20:15:36 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
if account.Password != "" {
|
2023-07-22 20:51:46 +08:00
|
|
|
|
biz.IsTrue(utils.CheckAccountPasswordLever(account.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
|
2023-07-21 17:07:04 +08:00
|
|
|
|
account.Password = cryptox.PwdHash(account.Password)
|
2022-08-26 20:15:36 +08:00
|
|
|
|
}
|
2023-07-24 22:36:07 +08:00
|
|
|
|
// 更新操作不允许修改用户名、防止误传更新
|
|
|
|
|
|
account.Username = ""
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
|
2022-08-26 20:15:36 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) ChangeStatus(rc *req.Ctx) {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
|
|
|
|
|
|
account := &entity.Account{}
|
|
|
|
|
|
account.Id = uint64(ginx.PathParamInt(g, "id"))
|
|
|
|
|
|
account.Status = int8(ginx.PathParamInt(g, "status"))
|
2023-10-12 21:50:55 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("accountId", account.Id, "status", account.Status)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) DeleteAccount(rc *req.Ctx) {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
idsStr := ginx.PathParam(rc.GinCtx, "id")
|
|
|
|
|
|
rc.ReqParam = idsStr
|
|
|
|
|
|
ids := strings.Split(idsStr, ",")
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range ids {
|
|
|
|
|
|
value, err := strconv.Atoi(v)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(a.AccountApp.Delete(rc.MetaCtx, uint64(value)), "删除失败:%s")
|
2023-07-01 14:34:42 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 获取账号角色id列表,用户回显角色分配
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) AccountRoleIds(rc *req.Ctx) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
rc.ResData = a.RoleApp.GetAccountRoleIds(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取账号角色id列表,用户回显角色分配
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) AccountRoles(rc *req.Ctx) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
vos := new([]vo.AccountRoleVO)
|
|
|
|
|
|
a.RoleApp.GetAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")), vos)
|
|
|
|
|
|
rc.ResData = vos
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) AccountResources(rc *req.Ctx) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
var resources vo.ResourceManageVOList
|
|
|
|
|
|
// 获取账号菜单资源
|
2023-10-26 17:15:49 +08:00
|
|
|
|
biz.ErrIsNil(a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
rc.ResData = resources.ToTrees(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存账号角色信息
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) SaveRoles(rc *req.Ctx) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
var form form.AccountRoleForm
|
2023-07-27 16:47:05 +08:00
|
|
|
|
ginx.BindJsonAndValid(rc.GinCtx, &form)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
rc.ReqParam = form
|
|
|
|
|
|
|
2023-07-20 22:41:13 +08:00
|
|
|
|
// 将,拼接的字符串进行切割并转换
|
2023-07-21 17:07:04 +08:00
|
|
|
|
newIds := collx.ArrayMap[string, uint64](strings.Split(form.RoleIds, ","), func(val string) uint64 {
|
2023-07-20 22:41:13 +08:00
|
|
|
|
id, _ := strconv.Atoi(val)
|
|
|
|
|
|
return uint64(id)
|
|
|
|
|
|
})
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
a.RoleApp.SaveAccountRole(rc.MetaCtx, form.Id, newIds)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-06-17 15:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 重置otp秘钥
|
|
|
|
|
|
func (a *Account) ResetOtpSecret(rc *req.Ctx) {
|
|
|
|
|
|
account := &entity.Account{OtpSecret: "-"}
|
|
|
|
|
|
accountId := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
|
|
|
|
|
|
account.Id = accountId
|
2023-10-12 21:50:55 +08:00
|
|
|
|
rc.ReqParam = collx.Kvs("accountId", accountId)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
|
2023-06-17 15:15:03 +08:00
|
|
|
|
}
|