forked from free-lancers/electricity_bill_calc_service
feat(login):基本完成用户登录,待测。
This commit is contained in:
52
controller/user.go
Normal file
52
controller/user.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/service"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type _UserController struct{}
|
||||
|
||||
var UserController _UserController
|
||||
|
||||
type LoginFormData struct {
|
||||
Username string `form:"uname"`
|
||||
Password string `form:"upass"`
|
||||
Type int8 `form:"type"`
|
||||
}
|
||||
|
||||
func (_UserController) Login(c *gin.Context) {
|
||||
result := response.NewResult(c)
|
||||
loginData := new(LoginFormData)
|
||||
c.BindJSON(loginData)
|
||||
var (
|
||||
session *model.Session
|
||||
err error
|
||||
)
|
||||
if loginData.Type == 0 {
|
||||
session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password)
|
||||
} else {
|
||||
session, err = service.UserService.ProcessManagementUserLogin(loginData.Username, loginData.Password)
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, &exceptions.AuthenticationError{}) {
|
||||
authError := err.(exceptions.AuthenticationError)
|
||||
if authError.NeedReset {
|
||||
result.LoginNeedReset()
|
||||
return
|
||||
}
|
||||
result.Error(int(authError.Code), authError.Message)
|
||||
return
|
||||
} else {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
result.LoginSuccess(session, false)
|
||||
}
|
Reference in New Issue
Block a user