You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.8 KiB
119 lines
4.8 KiB
import random
|
|
import re
|
|
# 从 flask 模块导入 current_app、jsonify、request 和 Blueprint 类
|
|
# current_app 用于获取当前活动的 Flask 应用实例
|
|
# jsonify 用于将 Python 字典或列表转换为 JSON 响应
|
|
# request 用于处理客户端的 HTTP 请求
|
|
# Blueprint 用于创建 Flask 应用的蓝图,方便组织路由
|
|
from flask import current_app, jsonify, request, Blueprint
|
|
|
|
# 导入自定义的 utils 模块,可能包含一些工具函数
|
|
import utils
|
|
# 从 app 包中导入 redis_client 和 LogService 类
|
|
# redis_client 用于与 Redis 数据库进行交互
|
|
# LogService 用于记录日志
|
|
from app import redis_client, LogService
|
|
# 从 presenter 模块导入 MobileCodePresenter 类,用于处理验证码相关的展示逻辑
|
|
from presenter import MobileCodePresenter
|
|
# 从 utils 模块导入 create_response 函数和 StateCode 类
|
|
# create_response 用于创建统一格式的响应
|
|
# StateCode 用于定义响应状态码
|
|
from utils import create_response, StateCode
|
|
|
|
# 从当前应用的配置中获取 SECRET_KEY
|
|
key = current_app.config['SECRET_KEY']
|
|
# 创建一个名为 'mobile_server' 的蓝图
|
|
mobile_bp = Blueprint('mobile_server', __name__)
|
|
|
|
@mobile_bp.route('/mobile/get_verify_code', methods=['POST'])
|
|
def getVerifyCode():
|
|
"""
|
|
处理获取手机验证码的请求。
|
|
|
|
该函数接收一个包含手机号码的 POST 请求,
|
|
验证手机号码的格式,若格式正确则生成验证码并存储到 Redis 中,
|
|
最后返回包含验证码信息的响应。
|
|
|
|
Returns:
|
|
tuple: 包含 JSON 响应和 HTTP 状态码的元组。
|
|
"""
|
|
# 从请求的 JSON 数据中获取手机号码
|
|
mobile_no = request.json.get('mobileNo')
|
|
# 调用 utils 模块中的 checkMobile 函数验证手机号码格式
|
|
if not utils.checkMobile(mobile_no):
|
|
# 若格式不正确,使用 create_response 函数创建包含手机号码错误状态码的响应
|
|
return jsonify(create_response(StateCode.MOBILE_ERROR)), 400
|
|
# 调用 getVerificationCode 函数生成并获取验证码
|
|
sent_code = getVerificationCode(mobile_no)
|
|
# 使用 MobileCodePresenter 类处理验证码信息,并转换为字典格式
|
|
mobile_present = MobileCodePresenter(sent_code).as_dict()
|
|
# 调用 LogService 的 log 方法记录日志
|
|
LogService.log()
|
|
# 使用 create_response 函数创建包含成功状态码和验证码展示数据的响应
|
|
return jsonify(create_response(StateCode.SUCCESS, data=mobile_present)), 200
|
|
|
|
@mobile_bp.route('/mobile/check', methods=['POST'])
|
|
def checkMobile():
|
|
"""
|
|
验证手机号码格式是否正确。
|
|
|
|
该函数接收一个包含手机号码的 POST 请求,
|
|
使用正则表达式验证手机号码格式,并返回验证结果。
|
|
|
|
Returns:
|
|
tuple: 包含 JSON 响应和 HTTP 状态码的元组。
|
|
"""
|
|
# 定义匹配中国手机号码的正则表达式模式
|
|
# 手机号码以 1 开头,第二位是 3 - 9 之间的数字,后面跟着 9 位数字
|
|
pattern = r'^1[3-9]\d{9}$'
|
|
# 从请求的 JSON 数据中获取手机号码
|
|
number = request.json.get('mobileNo')
|
|
# 定义验证结果的字典
|
|
state = {
|
|
# 使用 re.match 函数验证手机号码是否匹配模式
|
|
"result": bool(re.match(pattern, number))
|
|
}
|
|
# 调用 LogService 的 log 方法记录日志
|
|
LogService.log()
|
|
# 使用 create_response 函数创建包含成功状态码和验证结果的响应
|
|
return jsonify(create_response(StateCode.SUCCESS, data=state)), 200
|
|
|
|
def getVerificationCode(mobile_no):
|
|
"""
|
|
生成并存储手机验证码到 Redis 中。
|
|
|
|
该函数生成一个随机的 6 位数字验证码,
|
|
并将其存储到 Redis 中,设置过期时间为 300 秒。
|
|
|
|
Args:
|
|
mobile_no (str): 手机号码。
|
|
|
|
Returns:
|
|
str: 生成的验证码,如果存储失败则返回 None。
|
|
"""
|
|
# 调用 generateRandomNumber 函数生成验证码
|
|
verification_code = generateRandomNumber()
|
|
# 将验证码存储到 Redis 中,设置过期时间为 300 秒
|
|
if redis_client.set(mobile_no, verification_code, 300):
|
|
return verification_code
|
|
else:
|
|
# 若存储失败,打印 Redis 客户端信息
|
|
print(f"{redis_client.client}")
|
|
|
|
# 生成 6 位随机数字
|
|
def generateRandomNumber(length=6):
|
|
"""
|
|
生成指定长度的随机数字。
|
|
|
|
该函数生成一个指定长度的随机数字,确保生成的数字至少有指定长度,
|
|
即使前几位是 0。
|
|
|
|
Args:
|
|
length (int, optional): 随机数字的长度,默认为 6。
|
|
|
|
Returns:
|
|
int: 生成的随机数字。
|
|
"""
|
|
# 生成一个指定长度的随机数字,范围是从 10 的 (length - 1) 次方到 10 的 length 次方减 1
|
|
number = random.randint(10 ** (length - 1), 10 ** length - 1)
|
|
return number |