程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django使用騰訊雲發送短信並存入redis

編輯:Python

models.py

class UserInfo(models.Model):
username = models.CharField(verbose_name='用戶名', max_length=32)
email = models.CharField(verbose_name='郵箱', max_length=32)
mobile_phone = models.CharField(verbose_name='手機號', max_length=32)
password = models.CharField(verbose_name='密碼', max_length=32)

settings.py

# 短信的配置
TENCENT_SMS_APP_ID = 111 # 自己應用ID
TENCENT_SMS_APP_KEY = "222" # 自己應用Key
TENCENT_SMS_APP_SIGN = "333" # 自己騰訊雲創建簽名時填寫的簽名內容(使用公眾號的話這個值一般是公眾號全稱或簡稱)
TENCENT_SMS_TEMPLATE = {
'login': 1344088,
'register': 1344087
}

tencent/sms.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from qcloudsms_py import SmsMultiSender, SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from django.conf import settings
def send_sms_single(phone_num, template_id, template_param_list):
"""
單條發送短信
:param phone_num: 手機號
:param template_id: 騰訊雲短信模板ID
:param template_param_list: 短信模板所需參數列表,例如:【驗證碼:{1},描述:{2}】,則傳遞參數 [888,666]按順序去格式化模板
:return:
"""
appid = settings.TENCENT_SMS_APP_ID # 自己應用ID
appkey = settings.TENCENT_SMS_APP_KEY # 自己應用Key
sms_sign = settings.TENCENT_SMS_APP_SIGN # 自己騰訊雲創建簽名時填寫的簽名內容(使用公眾號的話這個值一般是公眾號全稱或簡稱)
sender = SmsSingleSender(appid, appkey)
try:
response = sender.send_with_param(86, phone_num, template_id, template_param_list, sign=sms_sign)
except HTTPError as e:
response = {'result': 1000, 'errmsg': "網絡異常發送失敗"}
return response
def send_sms_multi(phone_num_list, template_id, param_list):
"""
批量發送短信
:param phone_num_list:手機號列表
:param template_id:騰訊雲短信模板ID
:param param_list:短信模板所需參數列表,例如:【驗證碼:{1},描述:{2}】,則傳遞參數 [888,666]按順序去格式化模板
:return:
"""
appid = settings.TENCENT_SMS_APP_ID # 自己應用ID
appkey = settings.TENCENT_SMS_APP_KEY # 自己應用Key
sms_sign = settings.TENCENT_SMS_APP_SIGN # 自己騰訊雲創建簽名時填寫的簽名內容(使用公眾號的話這個值一般是公眾號全稱或簡稱)
sender = SmsMultiSender(appid, appkey)
try:
response = sender.send_with_param(86, phone_num_list, template_id, param_list, sign=sms_sign)
except HTTPError as e:
response = {'result': 1000, 'errmsg': "網絡異常發送失敗"}
return response

forms/account.py

from django import forms
from django.core.exceptions import ValidationError
import random
from app import models
from django.core.validators import RegexValidator
from django.conf import settings
from utils.tencent.sms import send_sms_single
from django_redis import get_redis_connection
class RegisterModelForm(forms.ModelForm):
mobile_phone = forms.CharField(label='手機號', validators=[RegexValidator(r'^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$', '手機號格式錯誤。')])
password = forms.CharField(label='密碼', widget=forms.PasswordInput())
confirm_password = forms.CharField(label='重復密碼', widget=forms.PasswordInput())
code = forms.CharField(label='驗證碼', widget=forms.TextInput())
class Meta:
model = models.UserInfo
fields = ['username', 'email', 'password', 'confirm_password', 'mobile_phone', 'code']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
field.widget.attrs['placeholder'] = '請輸入%s' % field.label
class SendSmsForm(forms.Form):
mobile_phone = forms.CharField(label='手機號', validators=[RegexValidator(r'^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$', '手機號格式錯誤。')])
def __init__(self, request, *args, **kwargs):
super().__init__(*args, **kwargs)
self.request = request
def clean_mobile_phone(self):
mobile_phone = self.cleaned_data['mobile_phone']
"""判斷短信模板是否有問題"""
tpl = self.request.GET.get("tpl")
template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
if not template_id:
raise ValidationError("短信模板錯誤。")
exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists()
if exists:
raise ValidationError("手機號已存在。")
code = random.randrange(1000, 9999)
sms = send_sms_single(mobile_phone, template_id, [code, ])
if sms['result'] != 0:
raise ValidationError("短信發送失敗{}".format(sms['errmsg']))
conn = get_redis_connection()
conn.set(mobile_phone, code, ex=60)
# print(conn.get('mobile_phone'))
return mobile_phone

views/account.py

from django.shortcuts import render, HttpResponse
from web.forms.account import RegisterModelForm, SendSmsForm
from django.http import JsonResponse
def register(request):
form = RegisterModelForm()
return render(request, 'register.html', {'form': form})
def smscode(request):
"""發送短信"""
form = SendSmsForm(request, data=request.GET)
if form.is_valid():
return JsonResponse({'status': True})
return JsonResponse({'status': False, 'error': form.errors})

urls.py

from django.urls import path ,re_path,include
from web.views import account
from app import views
urlpatterns = [
re_path(r'^send/sms/', views.send_sms, name='send_sms'),
re_path(r'^register/$', account.register, name='register'),
re_path(r'^send/smscode/$', account.smscode, name='send_smscode'),
]

前端

<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 account">
<h3>用戶注冊</h3>
<form id="form" method="POST" novalidate>
{% csrf_token %}
{% for item in form %}
{% if item.name == 'code' %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ item.label }}</label>
<div class="row">
<div class="col-md-6 col-xs-6">
{{ item }}
</div>
<div class="col-md-6 col-xs-6">
<input type="button" id="smsBtn" class="btn btn-default" value="發送驗證碼">
</div>
</div>
</div>
{% else %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ item.label }}</label>
{{ item }}
<span class="error-msg"></span>
</div>
{% endif %}
{% endfor %}
<button type="submit" class="btn btn-primary">注冊</button>
</form>
</div>
</div>
</div><script>
$(function () {
bindSmsBtn()
})
function bindSmsBtn() {
$('#smsBtn').on('click', function () {
$(".error-msg").empty();
var mobilePhone = $('#id_mobile_phone').val();
$.ajax({
url: "/send/smscode/",
data: {
"mobile_phone": mobilePhone,
"tpl": 'register'
},
type: "GET",
dataType:"JSON",
success: function (res) {
console.log(res)
if(res.status){
console.log("發送成功")
}
else{
console.log("sss")
$.each(res.error,function(key,value){
$("#id_"+key).next('span').text(value[0])
})
}
}
})
})
}
</script>

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved