Django- implement graphical codes, message authentication code functions (b)

Connect the text: Django- implement CAPTCHA, SMS verification code function (a)

Send SMS codes implemented backend

Add SMS settings redis verify code cache settings and choose the redis 3 is a graphical database storage location codes.

CACHES = {
    "default": { # 默认
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/0",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    "session": { # session
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    "verify_code": { # 图形验证码
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/2",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    "sms_code": { # 短信验证码
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/3",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },

View function definition

Standard is still receiving the correction process, in response to the four steps.

  • First receiver front-end transfer phone number, the user fill out the image verification code, UUID
  • Image verification code check code sent via SMS verification before
  • Verification code you need to set the expiration time
  • SMS provides communication with the cloud calm sending interface, copy the code itself is simple to use packaged into kits
  • Send text messages function uses celery asynchronous execution
from django_redis import get_redis_connection
from django.views.generic.base import View
from . import constants
from django import http
from meiduo_mall.utils.response_code import RETCODE
import random
# 自定义异步处理短信验证码的模块
from celery_tasks.sms.tasks import send_sms


class SmsCodeView(View):
    def get(self,request,mobile):
        # 接收图像验证码
        image_code = request.GET.get("image_code")
        # 接收uuid
        uuid = request.GET.get("image_code_id")
        # 连接到存放短信验证码的redis数据库
        redis_cli2 = get_redis_connection("sms_code")
        # 判断数据库短信验证标识符是否存在未过期(防止恶意请求短信)
        if redis_cli2.get(mobile + "_flag") is not None:
            return http.JsonResponse({"code":RETCODE.SMSCODERR,"errmsg":"短信验证太频繁,稍后再试"})

        if not all([image_code,uuid]):
            return http.JsonResponse({"code":RETCODE.PARAMERR, "errmsg":"参数不完整"})
        # 校验图像验证码
        redis_cli = get_redis_connection("verify_code")
        image_code_redis = redis_cli.get(uuid)
        if image_code_redis is None:
            return http.JsonResponse({"code":RETCODE.IMAGECODEERR, "errmsg":"图形验证码已过期"})
        # 成功获取数据库验证码后,删除数据库中的验证码,防止被恶意重复使用
        redis_cli.delete(uuid)
        if image_code_redis.decode().lower() != image_code:
            return http.JsonResponse({"code":RETCODE.IMAGECODEERR, "errmsg":"图形验证码不正确"})

        # 图形验证码通过,开始发送短信验证码
        sms_code = "%6d" % random.randint(0,999999)
        # sms_code = random.randint(100000, 999999) 另外的简单实现6位数验证码方式

        # 使用管道操作数据库,减少交互次数
        redis_p = redis_cli2.pipeline()
        # 短信验证码存入数据库
        # constants.SMS_CODE_EXPIRES为自定义的过期时间
        redis_p.setex(mobile,constants.SMS_CODE_EXPIRES, sms_code)
        # 短信验证码标识, 设定60秒过期,防止频繁请求短信验证码
        redis_p.setex(mobile + "_flag",constants.SMS_CODE_EXPIRES, 1)
        redis_p.execute()

        # 异步执行短信发送任务
        # 通过delay调用,可以将任务加到队列中,交给celery去执行
        send_sms.delay(mobile,sms_code)

        # 后台打印查看验证码
        # print(sms_code)
        return http.JsonResponse({"code":RETCODE.OK,"errmsg":"OK"})

Detailed comments in the code, celery, see next asynchronous implementation:
Django- send SMS codes using asynchronous Celery

Guess you like

Origin blog.csdn.net/weixin_34365417/article/details/90786467