forked from free-lancers/electricity_bill_calc_service
enhance(user):打通用户登录功能,调整程序基本结构。
This commit is contained in:
49
controller/user.go
Normal file
49
controller/user.go
Normal file
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user