This commit is contained in:
maxwell 2024-10-09 08:14:25 +08:00
parent 0d8e1cc7c4
commit 5686596a00
2 changed files with 8 additions and 54 deletions

View File

@ -55,7 +55,7 @@ func main() {
app.Get("/user", middleware.AuthMiddleware(redisClient), handlers.GetCurrentUser(db))
app.Put("/user", middleware.AuthMiddleware(redisClient), handlers.UpdateCurrentUser(db))
app.Get("/users/:id", middleware.AuthMiddleware(redisClient), handlers.GetUserByID(db))
app.Post("/getuser", middleware.GetUserInfo(redisClient))
app.Post("/getuser", middleware.GetUserInfo(true, redisClient))
// Start server
log.Fatal(app.Listen(":7777"))
}

View File

@ -15,7 +15,7 @@ import (
)
// POST 根据身份证和token来获取家长或老师的基本信息
func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
func GetUserInfo(isStudent bool, redisClient *redis.Storage) fiber.Handler {
return func(c *fiber.Ctx) error {
// 获取POST 请求参数
var apiUser models.ReqApi
@ -42,6 +42,9 @@ func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
}
// 验证身份证有效性
url := cfg.APIUserUrl
if isStudent {
url = cfg.APIStudentUrl
}
reqBody := map[string]string{
"token": token,
"nationalId": nationalId,
@ -50,7 +53,7 @@ func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
req, err := http.Post(url, "application/json", bytes.NewBuffer(reqBodyJson))
if err != nil {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
"message": "获取学生信息失败,无法解析URL",
"message": "获取身份信息失败,无法解析URL",
"error": err.Error(),
})
}
@ -60,58 +63,9 @@ func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Println(result)
if _, ok := result["code"]; !ok || result["code"].(int) != 1 {
if _, ok := result["code"]; !ok || int(result["code"].(float64)) != 1 {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
"message": "获取学生信息失败,身份证无效",
"error": "身份证无效",
})
}
// 如果身份正常,则返回所有信息
return c.JSON(result)
}
}
// 根据学生身份证和token获取学生的基本信息
func GetStudentInfo(redisClient *redis.Storage) fiber.Handler {
return func(c *fiber.Ctx) error {
// 获取POST 请求参数
nationalId := c.Params("nationalId")
if nationalId == "" {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
"message": "获取学生信息失败,身份证不能为空",
"error": "身份证不能为空",
})
}
cfg := config.New()
token, err := getEKTPlatformToken(redisClient, cfg.APIUser, cfg.APIPassword)
if err != nil {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
"message": "获取token失败,",
"error": err.Error(),
})
}
// 验证身份证有效性
url := cfg.APIStudentUrl
reqBody := map[string]string{
"token": token,
"nationalId": nationalId,
}
reqBodyJson, _ := json.Marshal(reqBody)
req, err := http.Post(url, "application/json", bytes.NewBuffer(reqBodyJson))
if err != nil {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
"message": "获取学生信息失败,无法解析URL",
"error": err.Error(),
})
}
defer req.Body.Close()
body, _ := io.ReadAll(req.Body)
// 解析返回的json数据判断是否有code字段并且code是否为1如果是则获取并返回token
var result map[string]interface{}
json.Unmarshal(body, &result)
if _, ok := result["code"]; !ok || result["code"] != 1 {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
"message": "获取学生信息失败,身份证无效",
"message": "获取身份信息失败,身份证无效",
"error": "身份证无效",
})
}