update
This commit is contained in:
1
addons/alisms/.addonrc
Normal file
1
addons/alisms/.addonrc
Normal file
@@ -0,0 +1 @@
|
||||
{"files":[],"license":"extended","licenseto":"10836","licensekey":"jxn5E1hW0TPR2yGK fdqVjV6k9iz5iqRCHoaYCQ==","domains":["localhost"],"licensecodes":[],"validations":["757319b447175b6ca1882635b132a594"]}
|
||||
83
addons/alisms/Alisms.php
Normal file
83
addons/alisms/Alisms.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace addons\alisms;
|
||||
|
||||
use think\Addons;
|
||||
|
||||
/**
|
||||
* Alisms
|
||||
*/
|
||||
class Alisms extends Addons
|
||||
{
|
||||
|
||||
/**
|
||||
* 插件安装方法
|
||||
* @return bool
|
||||
*/
|
||||
public function install()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件卸载方法
|
||||
* @return bool
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信发送行为
|
||||
* @param array $params 必须包含mobile,event,code
|
||||
* @return boolean
|
||||
*/
|
||||
public function smsSend(&$params)
|
||||
{
|
||||
$config = get_addon_config('alisms');
|
||||
$alisms = new \addons\alisms\library\Alisms();
|
||||
$result = $alisms->mobile($params['mobile'])
|
||||
->template($config['template'][$params['event']])
|
||||
->param(['code' => $params['code']])
|
||||
->send();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信发送通知
|
||||
* @param array $params 必须包含 mobile,event,msg
|
||||
* @return boolean
|
||||
*/
|
||||
public function smsNotice(&$params)
|
||||
{
|
||||
$config = get_addon_config('alisms');
|
||||
$alisms = \addons\alisms\library\Alisms::instance();
|
||||
if (isset($params['msg'])) {
|
||||
if (is_array($params['msg'])) {
|
||||
$param = $params['msg'];
|
||||
} else {
|
||||
parse_str($params['msg'], $param);
|
||||
}
|
||||
} else {
|
||||
$param = [];
|
||||
}
|
||||
$param = $param ? $param : [];
|
||||
$params['template'] = isset($params['template']) ? $params['template'] : (isset($params['event']) && isset($config['template'][$params['event']]) ? $config['template'][$params['event']] : '');
|
||||
$result = $alisms->mobile($params['mobile'])
|
||||
->template($params['template'])
|
||||
->param($param)
|
||||
->send();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测验证是否正确
|
||||
* @param $params
|
||||
* @return boolean
|
||||
*/
|
||||
public function smsCheck(&$params)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
66
addons/alisms/config.php
Normal file
66
addons/alisms/config.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
[
|
||||
'name' => 'key',
|
||||
'title' => '应用key',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => 'LTAI5t6oEsk1iPSXenJfDAxY',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => 'secret',
|
||||
'title' => '密钥secret',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => 'tvbeWgGITSWFaen2OAoolITz3t6UWR',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => 'sign',
|
||||
'title' => '签名',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => '域展科技',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => 'template',
|
||||
'title' => '短信模板',
|
||||
'type' => 'array',
|
||||
'content' => [],
|
||||
'value' => [
|
||||
'adminlogin' => 'SMS_176560077',
|
||||
],
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => '__tips__',
|
||||
'title' => '温馨提示',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => '应用key和密钥你可以通过 https://ak-console.aliyun.com/?spm=a2c4g.11186623.2.13.fd315777PX3tjy#/accesskey 获取',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
];
|
||||
52
addons/alisms/controller/Index.php
Normal file
52
addons/alisms/controller/Index.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace addons\alisms\controller;
|
||||
|
||||
use think\addons\Controller;
|
||||
|
||||
/**
|
||||
* 阿里短信
|
||||
*/
|
||||
class Index extends Controller
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
if (!\app\admin\library\Auth::instance()->id) {
|
||||
$this->error('暂无权限浏览');
|
||||
}
|
||||
parent::_initialize();
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
$config = get_addon_config('alisms');
|
||||
$mobile = $this->request->post('mobile');
|
||||
$template = $this->request->post('template');
|
||||
$sign = $this->request->post('sign');
|
||||
if (!$mobile || !$template) {
|
||||
$this->error('手机号、模板ID不能为空');
|
||||
}
|
||||
$sign = $sign ? $sign : $config['sign'];
|
||||
$param = (array)json_decode($this->request->post('param', '', 'trim'));
|
||||
$alisms = new \addons\alisms\library\Alisms();
|
||||
$ret = $alisms->mobile($mobile)
|
||||
->template($template)
|
||||
->sign($sign)
|
||||
->param($param)
|
||||
->send();
|
||||
if ($ret) {
|
||||
$this->success("发送成功");
|
||||
} else {
|
||||
$this->error("发送失败!失败原因:" . $alisms->getError());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
10
addons/alisms/info.ini
Normal file
10
addons/alisms/info.ini
Normal file
@@ -0,0 +1,10 @@
|
||||
name = alisms
|
||||
title = 阿里云短信发送
|
||||
intro = 阿里云短信发送插件
|
||||
author = FastAdmin
|
||||
website = https://www.fastadmin.net
|
||||
version = 1.0.9
|
||||
state = 1
|
||||
url = /putoufy/public/addons/alisms
|
||||
license = extended
|
||||
licenseto = 10836
|
||||
170
addons/alisms/library/Alisms.php
Normal file
170
addons/alisms/library/Alisms.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
namespace addons\alisms\library;
|
||||
|
||||
/**
|
||||
* 阿里大于SMS短信发送
|
||||
*/
|
||||
class Alisms
|
||||
{
|
||||
private $_params = [];
|
||||
public $error = '';
|
||||
protected $config = [];
|
||||
protected static $instance;
|
||||
|
||||
public function __construct($options = [])
|
||||
{
|
||||
if ($config = get_addon_config('alisms')) {
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
$this->config = array_merge($this->config, is_array($options) ? $options : []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单例
|
||||
* @param array $options 参数
|
||||
* @return Alisms
|
||||
*/
|
||||
public static function instance($options = [])
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new static($options);
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置签名
|
||||
* @param string $sign
|
||||
* @return Alisms
|
||||
*/
|
||||
public function sign($sign = '')
|
||||
{
|
||||
$this->_params['SignName'] = $sign;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
* @param array $param
|
||||
* @return Alisms
|
||||
*/
|
||||
public function param(array $param = [])
|
||||
{
|
||||
foreach ($param as $k => &$v) {
|
||||
$v = (string)$v;
|
||||
}
|
||||
unset($v);
|
||||
$param = array_filter($param);
|
||||
$this->_params['TemplateParam'] = $param ? json_encode($param) : '{}';
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模板
|
||||
* @param string $code 短信模板
|
||||
* @return Alisms
|
||||
*/
|
||||
public function template($code = '')
|
||||
{
|
||||
$this->_params['TemplateCode'] = $code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收手机
|
||||
* @param string $mobile 手机号码
|
||||
* @return Alisms
|
||||
*/
|
||||
public function mobile($mobile = '')
|
||||
{
|
||||
$this->_params['PhoneNumbers'] = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 立即发送
|
||||
* @return boolean
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$this->error = '';
|
||||
$params = $this->_params();
|
||||
$params['Signature'] = $this->_signed($params);
|
||||
$response = $this->_curl($params);
|
||||
if ($response !== false) {
|
||||
$res = (array)json_decode($response, true);
|
||||
if (isset($res['Code']) && $res['Code'] == 'OK') {
|
||||
return true;
|
||||
}
|
||||
$this->error = isset($res['Message']) ? $res['Message'] : 'InvalidResult';
|
||||
} else {
|
||||
$this->error = 'InvalidResult';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
* @return string
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
private function _params()
|
||||
{
|
||||
return array_merge([
|
||||
'AccessKeyId' => $this->config['key'],
|
||||
'SignName' => isset($this->config['sign']) ? $this->config['sign'] : '',
|
||||
'Action' => 'SendSms',
|
||||
'Format' => 'JSON',
|
||||
'Version' => '2017-05-25',
|
||||
'SignatureVersion' => '1.0',
|
||||
'SignatureMethod' => 'HMAC-SHA1',
|
||||
'SignatureNonce' => uniqid(),
|
||||
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
|
||||
], $this->_params);
|
||||
}
|
||||
|
||||
private function percentEncode($string)
|
||||
{
|
||||
$string = urlencode($string);
|
||||
$string = preg_replace('/\+/', '%20', $string);
|
||||
$string = preg_replace('/\*/', '%2A', $string);
|
||||
$string = preg_replace('/%7E/', '~', $string);
|
||||
return $string;
|
||||
}
|
||||
|
||||
private function _signed($params)
|
||||
{
|
||||
$sign = $this->config['secret'];
|
||||
ksort($params);
|
||||
$canonicalizedQueryString = '';
|
||||
foreach ($params as $key => $value) {
|
||||
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
|
||||
}
|
||||
$stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
|
||||
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $sign . '&', true));
|
||||
return $signature;
|
||||
}
|
||||
|
||||
private function _curl($params)
|
||||
{
|
||||
$uri = 'http://dysmsapi.aliyuncs.com/?' . http_build_query($params);
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_URL, $uri);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36");
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
$reponse = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return $reponse;
|
||||
}
|
||||
}
|
||||
75
addons/alisms/view/index/index.html
Normal file
75
addons/alisms/view/index/index.html
Normal file
@@ -0,0 +1,75 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
|
||||
<title>阿里云通信短信发送示例 - FastAdmin</title>
|
||||
|
||||
<!-- Bootstrap Core CSS -->
|
||||
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
|
||||
|
||||
<!-- Custom CSS -->
|
||||
<link href="__CDN__/assets/css/frontend.css" rel="stylesheet">
|
||||
|
||||
<!-- Plugin CSS -->
|
||||
<link href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
|
||||
<link href="https://cdn.staticfile.org/simple-line-icons/2.4.1/css/simple-line-icons.min.css" rel="stylesheet">
|
||||
|
||||
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
|
||||
<!--[if lt IE 9]>
|
||||
<script src="https://cdn.staticfile.org/html5shiv/3.7.3/html5shiv.min.js"></script>
|
||||
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="well" style="margin-top:30px;">
|
||||
<form class="form-horizontal" action="{:addon_url('alisms/index/send')}" method="POST">
|
||||
<fieldset>
|
||||
<legend>阿里云通信短信发送</legend>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">手机号</label>
|
||||
<div class="col-lg-10">
|
||||
<input type="text" class="form-control" name="mobile" placeholder="手机号">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">消息模板ID</label>
|
||||
<div class="col-lg-10">
|
||||
<input type="text" class="form-control" name="template" placeholder="消息模板ID,从阿里云通信获得,通常是:SMS_114000000这种格式">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">签名</label>
|
||||
<div class="col-lg-10">
|
||||
<input type="text" class="form-control" name="sign" placeholder="消息模板(可以留空,留空使用后台插件管理中的配置)">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">模板变量参数</label>
|
||||
<div class="col-lg-10">
|
||||
<textarea name="param" class="form-control" cols="30" rows="10" placeholder='必须是JSON字符串,如{"name":"李明"}'></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-lg-10 col-lg-offset-2">
|
||||
<button type="submit" class="btn btn-primary">发送</button>
|
||||
<button type="reset" class="btn btn-default">重置</button>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<!-- jQuery -->
|
||||
<script src="https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js"></script>
|
||||
|
||||
<!-- Bootstrap Core JavaScript -->
|
||||
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
$(function () {
|
||||
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user