user_Profile.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * @desc:个人中心
  3. * @company:云南省奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2021/7/12 22:14
  6. */
  7. package api
  8. import (
  9. "gfast/app/common/adapter"
  10. "gfast/app/system/model"
  11. "gfast/app/system/service"
  12. "github.com/gogf/gf/frame/g"
  13. "github.com/gogf/gf/net/ghttp"
  14. "github.com/gogf/gf/util/gvalid"
  15. )
  16. type userProfile struct {
  17. SystemBase
  18. }
  19. var UserProfile = new(userProfile)
  20. // Profile 获取个人信息
  21. func (c *userProfile) Profile(r *ghttp.Request) {
  22. //获取用户信息
  23. user, err := service.SysUser.GetUserInfoById(c.GetCurrentUser(r.GetCtx()).GetUserId())
  24. if err != nil {
  25. c.FailJsonExit(r, err.Error())
  26. }
  27. userInfo, err := service.SysUser.GetUserRoleDeptPost(user)
  28. if err != nil {
  29. c.FailJsonExit(r, err.Error())
  30. }
  31. c.SusJsonExit(r, userInfo)
  32. }
  33. // Avatar 修改头像
  34. func (c *userProfile) Avatar(r *ghttp.Request) {
  35. upFile := r.GetUploadFile("avatarfile")
  36. info, err := adapter.Upload.UpImg(upFile)
  37. if err != nil {
  38. c.FailJsonExit(r, "上传失败,"+err.Error())
  39. }
  40. res := g.Map{
  41. "fileInfo": info,
  42. }
  43. userId := c.GetCurrentUser(r.GetCtx()).GetUserId()
  44. err = service.SysUser.SetAvatar(userId, info.FileUrl)
  45. if err != nil {
  46. c.FailJsonExit(r, err.Error())
  47. }
  48. c.SusJsonExit(r, res)
  49. }
  50. // Edit 修改个人信息
  51. func (c *userProfile) Edit(r *ghttp.Request) {
  52. var req *model.ProfileUpReq
  53. if err := r.Parse(&req); err != nil {
  54. c.FailJsonExit(r, err.(gvalid.Error).FirstString())
  55. }
  56. req.UserId = c.GetCurrentUser(r.GetCtx()).GetUserId()
  57. if err := service.SysUser.ProfileEdit(req); err != nil {
  58. c.FailJsonExit(r, err.Error())
  59. }
  60. c.SusJsonExit(r, "修改成功")
  61. }
  62. // UpdatePwd 修改密码
  63. func (c *userProfile) UpdatePwd(r *ghttp.Request) {
  64. var req *model.ProfileUpdatePwdReq
  65. if err := r.Parse(&req); err != nil {
  66. c.FailJsonExit(r, err.(gvalid.Error).FirstString())
  67. }
  68. req.UserId = c.GetCurrentUser(r.GetCtx()).GetUserId()
  69. if err := service.SysUser.ProfileUpdatePwd(req); err != nil {
  70. c.FailJsonExit(r, err.Error())
  71. } else {
  72. c.SusJsonExit(r, "修改成功")
  73. }
  74. }