2018-07-04-django-21-发送短信

发送短信

1 视图函数vies.py

import json
from string import printable
from django.views.generic import View
from random import choice
from utils.dysms_python.demo_sms_send import send_sms

def sms_captcha(request):
    """04短信验证码"""
    telephone = request.GET.get('telephone')


    #生成随机验证码
    code = ''.join([choice(printable[:62]) for i in range(4)])

    #redis存储随机验证码
    cache.set('sms_captcha',code,timeout=120)
    result = send_sms(telephone,code).decode('utf-8')

    # print(result)
    #{"Message":"OK",
    # "RequestId":"A3AA09E0-528E-4446-AD07-0C2D4258BA4B",
    # "BizId":"454008730675547445^0","Code":"OK"}
    #print(type(result)) #<class 'str'>
    res = json.loads(result)
    # print(type(res)) #<class 'dict'>
    return HttpResponse(res["Message"])
1-1 utils/dysms_python/demo_sms_send.py
# -*- coding: utf-8 -*-
import json
import sys
from importlib import reload
from string import printable

from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
from aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest
from aliyunsdkcore.client import AcsClient
import uuid
from aliyunsdkcore.profile import region_provider
from aliyunsdkcore.http import method_type as MT
from aliyunsdkcore.http import format_type as FT
# import const
from random import choice

"""
短信业务调用接口示例,版本号:v20170525

Created on 2017-06-12

"""
if sys.version_info.major == 2:
    #下面的try是用来设置python2编码的
    try:
        reload(sys)
        sys.setdefaultencoding('utf8')
    except NameError:
        pass
    except Exception as err:
        raise err

# 注意:不要更改
REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"

#直接把const.py里的内容直接考过来
# ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
ACCESS_KEY_ID = "LTAIPJZY3plmWIks"
ACCESS_KEY_SECRET = "dGZGnJEIleNWmNHPQZqPkSaTWS9LVd"

# acs_client = AcsClient(const.ACCESS_KEY_ID, const.ACCESS_KEY_SECRET, REGION)
acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)
region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)

def send_sms(phone_numbers,code):
    business_id = uuid.uuid1()
    sign_name = "马浩浩"
    template_code = 'SMS_138079053'
    print('短信验证码:',code)
    template_param = json.dumps({"code":code})


    smsRequest = SendSmsRequest.SendSmsRequest()
    # 申请的短信模板编码,必填
    smsRequest.set_TemplateCode(template_code)

    # 短信模板变量参数
    if template_param is not None:
        smsRequest.set_TemplateParam(template_param)

    # 设置业务请求流水号,必填。
    smsRequest.set_OutId(business_id)

    # 短信签名
    smsRequest.set_SignName(sign_name)
	
    # 数据提交方式
	# smsRequest.set_method(MT.POST)
	
	# 数据提交格式
    # smsRequest.set_accept_format(FT.JSON)
	
    # 短信发送的号码列表,必填。
    smsRequest.set_PhoneNumbers(phone_numbers)

    # 调用短信发送接口,返回json
    smsResponse = acs_client.do_action_with_exception(smsRequest)

    # TODO 业务处理


    return smsResponse



# if __name__ == '__main__':
#     __business_id = uuid.uuid1()
#     #print(__business_id)
#     # params = "{\"code\":\"12345\",\"product\":\"云通信\"}"
#     params = {
#         'code':1234
#     }
# 	#params = u'{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"}'
#     print(send_sms(__business_id, "136131393592", "马浩浩", "SMS_138079053", json.dumps(params)).decode('utf-8'))
#     print(send_sms('13383153665').decode('utf-8'))

2 xfzauth/urls.py

from django.urls import path
from . import views

app_name = 'account'

urlpatterns = [
    path('login/',views.LoginView.as_view(),name='login'),
    path('register/',views.Register.as_view(),name='register'),
    path('img_captcha/',views.img_captcha,name='img_captcha'),
    path('sms_captcha/',views.sms_captcha,name='sms_captcha'),
]

3 前端js文件

$(function () {
    var smsCaptcha = $('.sms-captcha-btn');
    smsCaptcha.click(send_sms);


    function send_sms() {
        var telephone = $("input[name='telephone']").val();
        console.log(telephone);
        $.get({
            'url': '/account/sms_captcha/',
            'data': {'telephone': telephone},
            'success': function (res) {
                var count = 60;
                //设置不能点击
                //给span标签添加class
                smsCaptcha.addClass('disabled');
                //移除span标签点击事件
                smsCaptcha.unbind('click');

                var timer = setInterval(function () {
                    smsCaptcha.text(count);
                    count--;
                    if (count <= 0) {
                        smsCaptcha.bind("click", send_sms);
                        smsCaptcha.removeClass('disabled');
                        clearInterval(timer);
                        smsCaptcha.text('发送验证码');

                    }
                }, 1000);
            },
            'fail': function (error) {
                console.log(error);
            }
        });
    }
})

猜你喜欢

转载自blog.csdn.net/github_38970218/article/details/88616399