diff --git a/main.go b/main.go index 20818ba..46375a5 100644 --- a/main.go +++ b/main.go @@ -55,7 +55,10 @@ 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(true, redisClient)) + app.Post("/getuser", middleware.GetUserInfo(false, redisClient)) + app.Post("/getstudent", middleware.GetUserInfo(true, redisClient)) + app.Post("/getstudentsbynumber", middleware.GetStudentInfoByParent(redisClient)) + // Start server log.Fatal(app.Listen(":7777")) } diff --git a/middleware/student.go b/middleware/student.go index eba25f0..e25ff77 100644 --- a/middleware/student.go +++ b/middleware/student.go @@ -78,9 +78,16 @@ func GetUserInfo(isStudent bool, redisClient *redis.Storage) fiber.Handler { func GetStudentInfoByParent(redisClient *redis.Storage) fiber.Handler { return func(c *fiber.Ctx) error { // 获取POST 请求参数 - nationalId := c.Params("nationalId") - mobile := c.Params("mobile") - if nationalId == "" && mobile == "" { + var apiUser models.ReqApi + if err := c.BodyParser(&apiUser); err != nil { + return c.Status(http.StatusUnauthorized).JSON(fiber.Map{ + "message": "获取信息失败,参数错误", + "error": err.Error(), + }) + } + nationalId := apiUser.NationalId + phone := apiUser.Phone + if nationalId == "" && phone == "" { return c.Status(http.StatusUnauthorized).JSON(fiber.Map{ "message": "获取学生信息失败,身份证或手机号不能为空", "error": "身份证或手机号不能为空", @@ -97,21 +104,10 @@ func GetStudentInfoByParent(redisClient *redis.Storage) fiber.Handler { // 验证身份证有效性 url := cfg.APIParentUrl var reqBody map[string]string - if nationalId != "" { - reqBody = map[string]string{ - "token": token, - "nationalId": nationalId, - } - } else if mobile != "" { - reqBody = map[string]string{ - "token": token, - "mobile": mobile, - } - } else { - return c.Status(http.StatusUnauthorized).JSON(fiber.Map{ - "message": "获取学生信息失败,身份证或手机号不能为空", - "error": "身份证或手机号不能为空", - }) + reqBody = map[string]string{ + "token": token, + "nationalId": nationalId, + "phone": phone, } reqBodyJson, _ := json.Marshal(reqBody) req, err := http.Post(url, "application/json", bytes.NewBuffer(reqBodyJson)) @@ -126,7 +122,7 @@ func GetStudentInfoByParent(redisClient *redis.Storage) fiber.Handler { // 解析返回的json数据,判断是否有code字段并且code是否为1,如果是,则获取并返回token var result map[string]interface{} json.Unmarshal(body, &result) - if _, ok := result["code"]; !ok || result["code"] != 1 { + if _, ok := result["code"]; !ok || int(result["code"].(float64)) != 1 { return c.Status(http.StatusUnauthorized).JSON(fiber.Map{ "message": "获取学生信息失败,身份证或手机号无效", "error": "身份证或手机号无效", diff --git a/models/user.go b/models/user.go index d81b62d..15c5548 100644 --- a/models/user.go +++ b/models/user.go @@ -19,11 +19,11 @@ type User struct { } type ReqApi struct { - Code string `json:"code"` - Message string `json:"message"` + Code string `json:"code"` + Message string `json:"message"` NationalId string `json:"nationalId"` - Phtone string `json:"phone"` - Token string `json:"token"` + Phone string `json:"phone"` + Token string `json:"token"` } func (u *User) BeforeCreate(tx *gorm.DB) error {