50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"electricity_bill_calc/exceptions"
|
|
"electricity_bill_calc/model"
|
|
"electricity_bill_calc/response"
|
|
"electricity_bill_calc/service"
|
|
"net/http"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func InitializeUserHandlers(router *fiber.App) {
|
|
router.Post("/login", doLogin)
|
|
}
|
|
|
|
type _LoginForm struct {
|
|
Username string `json:"uname"`
|
|
Password string `json:"upass"`
|
|
Type int16 `json:"type"`
|
|
}
|
|
|
|
func doLogin(c *fiber.Ctx) error {
|
|
result := response.NewResult(c)
|
|
loginData := new(_LoginForm)
|
|
if err := c.BodyParser(loginData); err != nil {
|
|
return result.Error(http.StatusInternalServerError, "表单解析失败。")
|
|
}
|
|
var (
|
|
session *model.Session
|
|
err error
|
|
)
|
|
if loginData.Type == model.USER_TYPE_ENT {
|
|
session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password)
|
|
} else {
|
|
session, err = service.UserService.ProcessManagementUserLogin(loginData.Username, loginData.Password)
|
|
}
|
|
if err != nil {
|
|
if authError, ok := err.(*exceptions.AuthenticationError); ok {
|
|
if authError.NeedReset {
|
|
return result.LoginNeedReset()
|
|
}
|
|
return result.Error(int(authError.Code), authError.Message)
|
|
} else {
|
|
return result.Error(http.StatusInternalServerError, err.Error())
|
|
}
|
|
}
|
|
return result.LoginSuccess(session)
|
|
}
|