0
点赞
收藏
分享

微信扫一扫

【Flask项目2】发送短信验证码和测试验证码是否发送成功(9)

尤克乔乔 2022-04-13 阅读 32

发送短信验证码配置
在这里插入图片描述

financial—resource—user—user_resource.py
定义资源类

class SMSCode(Resource):
    '''
    发送手机短信,验证手机号码
    '''

    def get(self):  ##如果是get请求,请求的参数往往是:参数名+参数
        phone = request.args.get('phone').strip()
        # 生成一个随机的验证码
        code = random.randint(1000, 9999)
        result = send_message('1', mobile=phone, datas=(code, '1'))
        # 将res转化为字典格式的数据
        re_dict = json.loads(result)
        if re_dict:
            current_app.logger.info(f'给手机号:{phone},发送短信成功')
            # todo 验证码需要存起来,存在redis缓冲数据库中,还需要把验证码再注册的请求中去验证
            # todo redis中存放的是字节类型的数据,有时效性
            # todo setex有3个参数,参数1:键,参数2:时效,参数3:具体的值
            fr.setex('registerCode:{}'.format(phone), SMS_CODE_EXPIRES, code)
            return {'msg': 'success', 'smsCode': code}

        else:
            return {'message': '此手机号{}发送短信失败'.format(phone), 'code': 201}

加载资源类

from financial.resource.user.user_resource import *

user_api.add_resource(SMSCode,'/smscode',endpoint='smscode')

测试
在这里插入图片描述

举报

相关推荐

0 条评论