Skip to content

Commit b865f1c

Browse files
committed
API:增加自动登录和获取二维码功能
1 parent 868c496 commit b865f1c

File tree

10 files changed

+1037
-11
lines changed

10 files changed

+1037
-11
lines changed

app/api/auxiliary_routes.py

Lines changed: 220 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from app.auth import require_api_key
88
from app.unified_logger import logger
99
from app.wechat import wechat_manager
10+
from app.utils.wechat_path_detector import get_best_wechat_path, validate_wechat_path
11+
import base64
12+
import os
1013

1114
auxiliary_bp = Blueprint('auxiliary', __name__)
1215

@@ -60,7 +63,7 @@ def click_session():
6063
}
6164
})
6265
except Exception as e:
63-
logger.error("wxautox", f"点击会话失败: {str(e)}")
66+
logger.error(f"[wxautox] 点击会话失败: {str(e)}")
6467
return jsonify({
6568
'code': 3001,
6669
'message': f'点击会话失败: {str(e)}',
@@ -138,7 +141,7 @@ def accept_new_friend():
138141
}
139142
})
140143
except Exception as e:
141-
logger.error("wxautox", f"接受好友申请失败: {str(e)}")
144+
logger.error(f"[wxautox] 接受好友申请失败: {str(e)}")
142145
return jsonify({
143146
'code': 3001,
144147
'message': f'接受好友申请失败: {str(e)}',
@@ -205,9 +208,223 @@ def reject_new_friend():
205208
}
206209
})
207210
except Exception as e:
208-
logger.error("wxautox", f"拒绝好友申请失败: {str(e)}")
211+
logger.error(f"[wxautox] 拒绝好友申请失败: {str(e)}")
209212
return jsonify({
210213
'code': 3001,
211214
'message': f'拒绝好友申请失败: {str(e)}',
212215
'data': None
216+
}), 500
217+
218+
219+
@auxiliary_bp.route('/login/auto', methods=['POST'])
220+
@require_api_key
221+
def auto_login():
222+
"""自动登录微信 (Plus版)"""
223+
try:
224+
# 检查当前是否使用wxautox库
225+
wx_instance = wechat_manager.get_instance()
226+
if wx_instance:
227+
lib_name = getattr(wx_instance, '_lib_name', 'wxauto')
228+
if lib_name != 'wxautox':
229+
return jsonify({
230+
'code': 3001,
231+
'message': '自动登录功能需要wxautox库支持',
232+
'data': None
233+
}), 400
234+
235+
# 获取请求参数
236+
data = request.get_json() or {}
237+
wxpath = data.get('wxpath')
238+
timeout = data.get('timeout', 10)
239+
240+
logger.info(f"[wxautox] 自动登录请求参数: wxpath={wxpath}, timeout={timeout}")
241+
242+
# 如果没有提供微信路径,尝试自动检测
243+
if not wxpath:
244+
wxpath = get_best_wechat_path()
245+
logger.info(f"[wxautox] 自动检测到微信路径: {wxpath}")
246+
if not wxpath:
247+
return jsonify({
248+
'code': 1002,
249+
'message': '未找到微信安装路径,请手动指定wxpath参数',
250+
'data': None
251+
}), 400
252+
253+
# 验证微信路径
254+
logger.info(f"[wxautox] 验证微信路径: {wxpath}")
255+
if not validate_wechat_path(wxpath):
256+
logger.error(f"[wxautox] 微信路径验证失败: {wxpath}")
257+
return jsonify({
258+
'code': 1002,
259+
'message': f'无效的微信路径: {wxpath}',
260+
'data': None
261+
}), 400
262+
263+
# 导入wxautox库
264+
try:
265+
from wxautox import LoginWnd
266+
import pythoncom
267+
except ImportError:
268+
return jsonify({
269+
'code': 3001,
270+
'message': 'wxautox库未安装或不可用',
271+
'data': None
272+
}), 400
273+
274+
# 初始化COM环境
275+
try:
276+
pythoncom.CoInitialize()
277+
except:
278+
pass # 如果已经初始化过,忽略错误
279+
280+
try:
281+
# 创建登录窗口
282+
loginwnd = LoginWnd(wxpath)
283+
284+
# 执行自动登录
285+
login_result = loginwnd.login(timeout=timeout)
286+
finally:
287+
# 清理COM环境
288+
try:
289+
pythoncom.CoUninitialize()
290+
except:
291+
pass
292+
293+
return jsonify({
294+
'code': 0,
295+
'message': '自动登录执行完成',
296+
'data': {
297+
'wxpath': wxpath,
298+
'timeout': timeout,
299+
'login_result': login_result,
300+
'success': login_result is True
301+
}
302+
})
303+
304+
except Exception as e:
305+
logger.error(f"[wxautox] 自动登录失败: {str(e)}")
306+
return jsonify({
307+
'code': 3001,
308+
'message': f'自动登录失败: {str(e)}',
309+
'data': None
310+
}), 500
311+
312+
313+
@auxiliary_bp.route('/login/qrcode', methods=['POST'])
314+
@require_api_key
315+
def get_login_qrcode():
316+
"""获取登录二维码 (Plus版)"""
317+
try:
318+
# 检查当前是否使用wxautox库
319+
wx_instance = wechat_manager.get_instance()
320+
if wx_instance:
321+
lib_name = getattr(wx_instance, '_lib_name', 'wxauto')
322+
if lib_name != 'wxautox':
323+
return jsonify({
324+
'code': 3001,
325+
'message': '获取登录二维码功能需要wxautox库支持',
326+
'data': None
327+
}), 400
328+
329+
# 获取请求参数
330+
data = request.get_json() or {}
331+
wxpath = data.get('wxpath')
332+
333+
logger.info(f"[wxautox] 获取二维码请求参数: wxpath={wxpath}")
334+
335+
# 如果没有提供微信路径,尝试自动检测
336+
if not wxpath:
337+
wxpath = get_best_wechat_path()
338+
logger.info(f"[wxautox] 自动检测到微信路径: {wxpath}")
339+
if not wxpath:
340+
return jsonify({
341+
'code': 1002,
342+
'message': '未找到微信安装路径,请手动指定wxpath参数',
343+
'data': None
344+
}), 400
345+
346+
# 验证微信路径
347+
logger.info(f"[wxautox] 验证微信路径: {wxpath}")
348+
if not validate_wechat_path(wxpath):
349+
logger.error(f"[wxautox] 微信路径验证失败: {wxpath}")
350+
return jsonify({
351+
'code': 1002,
352+
'message': f'无效的微信路径: {wxpath}',
353+
'data': None
354+
}), 400
355+
356+
# 导入wxautox库
357+
try:
358+
from wxautox import LoginWnd
359+
import pythoncom
360+
except ImportError:
361+
return jsonify({
362+
'code': 3001,
363+
'message': 'wxautox库未安装或不可用',
364+
'data': None
365+
}), 400
366+
367+
# 初始化COM环境
368+
try:
369+
pythoncom.CoInitialize()
370+
except:
371+
pass # 如果已经初始化过,忽略错误
372+
373+
try:
374+
# 创建登录窗口
375+
loginwnd = LoginWnd(wxpath)
376+
377+
# 获取二维码图片路径
378+
qrcode_path = loginwnd.get_qrcode()
379+
380+
if not qrcode_path or not os.path.exists(qrcode_path):
381+
return jsonify({
382+
'code': 3001,
383+
'message': '获取二维码失败或二维码文件不存在',
384+
'data': None
385+
}), 500
386+
387+
# 读取二维码图片并转换为base64
388+
with open(qrcode_path, 'rb') as f:
389+
qrcode_data = f.read()
390+
391+
# 转换为base64编码
392+
qrcode_base64 = base64.b64encode(qrcode_data).decode('utf-8')
393+
394+
# 获取文件扩展名以确定MIME类型
395+
file_ext = os.path.splitext(qrcode_path)[1].lower()
396+
if file_ext == '.png':
397+
mime_type = 'image/png'
398+
elif file_ext in ['.jpg', '.jpeg']:
399+
mime_type = 'image/jpeg'
400+
else:
401+
mime_type = 'image/png' # 默认为PNG
402+
403+
# 构建data URL格式的base64字符串
404+
qrcode_data_url = f"data:{mime_type};base64,{qrcode_base64}"
405+
406+
return jsonify({
407+
'code': 0,
408+
'message': '获取登录二维码成功',
409+
'data': {
410+
'wxpath': wxpath,
411+
'qrcode_path': qrcode_path,
412+
'qrcode_base64': qrcode_base64,
413+
'qrcode_data_url': qrcode_data_url,
414+
'mime_type': mime_type
415+
}
416+
})
417+
finally:
418+
# 清理COM环境
419+
try:
420+
pythoncom.CoUninitialize()
421+
except:
422+
pass
423+
424+
except Exception as e:
425+
logger.error(f"[wxautox] 获取登录二维码失败: {str(e)}")
426+
return jsonify({
427+
'code': 3001,
428+
'message': f'获取登录二维码失败: {str(e)}',
429+
'data': None
213430
}), 500

0 commit comments

Comments
 (0)