175 lines
4.8 KiB
Go
175 lines
4.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"electricity_bill_calc/cache"
|
|
"electricity_bill_calc/exceptions"
|
|
"electricity_bill_calc/model"
|
|
"electricity_bill_calc/repository"
|
|
"electricity_bill_calc/response"
|
|
"electricity_bill_calc/security"
|
|
"electricity_bill_calc/service"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"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("/login", security.MustAuthenticated, UserController.Logout)
|
|
UserController.Router.DELETE("/password/:uid", security.OPSAuthorize, UserController.InvalidUserPassword)
|
|
UserController.Router.PUT("/password", UserController.ResetUserPassword)
|
|
UserController.Router.GET("/accounts", security.OPSAuthorize, UserController.ListPagedUser)
|
|
}
|
|
|
|
type LoginFormData struct {
|
|
Username string `json:"uname"`
|
|
Password string `json:"upass"`
|
|
Type int8 `json:"type"`
|
|
}
|
|
|
|
func (_UserController) Login(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
loginData := new(LoginFormData)
|
|
err := c.BindJSON(loginData)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, "表单解析失败。")
|
|
return
|
|
}
|
|
var (
|
|
session *model.Session
|
|
)
|
|
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 authError, ok := err.(*exceptions.AuthenticationError); ok {
|
|
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.Json(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 errors.Is(err, &exceptions.NotFoundError{}) {
|
|
result.NotFound("指定的用户不存在。")
|
|
return
|
|
}
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
if !verified {
|
|
result.Error(http.StatusUnauthorized, "验证码不正确。")
|
|
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.NotAccept("用户凭据未能成功更新。")
|
|
}
|
|
|
|
func (_UserController) ListPagedUser(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
if err != nil {
|
|
result.NotAccept("查询参数[page]格式不正确。")
|
|
return
|
|
}
|
|
requestKeyword := c.DefaultQuery("keyword", "")
|
|
requestUserType, err := strconv.Atoi(c.DefaultQuery("type", "-1"))
|
|
if err != nil {
|
|
result.NotAccept("查询参数[type]格式不正确。")
|
|
return
|
|
}
|
|
var requestUserStat *bool
|
|
state, err := strconv.ParseBool(c.Query("state"))
|
|
if err != nil {
|
|
requestUserStat = nil
|
|
} else {
|
|
requestUserStat = &state
|
|
}
|
|
users, total, err := repository.UserRepo.ListUserDetail(requestKeyword, requestUserType, requestUserStat, requestPage)
|
|
if err != nil {
|
|
result.Error(http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
result.Json(
|
|
http.StatusOK,
|
|
http.StatusOK,
|
|
"已取得符合条件的用户集合。",
|
|
response.NewPagedResponse(requestPage, total).ToMap(),
|
|
gin.H{"accounts": users},
|
|
)
|
|
}
|