初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"files":[],"license":"regular","licenseto":"7093","licensekey":"1Pr5yo6bOhNQ0Dgp ePtbL48xlJVd2b5qJeYcVQ==","domains":[],"licensecodes":[],"validations":[]}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace addons\hwsms;
|
||||
|
||||
use think\Addons;
|
||||
|
||||
/**
|
||||
* 华为云短信
|
||||
*/
|
||||
class Hwsms 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 = \app\common\model\config\System::getConfig('hwsms');
|
||||
$hwsms = new \addons\hwsms\library\Hwsms();
|
||||
$result = $hwsms->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 = \app\common\model\config\System::getConfig('alisms');
|
||||
$hwsms = \addons\hwsms\library\Hwsms::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 = $hwsms->mobile($params['mobile'])
|
||||
->template($params['template'])
|
||||
->param($param)
|
||||
->send();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测验证是否正确
|
||||
* @param $params
|
||||
* @return boolean
|
||||
*/
|
||||
public function smsCheck(&$params)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
[
|
||||
'name' => 'app_url',
|
||||
'title' => 'APP接入地址',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => 'https://smsapi.cn-north-4.myhuaweicloud.com:443',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '登录管理控制台,从国内短信“应用管理”或国际/港澳台短信“应用管理”页面获取。',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => 'key',
|
||||
'title' => '应用key',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => '23',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => 'secret',
|
||||
'title' => '密钥secret',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => '',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => 'sender',
|
||||
'title' => '发送者',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => '',
|
||||
'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' => [
|
||||
'register' => '8ff55xxxxxxxxxxxxxxxxxxx',
|
||||
'resetpwd' => '8ff55xxxxxxxxxxxxxxxxxxx',
|
||||
'changepwd' => '8ff55xxxxxxxxxxxxxxxxxxx',
|
||||
'changemobile' => '8ff55xxxxxxxxxxxxxxxxxxx',
|
||||
'profile' => '8ff55xxxxxxxxxxxxxxxxxxx',
|
||||
'mobilelogin' => '8ff55xxxxxxxxxxxxxxxxxxx',
|
||||
'bind' => '8ff55xxxxxxxxxxxxxxxxxxx',
|
||||
],
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
[
|
||||
'name' => '__tips__',
|
||||
'title' => '温馨提示',
|
||||
'type' => 'string',
|
||||
'content' => [],
|
||||
'value' => 'APP接入地址、应用key和secret可以通过登录管理控制台,从国内短信“应用管理”或国际/港澳台短信“应用管理”页面获取。',
|
||||
'rule' => 'required',
|
||||
'msg' => '',
|
||||
'tip' => '',
|
||||
'ok' => '',
|
||||
'extend' => '',
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace addons\hwsms\controller;
|
||||
|
||||
use think\addons\Controller;
|
||||
|
||||
/**
|
||||
* 华为云短信
|
||||
*/
|
||||
class Index extends Controller
|
||||
{
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->error("当前插件暂无前台页面");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
name = hwsms
|
||||
title = 华为云短信插件
|
||||
intro = 华为云短信发送插件
|
||||
author = FastAdmin
|
||||
website = https://www.fastadmin.net
|
||||
version = 1.0.1
|
||||
state = 1
|
||||
url = addons/hwsms
|
||||
license = regular
|
||||
licenseto = 7093
|
||||
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
namespace addons\hwsms\library;
|
||||
|
||||
use fast\Http;
|
||||
|
||||
/**
|
||||
* 华为云短信类
|
||||
*/
|
||||
class Hwsms
|
||||
{
|
||||
private $_params = [];
|
||||
public $error = '';
|
||||
protected $config = [];
|
||||
protected static $instance;
|
||||
|
||||
public function __construct($options = [])
|
||||
{
|
||||
if ($config = \app\common\model\config\System::getConfig('hwsms')) {
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
$this->config = array_merge($this->config, is_array($options) ? $options : []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单例
|
||||
* @param array $options 参数
|
||||
* @return Hwsms
|
||||
*/
|
||||
public static function instance($options = [])
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new static($options);
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置签名
|
||||
* @param string $sign
|
||||
* @return Hwsms
|
||||
*/
|
||||
public function sign($sign = '')
|
||||
{
|
||||
$this->_params['signature'] = $sign;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
* @param array $param
|
||||
* @return Hwsms
|
||||
*/
|
||||
public function param(array $param = [])
|
||||
{
|
||||
foreach ($param as $k => &$v) {
|
||||
$v = (string)$v;
|
||||
}
|
||||
unset($v);
|
||||
if ($param) {
|
||||
$this->_params['templateParas'] = json_encode(array_values($param));
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模板
|
||||
* @param string $code 短信模板
|
||||
* @return Hwsms
|
||||
*/
|
||||
public function template($code = '')
|
||||
{
|
||||
$this->_params['templateId'] = $code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收手机
|
||||
* @param string $mobile 手机号码
|
||||
* @return Hwsms
|
||||
*/
|
||||
public function mobile($mobile = '')
|
||||
{
|
||||
$this->_params['to'] = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送方
|
||||
* @param string $code 国内短信填写为短信平台为短信签名分配的通道号码
|
||||
* @return Hwsms
|
||||
*/
|
||||
public function from($code = '')
|
||||
{
|
||||
$this->_params['from'] = $code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 立即发送
|
||||
* @return boolean
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$this->error = '';
|
||||
$params = $this->_params();
|
||||
$headers = [
|
||||
'Content-Type: application/x-www-form-urlencoded',
|
||||
'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
|
||||
'X-WSSE: ' . $this->buildWsseHeader()
|
||||
];
|
||||
$data = http_build_query($params);
|
||||
$context_options = [
|
||||
'http' => ['method' => 'POST', 'header'=> $headers, 'content' => $data, 'ignore_errors' => true],
|
||||
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false] //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
|
||||
];
|
||||
|
||||
$response = file_get_contents($this->config['app_url'].'/sms/batchSendSms/v1', false, stream_context_create($context_options));
|
||||
$ret = json_decode($response);
|
||||
|
||||
if ($ret->result) {
|
||||
if (isset($ret->code) && $ret->code == '000000') {
|
||||
return true;
|
||||
}
|
||||
$this->error = isset($res->code) ? $res->code : 'Invalid result';
|
||||
} else {
|
||||
$this->error = 'Network error';
|
||||
}
|
||||
|
||||
if (config('app_debug')) {
|
||||
\think\Log::record('hwsms:' . $this->error);
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
* @return string
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取发送的参数信息
|
||||
* @return array
|
||||
*/
|
||||
private function _params()
|
||||
{
|
||||
$params = array_merge([
|
||||
'signature' => isset($this->config['sign']) ? $this->config['sign'] : '',
|
||||
'from' => isset($this->config['sender']) ? $this->config['sender'] : '',
|
||||
], $this->_params);
|
||||
if (isset($params['to']) && $params['to']) {
|
||||
$toArr = explode(',', $params['to']);
|
||||
foreach ($toArr as $index => &$item) {
|
||||
if (substr($item, 0, 1) !== '+') {
|
||||
$item = '+86' . $item;
|
||||
}
|
||||
}
|
||||
$params['to'] = implode(',', $toArr);
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造X-WSSE参数值
|
||||
* @return string
|
||||
*/
|
||||
function buildWsseHeader()
|
||||
{
|
||||
$now = date('Y-m-d\TH:i:s\Z'); //Created
|
||||
$nonce = uniqid(); //Nonce
|
||||
$base64 = base64_encode(hash('sha256', ($nonce . $now . $this->config['secret'])));
|
||||
return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"", $this->config['key'], $base64, $nonce, $now);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user