"""钉钉组织架构页面 API。""" from flask import Blueprint, jsonify from routes.auth import login_required, operation_required, role_required from services.db import add_operation_log from services.dingtalk_organization import organization_snapshot, sync_organization organization_bp = Blueprint('organization', __name__) @organization_bp.route('/api/organization/tree') @login_required def organization_tree(): if not role_required('organization'): return jsonify({'code': 403, 'msg': '无组织架构查看权限'}), 403 try: return jsonify({'code': 0, 'data': organization_snapshot()}) except Exception as exc: return jsonify({'code': 500, 'msg': str(exc)}), 500 @organization_bp.route('/api/organization/sync', methods=['POST']) @login_required def organization_sync(): if not operation_required('organization', 'sync'): return jsonify({'code': 403, 'msg': '无组织架构同步权限'}), 403 try: result = sync_organization('manual') add_operation_log( '同步组织架构', f"部门{result['department_count']}个,人员{result['user_count']}人,关联账号{result['matched_count']}人", page='organization', ) return jsonify({'code': 0, 'msg': '组织架构同步成功', 'data': result}) except Exception as exc: return jsonify({'code': 500, 'msg': str(exc)}), 500