package controllers import ( "basic.com/valib/bhomeclient.git" "basic.com/valib/logger.git" "strconv" "vamicro/system-service/models" "vamicro/system-service/service" "vamicro/system-service/vo" ) type UserController struct{} // @Summary 添加用户 // @Description 添加用户 // @Accept json // @Produce json // @Tags 用户 // @Param user body vo.UserVo 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/add [post] func (uc UserController) Add(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var userVo vo.UserVo if err := c.BindJSON(&userVo);err !=nil { return &bhomeclient.Reply{ Msg: "参数有误"} } var sv service.UserService flag,data := sv.AddUser(userVo) if flag { return &bhomeclient.Reply{ Success: true, Data: data } } else { return &bhomeclient.Reply{ Msg: data} } } // @Summary 用户登录 // @Description 用户登录 // @Accept json // @Produce json // @Tags 用户 // @Param user body vo.UserVo 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/sys/login [post] func (uc UserController) Login(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ userName := c.PostForm("username") password := c.PostForm("password") logger.Debug("username:", userName, "password:", password) logger.Debug("新的newcode") if userName == "" || password == "" { return &bhomeclient.Reply{ Msg: "参数有误"} } var sv service.UserService flag, info := sv.Login(vo.UserVo{ UserName: userName, Password: password, }) if flag { return &bhomeclient.Reply{ Success: true, Data: info } } else if "未授权用户" == info.RoleName { return &bhomeclient.Reply{ Msg: "授权已过期,请购买新的授权"} } else { return &bhomeclient.Reply{ Msg: "用户名或密码错误"} } } // @Summary 刷新用户token // @Description 刷新用户token // @Accept json // @Produce json // @Tags 用户 // @Param user body vo.UserVo 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/sys/refresh_token [get] func (uc UserController) RefreshToken(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ userId := c.Header("Login_user_id") logger.Debug("current userId:", userId) return &bhomeclient.Reply{ Success: true, Data: ""} } // @Summary 用户退出登录 // @Description 用户退出登录 // @Accept json // @Produce json // @Tags 用户 // @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/logout [post] func (uc UserController) Logout(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ return &bhomeclient.Reply{ Success: true, Msg: "退出成功"} } // @Summary 查找所有用户 // @Description 查找所有用户 // @Accept json // @Produce json // @Tags 用户 // @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/findAllUser [get] func (uc UserController) FindAllUser(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ userId := c.Header("Login_user_id") if userId == "" { return &bhomeclient.Reply{ Msg: "参数有误"} } var sv service.UserService d,err := sv.FindAll(userId) if err ==nil { return &bhomeclient.Reply{ Success: true, Data: d} } else { return &bhomeclient.Reply{ Msg: err.Error()} } } type DelUsers struct { Ids []string `json:"ids"` } // @Security ApiKeyAuth // @Summary 删除用户,支持批量删除 // @Description 删除用户,支持批量删除 // @Accept json // @Produce json // @Tags 用户 // @Param reqBody body controllers.DelUsers true "删除用户id数组参数" // @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/delUser [post] func (uc UserController) DelUser(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var reqBody DelUsers err := c.BindJSON(&reqBody) if err != nil || len(reqBody.Ids) == 0 { return &bhomeclient.Reply{ Msg: "请求参数有误"} } count := 0 for _,uId := range reqBody.Ids { var u models.SysUser if rows, e := u.SelectById(uId);e == nil && rows >0 && u.Username !="basic" { if b,_ := u.DeleteById(uId);b { count++ } } } if count >0 { return &bhomeclient.Reply{ Success: true, Data: "成功删除:"+strconv.Itoa(count)+"个"} } else { return &bhomeclient.Reply{ Msg: "删除失败"} } } // @Summary 编辑此用户,返回此用户的权限菜单 // @Description 编辑此用户,返回此用户的权限菜单 // @Accept json // @Produce json // @Tags 用户 // @Param userId formData string true "用户id" // @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/findById [post] func (uc UserController) FindById(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ loginUserId := c.Header("Login_user_id") userId := c.PostForm("userId") logger.Debug("userid拿到:::", userId) if userId == "" { return &bhomeclient.Reply{ Msg: "参数有误"} } var sv service.UserService userAuth, err := sv.FindByUserId(userId) if err != nil { logger.Debug("userid查询失败:::", userId) return &bhomeclient.Reply{ Msg: "查询失败"} } var sv2 service.SysMenuService var menuIds = make(map [string]string, 0) for _,item := range userAuth.Menus { menuIds[item.Id] = item.Id } d, err := sv2.GetMenuTree(loginUserId, menuIds) if err ==nil { return &bhomeclient.Reply{ Success: true, Data: d } } else { return &bhomeclient.Reply{ Msg: "查询失败"} } } // @Summary 更新用户名,密码和菜单权限 // @Description 更新用户名,密码和菜单权限 // @Accept json // @Produce json // @Tags 用户 // @Param userVo body vo.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] func (uc UserController) SaveAuth(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var userEditVo vo.UserEditVo userId := c.Header("Login_user_id") err := c.BindJSON(&userEditVo) if err !=nil || userEditVo.Id =="" { return &bhomeclient.Reply{ Msg: "参数有误"} } var sv service.UserService err = sv.SaveAuth(&userEditVo, userId) if nil == err { return &bhomeclient.Reply{ Success: true, Msg: "更新成功"} } else { return &bhomeclient.Reply{ Msg: "更新失败," + err.Error()} } } // @Security ApiKeyAuth // @Summary 修改用户密码 // @Description 修改用户密码 // @Accept x-www-form-urlencoded // @Produce json // @Tags 用户 // @Param userId formData string true "用户id" // @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/user/updatePwd [post] func (uc UserController) UpdatePwd(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ userId := c.Header("Login_user_id") oldPwd := c.PostForm("oldPwd") newPwd := c.PostForm("newPwd") tarGetId := c.PostForm("userId") if tarGetId != "" { if newPwd == "" { return &bhomeclient.Reply{ Msg: "参数有误"} } } else { if userId == "" || oldPwd == "" || newPwd == "" { return &bhomeclient.Reply{ Msg: "参数有误"} } tarGetId = userId } var sv service.UserService succ,err := sv.UpdatePwd(tarGetId, oldPwd, newPwd, userId) if succ { return &bhomeclient.Reply{ Success: true, Msg: "更新成功"} } else { return &bhomeclient.Reply{ Msg: err.Error()} } } // @Security ApiKeyAuth // @Summary 上传用户头像 // @Description 上传用户头像 // @Accept x-www-form-urlencoded // @Produce json // @Tags 用户 // @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/uploadHeadPic [post] func (uc UserController) UploadHeadPic(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply{ file,err := c.FormFile() if err==nil { var sv service.UserService if file.Name == "" || len(file.Bytes) == 0 { return &bhomeclient.Reply{ Msg: "头像上传错误"} } filename := file.Name headPic ,err := sv.UploadHeadPic(file.Bytes, filename) if err != nil { if err.Error() == "pic format error" { return &bhomeclient.Reply{ Msg: "头像只允许jpg,jpeg,png的格式"} } else { return &bhomeclient.Reply{ Msg: "头像上传错误!"} } } return &bhomeclient.Reply{ Success: true, Data: headPic } } return &bhomeclient.Reply{ Msg: "未找到上传图片"} } // @Security ApiKeyAuth // @Summary 更新用户头像,昵称 // @Description 更新用户头像,昵称 // @Accept json // @Produce json // @Tags 用户 // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}" func (uc UserController) UpdateProfile(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply{ var userEditVo vo.UserEditVo err := c.BindJSON(&userEditVo) if err !=nil || userEditVo.Id =="" { return &bhomeclient.Reply{ Msg: "参数有误"} } var sv service.UserService if sv.UpdateProfile(&userEditVo) { var userSer service.UserService Profile := userSer.GetProfile(userEditVo.Id) return &bhomeclient.Reply{ Success: true,Msg: "更新成功",Data: Profile} } else { return &bhomeclient.Reply{ Msg: "更新失败"} } } // @Security ApiKeyAuth // @Summary 上传用户头像 // @Description 上传用户头像 // @Accept x-www-form-urlencoded // @Produce json // @Tags 用户 // @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/defHeadPics [get] func (uc UserController) DefHeadPics(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply { var headPic models.DefHeadPic headPics, err := headPic.FindAll() if nil == err { return &bhomeclient.Reply{ Success: true, Data: headPics } } else { return &bhomeclient.Reply{ Success: false, Data: headPics } } } // @Security ApiKeyAuth // @Summary 获取当前用户信息 // @Description 获取当前用户信息 // @Accept x-www-form-urlencoded // @Produce json // @Tags 用户 // @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/profile [get] func (uc UserController) Profile(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply { var userSer service.UserService userId := c.Header("Login_user_id") Profile := userSer.GetProfile(userId) logger.Debug("getProfile1:",Profile.UseIconType) return &bhomeclient.Reply{ Success: true, Data: Profile } }