This commit is contained in:
huangzhenpc
2025-02-26 19:14:40 +08:00
parent 10ccab1da6
commit 064b44c3b3
2 changed files with 39 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from .config import Config from .config import Config
from .utils import get_latest_emails from .utils import get_latest_emails, get_latest_email_with_code
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
@@ -15,4 +15,14 @@ def create_app():
emails = get_latest_emails(recipient, limit) emails = get_latest_emails(recipient, limit)
return jsonify(emails) return jsonify(emails)
@app.route('/latest_email', methods=['GET'])
def get_latest_email():
recipient = request.args.get('recipient')
if not recipient:
return jsonify({'error': 'Recipient email is required'}), 400
email_data = get_latest_email_with_code(recipient)
if email_data:
return jsonify(email_data)
return jsonify({'error': 'No emails found for this recipient'}), 404
return app return app

View File

@@ -228,3 +228,30 @@ def get_latest_emails(recipient, limit=10):
except Exception as e: except Exception as e:
logger.error(f'Error fetching emails: {e}') logger.error(f'Error fetching emails: {e}')
return [] return []
def get_latest_email_with_code(recipient):
"""获取指定收件人的最新邮件并提取验证码"""
try:
recipient_key = f'recipient:{recipient}'
email_key = redis_client.lindex(recipient_key, 0) # 获取最新邮件的键
if email_key:
email_data = redis_client.hgetall(email_key.decode())
if email_data:
email_data = {k.decode(): v.decode() for k, v in email_data.items()}
body = email_data.get('body', '')
# 假设验证码是以某种格式存在于邮件正文中,例如 "验证码: 123456"
code = extract_code_from_body(body)
email_data['code'] = code # 将验证码添加到返回数据中
return email_data
return None
except Exception as e:
logger.error(f'Error fetching latest email with code: {e}')
return None
def extract_code_from_body(body):
"""从邮件正文中提取验证码"""
import re
match = re.search(r'\b(\d{6})\b', body)
return match.group(1) if match else None