Bootstrap

图片,短信验证码

  • 图片验证码,短信
    verify/views.py
import random
from . import models
from django.http import HttpResponseForbidden, HttpResponse
from django.shortcuts import render
from django_redis import get_redis_connection
from rest_framework.views import APIView,Response

from libs.captcha.captcha import captcha
# Create your views here.
from django.views import View
class ImageCodeView(View):
    def get(self, request):
        # 1.接收数据uuid
        uuid = request.GET.get('uuid')
        # 2.校验数据
        if not uuid:
            return HttpResponseForbidden('uuid无效')
        # 3.处理业务
        # 获取图片文本内容和图片二进制代码
        text, image = captcha.generate_captcha()  # text=GPMZ image=图片
        # 4.把uuid和图片文本存入redis
        redis_client = get_redis_connection('img_code')  # 获取redis客户端
        # 5.写入redis(是字符串)
        redis_client.setex(uuid, 60 * 5, text)
        # 6.返回响应图片
        return HttpResponse(image, content_type='image/jpg')
from .tasks import sms_code

class SmsCodeView(APIView):
    def post(self, request):
        phone = request.data.get('phone')
        image_code = request.data.get('image_code')
        image_code_uuid = request.data.get('image_code_uuid')
        print(image_code, image_code_uuid)
        if not all([phone, image_code, image_code_uuid]):
            return Response({'code': '4005', 'msg': '图形码输入不全'})
        redis_cli = get_redis_connection('img_code')
        print(redis_cli)
        redis_img_code = redis_cli.get(image_code_uuid).decode()
        print(redis_img_code)
        if image_code.lower() != redis_img_code.lower():
            return Response({'code': 4003, 'msg': '图形码输入有误'})
        num = random.randint(100000, 999999)
        print(num)
        send_data = {'code': num}
        res = sms_code(phone, send_data)
        print(res)
        if res:
        # sms_code.delay(phone,send_data)
        # pipeline管道:作用就是把多个命令放在一起来执行
            pl = redis_cli.pipeline()
            pl.setex(phone, 60 * 5, num)
            pl.delete(image_code_uuid)
            pl.execute()
            return Response({'code': 0, 'msg': '发送成功'})
        else:
            return Response({'code': 9999, 'msg': '发送失败'})

将captcha文件目录放到libs文件夹下,在verify中创建tasks.py文件
verify/task.py

# -*- coding: utf-8 -*-
import json

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from celery.task import task


@task
def sms_code(phone, data):
    client = AcsClient('LTAI4GJPSgHxWATtDM3QFvar', '0qgKQcUabGVuIPRih2KcwOhPT6X0pg', 'cn-hangzhou')

    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', phone)
    request.add_query_param('SignName', "美多商城")
    request.add_query_param('TemplateCode', "SMS_205875652")
    request.add_query_param('TemplateParam', data)

    response = client.do_action(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    ser = json.loads(str(response, encoding='utf-8'))
    if ser['Code'] == 'OK':
        return True
    else:
        return False

verify/urls.py

from django.urls import path
from verify import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()

urlpatterns = [
    path('image_codes/', views.ImageCodeView.as_view()),

    path('sms_codes/', views.SmsCodeView.as_view()),
    # path('sms_codes/', views.SendSMCode.as_view())
]
;