import json import re import random import redis from flask import Blueprint,jsonify from flask_restful import Api,Resource,reqparse # from models.model import UserModel,db user_bp = Blueprint("user_bp",__name__,url_prefix="/user_bp") api = Api(user_bp) class SmsView(Resource): def get(self,phone): rule = r'^1[3-9][0-9]{9}$' is_phone = re.findall(rule,phone) if not is_phone: return jsonify({ 'code':400, 'msg':'手机号格式错误' }) #生成验证码 code = random.randint(100000,999999) code = str(code) #把验证码存到redis rds = redis.Redis(host='127.0.0.1',port=6379,password=123456) rds.set('sms_%s'% phone,code,ex=5*60) #调用容联云 from ronglian_sms_sdk import SmsSDK acc_id = '8aaf07087e7b9872017f23f1b8f11b5a' acc_token = 'a717526eb784439ebc80bb5f93a3f2e5' app_id = '8aaf07087e7b9872017f23f1ba061b61' sms = SmsSDK(acc_id,acc_token,app_id) res = sms.sendMessage('1',phone,(code,'5')) rs_data = json.loads(res) if rs_data['statusCode'] == '000000': return jsonify({ 'code':200, 'msg':'发送成功' }) else: return jsonify({ 'code':400, 'msg':'发送失败-%s'% rs_data['statusMsg'] }) api.add_resource(SmsView,'/sms/<phone>')
flask发送验证码
最新推荐文章于 2024-06-19 12:02:32 发布