123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- package apis
- import (
- "go-admin/app/admin/models"
- "net/http"
- "github.com/gin-gonic/gin/binding"
- "golang.org/x/crypto/bcrypt"
- "github.com/gin-gonic/gin"
- "github.com/go-admin-team/go-admin-core/sdk/api"
- "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
- _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
- "github.com/google/uuid"
- "go-admin/app/admin/service"
- "go-admin/app/admin/service/dto"
- "go-admin/common/actions"
- )
- type SysUser struct {
- api.Api
- }
- // GetPage
- // @Summary 列表用户信息数据
- // @Description 获取JSON
- // @Tags 用户
- // @Param username query string false "username"
- // @Success 200 {string} {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/sys-user [get]
- // @Security Bearer
- func (e SysUser) GetPage(c *gin.Context) {
- s := service.SysUser{}
- req := dto.SysUserGetPageReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- list := make([]models.SysUser, 0)
- var count int64
- err = s.GetPage(&req, p, &list, &count)
- if err != nil {
- e.Error(500, err, "查询失败")
- return
- }
- e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
- }
- // Get
- // @Summary 获取用户
- // @Description 获取JSON
- // @Tags 用户
- // @Param userId path int true "用户编码"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/sys-user/{userId} [get]
- // @Security Bearer
- func (e SysUser) Get(c *gin.Context) {
- s := service.SysUser{}
- req := dto.SysUserById{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- var object models.SysUser
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.Get(&req, p, &object)
- if err != nil {
- e.Error(http.StatusUnprocessableEntity, err, "查询失败")
- return
- }
- e.OK(object, "查询成功")
- }
- // Insert
- // @Summary 创建用户
- // @Description 获取JSON
- // @Tags 用户
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.SysUserInsertReq true "用户数据"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/sys-user [post]
- // @Security Bearer
- func (e SysUser) Insert(c *gin.Context) {
- s := service.SysUser{}
- req := dto.SysUserInsertReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // 设置创建人
- req.SetCreateBy(user.GetUserId(c))
- err = s.Insert(&req)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- e.OK(req.GetId(), "创建成功")
- }
- // Update
- // @Summary 修改用户数据
- // @Description 获取JSON
- // @Tags 用户
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.SysUserUpdateReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/sys-user/{userId} [put]
- // @Security Bearer
- func (e SysUser) Update(c *gin.Context) {
- s := service.SysUser{}
- req := dto.SysUserUpdateReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.Update(&req, p)
- if err != nil {
- e.Logger.Error(err)
- return
- }
- e.OK(req.GetId(), "更新成功")
- }
- // Delete
- // @Summary 删除用户数据
- // @Description 删除数据
- // @Tags 用户
- // @Param userId path int true "userId"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/sys-user/{userId} [delete]
- // @Security Bearer
- func (e SysUser) Delete(c *gin.Context) {
- s := service.SysUser{}
- req := dto.SysUserById{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // 设置编辑人
- req.SetUpdateBy(user.GetUserId(c))
- // 数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.Remove(&req, p)
- if err != nil {
- e.Logger.Error(err)
- return
- }
- e.OK(req.GetId(), "删除成功")
- }
- // InsetAvatar
- // @Summary 修改头像
- // @Description 获取JSON
- // @Tags 个人中心
- // @Accept multipart/form-data
- // @Param file formData file true "file"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/user/avatar [post]
- // @Security Bearer
- func (e SysUser) InsetAvatar(c *gin.Context) {
- s := service.SysUser{}
- req := dto.UpdateSysUserAvatarReq{}
- err := e.MakeContext(c).
- MakeOrm().
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // 数据权限检查
- p := actions.GetPermissionFromContext(c)
- form, _ := c.MultipartForm()
- files := form.File["upload[]"]
- guid := uuid.New().String()
- filPath := "static/uploadfile/" + guid + ".jpg"
- for _, file := range files {
- e.Logger.Debugf("upload avatar file: %s", file.Filename)
- // 上传文件至指定目录
- err = c.SaveUploadedFile(file, filPath)
- if err != nil {
- e.Logger.Errorf("save file error, %s", err.Error())
- e.Error(500, err, "")
- return
- }
- }
- req.UserId = p.UserId
- req.Avatar = "/" + filPath
- err = s.UpdateAvatar(&req, p)
- if err != nil {
- e.Logger.Error(err)
- return
- }
- e.OK(filPath, "修改成功")
- }
- // UpdateStatus 修改用户状态
- // @Summary 修改用户状态
- // @Description 获取JSON
- // @Tags 用户
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.UpdateSysUserStatusReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/user/status [put]
- // @Security Bearer
- func (e SysUser) UpdateStatus(c *gin.Context) {
- s := service.SysUser{}
- req := dto.UpdateSysUserStatusReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON, nil).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.UpdateStatus(&req, p)
- if err != nil {
- e.Logger.Error(err)
- return
- }
- e.OK(req.GetId(), "更新成功")
- }
- // ResetPwd 重置用户密码
- // @Summary 重置用户密码
- // @Description 获取JSON
- // @Tags 用户
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.ResetSysUserPwdReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/user/pwd/reset [put]
- // @Security Bearer
- func (e SysUser) ResetPwd(c *gin.Context) {
- s := service.SysUser{}
- req := dto.ResetSysUserPwdReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.ResetPwd(&req, p)
- if err != nil {
- e.Logger.Error(err)
- return
- }
- e.OK(req.GetId(), "更新成功")
- }
- // UpdatePwd
- // @Summary 修改密码
- // @Description 获取JSON
- // @Tags 用户
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.PassWord true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/user/pwd/set [put]
- // @Security Bearer
- func (e SysUser) UpdatePwd(c *gin.Context) {
- s := service.SysUser{}
- req := dto.PassWord{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // 数据权限检查
- p := actions.GetPermissionFromContext(c)
- var hash []byte
- if hash, err = bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost); err != nil {
- req.NewPassword = string(hash)
- }
- err = s.UpdatePwd(user.GetUserId(c), req.OldPassword, req.NewPassword, p)
- if err != nil {
- e.Logger.Error(err)
- e.Error(http.StatusForbidden, err, "密码修改失败")
- return
- }
- e.OK(nil, "密码修改成功")
- }
- // GetProfile
- // @Summary 获取个人中心用户
- // @Description 获取JSON
- // @Tags 个人中心
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/user/profile [get]
- // @Security Bearer
- func (e SysUser) GetProfile(c *gin.Context) {
- s := service.SysUser{}
- req := dto.SysUserById{}
- err := e.MakeContext(c).
- MakeOrm().
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.Id = user.GetUserId(c)
- sysUser := models.SysUser{}
- roles := make([]models.SysRole, 0)
- posts := make([]models.SysPost, 0)
- err = s.GetProfile(&req, &sysUser, &roles, &posts)
- if err != nil {
- e.Logger.Errorf("get user profile error, %s", err.Error())
- e.Error(500, err, "获取用户信息失败")
- return
- }
- e.OK(gin.H{
- "user": sysUser,
- "roles": roles,
- "posts": posts,
- }, "查询成功")
- }
- // GetInfo
- // @Summary 获取个人信息
- // @Description 获取JSON
- // @Tags 个人中心
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/getinfo [get]
- // @Security Bearer
- func (e SysUser) GetInfo(c *gin.Context) {
- req := dto.SysUserById{}
- s := service.SysUser{}
- r := service.SysRole{}
- err := e.MakeContext(c).
- MakeOrm().
- MakeService(&r.Service).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- p := actions.GetPermissionFromContext(c)
- var roles = make([]string, 1)
- roles[0] = user.GetRoleName(c)
- var permissions = make([]string, 1)
- permissions[0] = "*:*:*"
- var buttons = make([]string, 1)
- buttons[0] = "*:*:*"
- var mp = make(map[string]interface{})
- mp["roles"] = roles
- if user.GetRoleName(c) == "admin" || user.GetRoleName(c) == "系统管理员" {
- mp["permissions"] = permissions
- mp["buttons"] = buttons
- } else {
- list, _ := r.GetById(user.GetRoleId(c))
- mp["permissions"] = list
- mp["buttons"] = list
- }
- sysUser := models.SysUser{}
- req.Id = user.GetUserId(c)
- err = s.Get(&req, p, &sysUser)
- if err != nil {
- e.Error(http.StatusUnauthorized, err, "登录失败")
- return
- }
- mp["introduction"] = " am a super administrator"
- mp["avatar"] = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
- if sysUser.Avatar != "" {
- mp["avatar"] = sysUser.Avatar
- }
- mp["userName"] = sysUser.NickName
- mp["userId"] = sysUser.UserId
- mp["deptId"] = sysUser.DeptId
- mp["name"] = sysUser.NickName
- mp["code"] = 200
- e.OK(mp, "")
- }
|