diff --git a/inventory-backend/app/api/v1/auth.py b/inventory-backend/app/api/v1/auth.py
index 23c5ae6..6441a9f 100644
--- a/inventory-backend/app/api/v1/auth.py
+++ b/inventory-backend/app/api/v1/auth.py
@@ -174,6 +174,49 @@ def create_user():
return jsonify({'msg': str(e)}), 400
+# ==============================================================================
+# 批量创建用户
+# ==============================================================================
+@auth_bp.route('/user/batch', methods=['POST'])
+@jwt_required()
+def batch_create_user():
+ try:
+ data_list = request.get_json()
+ if not data_list or not isinstance(data_list, list):
+ return jsonify({'msg': '请求数据必须是用户数组'}), 400
+
+ # 数据清洗:移除用户没有权限的字段
+ user_permissions = get_current_user_permissions()
+ for data in data_list:
+ if 'system_user:*' not in user_permissions:
+ field_to_perm = {
+ 'cn_name': 'system_user:username',
+ 'username': 'system_user:username',
+ 'password': 'system_user:password',
+ 'department': 'system_user:department',
+ 'role': 'system_user:role',
+ 'email': 'system_user:email',
+ }
+ for field in list(data.keys()):
+ perm_code = field_to_perm.get(field)
+ if field == 'password':
+ if 'system_user:operation' not in user_permissions:
+ data.pop(field, None)
+ continue
+ if perm_code and perm_code not in user_permissions:
+ data.pop(field, None)
+
+ claims = get_jwt()
+ operator_role = claims.get('role')
+
+ results = AuthService.batch_create_users(data_list, operator_role)
+ return jsonify({'msg': '批量处理完成', 'data': results}), 200
+
+ except Exception as e:
+ current_app.logger.error(f"Batch User Create Failed: {str(e)}")
+ return jsonify({'msg': str(e)}), 500
+
+
# ==============================================================================
# 更新用户(管理员)
# ==============================================================================
diff --git a/inventory-backend/app/services/auth_service.py b/inventory-backend/app/services/auth_service.py
index 6e8ee8a..dbe619a 100644
--- a/inventory-backend/app/services/auth_service.py
+++ b/inventory-backend/app/services/auth_service.py
@@ -260,6 +260,29 @@ class AuthService:
# 返回时,最好把生成的ID告诉前端
return new_user.to_dict()
+ @staticmethod
+ def batch_create_users(data_list, operator_role):
+ """
+ 批量创建新用户。复用 create_user 的核心防重逻辑。
+ """
+ results = []
+ for data in data_list:
+ try:
+ # 复用单条创建逻辑,它自带张三/zhangsan1的防重机制
+ new_user_dict = AuthService.create_user(data, operator_role)
+ results.append({
+ "cn_name": data.get('cn_name'),
+ "account_id": new_user_dict.get('account_id'),
+ "status": "success"
+ })
+ except Exception as e:
+ results.append({
+ "cn_name": data.get('cn_name'),
+ "error": str(e),
+ "status": "fail"
+ })
+ return results
+
@staticmethod
def update_user(user_id, data, operator_role):
"""
diff --git a/inventory-web/src/api/auth.ts b/inventory-web/src/api/auth.ts
index 7cd963a..02ca295 100644
--- a/inventory-web/src/api/auth.ts
+++ b/inventory-web/src/api/auth.ts
@@ -66,4 +66,13 @@ export function changeMyPassword(data: { new_password: string; confirm_password:
method: 'put',
data
})
+}
+
+// 【新增】批量创建用户
+export function batchCreateUser(data: any[]) {
+ return request({
+ url: '/v1/auth/user/batch',
+ method: 'post',
+ data
+ })
}
\ No newline at end of file
diff --git a/inventory-web/src/views/system/UserCreate.vue b/inventory-web/src/views/system/UserCreate.vue
index 747d715..b0bcf20 100644
--- a/inventory-web/src/views/system/UserCreate.vue
+++ b/inventory-web/src/views/system/UserCreate.vue
@@ -4,9 +4,14 @@
@@ -138,12 +143,52 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 取消
+ 确认创建
+
+
+
+
+
+ 请复制以下账号分发给员工:
+
+
+
+
+ {{ row.account_id }}
+ {{ row.error }}
+
+
+
+
+ 关闭
+
+