2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-17 15:15:03 +08:00
|
|
|
|
"encoding/json"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
"fmt"
|
2023-07-03 21:42:04 +08:00
|
|
|
|
msgapp "mayfly-go/internal/msg/application"
|
|
|
|
|
|
msgentity "mayfly-go/internal/msg/domain/entity"
|
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-06-17 15:15:03 +08:00
|
|
|
|
"mayfly-go/pkg/cache"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/captcha"
|
|
|
|
|
|
"mayfly-go/pkg/ginx"
|
2022-07-14 11:39:12 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-06-17 15:15:03 +08:00
|
|
|
|
"mayfly-go/pkg/otp"
|
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"
|
|
|
|
|
|
"mayfly-go/pkg/utils/jsonx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/netx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/timex"
|
2022-07-18 20:36:31 +08:00
|
|
|
|
"regexp"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-11 14:04:09 +08:00
|
|
|
|
/** 登录者个人相关操作 **/
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// @router /accounts/login [post]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (a *Account) Login(rc *req.Ctx) {
|
2023-07-20 22:41:13 +08:00
|
|
|
|
loginForm := ginx.BindJsonAndValid(rc.GinCtx, new(form.LoginForm))
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
accountLoginSecurity := a.ConfigApp.GetConfig(entity.ConfigKeyAccountLoginSecurity).ToAccountLoginSecurity()
|
2022-08-26 20:15:36 +08:00
|
|
|
|
// 判断是否有开启登录验证码校验
|
2023-06-17 15:15:03 +08:00
|
|
|
|
if accountLoginSecurity.UseCaptcha {
|
2022-08-26 20:15:36 +08:00
|
|
|
|
// 校验验证码
|
|
|
|
|
|
biz.IsTrue(captcha.Verify(loginForm.Cid, loginForm.Captcha), "验证码错误")
|
|
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
username := loginForm.Username
|
2023-07-20 22:41:13 +08:00
|
|
|
|
|
|
|
|
|
|
clientIp := getIpAndRegion(rc)
|
2023-06-17 15:15:03 +08:00
|
|
|
|
rc.ReqParam = fmt.Sprintf("username: %s | ip: %s", username, clientIp)
|
2023-04-13 20:11:22 +08:00
|
|
|
|
|
2023-07-21 17:07:04 +08:00
|
|
|
|
originPwd, err := cryptox.DefaultRsaDecrypt(loginForm.Password, true)
|
2022-07-18 20:36:31 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
|
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
account := &entity.Account{Username: username}
|
|
|
|
|
|
err = a.AccountApp.GetAccount(account, "Id", "Name", "Username", "Password", "Status", "LastLoginTime", "LastLoginIp", "OtpSecret")
|
|
|
|
|
|
|
|
|
|
|
|
failCountKey := fmt.Sprintf("account:login:failcount:%s", username)
|
|
|
|
|
|
nowFailCount := cache.GetInt(failCountKey)
|
|
|
|
|
|
loginFailCount := accountLoginSecurity.LoginFailCount
|
|
|
|
|
|
loginFailMin := accountLoginSecurity.LoginFailMin
|
2023-06-20 17:08:13 +08:00
|
|
|
|
biz.IsTrue(nowFailCount < loginFailCount, "登录失败超过%d次, 请%d分钟后再试", loginFailCount, loginFailMin)
|
2023-06-17 15:15:03 +08:00
|
|
|
|
|
2023-07-21 17:07:04 +08:00
|
|
|
|
if err != nil || !cryptox.CheckPwdHash(originPwd, account.Password) {
|
2023-06-17 15:15:03 +08:00
|
|
|
|
nowFailCount++
|
|
|
|
|
|
cache.SetStr(failCountKey, strconv.Itoa(nowFailCount), time.Minute*time.Duration(loginFailMin))
|
|
|
|
|
|
panic(biz.NewBizErr(fmt.Sprintf("用户名或密码错误【当前登录失败%d次】", nowFailCount)))
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
biz.IsTrue(account.IsEnable(), "该账号不可用")
|
|
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
// 校验密码强度(新用户第一次登录密码与账号名一致)
|
2022-07-18 20:36:31 +08:00
|
|
|
|
biz.IsTrueBy(CheckPasswordLever(originPwd), biz.NewBizErrCode(401, "您的密码安全等级较低,请修改后重新登录"))
|
2023-04-13 20:11:22 +08:00
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
res := map[string]any{
|
2023-04-13 20:11:22 +08:00
|
|
|
|
"name": account.Name,
|
2023-06-17 15:15:03 +08:00
|
|
|
|
"username": username,
|
2023-04-13 20:11:22 +08:00
|
|
|
|
"lastLoginTime": account.LastLoginTime,
|
|
|
|
|
|
"lastLoginIp": account.LastLoginIp,
|
|
|
|
|
|
}
|
2023-06-17 15:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 默认为不校验otp
|
|
|
|
|
|
otpStatus := OtpStatusNone
|
|
|
|
|
|
// 访问系统使用的token
|
|
|
|
|
|
accessToken := req.CreateToken(account.Id, username)
|
|
|
|
|
|
// 若系统配置中设置开启otp双因素校验,则进行otp校验
|
|
|
|
|
|
if accountLoginSecurity.UseOtp {
|
2023-07-21 21:18:31 +08:00
|
|
|
|
otpInfo, otpurl, otpToken := useOtp(account, accountLoginSecurity.OtpIssuer, accessToken)
|
|
|
|
|
|
otpStatus = otpInfo.OptStatus
|
|
|
|
|
|
if otpurl != "" {
|
|
|
|
|
|
res["otpUrl"] = otpurl
|
2023-06-17 15:15:03 +08:00
|
|
|
|
}
|
2023-07-21 21:18:31 +08:00
|
|
|
|
accessToken = otpToken
|
2023-06-17 15:15:03 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 不进行otp二次校验则直接返回accessToken
|
|
|
|
|
|
// 保存登录消息
|
2023-07-21 21:18:31 +08:00
|
|
|
|
go saveLogin(a.AccountApp, a.MsgApp, account, clientIp)
|
2023-06-17 15:15:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 赋值otp状态
|
|
|
|
|
|
res["otp"] = otpStatus
|
2023-07-21 21:18:31 +08:00
|
|
|
|
res["token"] = accessToken
|
2023-06-17 15:15:03 +08:00
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-21 21:18:31 +08:00
|
|
|
|
func useOtp(account *entity.Account, otpIssuer, accessToken string) (*OtpVerifyInfo, string, string) {
|
|
|
|
|
|
account.OtpSecretDecrypt()
|
|
|
|
|
|
otpSecret := account.OtpSecret
|
|
|
|
|
|
// 修改状态为已注册
|
|
|
|
|
|
otpStatus := OtpStatusReg
|
|
|
|
|
|
otpUrl := ""
|
|
|
|
|
|
// 该token用于otp双因素校验
|
2023-07-21 22:29:41 +08:00
|
|
|
|
token := stringx.Rand(32)
|
2023-07-21 21:18:31 +08:00
|
|
|
|
// 未注册otp secret或重置了秘钥
|
|
|
|
|
|
if otpSecret == "" || otpSecret == "-" {
|
|
|
|
|
|
otpStatus = OtpStatusNoReg
|
|
|
|
|
|
key, err := otp.NewTOTP(otp.GenerateOpts{
|
|
|
|
|
|
AccountName: account.Username,
|
|
|
|
|
|
Issuer: otpIssuer,
|
|
|
|
|
|
})
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "otp生成失败: %s")
|
|
|
|
|
|
otpUrl = key.URL()
|
|
|
|
|
|
otpSecret = key.Secret()
|
|
|
|
|
|
}
|
|
|
|
|
|
// 缓存otpInfo, 只有双因素校验通过才可返回真正的accessToken
|
|
|
|
|
|
otpInfo := &OtpVerifyInfo{
|
|
|
|
|
|
AccountId: account.Id,
|
|
|
|
|
|
Username: account.Username,
|
|
|
|
|
|
OptStatus: otpStatus,
|
|
|
|
|
|
OtpSecret: otpSecret,
|
|
|
|
|
|
AccessToken: accessToken,
|
|
|
|
|
|
}
|
2023-07-21 22:29:41 +08:00
|
|
|
|
cache.SetStr(fmt.Sprintf("otp:token:%s", token), jsonx.ToStr(otpInfo), time.Minute*time.Duration(3))
|
2023-07-21 21:18:31 +08:00
|
|
|
|
return otpInfo, otpUrl, token
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-20 22:41:13 +08:00
|
|
|
|
// 获取ip与归属地信息
|
|
|
|
|
|
func getIpAndRegion(rc *req.Ctx) string {
|
|
|
|
|
|
clientIp := rc.GinCtx.ClientIP()
|
2023-07-21 17:07:04 +08:00
|
|
|
|
return fmt.Sprintf("%s %s", clientIp, netx.Ip2Region(clientIp))
|
2023-07-20 22:41:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 15:15:03 +08:00
|
|
|
|
type OtpVerifyInfo struct {
|
|
|
|
|
|
AccountId uint64
|
|
|
|
|
|
Username string
|
|
|
|
|
|
OptStatus int
|
|
|
|
|
|
AccessToken string
|
|
|
|
|
|
OtpSecret string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// OTP双因素校验
|
|
|
|
|
|
func (a *Account) OtpVerify(rc *req.Ctx) {
|
|
|
|
|
|
otpVerify := new(form.OtpVerfiy)
|
|
|
|
|
|
ginx.BindJsonAndValid(rc.GinCtx, otpVerify)
|
|
|
|
|
|
|
|
|
|
|
|
tokenKey := fmt.Sprintf("otp:token:%s", otpVerify.OtpToken)
|
|
|
|
|
|
otpInfoJson := cache.GetStr(tokenKey)
|
|
|
|
|
|
biz.NotEmpty(otpInfoJson, "otpToken错误或失效, 请重新登陆获取")
|
|
|
|
|
|
otpInfo := new(OtpVerifyInfo)
|
|
|
|
|
|
json.Unmarshal([]byte(otpInfoJson), otpInfo)
|
|
|
|
|
|
|
|
|
|
|
|
failCountKey := fmt.Sprintf("account:otp:failcount:%d", otpInfo.AccountId)
|
|
|
|
|
|
failCount := cache.GetInt(failCountKey)
|
|
|
|
|
|
biz.IsTrue(failCount < 5, "双因素校验失败超过5次, 请10分钟后再试")
|
|
|
|
|
|
|
|
|
|
|
|
otpStatus := otpInfo.OptStatus
|
|
|
|
|
|
accessToken := otpInfo.AccessToken
|
|
|
|
|
|
accountId := otpInfo.AccountId
|
|
|
|
|
|
otpSecret := otpInfo.OtpSecret
|
|
|
|
|
|
|
|
|
|
|
|
if !otp.Validate(otpVerify.Code, otpSecret) {
|
|
|
|
|
|
cache.SetStr(failCountKey, strconv.Itoa(failCount+1), time.Minute*time.Duration(10))
|
|
|
|
|
|
panic(biz.NewBizErr("双因素认证授权码不正确"))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是未注册状态,则更新account表的otpSecret信息
|
|
|
|
|
|
if otpStatus == OtpStatusNoReg {
|
|
|
|
|
|
update := &entity.Account{OtpSecret: otpSecret}
|
|
|
|
|
|
update.Id = accountId
|
2023-06-21 15:18:43 +08:00
|
|
|
|
update.OtpSecretEncrypt()
|
2023-06-17 15:15:03 +08:00
|
|
|
|
a.AccountApp.Update(update)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
la := &entity.Account{Username: otpInfo.Username}
|
|
|
|
|
|
la.Id = accountId
|
2023-07-21 22:46:55 +08:00
|
|
|
|
go saveLogin(a.AccountApp, a.MsgApp, la, getIpAndRegion(rc))
|
2023-06-17 15:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
cache.Del(tokenKey)
|
|
|
|
|
|
rc.ResData = accessToken
|
2023-04-13 20:11:22 +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) {
|
|
|
|
|
|
account := rc.LoginAccount
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
var resources vo.AccountResourceVOList
|
|
|
|
|
|
// 获取账号菜单资源
|
|
|
|
|
|
a.ResourceApp.GetAccountResources(account.Id, &resources)
|
|
|
|
|
|
// 菜单树与权限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-06-01 12:31:32 +08:00
|
|
|
|
rc.ResData = map[string]any{
|
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}
|
|
|
|
|
|
err = a.AccountApp.GetAccount(account, "Id", "Username", "Password", "Status")
|
|
|
|
|
|
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")
|
|
|
|
|
|
biz.IsTrue(CheckPasswordLever(originNewPwd), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
|
|
|
|
|
|
|
|
|
|
|
|
updateAccount := new(entity.Account)
|
|
|
|
|
|
updateAccount.Id = account.Id
|
2023-07-21 17:07:04 +08:00
|
|
|
|
updateAccount.Password = cryptox.PwdHash(originNewPwd)
|
2022-07-18 20:36:31 +08:00
|
|
|
|
a.AccountApp.Update(updateAccount)
|
|
|
|
|
|
|
|
|
|
|
|
// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
|
|
|
|
|
|
rc.LoginAccount = &model.LoginAccount{Id: account.Id, Username: account.Username}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CheckPasswordLever(ps string) bool {
|
|
|
|
|
|
if len(ps) < 8 {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
num := `[0-9]{1}`
|
|
|
|
|
|
a_z := `[a-zA-Z]{1}`
|
|
|
|
|
|
symbol := `[!@#~$%^&*()+|_.,]{1}`
|
|
|
|
|
|
if b, err := regexp.MatchString(num, ps); !b || err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if b, err := regexp.MatchString(a_z, ps); !b || err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if b, err := regexp.MatchString(symbol, ps); !b || err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 保存更新账号登录信息
|
2023-07-21 21:18:31 +08:00
|
|
|
|
func saveLogin(accountApp application.Account, msgApp msgapp.Msg, account *entity.Account, ip string) {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 更新账号最后登录时间
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
updateAccount := &entity.Account{LastLoginTime: &now}
|
|
|
|
|
|
updateAccount.Id = account.Id
|
2021-09-11 14:04:09 +08:00
|
|
|
|
updateAccount.LastLoginIp = ip
|
2023-07-21 21:18:31 +08:00
|
|
|
|
accountApp.Update(updateAccount)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2021-09-11 14:04:09 +08:00
|
|
|
|
// 创建登录消息
|
2023-07-03 21:42:04 +08:00
|
|
|
|
loginMsg := &msgentity.Msg{
|
2021-09-11 14:04:09 +08:00
|
|
|
|
RecipientId: int64(account.Id),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
Msg: fmt.Sprintf("于[%s]-[%s]登录", ip, timex.DefaultFormat(now)),
|
2021-09-11 14:04:09 +08:00
|
|
|
|
Type: 1,
|
|
|
|
|
|
}
|
|
|
|
|
|
loginMsg.CreateTime = &now
|
|
|
|
|
|
loginMsg.Creator = account.Username
|
|
|
|
|
|
loginMsg.CreatorId = account.Id
|
2023-07-21 21:18:31 +08:00
|
|
|
|
msgApp.Create(loginMsg)
|
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)
|
|
|
|
|
|
a.RoleApp.GetAccountRoles(rc.LoginAccount.Id, roles)
|
|
|
|
|
|
|
|
|
|
|
|
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为登录者账号
|
|
|
|
|
|
updateAccount.Id = rc.LoginAccount.Id
|
|
|
|
|
|
|
|
|
|
|
|
if updateAccount.Password != "" {
|
2022-07-18 20:36:31 +08:00
|
|
|
|
biz.IsTrue(CheckPasswordLever(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
|
|
|
|
}
|
|
|
|
|
|
a.AccountApp.Update(updateAccount)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 后台账号操作 **/
|
|
|
|
|
|
|
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")
|
2021-06-07 17:22:07 +08:00
|
|
|
|
rc.ResData = a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
2021-07-28 18:03:19 +08:00
|
|
|
|
account.SetBaseInfo(rc.LoginAccount)
|
2022-08-26 20:15:36 +08:00
|
|
|
|
|
|
|
|
|
|
if account.Id == 0 {
|
|
|
|
|
|
a.AccountApp.Create(account)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if account.Password != "" {
|
|
|
|
|
|
biz.IsTrue(CheckPasswordLever(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
|
|
|
|
}
|
|
|
|
|
|
a.AccountApp.Update(account)
|
|
|
|
|
|
}
|
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"))
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("accountId: %d, status: %d", account.Id, account.Status)
|
|
|
|
|
|
a.AccountApp.Update(account)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
a.AccountApp.Delete(uint64(value))
|
|
|
|
|
|
}
|
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
|
|
|
|
|
|
// 获取账号菜单资源
|
|
|
|
|
|
a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources)
|
|
|
|
|
|
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
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
|
|
|
|
|
|
var form form.AccountRoleForm
|
|
|
|
|
|
ginx.BindJsonAndValid(g, &form)
|
|
|
|
|
|
aid := uint64(form.Id)
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
oIds := a.RoleApp.GetAccountRoleIds(uint64(form.Id))
|
|
|
|
|
|
|
2023-07-21 17:07:04 +08:00
|
|
|
|
addIds, delIds, _ := collx.ArrayCompare(newIds, oIds, func(i1, i2 uint64) bool {
|
2023-07-20 22:41:13 +08:00
|
|
|
|
return i1 == i2
|
2021-06-07 17:22:07 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
createTime := time.Now()
|
|
|
|
|
|
creator := rc.LoginAccount.Username
|
|
|
|
|
|
creatorId := rc.LoginAccount.Id
|
|
|
|
|
|
for _, v := range addIds {
|
2023-07-20 22:41:13 +08:00
|
|
|
|
rr := &entity.AccountRole{AccountId: aid, RoleId: v, CreateTime: &createTime, CreatorId: creatorId, Creator: creator}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
a.RoleApp.SaveAccountRole(rr)
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, v := range delIds {
|
2023-07-20 22:41:13 +08:00
|
|
|
|
a.RoleApp.DeleteAccountRole(aid, v)
|
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
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("accountId = %d", accountId)
|
|
|
|
|
|
a.AccountApp.Update(account)
|
|
|
|
|
|
}
|