123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- package admin
- import (
- "github.com/druidcaesa/gotool"
- "strconv"
- "ulink-admin/frame"
- "ulink-admin/modules/system/models/model"
- "ulink-admin/modules/system/models/req"
- "ulink-admin/modules/system/models/response"
- service2 "ulink-admin/modules/system/service"
- "ulink-admin/pkg/excels"
- "ulink-admin/pkg/jwt/admin"
- "ulink-admin/pkg/page"
- )
- // UserApi 用户操作api
- type UserApi struct {
- UserService *service2.UserService `inject:""`
- RoleService *service2.RoleService `inject:""`
- }
- // Find @Summary 用户列表查询接口
- // @Description 用户列表查询接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param object query req.UserQuery false "查询参数"
- // @Security ApiKeyAuth
- // @Success 200 {object} resp.Response{data=page.Page{list=model.SysUser},msg=string} "分页获取用户列表,返回包括列表,总数,页码,每页数量"
- // @Router /user/page [get]
- func (a UserApi) Find(c *frame.Context) {
- query := &req.UserQuery{}
- c.ValidteError(c.ShouldBind(query), query)
- list, i := a.UserService.Page(query)
- c.Ok(page.Page{
- Size: query.PageSize,
- Total: i,
- List: list,
- })
- }
- // Find @Summary 用户列表查询接口
- // @Description 用户列表查询接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param object query req.UserQuery false "查询参数"
- // @Security ApiKeyAuth
- // @Success 200 {object} resp.Response{data=page.Page{list=model.SysUser},msg=string} "分页获取用户列表,返回包括列表,总数,页码,每页数量"
- // @Router /user/list [get]
- func (a UserApi) List(c *frame.Context) {
- query := &req.UserQuery{}
- c.ValidteError(c.ShouldBind(query), query)
- list := make([]model.SysUser, 0)
- a.UserService.List(query, &list)
- c.Ok(list)
- }
- // GetInfo @Summary 用户详情查询接口
- // @Description 用户详情查询接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param id path int true "id" id
- // @Security ApiKeyAuth
- // @Success 200 {object} resp.Response{data=response.UserInfo,msg=string} "返回用户详情查询"
- // @Router /user/getInfo [get]
- func (a UserApi) GetInfo(c *frame.Context) {
- param := c.Param("id")
- r := new(response.UserInfo)
- roleAll := make([]model.SysRole, 0)
- //查询角色
- a.RoleService.List(&req.RoleQuery{}, &roleAll)
- //判断id传入的是否为空
- if !gotool.StrUtils.HasEmpty(param) {
- parseInt, err := strconv.ParseInt(param, 10, 64)
- if err == nil {
- //判断当前登录用户是否是admin
- m := new(model.SysUser)
- if m.IsAdmin(parseInt) {
- r.Roles = roleAll
- } else {
- roles := make([]model.SysRole, 0)
- for _, role := range roleAll {
- if role.Id != 1 {
- roles = append(roles, role)
- }
- }
- r.Roles = roles
- }
- user := new(model.SysUser)
- a.UserService.GetById(parseInt, user)
- if user == nil {
- frame.Throw(frame.BUSINESS_CODE, "用户查询异常")
- }
- role := a.RoleService.SelectRoleListByUserId(parseInt)
- if role == nil {
- frame.Throw(frame.BUSINESS_CODE, "角色查询异常")
- }
- //根据id获取用户数据
- r.User = user
- //根据用户ID查询角色id集合
- r.RoleIds = role
- }
- } else {
- //id为空不取管理员角色
- roles := make([]model.SysRole, 0)
- for _, role := range roleAll {
- if role.Id != 1 {
- roles = append(roles, role)
- }
- }
- r.Roles = roles
- }
- c.Ok(r)
- }
- // MyInfo @Summary 会员详情查询接口
- // @Description 会员详情等查询接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Security ApiKeyAuth
- // @Success 200 {object} resp.Response{data=response.MemberInfo,msg=string} "返回会员详情查询"
- // @Router /app/user/myInfo [get]
- func (a UserApi) MyInfo(c *frame.Context) {
- userId := admin.GetUserInfo(c).Id
- memberInfo := new(response.MemberInfo)
- user := new(model.SysUser)
- a.UserService.GetById(userId, user)
- //查询会员信息
- memberInfo.User = user
- if memberInfo.User == nil {
- c.Error("用户查询异常")
- return
- }
- c.Ok(memberInfo)
- }
- // AuthRole 根据用户编号获取授权角色
- func (a UserApi) AuthRole(c *frame.Context) {
- /* m := make(map[string]interface{})
- id := c.Param("id")
- parseInt, err := strconv.ParseInt(id, 10, 64)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- c.JSON(500, resp.ErrorResp(err))
- }
- user := a.UserService.GetUserById(parseInt)
- if user == nil {
- resp.Error(c, "用户查询异常")
- return
- }
- //查询角色
- roles := a.RoleService.GetRoleListByUserId(parseInt)
- if roles == nil {
- resp.Error(c, "角色查询异常")
- return
- }
- flag := model.SysUser{}.IsAdmin(parseInt)
- if flag {
- m["roles"] = roles
- } else {
- roleList := make([]model.SysRole, 0)
- for _, role := range *roles {
- if role.Id != 1 {
- roleList = append(roleList, role)
- }
- }
- m["roles"] = roleList
- }
- allRoles := a.RoleService.FindAll()
- if allRoles == nil {
- resp.Error(c, "角色查询异常")
- return
- }
- if flag {
- m["allRoles"] = allRoles
- } else {
- roleList := make([]model.SysRole, 0)
- for _, role := range allRoles {
- if role.Id != 1 {
- roleList = append(roleList, *role)
- }
- }
- m["allRoles"] = roleList
- }
- m["user"] = user
- c.JSON(200, resp.Success(m))*/
- }
- // Add @Summary 新增用户接口
- // @Description 新增用户接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param data body model.SysUser true "用户实体对象"
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/add [post]
- func (a UserApi) Add(c *frame.Context) {
- userBody := &req.UserBody{}
- c.ValidteError(c.ShouldBind(userBody), userBody)
- userBody.DelFlag = "0"
- userBody.Balance = "0.00"
- //添加用户
- a.UserService.Insert(userBody)
- }
- // Edit @Summary 修改用户接口
- // @Description 修改用户接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param data body model.SysUser true "用户实体对象"
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/edit [put]
- func (a UserApi) Edit(c *frame.Context) {
- userBody := &req.UserBody{}
- c.ValidteError(c.ShouldBind(userBody), userBody)
- //进行用户修改操作
- a.UserService.Edit(userBody)
- }
- // Remove @Summary 删除用户接口
- // @Description 删除用户接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param id path int true "id" id
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/remove [delete]
- func (a UserApi) Remove(c *frame.Context) {
- param := c.Param("id")
- id, err := strconv.ParseInt(param, 10, 64)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- c.Error("参数错误")
- return
- }
- a.UserService.Remove(id)
- }
- // ResetPwd @Summary 重置用户密码接口
- // @Description 重置用户密码接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param data body model.SysUser true "用户实体对象"
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/resetPwd [put]
- func (a UserApi) ResetPwd(c *frame.Context) {
- userBody := &req.UserBody{}
- c.ValidteError(c.ShouldBind(userBody), userBody)
- if a.UserService.CheckUserAllowed(userBody) {
- c.Error("不允许操作超级管理员用户")
- return
- }
- userBody.Password = gotool.BcryptUtils.Generate(userBody.Password)
- //进行密码修改
- a.UserService.ResetPwd(userBody)
- }
- // Export 导出excel
- func (a UserApi) Export(c *frame.Context) {
- query := &req.UserQuery{}
- c.ValidteError(c.ShouldBind(query), query)
- items := make([]interface{}, 0)
- list, _ := a.UserService.Page(query)
- for _, userResponse := range list {
- items = append(items, *userResponse)
- }
- _, file := excels.ExportExcel(items, "用户表")
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename="+gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- c.Header("FileName", gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
- _ = file.Write(c.Writer)
- }
- // Profile 查询个人信息
- // @Summary 查询个人信息接口
- // @Description 查询个人信息接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param id path int true "id" id
- // @Security ApiKeyAuth
- // @Success 200 {object} resp.Response{data=map[string]interface{},msg=string} "返回个人详情查询"
- // @Router /user/profile [get]
- func (a UserApi) Profile(c *frame.Context) {
- m := make(map[string]interface{})
- info := admin.GetUserInfo(c)
- user := new(model.SysUser)
- a.UserService.GetById(info.Id, user)
- m["user"] = user
- // 查询所属角色组
- m["roleGroup"] = a.RoleService.SelectRolesByUserName(info.UserName)
- c.Ok(m)
- }
- // UpdateProfile 修改个人数据
- // @Summary 修改个人数据接口
- // @Description 修改个人数据接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param data body model.SysUser true "用户实体对象"
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/profile [put]
- func (a UserApi) UpdateProfile(c *frame.Context) {
- user := &req.UserBody{}
- c.ValidteError(c.ShouldBind(user), user)
- a.UserService.EditProfile(user)
- }
- // ChangeAuthRole 修改状态
- // @Summary 修改状态接口
- // @Description 修改状态接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param id query int true "id" id
- // @Param status query string true "status" status
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/authRole [put]
- func (a UserApi) ChangeAuthRole(c *frame.Context) {
- userBody := &req.UserBody{}
- c.ValidteError(c.ShouldBind(userBody), userBody)
- a.UserService.UpdateAuthRole(userBody)
- }
- // ChangeStatus 修改状态
- // @Summary 修改状态接口
- // @Description 修改状态接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param id query int true "id" id
- // @Param status query string true "status" status
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/changeStatus [put]
- func (a UserApi) ChangeStatus(c *frame.Context) {
- param := c.Query("id")
- status := c.Query("status")
- id, err := strconv.ParseInt(param, 10, 64)
- if err != nil {
- c.Error("参数错误")
- return
- }
- a.UserService.UpdateStatus(id, status)
- }
- // UpdatePwd 修改个人密码
- // @Summary 修改个人密码接口
- // @Description 修改个人密码接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param oldPassword query string true "oldPassword" oldPassword
- // @Param newPassword query string true "newPassword" newPassword
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/profile/updatePwd [put]
- func (a UserApi) UpdatePwd(c *frame.Context) {
- oldPassword := c.Query("oldPassword")
- newPassword := c.Query("newPassword")
- info := admin.GetUserInfo(c)
- name := a.UserService.GetUserByUserName(info.UserName)
- hash := gotool.BcryptUtils.CompareHash(name.Password, oldPassword)
- if !hash {
- c.Error("修改密码失败,旧密码错误")
- return
- }
- generate := gotool.BcryptUtils.Generate(oldPassword)
- compareHash := gotool.BcryptUtils.CompareHash(generate, newPassword)
- if compareHash {
- c.Error("新密码不能与旧密码相同")
- return
- }
- a.UserService.UpdatePwd(info.Id, gotool.BcryptUtils.Generate(newPassword))
- }
- // Avatar 修改头像
- // UpdatePwd 修改个人密码
- // @Summary 修改个人密码接口
- // @Description 修改个人密码接口
- // @Tags 用户相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 用户令牌"
- // @Param file formData file true "avatarfile"
- // @Success 200 {object} resp.Response{msg=string} "操作状态"
- // @Router /user/profile/avatar [put]
- func (a UserApi) Avatar(c *frame.Context) {
- img := c.Request.FormValue("img")
- // 进行存储
- info := admin.GetUserInfo(c)
- info.Avatar = img
- a.UserService.UpdateAvatar(info)
- }
|