瀏覽代碼

本地登录功能修改

yaojianjun 9 月之前
父節點
當前提交
a53e94e64c
共有 4 個文件被更改,包括 133 次插入 和 59 次删除
  1. 1 0
      Tech/common/信息化接口定义.go
  2. 98 48
      Tech/controllers/用户.go
  3. 20 0
      Tech/controllers/课程.go
  4. 14 11
      Tech/controllers/课程运行.go

+ 1 - 0
Tech/common/信息化接口定义.go

@@ -200,6 +200,7 @@ type PersonalInfo struct {
 	JSDJ           string        `json:"JSDJ"`           // 讲师等级
 	JSDJ           string        `json:"JSDJ"`           // 讲师等级
 	XL             string        `json:"XL"`             // 学历
 	XL             string        `json:"XL"`             // 学历
 	Count          string        `json:"Count"`          // 数量
 	Count          string        `json:"Count"`          // 数量
+
 }
 }
 
 
 // GetPersonalInfoResponse 获取个人信息响应结构体
 // GetPersonalInfoResponse 获取个人信息响应结构体

+ 98 - 48
Tech/controllers/用户.go

@@ -7,6 +7,7 @@ import (
 	"crypto/rand"
 	"crypto/rand"
 	"encoding/base64"
 	"encoding/base64"
 	"encoding/json"
 	"encoding/json"
+	"fmt"
 	"sync"
 	"sync"
 
 
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/beego/beego/v2/client/orm"
@@ -24,6 +25,8 @@ var cacheMutex sync.RWMutex // 保护缓存的读写锁
 
 
 var simpleTokenStore = make(map[string]string) // token -> userId
 var simpleTokenStore = make(map[string]string) // token -> userId
 
 
+var CurrentLoginMode int = 0 // 0-信息化验证,1-本地数据库验证
+
 // GetUserInfoFromCache 通过用户名从缓存中获取用户信息
 // GetUserInfoFromCache 通过用户名从缓存中获取用户信息
 func GetUserInfoFromCache(userName string) (common.PersonalInfo, bool) {
 func GetUserInfoFromCache(userName string) (common.PersonalInfo, bool) {
 	// 先尝试读取缓存
 	// 先尝试读取缓存
@@ -106,14 +109,14 @@ func InitUserInfoCache() {
 
 
 	// 批量插入数据库以提高效率
 	// 批量插入数据库以提高效率
 	o := orm.NewOrm()
 	o := orm.NewOrm()
-	
+
 	// 开始事务
 	// 开始事务
 	txOrmer, err := o.Begin()
 	txOrmer, err := o.Begin()
 	if err != nil {
 	if err != nil {
 		logs.Error("开启事务失败: ", err)
 		logs.Error("开启事务失败: ", err)
 		return
 		return
 	}
 	}
-	
+
 	// 先清空现有的UserData表
 	// 先清空现有的UserData表
 	_, err = txOrmer.Raw("DELETE FROM UserData").Exec()
 	_, err = txOrmer.Raw("DELETE FROM UserData").Exec()
 	if err != nil {
 	if err != nil {
@@ -121,7 +124,7 @@ func InitUserInfoCache() {
 		logs.Error("清空用户数据表失败: ", err)
 		logs.Error("清空用户数据表失败: ", err)
 		return
 		return
 	}
 	}
-	
+
 	// 批量插入用户数据
 	// 批量插入用户数据
 	if len(tempUser) > 0 {
 	if len(tempUser) > 0 {
 		// 使用批量插入提高效率
 		// 使用批量插入提高效率
@@ -132,7 +135,7 @@ func InitUserInfoCache() {
 			return
 			return
 		}
 		}
 	}
 	}
-	
+
 	// 提交事务
 	// 提交事务
 	err = txOrmer.Commit()
 	err = txOrmer.Commit()
 	if err != nil {
 	if err != nil {
@@ -162,8 +165,9 @@ func deleteShortToken(token string) {
 // UserLogin 用户登录认证并生成Token
 // UserLogin 用户登录认证并生成Token
 func (c *UserController) UserLogin() {
 func (c *UserController) UserLogin() {
 	var req struct {
 	var req struct {
-		UserName string `json:"userName"`
-		Password string `json:"password"`
+		UserName  string `json:"userName"`
+		Password  string `json:"password"`
+		LoginMode int    `json:"loginMode"` // 0-信息化验证,1-本地数据库验证
 	}
 	}
 	requestBody := c.Ctx.Input.RequestBody
 	requestBody := c.Ctx.Input.RequestBody
 	println("Request Body:", string(requestBody))
 	println("Request Body:", string(requestBody))
@@ -187,56 +191,102 @@ func (c *UserController) UserLogin() {
 		return
 		return
 	}
 	}
 
 
-	// 使用信息化系统进行身份验证
-	loginResp, err := common.LoginToInformationSystem(req.UserName, req.Password)
-	if err != nil {
-		c.Ctx.Output.SetStatus(500)
-		c.Data["json"] = map[string]interface{}{
-			"code": 500,
-			"msg":  "信息化系统验证失败: " + err.Error(),
+	var personalInfo common.PersonalInfo
+
+	// 根据loginMode选择验证方式
+	if req.LoginMode == 1 {
+		// 本地数据库验证
+		o := orm.NewOrm()
+		var userData models.UserData
+		err := o.QueryTable(new(models.UserData)).Filter("人员ID", req.UserName).One(&userData)
+		if err != nil {
+			c.Ctx.Output.SetStatus(401)
+			c.Data["json"] = map[string]interface{}{
+				"code": 401,
+				"msg":  "用户名或密码错误",
+			}
+			c.ServeJSON()
+			return
 		}
 		}
-		c.ServeJSON()
-		return
-	}
 
 
-	// 检查登录结果
-	if loginResp.ResultCode != "0" {
-		c.Ctx.Output.SetStatus(401)
-		c.Data["json"] = map[string]interface{}{
-			"code": 401,
-			"msg":  loginResp.ResultDesc,
+		// 验证密码
+		if userData.Password != req.Password {
+			c.Ctx.Output.SetStatus(401)
+			c.Data["json"] = map[string]interface{}{
+				"code": 401,
+				"msg":  "用户名或密码错误",
+			}
+			c.ServeJSON()
+			return
 		}
 		}
-		c.ServeJSON()
-		return
-	}
 
 
-	// 登录成功,获取用户详细信息
-	personalInfoResp, err := common.GetPersonalInfoFromInformationSystem(loginResp.RYEMPID)
-	if err != nil {
-		c.Ctx.Output.SetStatus(500)
-		c.Data["json"] = map[string]interface{}{
-			"code": 500,
-			"msg":  "获取用户信息失败: " + err.Error(),
+		// 从缓存中获取详细信息(如果有)
+		if info, exists := GetUserInfoFromCache(userData.RYID); exists {
+			personalInfo = info
+		} else {
+			// 如果缓存中没有,创建基本信息
+			personalInfo = common.PersonalInfo{
+				RYID:    userData.RYID,
+				RYEMPID: userData.RYEMPID,
+				XM:      userData.UserName,
+				SF:      fmt.Sprintf("%d", userData.UserType),
+			}
 		}
 		}
-		c.ServeJSON()
-		return
-	}
 
 
-	// 检查获取用户信息的结果
-	if personalInfoResp.ResultCode != "0" {
-		c.Ctx.Output.SetStatus(500)
-		c.Data["json"] = map[string]interface{}{
-			"code": 500,
-			"msg":  "获取用户信息失败: " + personalInfoResp.ResultDesc,
+		CurrentLoginMode = 1
+	} else {
+		// 默认使用信息化系统验证 (req.LoginMode == 0)
+		loginResp, err := common.LoginToInformationSystem(req.UserName, req.Password)
+		if err != nil {
+			c.Ctx.Output.SetStatus(500)
+			c.Data["json"] = map[string]interface{}{
+				"code": 500,
+				"msg":  "信息化系统验证失败: " + err.Error(),
+			}
+			c.ServeJSON()
+			return
 		}
 		}
-		c.ServeJSON()
-		return
-	}
 
 
-	// 获取第一个用户的信息(通常只有一个)
-	var personalInfo common.PersonalInfo
-	if len(personalInfoResp.PersonalInfos) > 0 {
-		personalInfo = personalInfoResp.PersonalInfos[0]
+		// 检查登录结果
+		if loginResp.ResultCode != "0" {
+			c.Ctx.Output.SetStatus(401)
+			c.Data["json"] = map[string]interface{}{
+				"code": 401,
+				"msg":  loginResp.ResultDesc,
+			}
+			c.ServeJSON()
+			return
+		}
+
+		// 登录成功,获取用户详细信息
+		personalInfoResp, err := common.GetPersonalInfoFromInformationSystem(loginResp.RYEMPID)
+		if err != nil {
+			c.Ctx.Output.SetStatus(500)
+			c.Data["json"] = map[string]interface{}{
+				"code": 500,
+				"msg":  "获取用户信息失败: " + err.Error(),
+			}
+			c.ServeJSON()
+			return
+		}
+
+		// 检查获取用户信息的结果
+		if personalInfoResp.ResultCode != "0" {
+			c.Ctx.Output.SetStatus(500)
+			c.Data["json"] = map[string]interface{}{
+				"code": 500,
+				"msg":  "获取用户信息失败: " + personalInfoResp.ResultDesc,
+			}
+			c.ServeJSON()
+			return
+		}
+
+		// 获取第一个用户的信息(通常只有一个)
+		if len(personalInfoResp.PersonalInfos) > 0 {
+			personalInfo = personalInfoResp.PersonalInfos[0]
+		}
+
+		CurrentLoginMode = 0
 	}
 	}
 
 
 	// 将用户信息存入缓存
 	// 将用户信息存入缓存

+ 20 - 0
Tech/controllers/课程.go

@@ -1913,6 +1913,16 @@ func (c *CourseController) GetTMSCoursePlan() {
 	}
 	}
 	var req Request
 	var req Request
 
 
+	if CurrentLoginMode == 0 {
+		c.Ctx.Output.SetStatus(500)
+		c.Data["json"] = map[string]interface{}{
+			"code": 500,
+			"msg":  "本地模式不支持该功能 ",
+		}
+		c.ServeJSON()
+		return
+	}
+
 	// 从app.conf中读取SYSDEVICEID作为SBID的值
 	// 从app.conf中读取SYSDEVICEID作为SBID的值
 	sbid, err := beego.AppConfig.String("SYSDEVICEID")
 	sbid, err := beego.AppConfig.String("SYSDEVICEID")
 	if err != nil {
 	if err != nil {
@@ -2047,6 +2057,16 @@ func (c *CourseController) GetTMSCoursePlanStudent() {
 		PXSSID string `json:"PXSSID"`
 		PXSSID string `json:"PXSSID"`
 	}
 	}
 
 
+	if CurrentLoginMode == 0 {
+		c.Ctx.Output.SetStatus(500)
+		c.Data["json"] = map[string]interface{}{
+			"code": 500,
+			"msg":  "本地模式不支持该功能 ",
+		}
+		c.ServeJSON()
+		return
+	}
+
 	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
 	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
 		c.Ctx.Output.SetStatus(400)
 		c.Ctx.Output.SetStatus(400)
 		c.Data["json"] = map[string]interface{}{
 		c.Data["json"] = map[string]interface{}{

+ 14 - 11
Tech/controllers/课程运行.go

@@ -153,7 +153,7 @@ func (c *CourseRunController) CourseRun() {
 		return
 		return
 	}
 	}
 
 
-	if req.TrainingImplementationID != "" && req.TrainingPlanID != "" {
+	if req.TrainingImplementationID != "" && req.TrainingPlanID != "" && CurrentLoginMode == 0 {
 		// 通知信息化系统开始培训
 		// 通知信息化系统开始培训
 		err = common.NotifyTMSStartTraining(int(id), course, req.TraineeGUID, req.TrainingImplementationID, req.TrainingPlanID, "")
 		err = common.NotifyTMSStartTraining(int(id), course, req.TraineeGUID, req.TrainingImplementationID, req.TrainingPlanID, "")
 		if err != nil {
 		if err != nil {
@@ -592,13 +592,6 @@ func (c *CourseRunController) CourseStop() {
 		},
 		},
 	}
 	}
 
 
-	// 通知信息化系统结束培训
-	err = common.NotifyTMSStopTraining(latestCourse.TrainingRecordGUID)
-	if err != nil {
-		// 记录错误日志,但不中断操作
-		fmt.Println("通知信息化系统结束培训失败: " + err.Error())
-	}
-
 	// 通知电调服务课程结束
 	// 通知电调服务课程结束
 	_, err = c.tellEStopCourse(latestCourse.RunCourseID, latestCourse.RunLogID)
 	_, err = c.tellEStopCourse(latestCourse.RunCourseID, latestCourse.RunLogID)
 	if err != nil {
 	if err != nil {
@@ -611,10 +604,20 @@ func (c *CourseRunController) CourseStop() {
 		// return
 		// return
 	}
 	}
 
 
-	err = DoUploadTMS(latestCourse.RunLogID)
-	if err != nil {
-		fmt.Println("上传电子考评表失败: " + err.Error())
+	if CurrentLoginMode == 0 {
+		// 通知信息化系统结束培训
+		err = common.NotifyTMSStopTraining(latestCourse.TrainingRecordGUID)
+		if err != nil {
+			// 记录错误日志,但不中断操作
+			fmt.Println("通知信息化系统结束培训失败: " + err.Error())
+		}
+
+		err = DoUploadTMS(latestCourse.RunLogID)
+		if err != nil {
+			fmt.Println("上传电子考评表失败: " + err.Error())
+		}
 	}
 	}
+
 	c.ServeJSON()
 	c.ServeJSON()
 }
 }