123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package api
- import (
- "AIT/common"
- "AIT/constant"
- "AIT/exception/errmsg"
- "AIT/models"
- "AIT/response"
- "AIT/service"
- "fmt"
- "github.com/gin-gonic/gin"
- )
- type UserService struct {
- service.UserService
- }
- func GetUser() *UserService {
- return &UserService{}
- }
- // UseRegister
- // PingExample kjb-doc
- // @Summary 用户注册
- // @Schemes
- // @Description 用户注册
- // @Tags 用户管理
- // @Param user body string true "userName:用户名;password:密码;role:角色"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/user/register [post]
- func (u *UserService) UseRegister(c *gin.Context) {
- var param models.UserRegister
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errmsg.ParamInvalid, c)
- return
- }
- if user, count := u.FindUserByUsername(param.UserName); user.Id != "" || count > 0 {
- response.Failed(errmsg.UserAlreadyExists, c)
- return
- }
- if result := u.CreateUser(param); result.Error == nil {
- response.Success(errmsg.UserRegisterSuccess, result.RowsAffected, c)
- return
- } else {
- response.Failed(errmsg.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
- return
- }
- }
- // UserLogin
- // PingExample kjb-doc
- // @Summary 用户登录
- // @Schemes
- // @Description 用户登录
- // @Tags 用户管理
- // @Param user body string true "username:编号;password:密码;role:角色;"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/user/login [post]
- func (u *UserService) UserLogin(c *gin.Context) {
- var param models.UserLogin
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errmsg.ParamInvalid, c)
- return
- }
- if user := u.Login(param); user != nil {
- //生成token
- toke, _ := common.GenerateToke(*user)
- //resultMap := make(map[string]string)
- //resultMap["token"] = toke
- resultMap := map[string]string{
- "token": toke,
- }
- response.Success(errmsg.UserLoginSuccess, resultMap, c)
- }
- }
- // ResetPassword
- // PingExample kjb-doc
- // @Summary 管理员重置普通用户密码
- // @Schemes
- // @Description 管理员重置普通用户密码
- // @Tags 用户管理
- // @Param user body string true "username:管理员编号;usernameInit:被重置用户编号"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/user/reset [post]
- func (u *UserService) ResetPassword(c *gin.Context) {
- var param models.ResetPassword
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errmsg.ParamInvalid, c)
- return
- }
- if admin := u.FindUserByUsernameAndRole(param.Username, constant.RoleAdmin); admin == nil {
- fmt.Printf("管理员"+errmsg.UserNotExists+"%s", param.Username)
- response.Failed("管理员"+errmsg.UserNotExists+param.Username, c)
- return
- }
- if normalUser := u.FindUserByUsernameAndRole(param.UsernameInit, constant.RoleNormal); normalUser == nil {
- fmt.Printf(errmsg.UserNotExists+"%s", param.UsernameInit)
- response.Failed(errmsg.UserNotExists+param.UsernameInit, c)
- return
- }
- result := u.RestPassword(param.UsernameInit)
- if result == nil {
- response.Failed(errmsg.UserPasswordResetError, c)
- return
- }
- if result.Error != nil {
- response.Failed(errmsg.UserPasswordResetError+result.Error.Error(), c)
- return
- }
- if result.RowsAffected >= 0 {
- response.Success(errmsg.UserPasswordResetSuccess, result.RowsAffected, c)
- return
- }
- }
- // UserList
- // PingExample kjb-doc
- // @Summary 用户列表查询
- // @Schemes
- // @Description 用户列表查询
- // @Tags 用户管理
- // @Param user body string true "role:角色;userName:用户名;pageNum:页数;pageSize:每页记录数"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/user/find [post]
- func (u *UserService) UserList(c *gin.Context) {
- var param models.UserListParam
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf(errmsg.ParamInvalid+"%s", err.Error())
- response.Failed(errmsg.ParamInvalid, c)
- return
- }
- userList, rows := u.GetUserList(param)
- response.SuccessPage(errmsg.FindSuccess, userList, rows, c)
- }
- // ModePass
- // PingExample kjb-doc
- // @Summary 用户修改密码
- // @Schemes
- // @Description 用户修改密码
- // @Tags 用户管理
- // @Param user body string true "userName:用户名 password:密码 newPassword:新密码"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/user/change/password [post]
- func (u *UserService) ModePass(c *gin.Context) {
- var param models.ChangePassword
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- response.Failed(errmsg.ParamInvalid, c)
- return
- }
- user, count := u.FindUserByUsername(param.UserName)
- if count <= 0 {
- response.Failed(errmsg.UserNotExists, c)
- return
- }
- if user.Password != param.Password {
- response.Failed(errmsg.UserOldPasswordError, c)
- return
- }
- count = u.ChangePassword(user.Id, param.NewPassword)
- if count <= 0 {
- response.Failed(errmsg.UserPasswordChangeError, c)
- return
- }
- response.Success(errmsg.UserPasswordChangeSuccess, count, c)
- }
|