华为云短信发送
This commit is contained in:
parent
988b6f805a
commit
0a61fffe7a
|
|
@ -9,12 +9,12 @@ import requests #需要先使用pip install requests命令安装依赖
|
||||||
url = 'https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1' #APP接入地址+接口访问URI
|
url = 'https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1' #APP接入地址+接口访问URI
|
||||||
APP_KEY = "Zee8JqWxdjEFteKTIK2xBs1T4QVq" #APP_Key
|
APP_KEY = "Zee8JqWxdjEFteKTIK2xBs1T4QVq" #APP_Key
|
||||||
APP_SECRET = "IU035p6THoKmf0joKMEo7GuXR2YH" #APP_Secret
|
APP_SECRET = "IU035p6THoKmf0joKMEo7GuXR2YH" #APP_Secret
|
||||||
sender = "csms12345678" #国内短信签名通道号或国际/港澳台短信通道号
|
sender = "8821061835858" #国内短信签名通道号或国际/港澳台短信通道号
|
||||||
TEMPLATE_ID = "f81f97bd0ffe4c2292a02505e0fc20ec" #模板ID
|
TEMPLATE_ID = "da24fc8c77dc4789917a7cd1d7ad4026" #模板ID
|
||||||
|
|
||||||
#条件必填,国内短信关注,当templateId指定的模板类型为通用模板时生效且必填,必须是已审核通过的,与模板类型一致的签名名称
|
#条件必填,国内短信关注,当templateId指定的模板类型为通用模板时生效且必填,必须是已审核通过的,与模板类型一致的签名名称
|
||||||
#国际/港澳台短信不用关注该参数
|
#国际/港澳台短信不用关注该参数
|
||||||
signature = "华为云短信测试" #签名名称
|
signature = "中科辐射学堂" #签名名称
|
||||||
|
|
||||||
# 必填,全局号码格式(包含国家码),示例:+86151****6789,多个号码之间用英文逗号分隔
|
# 必填,全局号码格式(包含国家码),示例:+86151****6789,多个号码之间用英文逗号分隔
|
||||||
receiver = "+8618911936305" #短信接收人号码
|
receiver = "+8618911936305" #短信接收人号码
|
||||||
|
|
@ -45,6 +45,24 @@ def buildWSSEHeader(appKey, appSecret):
|
||||||
digestBase64 = base64.b64encode(digest.encode()).decode() #PasswordDigest
|
digestBase64 = base64.b64encode(digest.encode()).decode() #PasswordDigest
|
||||||
return 'UsernameToken Username="{}",PasswordDigest="{}",Nonce="{}",Created="{}"'.format(appKey, digestBase64, nonce, now);
|
return 'UsernameToken Username="{}",PasswordDigest="{}",Nonce="{}",Created="{}"'.format(appKey, digestBase64, nonce, now);
|
||||||
|
|
||||||
|
def sendMsg(phone, code):
|
||||||
|
phone = '+86'+phone
|
||||||
|
code = '["'+ str(code) + '"]'
|
||||||
|
# 请求Headers
|
||||||
|
header = {'Authorization': 'WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
||||||
|
'X-WSSE': buildWSSEHeader(APP_KEY, APP_SECRET)}
|
||||||
|
# 请求Body
|
||||||
|
formData = {'from': sender,
|
||||||
|
'to': phone,
|
||||||
|
'templateId': TEMPLATE_ID,
|
||||||
|
'templateParas': code,
|
||||||
|
'statusCallback': statusCallBack,
|
||||||
|
# 'signature': signature #使用国内短信通用模板时,必须填写签名名称
|
||||||
|
}
|
||||||
|
# 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
|
||||||
|
r = requests.post(url, data=formData, headers=header, verify=False)
|
||||||
|
return r.text
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# 请求Headers
|
# 请求Headers
|
||||||
header = {'Authorization': 'WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
header = {'Authorization': 'WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Generated by Django 3.0.4 on 2021-06-22 06:20
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('crm', '0034_candidate_create_admin'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='candidate',
|
||||||
|
name='consumer',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='candidate_consumer', to='crm.Consumer'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='sendcode',
|
||||||
|
name='code',
|
||||||
|
field=models.CharField(max_length=6, verbose_name='验证码'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -102,7 +102,7 @@ class SendCode(CommonModel):
|
||||||
验证码发送记录
|
验证码发送记录
|
||||||
'''
|
'''
|
||||||
phone = models.CharField(max_length=11, verbose_name='发送号码')
|
phone = models.CharField(max_length=11, verbose_name='发送号码')
|
||||||
code = models.CharField(max_length=4, verbose_name= '验证码')
|
code = models.CharField(max_length=6, verbose_name= '验证码')
|
||||||
|
|
||||||
class Candidate(CommonModel):
|
class Candidate(CommonModel):
|
||||||
consumer = models.ForeignKey(Consumer, on_delete=models.DO_NOTHING, related_name='candidate_consumer')
|
consumer = models.ForeignKey(Consumer, on_delete=models.DO_NOTHING, related_name='candidate_consumer')
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ from django.http import Http404
|
||||||
from .spider import getZs
|
from .spider import getZs
|
||||||
import time
|
import time
|
||||||
from server.config import *
|
from server.config import *
|
||||||
|
from .hwmsg import sendMsg
|
||||||
|
|
||||||
def my_payload_handler(user, dtype="admin"):
|
def my_payload_handler(user, dtype="admin"):
|
||||||
payload = {
|
payload = {
|
||||||
|
|
@ -320,7 +321,7 @@ class ConsumerViewSet(ModelViewSet):
|
||||||
发送验证码
|
发送验证码
|
||||||
'''
|
'''
|
||||||
client = ZhenziSmsClient(sms_url, sms_appid, sms_appsecret)
|
client = ZhenziSmsClient(sms_url, sms_appid, sms_appsecret)
|
||||||
code = random.randint(1000,9999)
|
code = random.randint(10000,99999)
|
||||||
phone = request.query_params.get('phone')
|
phone = request.query_params.get('phone')
|
||||||
params = {'message':'您的验证码为:' + str(code) +',5分钟内有效', 'number': phone}
|
params = {'message':'您的验证码为:' + str(code) +',5分钟内有效', 'number': phone}
|
||||||
result = json.loads(client.send(params))
|
result = json.loads(client.send(params))
|
||||||
|
|
@ -328,7 +329,14 @@ class ConsumerViewSet(ModelViewSet):
|
||||||
SendCode.objects.create(phone=phone, code=code)
|
SendCode.objects.create(phone=phone, code=code)
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
return Response({'error':result['data']})
|
# 尝试华为云短信发送
|
||||||
|
result = json.loads(sendMsg(phone, code))
|
||||||
|
if result['code'] == '000000':
|
||||||
|
SendCode.objects.create(phone=phone, code=code)
|
||||||
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
else:
|
||||||
|
return Response({'error':'发送失败'})
|
||||||
|
# return Response({'error':result['data']})
|
||||||
|
|
||||||
@action(methods=['post','delete','get'], detail=False,
|
@action(methods=['post','delete','get'], detail=False,
|
||||||
url_path='collects', url_name='create_collects', perms_map=[{'*':'my_collects'}])
|
url_path='collects', url_name='create_collects', perms_map=[{'*':'my_collects'}])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue