diff --git a/admin/dashboard/blueprints/system.py b/admin/dashboard/blueprints/system.py index cc64a92..b592fe3 100644 --- a/admin/dashboard/blueprints/system.py +++ b/admin/dashboard/blueprints/system.py @@ -86,4 +86,13 @@ def api_wx_logs(): }) except Exception as e: logger.error(f"获取微信日志失败: {e}") - return jsonify({"success": False, "error": str(e)}), 500 \ No newline at end of file + return jsonify({"success": False, "error": str(e)}), 500 + +# 在现有路由下添加 +@system_bp.route('/api/current_user_info', methods=['GET']) +@login_required +def get_current_user_info(): + """获取当前登录的微信用户信息""" + dashboard_server = current_app.dashboard_server + result = dashboard_server.get_current_user_info() + return jsonify(result) diff --git a/admin/dashboard/server.py b/admin/dashboard/server.py index 4c39bc7..41d10c8 100644 --- a/admin/dashboard/server.py +++ b/admin/dashboard/server.py @@ -44,6 +44,8 @@ class DashboardServer: self.contact_manager = robot_instance.contact_manager self.plugin_manager = robot_instance.plugin_manager self.plugin_registry = robot_instance.plugin_registry + # 获取WCF实例 + self.wcf = robot_instance.wcf self.logger.info("使用Robot实例的对象进行初始化") else: self.logger.error("未提供Robot实例,Dashboard无法正常工作") @@ -165,3 +167,42 @@ class DashboardServer: self._server.shutdown() self.logger.info("服务器已停止") + + def get_current_user_info(self): + """获取当前登录的微信用户信息""" + try: + if not self.wcf: + self.logger.error("WCF实例不可用,无法获取当前用户信息") + return {"success": False, "message": "WCF实例不可用"} + + # 获取当前登录的微信ID + wx_id = self.wcf.get_self_wxid() + if not wx_id: + return {"success": False, "message": "未获取到微信ID"} + + # 获取用户详细信息 + user_info = self.wcf.get_user_info() + + # 获取头像 + avatar_path = None + try: + avatar_path = self.wcf.get_avatar(wx_id) + except Exception as e: + self.logger.warning(f"获取用户头像失败: {e}") + + return { + "success": True, + "data": { + "wx_id": wx_id, + "nickname": user_info.get("nickname", "未知用户"), + "avatar": avatar_path, + "signature": user_info.get("signature", ""), + "gender": user_info.get("gender", 0), + "country": user_info.get("country", ""), + "province": user_info.get("province", ""), + "city": user_info.get("city", "") + } + } + except Exception as e: + self.logger.error(f"获取当前用户信息失败: {e}") + return {"success": False, "message": f"获取用户信息出错: {str(e)}"} diff --git a/admin/dashboard/templates/index.html b/admin/dashboard/templates/index.html index c3f40a5..5387c82 100644 --- a/admin/dashboard/templates/index.html +++ b/admin/dashboard/templates/index.html @@ -6,6 +6,28 @@