django-短信验证码

1.视图函数中使用#


1.1 verifications/urls.py中添加路由#

urlpatterns = [
     path('sms_codes/', views.SmsCodeView.as_view()),
]

1.2写试图函数#

# verifications/views.py

容联云配置可参考博客:https://www.cnblogs.com/tjw-bk/p/13778017.html

短信验证码我之前是搭配容联云

短信验证码的原理图:#

from rest_framework.permissions import AllowAny 
from rest_framework.views import APIView 
from rest_framework.response import Response 
import re 
import random 
from utils.rl_sms import send_message #这个是配置的容联云



'''短信验证码'''


class SmsCodeView(APIView):
    '''使用apiview的限流'''
    # 1. 所有人可以访问
    permission_classes = (AllowAny,)

    def post(self, request):
        # 1. 获取参数
        phone = request.data.get('phone')  # 手机号 
        image_code = request.data.get('image_code')  # 字符串验证码 
        image_code_uuid = request.data.get('image_code_uuid')  # 前端生成的uuid,是redis中图片验证码的key

        # 2. 检查参数
        if not all([phone, image_code, image_code_uuid]):
            return Response({"code": 999, "msg": "参数不全"})

        if not re.match(r'^1[3456789]\d{9}$', phone):
            return Response({"code": 999, "msg": "手机号码不正确"})

        # 3. 检查是否发送
        redis_client = get_redis_connection('img_code')  # 连接redis数据库
        phone_exists = redis_client.get(phone)
        if phone_exists:
            return Response({"code": 999, "msg": "频繁发送, 请稍后再试"})

        # 4.检查图片验证码是否合法
        redis_image_code = redis_client.get(image_code_uuid)  # 字符串验证码
        if redis_image_code:
            # bytes 转成 string
            redis_image_code = redis_image_code.decode()  # 把字符串验证码转换一下大小写

        # 比较用户提供的图片内容是否和redis中保存的一致
        if image_code.upper() != redis_image_code:
            return Response({'code': 999, 'msg': '图片验证码不正确'})

        # 5. 发送
        code = '%06d' % random.randint(0, 999999)  # 随机6位验证码
        print('code===============================================', code)
        send_resp = send_message(phone, (code, "5"))

        # 5.1 保存code 到 redis中
        redis_client.setex(phone, 60 * 5, code)  # phone:code, 5分钟有效期
        # 5.2 从redis中删除这个图片验证码, 以防再次被使用
        redis_client.delete(image_code_uuid)

        # 6.存储这个已经发送验证码的手机号,防止频繁发送(使用pipeline 批量操作 )
        pl = redis_client.pipeline()
        pl.setex(phone, 60 * 5, code)
        pl.delete(image_code_uuid)
        pl.execute()

        # 7. 返回结果
        return Response({"code": 0, "msg": "短信发送成功"})


posted @   就学45分钟  阅读(265)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩