From 3af6aec8fb0ccb44636b30cacb87328c1677fa3a Mon Sep 17 00:00:00 2001 From: baoliang Date: Mon, 24 Aug 2020 21:43:19 +0800 Subject: [PATCH] add polls --- polls/utils.py | 27 +++++++++++++++++++++++++-- polls/views.py | 9 +++++++-- requirements.txt | 2 ++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/polls/utils.py b/polls/utils.py index 6285ae9..9aa983e 100644 --- a/polls/utils.py +++ b/polls/utils.py @@ -1,7 +1,30 @@ +from aliyunsdkcore.client import AcsClient +from aliyunsdkcore.request import CommonRequest + import random def sent_sms_code(phone): - pass + client = AcsClient('LTAIBfgqfC2fpIDF', 'ocBzC2UvguYbyR6coNGYdPiV5HdWbC', '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', "13993199566") + request.add_query_param('SignName', "短信验证") + request.add_query_param('TemplateCode', "SMS_12330409") + request.add_query_param('TemplateParam', '{"number":"1111"}') + + response = client.do_action(request) + print(type(response.decode('utf8'))) def generate_code(): - return random.randint(1000, 9999) \ No newline at end of file + return random.randint(1000, 9999) + + +if __name__ == '__main__': + sent_sms_code('13993199566') \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index a3d3c2b..83224be 100644 --- a/polls/views.py +++ b/polls/views.py @@ -66,8 +66,13 @@ def send_code(request): code = generate_code() now = datetime.datetime.now() after_10mins = now + datetime.timedelta(minutes=10) - VerifyCode.objects.create(code=code, phone=phone, category=category) - return JsonResponse({'status': 'success'}) + response = send_sms_code(phone, code) + result = response.decode('utf8') + if "OK" in result: + VerifyCode.objects.create(code=code, phone=phone, category=category) + return JsonResponse({'status': 'success'}) + return JsonResponse({'status': 'error', 'message': '验证码发送失败'}) + @csrf_exempt diff --git a/requirements.txt b/requirements.txt index 13355e8..9ecb5e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ django-bootstrap3 django-simple-captcha psycopg2-binary django-cors-headers +aliyun-python-sdk-core-v3 +