132 lines
3.6 KiB
Go
132 lines
3.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"electricity_bill_calc/cache"
|
|
"electricity_bill_calc/exceptions"
|
|
"electricity_bill_calc/model"
|
|
"electricity_bill_calc/response"
|
|
"electricity_bill_calc/security"
|
|
"electricity_bill_calc/service"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type _UserController struct {
|
|
Router *gin.Engine
|
|
}
|
|
|
|
var UserController *_UserController
|
|
|
|
func InitializeUserController(router *gin.Engine) {
|
|
UserController = &_UserController{
|
|
Router: router,
|
|
}
|
|
UserController.Router.POST("/login", UserController.Login)
|
|
UserController.Router.DELETE("/logout", security.MustAuthenticated, UserController.Logout)
|
|
UserController.Router.DELETE("/password/:uid", security.OPSAuthorize, UserController.InvalidUserPassword)
|
|
UserController.Router.PUT("/password", UserController.ResetUserPassword)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (_UserController) Logout(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
session, exists := c.Get("session")
|
|
if !exists {
|
|
result.Success("用户会话已结束。")
|
|
return
|
|
}
|
|
_, err := cache.ClearSession(session.(*model.Session).Token)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
result.Success("用户已成功登出系统。")
|
|
}
|
|
|
|
func (_UserController) InvalidUserPassword(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
targetUserId := c.Param("uid")
|
|
verifyCode, err := service.UserService.InvalidUserPassword(targetUserId)
|
|
if errors.Is(err, &exceptions.NotFoundError{}) {
|
|
result.NotFound("未找到指定用户。")
|
|
return
|
|
}
|
|
if errors.Is(err, &exceptions.UnsuccessfulOperationError{}) {
|
|
result.Error(500, "未能成功更新用户的密码。")
|
|
return
|
|
}
|
|
if err != nil {
|
|
result.Error(500, err.Error())
|
|
return
|
|
}
|
|
result.QuickJson(http.StatusOK, http.StatusAccepted, "用户密码已经失效", gin.H{"verify": verifyCode})
|
|
}
|
|
|
|
type ResetPasswordFormData struct {
|
|
VerifyCode string `json:"verifyCode"`
|
|
Username string `json:"uname"`
|
|
NewPassword string `json:"newPass"`
|
|
}
|
|
|
|
func (_UserController) ResetUserPassword(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
resetForm := new(ResetPasswordFormData)
|
|
c.BindJSON(resetForm)
|
|
verified, err := service.UserService.VerifyUserPassword(resetForm.Username, resetForm.VerifyCode)
|
|
if !verified {
|
|
result.Error(http.StatusUnauthorized, "验证码不正确。")
|
|
return
|
|
}
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
completed, err := service.UserService.ResetUserPassword(resetForm.Username, resetForm.NewPassword)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
if completed {
|
|
result.Success("用户凭据已更新。")
|
|
return
|
|
}
|
|
result.Error(http.StatusNotAcceptable, "用户凭据未能成功更新。")
|
|
}
|