liuxiaolong
2020-06-05 7c811247ecf143e08c576986a884bedadc57dd66
controllers/user.go
@@ -2,7 +2,6 @@
import (
   "basic.com/dbapi.git"
   "basic.com/valib/logger.git"
   "github.com/gin-gonic/gin"
   "net/http"
   "time"
@@ -22,7 +21,7 @@
// @Summary 用户登录
// @Description 用户登录
// @Accept json
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Param username formData string true "用户名"
@@ -46,20 +45,57 @@
      tokenM["id"] = loginedM["id"]
      tokenM["username"] = loginedM["username"]
      tokenM["permissions"] = loginedM["permissions"]
      tokenStr := (*authDriver).Login(c.Request, c.Writer, tokenM)
      c.JSON(200,map[string]interface{}{
         "userInfo":loginedM,
         "access_token":tokenStr,
         "refresh_token":tokenStr,
         "scope":"app",
         "token_type":"Bearer",
         "expires_in":time.Now().Add(time.Hour * 8).Unix(),
      })
      b,tokenStr,refreshTokenStr := (*authDriver).Login(c.Request, c.Writer, tokenM)
      if b {
         userId := loginedM["id"].(string)
         auth.RemoveOutUser(userId)
         c.JSON(200,map[string]interface{}{
            "userInfo":loginedM,
            "access_token": tokenStr,
            "refresh_token": refreshTokenStr,
            "scope":"app",
            "token_type":"Bearer",
            "expires_in":time.Now().Add(time.Hour * 8).Unix(),
         })
      } else {
         c.JSON(500,"用户名或密码错误")
      }
   } else {
      c.JSON(500,"用户名或密码错误")
   }
}
// @Security ApiKeyAuth
// @Summary 修改当前登录用户的密码
// @Description 修改当前登录用户的密码
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Param oldPwd formData string true "旧密码"
// @Param newPwd formData string true "新密码"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/users/updatePwd [post]
func (uc UserController) UpdatePwd(c *gin.Context) {
   oldPwd := c.PostForm("oldPwd")
   newPwd := c.PostForm("newPwd")
   if oldPwd == "" || newPwd == "" {
      util.ResponseFormat(c,code.RequestParamError,"参数有误")
      return
   }
   authDriver := auth.GenerateAuthDriver()
   userM := (*authDriver).User(c)
   userId := userM["id"].(string)
   var userApi dbapi.UserApi
   if b,_ :=userApi.UpdatePwd(userId, oldPwd, newPwd);b {
      util.ResponseFormat(c,code.UpdateSuccess,"更新成功")
   } else {
      util.ResponseFormat(c,code.AccountPassUnmatch,"密码有误")
   }
}
// @Security ApiKeyAuth
// @Summary 获取当前用户信息
// @Description 获取当前用户信息
// @Accept json
@@ -77,9 +113,31 @@
      c.JSON(http.StatusUnauthorized,"")
   }
}
// @Summary token过期后刷新token
// @Description token过期后刷新token
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Param refresh_token formData string true "上次获取token时返回的refresh_token值"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/sys/refresh_token [post]
func (uc UserController) RefreshToken(c *gin.Context){
   refreshToken := c.PostForm("refresh_token")
   if refreshToken == "" {
      util.ResponseFormat(c,code.TokenNotFound,"未找到refresh_token")
      return
   }
   authDriver := auth.GenerateAuthDriver()
   if b,newToken,newRefreshToken := (*authDriver).RefreshToken(refreshToken);b {
      util.ResponseFormat(c,code.Success,map[string]string{
         "token": newToken,
         "refresh_token": newRefreshToken,
      })
   } else {
      util.ResponseFormat(c,code.NotLogin,"请重新登录")
   }
}
// @Router /data/api-u/sys/logout [get]
@@ -87,6 +145,7 @@
   c.JSON(http.StatusOK,"退出成功")
}
// @Security ApiKeyAuth
// @Summary 查找所有用户
// @Description 查找所有用户
// @Accept json
@@ -97,10 +156,8 @@
// @Router /data/api-u/users/findAllUser [get]
func (uc UserController) FindAllUser(c *gin.Context) {
   authDriver := auth.GenerateAuthDriver()
   user := (*authDriver).User(c)
   logger.Debug("cur userInfo:",user)
   loginM := user.(map[string]interface{})
   userId := loginM["id"].(string)
   userM := (*authDriver).User(c)
   userId := userM["id"].(string)
   var api dbapi.UserApi
   b,d := api.FindAllUser(userId)
   if b {
@@ -117,9 +174,10 @@
   MenuIds []string `json:"menuIds"`
}
// @Security ApiKeyAuth
// @Summary 编辑此用户,返回此用户的权限菜单
// @Description 编辑此用户,返回此用户的权限菜单
// @Accept json
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Param userId formData string true "用户id"
@@ -141,6 +199,7 @@
   }
}
// @Security ApiKeyAuth
// @Summary 更新用户名,密码和菜单权限
// @Description 更新用户名,密码和菜单权限
// @Accept json
@@ -149,7 +208,7 @@
// @Param userVo body controllers.UserEditVo true "用户及权限信息"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/user/saveAuth [post]
// @Router /data/api-v/users/saveAuth [post]
func (uc UserController) SaveAuth(c *gin.Context) {
   var userEditVo UserEditVo
   err := c.BindJSON(&userEditVo)