119 lines
3.7 KiB
Python
119 lines
3.7 KiB
Python
"""
|
|
backend/app/api/admin/wecom.py
|
|
|
|
用途:管理后台企微绑定、配置与诊断 API(薄路由层)。
|
|
依赖:admin.blueprint、services/admin/wecom_admin_service。
|
|
逻辑:/wecom/* 路由。
|
|
"""
|
|
from flask import jsonify, request
|
|
|
|
from app.api.admin.blueprint import admin_bp
|
|
from app.core.decorators import require_admin
|
|
from app.services.admin import wecom_admin_service
|
|
|
|
|
|
def _query_params() -> dict[str, str]:
|
|
return {k: str(v) for k, v in request.args.items()}
|
|
|
|
|
|
@admin_bp.route('/wecom/debug-user-by-code', methods=['POST'])
|
|
@require_admin
|
|
def debug_wecom_user_by_code():
|
|
"""通过 OAuth code 诊断企微用户匹配结果。"""
|
|
data = request.get_json() or {}
|
|
return jsonify(wecom_admin_service.debug_user_by_code(data.get('code')))
|
|
|
|
|
|
@admin_bp.route('/wecom/bindings', methods=['GET'])
|
|
@require_admin
|
|
def list_wecom_bindings():
|
|
"""分页列出企微绑定。"""
|
|
return jsonify(wecom_admin_service.list_bindings(_query_params()))
|
|
|
|
|
|
@admin_bp.route('/wecom/bind', methods=['POST'])
|
|
@require_admin
|
|
def bind_wecom_user():
|
|
"""管理员手动绑定企微账号。"""
|
|
data = request.get_json() or {}
|
|
return jsonify(wecom_admin_service.bind_user(data))
|
|
|
|
|
|
@admin_bp.route('/wecom/unbind', methods=['POST'])
|
|
@require_admin
|
|
def unbind_wecom_user():
|
|
"""管理员解绑企微账号。"""
|
|
data = request.get_json() or {}
|
|
return jsonify(wecom_admin_service.unbind_user(data))
|
|
|
|
|
|
@admin_bp.route('/wecom/provision', methods=['POST'])
|
|
@require_admin
|
|
def provision_wecom_user():
|
|
"""创建/更新用户并绑定企微、分配权限。"""
|
|
data = request.get_json() or {}
|
|
payload, status_code = wecom_admin_service.provision_user(data)
|
|
return jsonify(payload), status_code
|
|
|
|
|
|
@admin_bp.route('/wecom/login-audits', methods=['GET'])
|
|
@require_admin
|
|
def get_wecom_login_audits():
|
|
"""查询登录审计(企微/密码等来源)。"""
|
|
return jsonify(wecom_admin_service.list_login_audits(_query_params()))
|
|
|
|
|
|
@admin_bp.route('/wecom/notify-settings', methods=['GET', 'PUT'])
|
|
@require_admin
|
|
def wecom_notify_settings():
|
|
"""读取或更新企微通知配置。"""
|
|
if request.method == 'GET':
|
|
return jsonify(wecom_admin_service.serialize_notify_settings())
|
|
data = request.get_json() or {}
|
|
return jsonify(wecom_admin_service.update_notify_settings(data))
|
|
|
|
|
|
@admin_bp.route('/wecom/system-settings', methods=['GET', 'PUT'])
|
|
@require_admin
|
|
def wecom_system_settings():
|
|
"""读取或更新企微 OAuth 系统配置。"""
|
|
if request.method == 'GET':
|
|
return jsonify(wecom_admin_service.serialize_system_settings())
|
|
data = request.get_json() or {}
|
|
return jsonify(wecom_admin_service.update_system_settings(data))
|
|
|
|
|
|
@admin_bp.route('/wecom/integration-manifest', methods=['GET'])
|
|
@require_admin
|
|
def wecom_integration_manifest():
|
|
"""返回企微集成 manifest(回调 URL、OAuth 端点等)。"""
|
|
return jsonify(
|
|
wecom_admin_service.integration_manifest(
|
|
host_url=request.host_url,
|
|
host=request.host,
|
|
)
|
|
)
|
|
|
|
|
|
@admin_bp.route('/wecom/notify-metrics', methods=['GET'])
|
|
@require_admin
|
|
def wecom_notify_metrics():
|
|
"""企微通知发送/打开率指标(近 30 天)。"""
|
|
return jsonify(wecom_admin_service.notify_metrics())
|
|
|
|
|
|
@admin_bp.route('/wecom/binding-stats', methods=['GET'])
|
|
@require_admin
|
|
def wecom_binding_stats():
|
|
"""活跃用户企微绑定统计。"""
|
|
return jsonify(wecom_admin_service.binding_stats())
|
|
|
|
|
|
@admin_bp.route('/wecom/sync-userids', methods=['POST'])
|
|
@require_admin
|
|
def sync_wecom_userids_admin():
|
|
"""从 OA 按工号同步 users.wecom_userid。"""
|
|
data = request.get_json() or {}
|
|
apply = bool(data.get('apply'))
|
|
return jsonify(wecom_admin_service.sync_userids(apply=apply))
|