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