| | |
| | | import ( |
| | | "basic.com/dbapi.git" |
| | | "github.com/gin-gonic/gin" |
| | | "net/http" |
| | | "time" |
| | | "webserver/extend/code" |
| | | "webserver/extend/util" |
| | |
| | | // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" |
| | | // @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}" |
| | | // @Router /data/api-v/sys/login [post] |
| | | func (controller UserController) Login(c *gin.Context) { |
| | | func (uc UserController) Login(c *gin.Context) { |
| | | userName := c.PostForm("username") |
| | | password := c.PostForm("password") |
| | | if userName == "" || password == "" { |
| | |
| | | authDriver := auth.GenerateAuthDriver() |
| | | loginedM := util.Struct2Map(data) |
| | | tokenStr := (*authDriver).Login(c.Request, c.Writer, loginedM) |
| | | c.JSON(200,map[string]interface{}{ |
| | | util.ResponseFormat(c,code.Success,map[string]interface{}{ |
| | | "userInfo":loginedM, |
| | | "access_token":tokenStr, |
| | | "refresh_token":tokenStr, |
| | |
| | | "expires_in":time.Now().Add(time.Hour * 8).Unix(), |
| | | }) |
| | | } else { |
| | | c.JSON(500,"用户名或密码错误") |
| | | util.ResponseFormat(c,code.LoginInfoError,"用户名或密码错误") |
| | | } |
| | | } |
| | | |
| | |
| | | // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" |
| | | // @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}" |
| | | // @Router /data/api-v/users/current [get] |
| | | func (controller UserController) Current(c *gin.Context) { |
| | | func (uc UserController) Current(c *gin.Context) { |
| | | authDriver := auth.GenerateAuthDriver() |
| | | user := (*authDriver).User(c) |
| | | if user !=nil { |
| | | c.JSON(http.StatusOK,user) |
| | | util.ResponseFormat(c,code.Success,user) |
| | | } else { |
| | | c.JSON(http.StatusUnauthorized,"") |
| | | util.ResponseFormat(c,code.TokenNotFound,"") |
| | | } |
| | | } |
| | | // @Router /data/api-u/sys/refresh_token [post] |
| | | func (controller UserController) RefreshToken(c *gin.Context){ |
| | | func (uc UserController) RefreshToken(c *gin.Context){ |
| | | |
| | | } |
| | | |
| | | // @Router /data/api-u/sys/logout [get] |
| | | func (controller UserController) Logout(c *gin.Context){ |
| | | c.JSON(http.StatusOK,"退出成功") |
| | | func (uc UserController) Logout(c *gin.Context){ |
| | | util.ResponseFormat(c,code.LogoutSuccess,"退出成功") |
| | | } |
| | | |
| | | // @Router /data/api-u/users/findAllUser [get] |
| | | func (uc UserController) FindAllUser(c *gin.Context) { |
| | | var api dbapi.UserApi |
| | | b,d := api.FindAllUser() |
| | | if b { |
| | | util.ResponseFormat(c,code.Success,d) |
| | | } else { |
| | | util.ResponseFormat(c,code.ComError,"") |
| | | } |
| | | } |
| | | |
| | | type UserEditVo struct { |
| | | Id string `json:"id"` |
| | | UserName string `json:"username"` |
| | | OldPwd string `json:"oldPwd"` |
| | | NewPwd string `json:"newPwd"` |
| | | RoleIds []string `json:"roleIds"` |
| | | } |
| | | |
| | | // @Summary 更新用户名,密码和角色权限 |
| | | // @Description 更新用户名,密码和角色权限 |
| | | // @Accept json |
| | | // @Produce json |
| | | // @Tags 用户 |
| | | // @Param userVo body 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/users/edit [post] |
| | | func (uc UserController) Edit(c *gin.Context) { |
| | | var userEditVo UserEditVo |
| | | err := c.BindJSON(&userEditVo) |
| | | if err !=nil || userEditVo.Id =="" || userEditVo.OldPwd == "" || userEditVo.NewPwd =="" || userEditVo.UserName == "" { |
| | | util.ResponseFormat(c,code.RequestParamError,"") |
| | | return |
| | | } |
| | | paramBody := util.Struct2Map(userEditVo) |
| | | var api dbapi.UserApi |
| | | b,d := api.Edit(paramBody) |
| | | if b { |
| | | util.ResponseFormat(c,code.UpdateSuccess,d) |
| | | } else { |
| | | util.ResponseFormat(c,code.UpdateFail,"更新失败") |
| | | } |
| | | } |