更新密码找回uniapp

This commit is contained in:
hero920103 committed 2026-11-16 17:22:50 +08:00
1 parent 7f9a66da0f
commit 3fdda89833
1304 files changed
+3503 -26132

No files matched your search

+1 -36
View File
@@ -1,22 +1,3 @@
// 统一认证中心一次性数据迁移:
// yz_system_tenant_user → yz_auth_identity(自然人)+ yz_auth_tenant_user(身份-企业绑定)
//
// 前置条件:
// 1. 已人工执行 docs/sql/create_auth_tables.sql 建表;
// 2. 在 go/ 目录下运行(需读取 conf/app.conf)。
//
// 用法:
// go run scripts/migrate_auth.go -check 预检:只输出统计与冲突报告,不写任何数据
// go run scripts/migrate_auth.go -apply 执行迁移(自动备份源表后再写入)
// go run scripts/migrate_auth.go -apply -force 目标表已有数据时强制重跑
//
// 迁移规则:
// - 归并键优先级:手机号 > 邮箱 > 租户内账号;三者皆空时按 tid+原 uid 兜底;
// - 同一归并键命中多条(多企业任职)→ 只建 1 个 identity,再建 N 条企业绑定;
// - 同一归并键下密码不一致 → 记为冲突,取 is_default 优先、其次 id 最大的那条,
// 并写入冲突报告供人工核对;
// - 历史密码为 legacy(salt$sha256),无法离线转 argon2id,原样搬迁,
// 用户登录成功时自动重新哈希升级。
package main
import (
@@ -36,7 +17,6 @@ import (
"server/pkg/passwordutil"
)
// srcRow 源表行(yz_system_tenant_user)
type srcRow struct {
ID uint64
Tid uint64
@@ -52,11 +32,10 @@ type srcRow struct {
Status int8
}
// group 同一归并键下的多条源记录
type group struct {
key string
rows []srcRow
conflict bool // 同键下密码不一致
conflict bool
pwdValues int
}
@@ -67,7 +46,6 @@ func trimPtr(p *string) string {
return strings.TrimSpace(*p)
}
// mergeKey 归并键:手机 > 邮箱 > 账号;都为空时返回空串(由调用方兜底)
func mergeKey(r srcRow) string {
if r.Phone != "" {
return "m:" + r.Phone
@@ -109,7 +87,6 @@ func main() {
groups := buildGroups(rows)
log.Printf("归并后身份数: %d 个(其中冲突 %d 组)", len(groups), countConflict(groups))
// 冲突报告
if err := writeConflictReport(groups); err != nil {
log.Printf("写入冲突报告失败: %v", err)
}
@@ -120,7 +97,6 @@ func main() {
return
}
// ---- 执行迁移 ----
if err := ensureTargetWritable(*force); err != nil {
log.Fatalf("目标表检查失败: %v", err)
}
@@ -140,7 +116,6 @@ func main() {
_ = mapWriter.Write([]string{"old_id", "old_uid", "tid", "identity_id", "tenant_user_id", "account", "merge_key", "conflict"})
for _, g := range groups {
// 代表行:is_default 优先,其次 id 最大
rep := pickRepresentative(g)
identityID, err := insertIdentity(rep, g.key)
if err != nil {
@@ -188,7 +163,6 @@ func main() {
log.Printf("映射文件已生成:%s(保存好,回滚或排查用)", mapFile.Name())
}
// loadSourceRows 读取未软删的源记录
func loadSourceRows() ([]srcRow, error) {
var list []models.SystemTenantUser
_, err := models.Orm.QueryTable(new(models.SystemTenantUser)).
@@ -217,14 +191,12 @@ func loadSourceRows() ([]srcRow, error) {
return rows, nil
}
// buildGroups 按归并键分组,并标记密码冲突
func buildGroups(rows []srcRow) []*group {
buckets := map[string]*group{}
order := make([]string, 0)
for _, r := range rows {
key := mergeKey(r)
if key == "" {
// 手机/邮箱/账号全空:无法归并,按 tid+uid 兜底为独立身份
key = fmt.Sprintf("u:%d-%d", r.Tid, r.Uid)
}
g, ok := buckets[key]
@@ -250,7 +222,6 @@ func buildGroups(rows []srcRow) []*group {
return out
}
// pickRepresentative 选出代表行:is_default 优先,其次 id 最大
func pickRepresentative(g *group) srcRow {
sorted := make([]srcRow, len(g.rows))
copy(sorted, g.rows)
@@ -273,7 +244,6 @@ func countConflict(groups []*group) int {
return n
}
// insertIdentity 写入身份;密码算法按原值识别(legacy 保持原样,首登自动升级)
func insertIdentity(rep srcRow, key string) (uint64, error) {
algo := passwordutil.AlgoLegacy
if passwordutil.AlgoOf(rep.Password) == passwordutil.AlgoArgon2id {
@@ -300,7 +270,6 @@ func insertIdentity(rep srcRow, key string) (uint64, error) {
return uint64(id), err
}
// ensureTargetWritable 目标表已有数据时需 -force 才继续,避免重复迁移
func ensureTargetWritable(force bool) error {
cnt, err := models.Orm.QueryTable(new(models.AuthTenantUser)).Count()
if err != nil {
@@ -312,7 +281,6 @@ func ensureTargetWritable(force bool) error {
return nil
}
// backupSourceTable 备份源表
func backupSourceTable() error {
name := fmt.Sprintf("yz_system_tenant_user_bak_%s", time.Now().Format("20060102150405"))
_, err := models.Orm.Raw(fmt.Sprintf("CREATE TABLE `%s` AS SELECT * FROM `yz_system_tenant_user`", name)).Exec()
@@ -323,7 +291,6 @@ func backupSourceTable() error {
return nil
}
// writeConflictReport 输出冲突报告 CSV
func writeConflictReport(groups []*group) error {
conflicts := make([]*group, 0)
for _, g := range groups {
@@ -331,7 +298,6 @@ func writeConflictReport(groups []*group) error {
conflicts = append(conflicts, g)
}
}
// 无法归并(手机/邮箱/账号全空)的记录也提示
unkeyed := 0
for _, g := range groups {
if strings.HasPrefix(g.key, "u:") {
@@ -370,7 +336,6 @@ func writeConflictReport(groups []*group) error {
return nil
}
// printSummary 预检模式输出摘要
func printSummary(rows []srcRow, groups []*group) {
tenantSet := map[uint64]bool{}
for _, r := range rows {