update
This commit is contained in:
parent
0d8e1cc7c4
commit
5686596a00
2
main.go
2
main.go
@ -55,7 +55,7 @@ func main() {
|
|||||||
app.Get("/user", middleware.AuthMiddleware(redisClient), handlers.GetCurrentUser(db))
|
app.Get("/user", middleware.AuthMiddleware(redisClient), handlers.GetCurrentUser(db))
|
||||||
app.Put("/user", middleware.AuthMiddleware(redisClient), handlers.UpdateCurrentUser(db))
|
app.Put("/user", middleware.AuthMiddleware(redisClient), handlers.UpdateCurrentUser(db))
|
||||||
app.Get("/users/:id", middleware.AuthMiddleware(redisClient), handlers.GetUserByID(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
|
// Start server
|
||||||
log.Fatal(app.Listen(":7777"))
|
log.Fatal(app.Listen(":7777"))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// POST 根据身份证和token,来获取家长或老师的基本信息
|
// POST 根据身份证和token,来获取家长或老师的基本信息
|
||||||
func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
|
func GetUserInfo(isStudent bool, redisClient *redis.Storage) fiber.Handler {
|
||||||
return func(c *fiber.Ctx) error {
|
return func(c *fiber.Ctx) error {
|
||||||
// 获取POST 请求参数
|
// 获取POST 请求参数
|
||||||
var apiUser models.ReqApi
|
var apiUser models.ReqApi
|
||||||
@ -42,6 +42,9 @@ func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
|
|||||||
}
|
}
|
||||||
// 验证身份证有效性
|
// 验证身份证有效性
|
||||||
url := cfg.APIUserUrl
|
url := cfg.APIUserUrl
|
||||||
|
if isStudent {
|
||||||
|
url = cfg.APIStudentUrl
|
||||||
|
}
|
||||||
reqBody := map[string]string{
|
reqBody := map[string]string{
|
||||||
"token": token,
|
"token": token,
|
||||||
"nationalId": nationalId,
|
"nationalId": nationalId,
|
||||||
@ -50,7 +53,7 @@ func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
|
|||||||
req, err := http.Post(url, "application/json", bytes.NewBuffer(reqBodyJson))
|
req, err := http.Post(url, "application/json", bytes.NewBuffer(reqBodyJson))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"message": "获取学生信息失败,无法解析URL",
|
"message": "获取身份信息失败,无法解析URL",
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -60,58 +63,9 @@ func GetUserInfo(redisClient *redis.Storage) fiber.Handler {
|
|||||||
var result map[string]interface{}
|
var result map[string]interface{}
|
||||||
json.Unmarshal(body, &result)
|
json.Unmarshal(body, &result)
|
||||||
fmt.Println(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{
|
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"message": "获取学生信息失败,身份证无效",
|
"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": "获取学生信息失败,身份证无效",
|
|
||||||
"error": "身份证无效",
|
"error": "身份证无效",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user