一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

时间:2021-09-28 14:12:42

阿里大于短信平台:https://dysms.console.aliyun.com/dysms.htm#/overview

使用教程:https://blog.csdn.net/qq1031893936/article/details/81173878

控制台-->短信服务

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

进去后开通服务,点击添加签名

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

根据自己的情况设置

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

审核通过后,添加模板

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

添加对应的内容

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

需要用到的两个数据:签名名称、模板CODE

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

创建AccessKey

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

实现调接口发验证码:由于使用这个验证码需要购买套餐,所以这里就只封装发短信的模块就行

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

# 阿里大于相关配置
ALIDAYU_APP_KEY = 'LTAI4FmvBxyEb5Sip5b4PbZo' # AccessKey
ALIDAYU_APP_SECRET = '25zBQkqQNXjzwVDHH4m2I6qyoZlxOp' # AccessKeySecret
ALIDAYU_SIGN_NAME = 'cms' # 签名名称
ALIDAYU_TEMPLATE_CODE = 'SMS_178771780' # 模板id

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

# 阿里大于短信验证码sdk
import hashlib
from time import time
import logging
import requests class AlidayuAPI(object):
APP_KEY_FIELD = 'ALIDAYU_APP_KEY'
APP_SECRET_FIELD = 'ALIDAYU_APP_SECRET'
SMS_SIGN_NAME_FIELD = 'ALIDAYU_SIGN_NAME'
SMS_TEMPLATE_CODE_FIELD = 'ALIDAYU_TEMPLATE_CODE' def __init__(self, app=None):
self.url = 'https://eco.taobao.com/router/rest'
self.headers = {
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
"Cache-Control": "no-cache",
"Connection": "Keep-Alive",
}
if app:
self.init_app(app) def init_app(self, app):
config = app.config
try:
self.key = config[self.APP_KEY_FIELD]
self.secret = config[self.APP_SECRET_FIELD]
self.sign_name = config[self.SMS_SIGN_NAME_FIELD]
self.api_params = {
'sms_free_sign_name': config[self.SMS_SIGN_NAME_FIELD],
'sms_template_code': config[self.SMS_TEMPLATE_CODE_FIELD],
'extend': '',
'sms_type': "normal",
"method": "alibaba.aliqin.fc.sms.num.send",
"app_key": self.key,
"format": "json",
"v": "2.0",
"partner_id": "",
"sign_method": "md5",
}
except Exception as e:
logging.error(e.args)
raise ValueError('请填写正确的阿里大鱼配置!') def send_sms(self, telephone, **params):
self.api_params['timestamp'] = str(int(time() * 1000))
self.api_params['sms_param'] = str(params)
self.api_params['rec_num'] = telephone newparams = "".join(["%s%s" % (k, v) for k, v in sorted(self.api_params.items())])
newparams = self.secret + newparams + self.secret
sign = hashlib.md5(newparams.encode("utf-8")).hexdigest().upper()
self.api_params['sign'] = sign resp = requests.post(self.url, params=self.api_params, headers=self.headers)
data = resp.json()
try:
result = data['alibaba_aliqin_fc_sms_num_send_response']['result']['success']
return result
except:
print('=' * 10)
print("阿里大于错误信息:", data)
print('=' * 10)
return False

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
from utils.alidayu import AlidayuAPI db = SQLAlchemy()
mail = Mail()
alidayu = AlidayuAPI()

一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

from flask import Flask
from apps.cms import bp as cms_bp
from apps.front import bp as front_bp
from apps.common import bp as common_bp
import config
from exts import db, mail, alidayu
from flask_wtf import CSRFProtect def create_app():
app = Flask(__name__)
app.config.from_object(config) app.register_blueprint(cms_bp)
app.register_blueprint(front_bp)
app.register_blueprint(common_bp) db.init_app(app)
mail.init_app(app)
alidayu.init_app(app)
CSRFProtect(app) return app if __name__ == '__main__':
app = create_app()
app.run(port=8000)