profile.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from typing import Annotated
  4. from flask_login import login_required
  5. from pydantic import Field, SecretStr
  6. from ruoyi_common.config import RuoYiConfig
  7. from ruoyi_common.constant import UserConstants
  8. from ruoyi_common.base.model import AjaxResponse, MultiFile
  9. from ruoyi_common.domain.entity import LoginUser, SysUser
  10. from ruoyi_common.domain.enum import BusinessType
  11. from ruoyi_common.descriptor.serializer import JsonSerializer
  12. from ruoyi_common.descriptor.validator import BodyValidator, FileValidator
  13. from ruoyi_common.utils.base import FileUploadUtil
  14. from ruoyi_common.utils import security_util as SecurityUtil
  15. from ruoyi_system.service import SysUserService
  16. from ruoyi_framework.service.token import TokenService
  17. from ruoyi_framework.descriptor.log import Log
  18. from ... import reg
  19. @reg.api.route("/system/user/profile", methods=["GET"])
  20. @login_required
  21. @JsonSerializer()
  22. def system_user_profile():
  23. '''
  24. 获取个人信息
  25. '''
  26. login_user: LoginUser = SecurityUtil.get_login_user()
  27. user: SysUser = login_user.user
  28. return AjaxResponse.from_success(data=user) \
  29. if user else AjaxResponse.from_error(msg="用户信息丢失")
  30. @reg.api.route("/system/user/profile", methods=["PUT"])
  31. @BodyValidator()
  32. @login_required
  33. @Log(title = "个人信息", business_type = BusinessType.UPDATE)
  34. @JsonSerializer()
  35. def system_user_profile_update(dto:SysUser):
  36. '''
  37. 修改个人信息
  38. '''
  39. login_user: LoginUser = SecurityUtil.get_login_user()
  40. user: SysUser = login_user.user
  41. dto.user_name = user.user_name
  42. if dto.phonenumber and UserConstants.NOT_UNIQUE == \
  43. SysUserService.check_phone_unique(dto):
  44. return AjaxResponse.from_error(msg=f"修改用户'{user.user_name}'失败,手机号码已存在")
  45. if dto.email and UserConstants.NOT_UNIQUE == SysUserService.check_email_unique(dto):
  46. return AjaxResponse.from_error(msg=f"修改用户'{user.user_name}'失败,邮箱账号已存在")
  47. dto.user_id = user.user_id
  48. dto.password = None
  49. if SysUserService.update_user_profile(dto):
  50. user.nick_name = dto.nick_name
  51. user.email = dto.email
  52. user.phonenumber = dto.phonenumber
  53. user.sex = dto.sex
  54. TokenService.set_login_user(login_user)
  55. return AjaxResponse.from_success()
  56. else:
  57. return AjaxResponse.from_error(msg="修改个人信息异常,请联系管理员")
  58. @reg.api.route("/system/user/profile/updatePwd", methods=["PUT"])
  59. @BodyValidator()
  60. @login_required
  61. @Log(title = "个人信息", business_type = BusinessType.UPDATE)
  62. @JsonSerializer()
  63. def system_user_profile_update_pwd(
  64. old_password:Annotated[SecretStr, Field(..., example='admin')],
  65. new_password:Annotated[SecretStr, Field(..., example='admin123')]
  66. ):
  67. '''
  68. 修改密码
  69. '''
  70. login_user: LoginUser = SecurityUtil.get_login_user()
  71. if not SecurityUtil.matches_password(
  72. old_password.get_secret_value(), login_user.user.password
  73. ):
  74. return AjaxResponse.from_error(msg="修改密码失败,旧密码错误")
  75. if SecurityUtil.matches_password(
  76. new_password.get_secret_value(), login_user.user.password
  77. ):
  78. return AjaxResponse.from_error(msg="修改密码失败,新密码不能与旧密码相同")
  79. if SysUserService.reset_user_pwd(
  80. login_user.user.user_name,
  81. SecurityUtil.encrypt_password(new_password.get_secret_value())
  82. ):
  83. login_user.user.password = SecurityUtil.encrypt_password(
  84. new_password.get_secret_value()
  85. )
  86. TokenService.set_login_user(login_user)
  87. return AjaxResponse.from_success()
  88. else:
  89. return AjaxResponse.from_error(msg="修改密码异常,请联系管理员")
  90. @reg.api.route("/system/user/profile/avatar", methods=["POST"])
  91. @FileValidator(include={"avatarfile"})
  92. @login_required
  93. @Log(title = "用户头像", business_type = BusinessType.UPDATE)
  94. @JsonSerializer()
  95. def system_user_profile_avatar(file:MultiFile):
  96. '''
  97. 头像上传
  98. '''
  99. file = file.get("avatarfile")
  100. if file:
  101. login_user: LoginUser = SecurityUtil.get_login_user()
  102. avatar_path = FileUploadUtil.upload(file,RuoYiConfig.profile)
  103. num = SysUserService.update_user_avatar(
  104. login_user.user_name,
  105. avatar_path
  106. )
  107. if num > 0:
  108. ajax_response = AjaxResponse.from_success()
  109. setattr(ajax_response, "img_url", avatar_path)
  110. login_user.user.avatar = avatar_path
  111. TokenService.set_login_user(login_user)
  112. return ajax_response
  113. else:
  114. return AjaxResponse.from_error(msg="上传头像失败,请联系管理员")
  115. else:
  116. return AjaxResponse.from_error(msg="上传头像异常,请联系管理员")