admin.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. """Admin management API routes."""
  2. from flask_restx import Namespace, Resource, fields
  3. from flask import request
  4. from app.services.admin_service import AdminService
  5. from app.utils.auth_decorator import require_auth
  6. admin_ns = Namespace('admins', description='管理员管理接口')
  7. # API models for Swagger documentation
  8. admin_input = admin_ns.model('AdminInput', {
  9. 'username': fields.String(required=True, description='用户名'),
  10. 'password': fields.String(required=True, description='密码(至少6个字符)')
  11. })
  12. admin_update_input = admin_ns.model('AdminUpdateInput', {
  13. 'id': fields.Integer(required=True, description='管理员ID'),
  14. 'username': fields.String(description='新用户名(可选)'),
  15. 'password': fields.String(description='新密码(可选,至少6个字符)')
  16. })
  17. admin_delete_input = admin_ns.model('AdminDeleteInput', {
  18. 'id': fields.Integer(required=True, description='管理员ID')
  19. })
  20. admin_info = admin_ns.model('AdminInfo', {
  21. 'id': fields.Integer(description='管理员ID'),
  22. 'username': fields.String(description='用户名'),
  23. 'created_at': fields.String(description='创建时间'),
  24. 'updated_at': fields.String(description='更新时间')
  25. })
  26. admin_response = admin_ns.model('AdminResponse', {
  27. 'success': fields.Boolean(description='操作是否成功'),
  28. 'data': fields.Nested(admin_info, description='管理员信息')
  29. })
  30. admin_list_response = admin_ns.model('AdminListResponse', {
  31. 'success': fields.Boolean(description='操作是否成功'),
  32. 'data': fields.List(fields.Nested(admin_info), description='管理员列表')
  33. })
  34. delete_response = admin_ns.model('DeleteResponse', {
  35. 'success': fields.Boolean(description='操作是否成功'),
  36. 'message': fields.String(description='操作结果消息')
  37. })
  38. error_response = admin_ns.model('AdminErrorResponse', {
  39. 'success': fields.Boolean(description='操作是否成功'),
  40. 'error': fields.String(description='错误信息'),
  41. 'code': fields.String(description='错误代码')
  42. })
  43. @admin_ns.route('')
  44. class AdminList(Resource):
  45. """Resource for listing admins."""
  46. @admin_ns.doc('list_admins')
  47. @admin_ns.response(200, 'Success', admin_list_response)
  48. @admin_ns.response(401, 'Unauthorized', error_response)
  49. @require_auth
  50. def get(self):
  51. """获取所有管理员列表(不包含密码)"""
  52. admins = AdminService.get_all()
  53. return {
  54. 'success': True,
  55. 'data': admins
  56. }, 200
  57. @admin_ns.route('/<int:admin_id>')
  58. class AdminDetail(Resource):
  59. """Resource for getting a single admin."""
  60. @admin_ns.doc('get_admin')
  61. @admin_ns.response(200, 'Success', admin_response)
  62. @admin_ns.response(401, 'Unauthorized', error_response)
  63. @admin_ns.response(404, 'Not found', error_response)
  64. @require_auth
  65. def get(self, admin_id):
  66. """根据ID获取管理员信息"""
  67. admin, error = AdminService.get_by_id(admin_id)
  68. if error:
  69. return {
  70. 'success': False,
  71. 'error': error,
  72. 'code': 'NOT_FOUND'
  73. }, 404
  74. return {
  75. 'success': True,
  76. 'data': admin
  77. }, 200
  78. @admin_ns.route('/create')
  79. class AdminCreate(Resource):
  80. """Resource for creating admins."""
  81. @admin_ns.doc('create_admin')
  82. @admin_ns.expect(admin_input)
  83. @admin_ns.response(201, 'Created', admin_response)
  84. @admin_ns.response(400, 'Validation error', error_response)
  85. @admin_ns.response(401, 'Unauthorized', error_response)
  86. @require_auth
  87. def post(self):
  88. """创建新管理员"""
  89. data = admin_ns.payload or {}
  90. username = data.get('username', '')
  91. password = data.get('password', '')
  92. admin, error = AdminService.create(username, password)
  93. if error:
  94. return {
  95. 'success': False,
  96. 'error': error,
  97. 'code': 'VALIDATION_ERROR'
  98. }, 400
  99. return {
  100. 'success': True,
  101. 'data': admin
  102. }, 201
  103. @admin_ns.route('/update')
  104. class AdminUpdate(Resource):
  105. """Resource for updating admins."""
  106. @admin_ns.doc('update_admin')
  107. @admin_ns.expect(admin_update_input)
  108. @admin_ns.response(200, 'Success', admin_response)
  109. @admin_ns.response(400, 'Validation error', error_response)
  110. @admin_ns.response(401, 'Unauthorized', error_response)
  111. @admin_ns.response(403, 'Forbidden', error_response)
  112. @admin_ns.response(404, 'Not found', error_response)
  113. @require_auth
  114. def post(self):
  115. """更新管理员信息"""
  116. data = admin_ns.payload or {}
  117. admin_id = data.get('id')
  118. username = data.get('username')
  119. password = data.get('password')
  120. if not admin_id:
  121. return {
  122. 'success': False,
  123. 'error': 'Admin ID is required',
  124. 'code': 'VALIDATION_ERROR'
  125. }, 400
  126. # Protect primary admin (ID=1) from being modified by others
  127. current_admin_id = request.current_admin.get('admin_id')
  128. if admin_id == 1 and current_admin_id != 1:
  129. return {
  130. 'success': False,
  131. 'error': '无权修改主管理员信息',
  132. 'code': 'FORBIDDEN'
  133. }, 403
  134. admin, error = AdminService.update(admin_id, username=username, password=password)
  135. if error:
  136. if 'not found' in error.lower() or '未找到' in error:
  137. return {
  138. 'success': False,
  139. 'error': error,
  140. 'code': 'NOT_FOUND'
  141. }, 404
  142. return {
  143. 'success': False,
  144. 'error': error,
  145. 'code': 'VALIDATION_ERROR'
  146. }, 400
  147. return {
  148. 'success': True,
  149. 'data': admin
  150. }, 200
  151. @admin_ns.route('/delete')
  152. class AdminDelete(Resource):
  153. """Resource for deleting admins."""
  154. @admin_ns.doc('delete_admin')
  155. @admin_ns.expect(admin_delete_input)
  156. @admin_ns.response(200, 'Success', delete_response)
  157. @admin_ns.response(400, 'Validation error', error_response)
  158. @admin_ns.response(401, 'Unauthorized', error_response)
  159. @admin_ns.response(404, 'Not found', error_response)
  160. @require_auth
  161. def post(self):
  162. """删除管理员"""
  163. data = admin_ns.payload or {}
  164. admin_id = data.get('id')
  165. if not admin_id:
  166. return {
  167. 'success': False,
  168. 'error': 'Admin ID is required',
  169. 'code': 'VALIDATION_ERROR'
  170. }, 400
  171. current_admin_id = request.current_admin.get('admin_id')
  172. success, error = AdminService.delete(admin_id, current_admin_id=current_admin_id)
  173. if error:
  174. if 'not found' in error.lower() or '未找到' in error:
  175. return {
  176. 'success': False,
  177. 'error': error,
  178. 'code': 'NOT_FOUND'
  179. }, 404
  180. return {
  181. 'success': False,
  182. 'error': error,
  183. 'code': 'VALIDATION_ERROR'
  184. }, 400
  185. return {
  186. 'success': True,
  187. 'message': 'Admin deleted successfully'
  188. }, 200