123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- package system
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/go-redis/redis/v8"
- "github.com/sirupsen/logrus"
- "lc-fangdaosha/global"
- "lc-fangdaosha/model/common/request"
- "lc-fangdaosha/model/common/response"
- "lc-fangdaosha/model/system"
- systemReq "lc-fangdaosha/model/system/request"
- systemResp "lc-fangdaosha/model/system/response"
- "lc-fangdaosha/utils"
- "strconv"
- "time"
- )
- // Login
- // @Tags Base
- // @Summary 用户登录
- // @Produce application/json
- // @Param data body systemReq.Login true "用户名, 密码, 验证码"
- // @Success 200 {object} response.Response{data=systemRes.LoginResponse,msg=string} "返回包括用户信息,token,过期时间"
- // @Router /base/login [post]
- func (b *BaseApi) Login(c *gin.Context) {
- //获取登录数据
- var l systemReq.Login
- err := c.ShouldBindJSON(&l)
- key := c.ClientIP()
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- //校验数据
- err = utils.Verify(l, utils.LoginVerify)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- // 判断验证码是否开启
- openCaptcha := global.Config.Captcha.OpenCaptcha
- openCaptchaTimeOut := global.Config.Captcha.OpenCaptchaTimeOut // 缓存超时时间
- v, ok := global.BlackCache.Get(key)
- if !ok {
- global.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
- }
- var oc bool = openCaptcha == 0 || openCaptcha < interfaceToInt(v)
- if !oc || store.Verify(l.CaptchaId, l.Captcha, true) {
- u := &system.SysUser{Username: l.Username, Password: l.Password}
- user, err := userService.Login(u)
- fmt.Println("user:", user)
- if err != nil {
- logrus.Error("登陆失败! 用户名不存在或者密码错误!", err)
- // 验证码次数+1
- global.BlackCache.Increment(key, 1)
- response.FailWithMessage("用户名不存在或者密码错误", c)
- return
- }
- if user.Enable != 1 {
- logrus.Error("登陆失败! 用户被禁止登录!")
- // 验证码次数+1
- global.BlackCache.Increment(key, 1)
- response.FailWithMessage("用户被禁止登录", c)
- return
- }
- b.TokenNext(c, *user)
- return
- }
- // 验证码次数+1
- global.BlackCache.Increment(key, 1)
- response.FailWithMessage("验证码错误", c)
- }
- // TokenNext 登录以后签发jwt
- func (b *BaseApi) TokenNext(c *gin.Context, user system.SysUser) {
- j := &utils.JWT{SigningKey: []byte(global.Config.JWT.SigningKey)} // 唯一签名
- claims := j.CreateClaims(systemReq.BaseClaims{
- UUID: user.UUID,
- ID: user.ID,
- NickName: user.NickName,
- Username: user.Username,
- AuthorityId: user.AuthorityId,
- })
- token, err := j.CreateToken(claims)
- if err != nil {
- logrus.Error("获取token失败!", err)
- response.FailWithMessage("获取token失败", c)
- return
- }
- if !global.Config.System.UseMultipoint {
- response.OkWithDetailed(systemResp.LoginResponse{
- User: user,
- Token: token,
- ExpiresAt: claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
- }, "登录成功", c)
- return
- }
- if jwtStr, err := jwtService.GetRedisJWT(user.Username); err == redis.Nil {
- if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
- logrus.Error("设置登录状态失败!", err)
- response.FailWithMessage("设置登录状态失败", c)
- return
- }
- response.OkWithDetailed(systemResp.LoginResponse{
- User: user,
- Token: token,
- ExpiresAt: claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
- }, "登录成功", c)
- } else if err != nil {
- logrus.Error("设置登录状态失败!", err)
- response.FailWithMessage("设置登录状态失败", c)
- } else {
- var blackJWT system.JwtBlacklist
- blackJWT.Jwt = jwtStr
- if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
- response.FailWithMessage("jwt作废失败", c)
- return
- }
- if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
- response.FailWithMessage("设置登录状态失败", c)
- return
- }
- response.OkWithDetailed(systemResp.LoginResponse{
- User: user,
- Token: token,
- ExpiresAt: claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
- }, "登录成功", c)
- }
- }
- // Register
- // @Tags SysUser
- // @Summary 用户注册账号
- // @Produce application/json
- // @Param data body systemReq.Register true "用户名, 昵称, 密码, 角色ID"
- // @Success 200 {object} response.Response{data=systemRes.SysUserResponse,msg=string} "用户注册账号,返回包括用户信息"
- // @Router /user/admin_register [post]
- func (b *BaseApi) Register(c *gin.Context) {
- var r systemReq.Register
- err := c.ShouldBindJSON(&r)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- err = utils.Verify(r, utils.RegisterVerify)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- var authorities []system.SysAuthority
- for _, v := range r.AuthorityIds {
- authorities = append(authorities, system.SysAuthority{
- AuthorityId: v,
- })
- }
- user := &system.SysUser{Username: r.Username, NickName: r.NickName, Password: r.Password, HeaderImg: r.HeaderImg, AuthorityId: r.AuthorityId, Authorities: authorities, Enable: r.Enable, Phone: r.Phone, Email: r.Email}
- userReturn, err := userService.Register(*user)
- if err != nil {
- logrus.Error("注册失败!", err)
- response.FailWithDetailed(systemResp.SysUserResponse{User: userReturn}, "注册失败", c)
- return
- }
- response.OkWithDetailed(systemResp.SysUserResponse{User: userReturn}, "注册成功", c)
- }
- // ChangePassword
- // @Tags SysUser
- // @Summary 用户修改密码
- // @Security ApiKeyAuth
- // @Produce application/json
- // @Param data body systemReq.ChangePasswordReq true "用户名, 原密码, 新密码"
- // @Success 200 {object} response.Response{msg=string} "用户修改密码"
- // @Router /user/changePassword [post]
- func (b *BaseApi) ChangePassword(c *gin.Context) {
- var req systemReq.ChangePasswordReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- err = utils.Verify(req, utils.ChangePasswordVerify)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- uid := utils.GetUserID(c)
- u := &system.SysUser{GVA_MODEL: global.GVA_MODEL{ID: uid}, Password: req.Password}
- _, err = userService.ChangePassword(u, req.NewPassword)
- if err != nil {
- logrus.Error("修改失败!", err)
- response.FailWithMessage("修改失败,原密码与当前账户不符", c)
- return
- }
- response.OkWithMessage("修改成功", c)
- }
- // GetUserList
- // @Tags SysUser
- // @Summary 分页获取用户列表
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body request.PageInfo true "页码, 每页大小"
- // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "分页获取用户列表,返回包括列表,总数,页码,每页数量"
- // @Router /user/getUserList [post]
- func (b *BaseApi) GetUserList(c *gin.Context) {
- var pageInfo request.PageInfo
- err := c.ShouldBindJSON(&pageInfo)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- err = utils.Verify(pageInfo, utils.PageInfoVerify)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- list, total, err := userService.GetUserInfoList(pageInfo)
- if err != nil {
- logrus.Error("获取失败!", err)
- response.FailWithMessage("获取失败", c)
- return
- }
- response.OkWithDetailed(response.PageResult{
- List: list,
- Total: total,
- Page: pageInfo.Page,
- PageSize: pageInfo.PageSize,
- }, "获取成功", c)
- }
- // SetUserAuthority
- // @Tags SysUser
- // @Summary 更改用户权限
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body systemReq.SetUserAuth true "用户UUID, 角色ID"
- // @Success 200 {object} response.Response{msg=string} "设置用户权限"
- // @Router /user/setUserAuthority [post]
- func (b *BaseApi) SetUserAuthority(c *gin.Context) {
- var sua systemReq.SetUserAuth
- err := c.ShouldBindJSON(&sua)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- if UserVerifyErr := utils.Verify(sua, utils.SetUserAuthorityVerify); UserVerifyErr != nil {
- response.FailWithMessage(UserVerifyErr.Error(), c)
- return
- }
- userID := utils.GetUserID(c)
- err = userService.SetUserAuthority(userID, sua.AuthorityId)
- if err != nil {
- logrus.Error("修改失败!", err)
- response.FailWithMessage(err.Error(), c)
- return
- }
- claims := utils.GetUserInfo(c)
- j := &utils.JWT{SigningKey: []byte(global.Config.JWT.SigningKey)} // 唯一签名
- claims.AuthorityId = sua.AuthorityId
- if token, err := j.CreateToken(*claims); err != nil {
- logrus.Error("修改失败!", err)
- response.FailWithMessage(err.Error(), c)
- } else {
- c.Header("new-token", token)
- c.Header("new-expires-at", strconv.FormatInt(claims.ExpiresAt.Unix(), 10))
- response.OkWithMessage("修改成功", c)
- }
- }
- // SetUserAuthorities
- // @Tags SysUser
- // @Summary 设置用户权限
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body systemReq.SetUserAuthorities true "用户UUID, 角色ID"
- // @Success 200 {object} response.Response{msg=string} "设置用户权限"
- // @Router /user/setUserAuthorities [post]
- func (b *BaseApi) SetUserAuthorities(c *gin.Context) {
- var sua systemReq.SetUserAuthorities
- err := c.ShouldBindJSON(&sua)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- err = userService.SetUserAuthorities(sua.ID, sua.AuthorityIds)
- if err != nil {
- logrus.Error("修改失败!", err)
- response.FailWithMessage("修改失败", c)
- return
- }
- response.OkWithMessage("修改成功", c)
- }
- // DeleteUser
- // @Tags SysUser
- // @Summary 删除用户
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body request.GetById true "用户ID"
- // @Success 200 {object} response.Response{msg=string} "删除用户"
- // @Router /user/deleteUser [delete]
- func (b *BaseApi) DeleteUser(c *gin.Context) {
- var reqId request.GetById
- err := c.ShouldBindJSON(&reqId)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- err = utils.Verify(reqId, utils.IdVerify)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- jwtId := utils.GetUserID(c)
- if jwtId == uint(reqId.ID) {
- response.FailWithMessage("删除失败, 自杀失败", c)
- return
- }
- err = userService.DeleteUser(reqId.ID)
- if err != nil {
- logrus.Error("删除失败!", err)
- response.FailWithMessage("删除失败", c)
- return
- }
- response.OkWithMessage("删除成功", c)
- }
- // SetUserInfo
- // @Tags SysUser
- // @Summary 设置用户信息
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.SysUser true "ID, 用户名, 昵称, 头像链接"
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "设置用户信息"
- // @Router /user/setUserInfo [put]
- func (b *BaseApi) SetUserInfo(c *gin.Context) {
- var user systemReq.ChangeUserInfo
- err := c.ShouldBindJSON(&user)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- err = utils.Verify(user, utils.IdVerify)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- if len(user.AuthorityIds) != 0 {
- err = userService.SetUserAuthorities(user.ID, user.AuthorityIds)
- if err != nil {
- logrus.Error("设置失败!", err)
- response.FailWithMessage("设置失败", c)
- return
- }
- }
- err = userService.SetUserInfo(system.SysUser{
- GVA_MODEL: global.GVA_MODEL{
- ID: user.ID,
- },
- NickName: user.NickName,
- HeaderImg: user.HeaderImg,
- Phone: user.Phone,
- Email: user.Email,
- SideMode: user.SideMode,
- Enable: user.Enable,
- })
- if err != nil {
- logrus.Error("设置失败!", err)
- response.FailWithMessage("设置失败", c)
- return
- }
- response.OkWithMessage("设置成功", c)
- }
- // SetSelfInfo
- // @Tags SysUser
- // @Summary 设置用户信息
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.SysUser true "ID, 用户名, 昵称, 头像链接"
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "设置用户信息"
- // @Router /user/SetSelfInfo [put]
- func (b *BaseApi) SetSelfInfo(c *gin.Context) {
- var user systemReq.ChangeUserInfo
- err := c.ShouldBindJSON(&user)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- user.ID = utils.GetUserID(c)
- err = userService.SetSelfInfo(system.SysUser{
- GVA_MODEL: global.GVA_MODEL{
- ID: user.ID,
- },
- NickName: user.NickName,
- HeaderImg: user.HeaderImg,
- Phone: user.Phone,
- Email: user.Email,
- SideMode: user.SideMode,
- Enable: user.Enable,
- })
- if err != nil {
- logrus.Error("设置失败!", err)
- response.FailWithMessage("设置失败", c)
- return
- }
- response.OkWithMessage("设置成功", c)
- }
- // GetUserInfo
- // @Tags SysUser
- // @Summary 获取用户信息
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取用户信息"
- // @Router /user/getUserInfo [get]
- func (b *BaseApi) GetUserInfo(c *gin.Context) {
- uuid := utils.GetUserUuid(c)
- ReqUser, err := userService.GetUserInfo(uuid)
- if err != nil {
- logrus.Error("获取失败!", err)
- response.FailWithMessage("获取失败", c)
- return
- }
- response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "获取成功", c)
- }
- // ResetPassword
- // @Tags SysUser
- // @Summary 重置用户密码
- // @Security ApiKeyAuth
- // @Produce application/json
- // @Param data body system.SysUser true "ID"
- // @Success 200 {object} response.Response{msg=string} "重置用户密码"
- // @Router /user/resetPassword [post]
- func (b *BaseApi) ResetPassword(c *gin.Context) {
- var user system.SysUser
- err := c.ShouldBindJSON(&user)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- err = userService.ResetPassword(user.ID)
- if err != nil {
- logrus.Error("重置失败!", err)
- response.FailWithMessage("重置失败"+err.Error(), c)
- return
- }
- response.OkWithMessage("重置成功", c)
- }
- // UserIdList 获取用户id,username列表
- func (b *BaseApi) UserIdList(c *gin.Context) {
- id := utils.GetUserID(c)
- ids, err := userService.UserId(id)
- if err != nil {
- logrus.Error("获取失败!", err)
- response.FailWithMessage("获取失败", c)
- return
- }
- response.OkWithDetailed(ids, "获取成功", c)
- }
- func (b *BaseApi) AddEmails(c *gin.Context) {
- var emails []systemReq.Email
- err := c.ShouldBindJSON(&emails)
- if err != nil {
- logrus.Error("数据绑定失败", err)
- response.FailWithMessage("参数绑定失败"+err.Error(), c)
- return
- }
- for i, v := range emails {
- err = utils.Verify(v, utils.EmailVerify)
- if err != nil {
- response.FailWithDetailed(err.Error(), fmt.Sprintf("第%d邮箱格式错误或长度超过50", i+1), c)
- return
- }
- }
- id := utils.GetUserID(c)
- result := userService.SetAlarmEmail(id, emails)
- if result != "" {
- response.FailWithMessage("邮箱已绑定"+result, c)
- return
- }
- response.Ok(c)
- }
- func (b *BaseApi) ConfirmEmail(c *gin.Context) {
- id := c.Query("id")
- code := c.Query("code")
- result := userService.ConfirmEmail(id, code)
- c.Header("Content-type", "text/html; charset=utf-8")
- c.Writer.WriteString(result)
- }
- // UnbindEmail 管理员解绑报警邮箱
- func (b *BaseApi) UnbindEmail(c *gin.Context) {
- uid := utils.GetUserID(c)
- var emails []systemReq.Email
- err := c.ShouldBind(&emails)
- if err != nil {
- response.FailWithMessage("参数绑定失败", c)
- return
- }
- result := userService.UnbindEmail(uid, emails)
- if result != "" {
- response.FailWithMessage(result, c)
- return
- }
- response.Ok(c)
- }
- // UserUnbindEmail 用户解绑报警邮箱
- func (b *BaseApi) UserUnbindEmail(c *gin.Context) {
- id := c.Query("id")
- email := c.Query("email")
- atoi, err := strconv.Atoi(id)
- if err != nil {
- logrus.Error(err)
- return
- }
- var emails []systemReq.Email
- result := userService.UnbindEmail(uint(atoi), append(emails, systemReq.Email{Email: email}))
- if result != "" {
- response.FailWithMessage(result, c)
- return
- }
- response.Ok(c)
- }
|