更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -24,7 +24,6 @@ type passwordPayload struct {
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// PasswordStoreItemDTO 统一返回给前端的结构体,包含解析后的账号列表
|
||||
type PasswordStoreItemDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
Tid int `json:"tid,omitempty"`
|
||||
@@ -74,8 +73,6 @@ func accountsFromJSON(raw string) []models.PasswordAccountItem {
|
||||
}
|
||||
|
||||
func passwordClaims(c *beego.Controller, kind string) (*jwtutil.Claims, error) {
|
||||
// 口令保险柜按端隔离:平台账号存的口令和租户账号存的口令互不可见,
|
||||
// 所以 kind(platform/backend)必须由令牌证实,而不只是形参对得上。
|
||||
claims, err := identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.End(kind))
|
||||
if err != nil || claims.UserID <= 0 {
|
||||
return nil, orm.ErrNoRows
|
||||
@@ -105,8 +102,6 @@ func validPassword(p passwordPayload) bool {
|
||||
return p.Platform != ""
|
||||
}
|
||||
|
||||
// sameAccount 判定两个账号是否重复:
|
||||
// 登录账号都非空时按账号比较(忽略大小写);账号为空时按密码比较。
|
||||
func sameAccount(a, b models.PasswordAccountItem) bool {
|
||||
u1 := strings.TrimSpace(a.Username)
|
||||
u2 := strings.TrimSpace(b.Username)
|
||||
@@ -119,7 +114,6 @@ func sameAccount(a, b models.PasswordAccountItem) bool {
|
||||
return strings.TrimSpace(a.Password) == strings.TrimSpace(b.Password)
|
||||
}
|
||||
|
||||
// validateAccountsUnique 校验同一平台下的登录账号不重复
|
||||
func validateAccountsUnique(list []models.PasswordAccountItem) error {
|
||||
seen := map[string]bool{}
|
||||
for _, item := range list {
|
||||
@@ -136,7 +130,6 @@ func validateAccountsUnique(list []models.PasswordAccountItem) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// dedupeAccounts 去掉同组内的重复账号(保留首次出现的一条),返回去重后的列表与去重数量
|
||||
func dedupeAccounts(list []models.PasswordAccountItem) ([]models.PasswordAccountItem, int) {
|
||||
out := make([]models.PasswordAccountItem, 0, len(list))
|
||||
removed := 0
|
||||
@@ -157,13 +150,11 @@ func dedupeAccounts(list []models.PasswordAccountItem) ([]models.PasswordAccount
|
||||
return out, removed
|
||||
}
|
||||
|
||||
// findSamePlatformRecord 查询同用户(同租户)下是否已存在「平台名称 + 网址」相同的记录
|
||||
func findSamePlatformRecord(qs orm.QuerySeter, excludeID uint64, platform, url string) (uint64, error) {
|
||||
q := qs.Filter("is_deleted", 0).Filter("platform", platform).Filter("url", url)
|
||||
if excludeID > 0 {
|
||||
q = q.Exclude("id", excludeID)
|
||||
}
|
||||
// 用 Values 只取 ID,避免不同模型的 One() 类型差异
|
||||
var rows []orm.Params
|
||||
if _, err := q.OrderBy("id").Limit(1).Values(&rows, "id"); err != nil {
|
||||
return 0, err
|
||||
@@ -181,7 +172,6 @@ func findSamePlatformRecord(qs orm.QuerySeter, excludeID uint64, platform, url s
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// passwordPageParams 解析分页参数:page 默认 1,pageSize 默认 20、上限 200
|
||||
func passwordPageParams(c *beego.Controller) (page, pageSize int) {
|
||||
page, _ = c.GetInt("page", 1)
|
||||
pageSize, _ = c.GetInt("pageSize", 20)
|
||||
@@ -194,7 +184,6 @@ func passwordPageParams(c *beego.Controller) (page, pageSize int) {
|
||||
return page, pageSize
|
||||
}
|
||||
|
||||
// passwordPageReply 分页列表统一响应(list + total + 当前分页信息)
|
||||
func passwordPageReply(c *beego.Controller, msg string, list interface{}, total int64, page, pageSize int) {
|
||||
passwordReply(c, 200, msg, map[string]interface{}{
|
||||
"list": list,
|
||||
@@ -204,8 +193,6 @@ func passwordPageReply(c *beego.Controller, msg string, list interface{}, total
|
||||
})
|
||||
}
|
||||
|
||||
// ==================== 平台端 Password Store ====================
|
||||
|
||||
type PlatformPasswordStoreController struct{ beego.Controller }
|
||||
|
||||
func (c *PlatformPasswordStoreController) List() {
|
||||
@@ -224,7 +211,6 @@ func (c *PlatformPasswordStoreController) List() {
|
||||
Or("remark__icontains", k)
|
||||
qs = qs.SetCond(orm.NewCondition().And("is_deleted", 0).And("user_id", cl.UserID).AndCond(cond))
|
||||
}
|
||||
// 分页:默认每页 20 条,单页最多 200 条
|
||||
page, pageSize := passwordPageParams(&c.Controller)
|
||||
total, _ := qs.Count()
|
||||
var list []models.PlatformPasswordStore
|
||||
@@ -287,14 +273,12 @@ func (c *PlatformPasswordStoreController) Create() {
|
||||
passwordReply(&c.Controller, 400, "平台名称不能为空", nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:平台名称 + 网址 已存在则拒绝新增
|
||||
if existID, ferr := findSamePlatformRecord(
|
||||
models.Orm.QueryTable(new(models.PlatformPasswordStore)).Filter("user_id", cl.UserID),
|
||||
0, p.Platform, p.URL); ferr == nil && existID > 0 {
|
||||
passwordReply(&c.Controller, 400, fmt.Sprintf("已存在同名同网址的平台「%s」(ID %d),请直接编辑该记录", p.Platform, existID), nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:同一平台下的登录账号不能重复
|
||||
if verr := validateAccountsUnique(p.Accounts); verr != nil {
|
||||
passwordReply(&c.Controller, 400, verr.Error(), nil)
|
||||
return
|
||||
@@ -345,14 +329,12 @@ func (c *PlatformPasswordStoreController) Update() {
|
||||
passwordReply(&c.Controller, 404, "记录不存在", nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:改成的平台名称 + 网址 不能与自己的其它记录冲突
|
||||
if existID, ferr := findSamePlatformRecord(
|
||||
models.Orm.QueryTable(new(models.PlatformPasswordStore)).Filter("user_id", cl.UserID),
|
||||
id, p.Platform, p.URL); ferr == nil && existID > 0 {
|
||||
passwordReply(&c.Controller, 400, fmt.Sprintf("已存在同名同网址的平台「%s」(ID %d),请先合并或改名", p.Platform, existID), nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:同一平台下的登录账号不能重复
|
||||
if verr := validateAccountsUnique(p.Accounts); verr != nil {
|
||||
passwordReply(&c.Controller, 400, verr.Error(), nil)
|
||||
return
|
||||
@@ -404,8 +386,6 @@ func (c *PlatformPasswordStoreController) Delete() {
|
||||
passwordReply(&c.Controller, 200, "删除成功", nil)
|
||||
}
|
||||
|
||||
// ==================== 租户端 Password Store ====================
|
||||
|
||||
type BackendPasswordStoreController struct{ beego.Controller }
|
||||
|
||||
func (c *BackendPasswordStoreController) List() {
|
||||
@@ -424,7 +404,6 @@ func (c *BackendPasswordStoreController) List() {
|
||||
Or("remark__icontains", k)
|
||||
qs = qs.SetCond(orm.NewCondition().And("is_deleted", 0).And("tid", cl.TenantId).And("user_id", cl.UserID).AndCond(cond))
|
||||
}
|
||||
// 分页:默认每页 20 条,单页最多 200 条
|
||||
page, pageSize := passwordPageParams(&c.Controller)
|
||||
total, _ := qs.Count()
|
||||
var list []models.BackendPasswordStore
|
||||
@@ -489,14 +468,12 @@ func (c *BackendPasswordStoreController) Create() {
|
||||
passwordReply(&c.Controller, 400, "平台名称不能为空", nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:同租户下平台名称 + 网址 已存在则拒绝新增
|
||||
if existID, ferr := findSamePlatformRecord(
|
||||
models.Orm.QueryTable(new(models.BackendPasswordStore)).Filter("tid", cl.TenantId).Filter("user_id", cl.UserID),
|
||||
0, p.Platform, p.URL); ferr == nil && existID > 0 {
|
||||
passwordReply(&c.Controller, 400, fmt.Sprintf("已存在同名同网址的平台「%s」(ID %d),请直接编辑该记录", p.Platform, existID), nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:同一平台下的登录账号不能重复
|
||||
if verr := validateAccountsUnique(p.Accounts); verr != nil {
|
||||
passwordReply(&c.Controller, 400, verr.Error(), nil)
|
||||
return
|
||||
@@ -549,14 +526,12 @@ func (c *BackendPasswordStoreController) Update() {
|
||||
passwordReply(&c.Controller, 404, "记录不存在", nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:改成的平台名称 + 网址 不能与自己的其它记录冲突
|
||||
if existID, ferr := findSamePlatformRecord(
|
||||
models.Orm.QueryTable(new(models.BackendPasswordStore)).Filter("tid", cl.TenantId).Filter("user_id", cl.UserID),
|
||||
id, p.Platform, p.URL); ferr == nil && existID > 0 {
|
||||
passwordReply(&c.Controller, 400, fmt.Sprintf("已存在同名同网址的平台「%s」(ID %d),请先合并或改名", p.Platform, existID), nil)
|
||||
return
|
||||
}
|
||||
// 重复检测:同一平台下的登录账号不能重复
|
||||
if verr := validateAccountsUnique(p.Accounts); verr != nil {
|
||||
passwordReply(&c.Controller, 400, verr.Error(), nil)
|
||||
return
|
||||
@@ -609,14 +584,6 @@ func (c *BackendPasswordStoreController) Delete() {
|
||||
passwordReply(&c.Controller, 200, "删除成功", nil)
|
||||
}
|
||||
|
||||
// ==================== 批量导入 / 导出 ====================
|
||||
//
|
||||
// Excel 列顺序(与数据库字段一一对应):
|
||||
// 平台名称 platform | 网址 url | 账号 username | 密码 password |
|
||||
// 注册信息 registration_info | 账号备注 account_remark | 备注 remark
|
||||
// 一个平台可挂多个账号:多行「平台名称 + 网址」相同即合并为一条记录的多个账号;
|
||||
// 已存在的同平台同网址记录按「追加账号(跳过完全重复项)」处理。
|
||||
|
||||
type passwordImportRow struct {
|
||||
Platform string `json:"platform"`
|
||||
URL string `json:"url"`
|
||||
@@ -653,7 +620,6 @@ func parsePasswordImportRows(c *beego.Controller) ([]passwordImportRow, error) {
|
||||
return p.Rows, e
|
||||
}
|
||||
|
||||
// groupPasswordImportRows 按「平台名称 + 网址」聚合成记录,返回有序分组与行级错误
|
||||
func groupPasswordImportRows(rows []passwordImportRow) ([]*passwordImportGroup, []string) {
|
||||
order := make([]string, 0, len(rows))
|
||||
index := map[string]*passwordImportGroup{}
|
||||
@@ -685,7 +651,6 @@ func groupPasswordImportRows(rows []passwordImportRow) ([]*passwordImportGroup,
|
||||
RegistrationInfo: reg,
|
||||
Remark: accRemark,
|
||||
}
|
||||
// 文件内重复检测:同一「平台名称 + 网址」下登录账号重复则跳过
|
||||
dup := false
|
||||
for _, keep := range g.Accounts {
|
||||
if sameAccount(keep, item) {
|
||||
@@ -714,8 +679,6 @@ func groupPasswordImportRows(rows []passwordImportRow) ([]*passwordImportGroup,
|
||||
return out, failures
|
||||
}
|
||||
|
||||
// mergeAccounts 追加导入账号到已有账号之后,返回合并结果与「实际新增条数」。
|
||||
// 重复判定:登录账号相同(忽略大小写)即视为重复,账号为空时按密码比较。
|
||||
func mergeAccounts(exist, incoming []models.PasswordAccountItem) ([]models.PasswordAccountItem, int) {
|
||||
out := make([]models.PasswordAccountItem, 0, len(exist)+len(incoming))
|
||||
out = append(out, exist...)
|
||||
@@ -737,7 +700,6 @@ func mergeAccounts(exist, incoming []models.PasswordAccountItem) ([]models.Passw
|
||||
return out, added
|
||||
}
|
||||
|
||||
// Import POST /platform/passwordStore/import { rows: [...] }
|
||||
func (c *PlatformPasswordStoreController) Import() {
|
||||
cl, e := passwordClaims(&c.Controller, "platform")
|
||||
if e != nil {
|
||||
@@ -797,7 +759,6 @@ func (c *PlatformPasswordStoreController) Import() {
|
||||
passwordReply(&c.Controller, 200, "导入完成", res)
|
||||
}
|
||||
|
||||
// Export GET /platform/passwordStore/export 导出全部(不受列表 200 条限制)
|
||||
func (c *PlatformPasswordStoreController) Export() {
|
||||
cl, e := passwordClaims(&c.Controller, "platform")
|
||||
if e != nil {
|
||||
@@ -827,7 +788,6 @@ func (c *PlatformPasswordStoreController) Export() {
|
||||
passwordReply(&c.Controller, 200, "success", map[string]interface{}{"list": res, "total": len(res)})
|
||||
}
|
||||
|
||||
// Import POST /backend/passwordStore/import { rows: [...] }
|
||||
func (c *BackendPasswordStoreController) Import() {
|
||||
cl, e := passwordClaims(&c.Controller, "backend")
|
||||
if e != nil || cl.TenantId <= 0 {
|
||||
@@ -889,7 +849,6 @@ func (c *BackendPasswordStoreController) Import() {
|
||||
passwordReply(&c.Controller, 200, "导入完成", res)
|
||||
}
|
||||
|
||||
// Export GET /backend/passwordStore/export 导出当前租户下本人全部记录
|
||||
func (c *BackendPasswordStoreController) Export() {
|
||||
cl, e := passwordClaims(&c.Controller, "backend")
|
||||
if e != nil || cl.TenantId <= 0 {
|
||||
|
||||
Reference in new issue
Block a user