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.

57 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import random
import re
from flask import current_app, jsonify, request, Blueprint
import utils
from app import redis_client, LogService
from presenter import MobileCodePresenter
from utils import create_response, StateCode
key = current_app.config['SECRET_KEY']
mobile_bp = Blueprint('mobile_server', __name__)
@mobile_bp.route('/mobile/get_verify_code', methods=['POST'])
# 发送一次性密码(验证码)
def getVerifyCode():
mobile_no = request.json.get('mobileNo')
if not utils.checkMobile(mobile_no):
return jsonify(create_response(StateCode.MOBILE_ERROR)), 400
sent_code = getVerificationCode(mobile_no)
mobile_present = MobileCodePresenter(sent_code).as_dict()
LogService.log()
return jsonify(create_response(StateCode.SUCCESS, data=mobile_present)), 200
#检验手机号的格式
@mobile_bp.route('/mobile/check', methods=['POST'])
def checkMobile():
#正则表达式用户输入的必须是中国大陆的手机号码格式例如11位第一位必须是1等
pattern = r'^1[3-9]\d{9}$'
#获取到客户输入的手机号
number = request.json.get('mobileNo')
print(number)
state = {
#进行匹配将匹配的结果保存在state字典中
"result": bool(re.match(pattern, number))
}
LogService.log() #记录日志
#返回一个json格式的数据
return jsonify(create_response(StateCode.SUCCESS, data=state)), 200
def getVerificationCode(mobile_no):
verification_code = generateRandomNumber() # 生成验证码
print(verification_code)
if redis_client.set(mobile_no, verification_code, 300):
return verification_code
else:
print(f"{redis_client.client}")
# 生成6位随机数字
def generateRandomNumber(length=6):
# 确保生成的数字至少有6位即使前几位是0
number = random.randint(10 ** (length - 1), 10 ** length - 1)
return number