其他分享
首页 > 其他分享> > 突然间想起来好像写过调用阿里短信接口

突然间想起来好像写过调用阿里短信接口

作者:互联网

# 短信验证码
# 其实吧 还是看阿里上面那个直接有模板copy下来改下 写写逻辑就好了。。。

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
# 调用
def note(request):
response = {'user': None, 'msg': None}
if request.method == 'GET':
return render(request,'user/get_note_number.html')
if request.method == 'POST':
number = request.POST.get('number')
print(number)
number_note = request.POST.get('count')
if not number_note and number :
count = random_number()
print(count)
client = AcsClient('LTAIqXneegi8yG2A', 'sZjLNbG8Bk3XNOv3Uvq48u7b8sKmUh', 'default')
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('TemplateCode', 'xxxxxx')
request.add_query_param('SignName', 'xxxxxx')
request.add_query_param('PhoneNumbers', number)
request.add_query_param('TemplateParam', '{"code": %s}'%count)
response = client.do_action(request)
# python2: print(response)
print(str(response, encoding = 'utf-8'))
response = JsonResponse(str(response, encoding = 'utf-8'), safe=False)
response.set_cookie('my_cookie',count)
return response
count = request.COOKIES['my_cookie']
if number_note == str(count):
pwd = request.POST.get('pwd')
print(pwd)
user_dic = models.UserInfo.objects.filter(telephone=int(number)).first()
name = user_dic.username
user = auth.authenticate(request,username=name,password=pwd)
if user:
print(user)
auth.login(request, user)
response['user'] = name
response['msg'] = '登录成功'
user_id = models.UserInfo.objects.filter(username=str(name)).values('nid')[0]['nid']
models.UserLog.objects.create(user_id_id=str(user_id))
return redirect('/user/index/')
else:
response['msg'] = '用户名或者密码错误'
return JsonResponse(response)
else:
response['msg'] = '验证码错误'
return JsonResponse(response)
else:
return render(request,'user/404.html')

标签:count,调用,request,number,接口,set,写过,response,user
来源: https://www.cnblogs.com/yanhui1995/p/10896038.html