Pārlūkot izejas kodu

同步管理员信息

xyxie 11 mēneši atpakaļ
vecāks
revīzija
b830381b61
2 mainītis faili ar 37 papildinājumiem un 27 dzēšanām
  1. 7 1
      models/db.go
  2. 30 26
      services/admin.go

+ 7 - 1
models/db.go

@@ -1,6 +1,7 @@
 package models
 
 import (
+	"eta/eta_forum_hub/models/system"
 	"eta/eta_forum_hub/utils"
 	"github.com/beego/beego/v2/client/orm"
 	_ "github.com/go-sql-driver/mysql"
@@ -17,9 +18,10 @@ func init() {
 
 	orm.Debug = true
 	orm.DebugLog = orm.NewLog(utils.Binlog)
-	
+
 	initChart()
 	initEdbData()
+	initSystem()
 }
 
 // initChart 图表 数据表
@@ -40,3 +42,7 @@ func initEdbData() {
 		new(EdbInfo),
 	)
 }
+
+func initSystem() {
+	orm.RegisterModel(new(system.Admin))
+}

+ 30 - 26
services/admin.go

@@ -35,37 +35,41 @@ func AddOrUpdateAdmin(req system.Admin) (err error, isSendEmail bool) {
 		return
 	}
 	if req.Mobile != "" {
-		if req.TelAreaCode == "86" {
-			if !utils.ValidateMobileFormatat(req.Mobile) {
-				err = fmt.Errorf("手机号格式有误, 请检查")
+		if isAdd || (!isAdd && req.Mobile != admin.Mobile) {
+			if req.TelAreaCode == "86" {
+				if !utils.ValidateMobileFormatat(req.Mobile) {
+					err = fmt.Errorf("手机号格式有误, 请检查")
+					return
+				}
+			}
+			mobileCount, e := system.GetSysAdminCountByMobile(req.Mobile, 0)
+			if e != nil {
+				err = fmt.Errorf("判断手机号是否存在失败,%w", err)
+				return
+			}
+			if mobileCount > 0 {
+				err = fmt.Errorf("手机号已存在,请重新填写")
+				isSendEmail = false
 				return
 			}
 		}
 
-		mobileCount, e := system.GetSysAdminCountByMobile(req.Mobile, 0)
-		if e != nil {
-			err = fmt.Errorf("判断手机号是否存在失败,%w", err)
-			return
-		}
-		if mobileCount > 0 {
-			err = fmt.Errorf("手机号已存在,请重新填写")
-			isSendEmail = false
-			return
-		}
 	}
 	if req.Email != "" {
-		if !utils.ValidateEmailFormatat(req.Email) {
-			err = fmt.Errorf("邮箱格式有误, 请检查")
-			return
-		}
-		emailUser, e := system.GetSysUserByEmail(req.Email)
-		if e != nil && e.Error() != utils.ErrNoRow() {
-			err = fmt.Errorf("邮箱获取用户失败,%w", e)
-			return
-		}
-		if emailUser != nil && emailUser.AdminId > 0 {
-			err = fmt.Errorf("邮箱已存在, 请重新填写")
-			return
+		if isAdd || (!isAdd && req.Mobile != admin.Mobile) {
+			if !utils.ValidateEmailFormatat(req.Email) {
+				err = fmt.Errorf("邮箱格式有误, 请检查")
+				return
+			}
+			emailUser, e := system.GetSysUserByEmail(req.Email)
+			if e != nil && e.Error() != utils.ErrNoRow() {
+				err = fmt.Errorf("邮箱获取用户失败,%w", e)
+				return
+			}
+			if emailUser != nil && emailUser.AdminId > 0 {
+				err = fmt.Errorf("邮箱已存在, 请重新填写")
+				return
+			}
 		}
 	}
 
@@ -76,7 +80,7 @@ func AddOrUpdateAdmin(req system.Admin) (err error, isSendEmail bool) {
 	admin.RealName = req.RealName
 	admin.Password = req.Password
 	admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
-	admin.Enabled = 1
+	admin.Enabled = req.Enabled
 	admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
 	admin.CreatedTime = time.Now()
 	admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)