更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -16,32 +16,24 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// 统一认证中心自助账户能力:注册、找回密码、验证码。
|
||||
//
|
||||
// 与旧版 password_reset.go(按「租户名+账号」定位老表)不同,这里直接操作
|
||||
// 统一身份层 yz_auth_identity:手机号即全局账号,一处改密全平台生效。
|
||||
|
||||
var mobileRegexp = regexp.MustCompile(`^1[3-9]\d{9}$`)
|
||||
|
||||
// identityCodeItem 短信验证码条目
|
||||
type identityCodeItem struct {
|
||||
Code string
|
||||
ExpiredAt time.Time
|
||||
IssuedAt time.Time
|
||||
}
|
||||
|
||||
var identityCodeStore sync.Map // key: purpose|phone
|
||||
var identityCodeStore sync.Map
|
||||
|
||||
func identityCodeKey(purpose, phone string) string {
|
||||
return strings.TrimSpace(purpose) + "|" + strings.TrimSpace(phone)
|
||||
}
|
||||
|
||||
// IsMobile 校验手机号格式
|
||||
func IsMobile(phone string) bool {
|
||||
return mobileRegexp.MatchString(strings.TrimSpace(phone))
|
||||
}
|
||||
|
||||
// findIdentityByMobile 按手机号查身份(含软删过滤)
|
||||
func findIdentityByMobile(phone string) *models.AuthIdentity {
|
||||
if !IsMobile(phone) {
|
||||
return nil
|
||||
@@ -56,11 +48,6 @@ func findIdentityByMobile(phone string) *models.AuthIdentity {
|
||||
return &m
|
||||
}
|
||||
|
||||
// SendIdentityCode 发送自助账户短信验证码。
|
||||
// - purpose=register:手机号必须未注册
|
||||
// - purpose=reset:手机号必须已注册
|
||||
//
|
||||
// 60 秒服务端冷却,防短信轰炸;验证码 5 分钟有效。
|
||||
func SendIdentityCode(purpose, phone string) error {
|
||||
phone = strings.TrimSpace(phone)
|
||||
if !IsMobile(phone) {
|
||||
@@ -75,18 +62,15 @@ func SendIdentityCode(purpose, phone string) error {
|
||||
return errors.New("该手机号已注册,请直接登录或找回密码")
|
||||
}
|
||||
if purpose == "reset" && existed == nil {
|
||||
// 不暴露「手机号是否存在」会牺牲可用性;注册页已明示手机号唯一,这里直接提示
|
||||
return errors.New("该手机号尚未注册")
|
||||
}
|
||||
|
||||
// 服务端 60 秒冷却
|
||||
if v, ok := identityCodeStore.Load(identityCodeKey(purpose, phone)); ok {
|
||||
if item, ok := v.(identityCodeItem); ok && time.Since(item.IssuedAt) < 60*time.Second {
|
||||
return errors.New("验证码发送过于频繁,请稍后再试")
|
||||
}
|
||||
}
|
||||
|
||||
// 短信网关配置校验前置,避免生成验证码后发送失败造成困惑
|
||||
if _, _, err := getDefaultSystemSMSConfig(); err != nil {
|
||||
return errors.New("短信服务未配置,请联系平台管理员")
|
||||
}
|
||||
@@ -106,7 +90,6 @@ func SendIdentityCode(purpose, phone string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// VerifyIdentityCode 校验自助账户短信验证码,验证通过后立即销毁(一次性)
|
||||
func VerifyIdentityCode(purpose, phone, code string) error {
|
||||
phone = strings.TrimSpace(phone)
|
||||
code = strings.TrimSpace(code)
|
||||
@@ -133,27 +116,21 @@ func VerifyIdentityCode(purpose, phone, code string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterIdentityInput 自助注册入参
|
||||
type RegisterIdentityInput struct {
|
||||
Phone string
|
||||
Code string
|
||||
Password string
|
||||
Confirm string
|
||||
Nickname string
|
||||
TenantCode string // 选填:填了则注册后直接加入该企业(默认「租户用户」角色)
|
||||
TenantCode string
|
||||
}
|
||||
|
||||
// RegisterIdentityResult 注册结果
|
||||
type RegisterIdentityResult struct {
|
||||
IdentityID uint64
|
||||
JoinedTid uint64 // 成功加入的企业;0 表示未加入
|
||||
Warning string // 加入企业失败的提示(身份已创建成功)
|
||||
JoinedTid uint64
|
||||
Warning string
|
||||
}
|
||||
|
||||
// RegisterIdentity 自助注册:创建统一身份,可选加入企业。
|
||||
//
|
||||
// 统一认证原则:手机号即全局账号,密码在身份层一份;加入企业只是建绑定,
|
||||
// 企业内角色默认「租户用户」,后续由企业管理员调整。
|
||||
func RegisterIdentity(in RegisterIdentityInput) (*RegisterIdentityResult, error) {
|
||||
phone := strings.TrimSpace(in.Phone)
|
||||
if !IsMobile(phone) {
|
||||
@@ -169,7 +146,6 @@ func RegisterIdentity(in RegisterIdentityInput) (*RegisterIdentityResult, error)
|
||||
return nil, errors.New("两次输入的密码不一致")
|
||||
}
|
||||
|
||||
// 防并发重复注册
|
||||
if findIdentityByMobile(phone) != nil {
|
||||
return nil, errors.New("该手机号已注册,请直接登录")
|
||||
}
|
||||
@@ -184,7 +160,6 @@ func RegisterIdentity(in RegisterIdentityInput) (*RegisterIdentityResult, error)
|
||||
nickname = "用户" + phone[len(phone)-4:]
|
||||
}
|
||||
|
||||
// 选填企业编码:先校验企业存在且启用、配额未满,失败直接拒绝(身份尚未创建)
|
||||
tenantCode := strings.TrimSpace(in.TenantCode)
|
||||
var tenant *models.SystemTenant
|
||||
if tenantCode != "" {
|
||||
@@ -220,7 +195,6 @@ func RegisterIdentity(in RegisterIdentityInput) (*RegisterIdentityResult, error)
|
||||
|
||||
result := &RegisterIdentityResult{IdentityID: identity.ID}
|
||||
|
||||
// 加入企业:建绑定(老表 + 认证中心),角色默认「租户用户」
|
||||
if tenant != nil {
|
||||
warn := joinTenantAfterRegister(identity, tenant, nickname, phone, hashed)
|
||||
if warn != "" {
|
||||
@@ -233,10 +207,7 @@ func RegisterIdentity(in RegisterIdentityInput) (*RegisterIdentityResult, error)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// joinTenantAfterRegister 注册后加入企业;返回警告信息(空串表示成功)。
|
||||
// 身份已创建成功,加入失败仅降级为警告,不回滚注册。
|
||||
func joinTenantAfterRegister(identity *models.AuthIdentity, tenant *models.SystemTenant, nickname, phone, passwordHash string) string {
|
||||
// 企业内账号默认用手机号;若已被本企业其他身份占用则放弃自动加入
|
||||
var dup models.SystemTenantUser
|
||||
if err := models.Orm.QueryTable(new(models.SystemTenantUser)).
|
||||
Filter("tid", tenant.ID).
|
||||
@@ -258,7 +229,6 @@ func joinTenantAfterRegister(identity *models.AuthIdentity, tenant *models.Syste
|
||||
return "注册成功,但加入企业失败,请联系企业管理员邀请你加入"
|
||||
}
|
||||
|
||||
// 默认「租户用户」角色;角色 ID 一并写入认证中心绑定
|
||||
groupID := uint64(0)
|
||||
if roleID, err := models.GetTenantUserRole(); err == nil && roleID > 0 {
|
||||
groupID = roleID
|
||||
@@ -268,7 +238,6 @@ func joinTenantAfterRegister(identity *models.AuthIdentity, tenant *models.Syste
|
||||
Update(map[string]interface{}{"group_id": roleID})
|
||||
}
|
||||
|
||||
// 双写认证中心绑定
|
||||
if err := authsvc.SyncTenantUser(authsvc.SyncTenantUserInput{
|
||||
Tid: tenant.ID,
|
||||
Account: phoneStr,
|
||||
@@ -285,8 +254,6 @@ func joinTenantAfterRegister(identity *models.AuthIdentity, tenant *models.Syste
|
||||
return ""
|
||||
}
|
||||
|
||||
// ResetIdentityPasswordByMobile 自助找回密码:验证码通过后重置身份层密码。
|
||||
// 密码属于身份层,重置后该用户在所有企业的登录密码同步更新。
|
||||
func ResetIdentityPasswordByMobile(phone, code, newPassword, confirm string) error {
|
||||
phone = strings.TrimSpace(phone)
|
||||
if !IsMobile(phone) {
|
||||
@@ -307,30 +274,44 @@ func ResetIdentityPasswordByMobile(phone, code, newPassword, confirm string) err
|
||||
return errors.New("该手机号尚未注册")
|
||||
}
|
||||
|
||||
hashed, err := passwordutil.Hash(strings.TrimSpace(newPassword))
|
||||
if err != nil {
|
||||
return errors.New("密码处理失败")
|
||||
}
|
||||
|
||||
_, err = models.Orm.QueryTable(new(models.AuthIdentity)).
|
||||
Filter("id", identity.ID).
|
||||
Update(map[string]interface{}{
|
||||
"password_hash": hashed,
|
||||
"password_algo": passwordutil.AlgoOf(hashed),
|
||||
"fail_count": 0,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("密码重置失败,请稍后重试")
|
||||
}
|
||||
|
||||
// 老表镜像同步:把新密码写到该身份在各企业的 yz_system_tenant_user 行,
|
||||
// 保持「老表密码 = 身份层密码」的双写口径(登录以认证中心为准)
|
||||
mirrorPasswordToTenantRows(identity.ID, hashed)
|
||||
|
||||
return nil
|
||||
return applyIdentityPassword(identity, newPassword)
|
||||
}
|
||||
|
||||
func ChangeIdentityPassword(identityID uint64, oldPassword, newPassword, confirm string) error {
|
||||
oldPassword = strings.TrimSpace(oldPassword)
|
||||
newPassword = strings.TrimSpace(newPassword)
|
||||
if identityID == 0 {
|
||||
return errors.New("登录已失效,请重新登录")
|
||||
}
|
||||
if oldPassword == "" {
|
||||
return errors.New("请输入当前密码")
|
||||
}
|
||||
if len(newPassword) < 6 {
|
||||
return errors.New("新密码长度不能少于6个字符")
|
||||
}
|
||||
if newPassword != strings.TrimSpace(confirm) {
|
||||
return errors.New("两次输入的新密码不一致")
|
||||
}
|
||||
if newPassword == oldPassword {
|
||||
return errors.New("新密码不能与当前密码相同")
|
||||
}
|
||||
|
||||
var identity models.AuthIdentity
|
||||
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
|
||||
Filter("id", identityID).
|
||||
One(&identity); err != nil {
|
||||
return errors.New("账号不存在")
|
||||
}
|
||||
if identity.Status == models.AuthIdentityStatusDisabled {
|
||||
return errors.New("账号已禁用,无法修改密码")
|
||||
}
|
||||
if identity.PasswordHash == nil || !passwordutil.Verify(*identity.PasswordHash, oldPassword) {
|
||||
return errors.New("当前密码不正确")
|
||||
}
|
||||
|
||||
return applyIdentityPassword(&identity, newPassword)
|
||||
}
|
||||
|
||||
// mirrorPasswordToTenantRows 把身份层密码镜像到该身份所有企业绑定的老表行
|
||||
func mirrorPasswordToTenantRows(identityID uint64, passwordHash string) {
|
||||
var binds []models.AuthTenantUser
|
||||
if _, err := models.Orm.QueryTable(new(models.AuthTenantUser)).
|
||||
|
||||
Reference in new issue
Block a user