git地址
扩展移动云的模板发送短信
<?php
require_once dirname(__DIR__).'/vendor/autoload.php';
use GuzzleHttp\Exception\GuzzleException;
use Overtrue\EasySms\Contracts\GatewayInterface;
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Gateways\Gateway;
use Overtrue\EasySms\Support\Config;
use Psr\Http\Message\ResponseInterface;
class YidongmasblackTemplateGateway extends Gateway
{
public const ENDPOINT_URL = 'http://112.33.46.17:37891/sms/tmpsubmit';
public const ENDPOINT_METHOD = 'send';
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config): ResponseInterface
{
$params['ecName'] = $config->get('ecName');
$params['apId'] = $config->get('apId');
$params['secretKey'] = $config->get('secretKey');
$params['sign'] = $config->get('sign');
$params['addSerial'] = $config->get('addSerial');
$params['mobiles'] = $to->getNumber();
$params['templateId'] = $message->getTemplate();
$params['params'] = json_encode(array_values($message->getData()), JSON_UNESCAPED_UNICODE);
$params['mac'] = $this->makeMacString($config, $to->getNumber(), array_values($message->getData()), $message->getTemplate());
$clientFactory = new ClientFactory(container());
$client = $clientFactory->create();
$response = $client->post(self::ENDPOINT_URL, [
"body" => base64_encode(json_encode($params, JSON_UNESCAPED_UNICODE))
]);
$result = json_decode($response->getBody(), true);
if ('true' != $result['success']) {
throw new GatewayErrorException($result['success'], $result['rspcod'], $result);
}
return $response;
}
public function makeMacString(Config $config, string $mobiles, array $params, string $templateId): string
{
$macstr = $config->get('ecName') . $config->get('apId') . $config->get('secretKey') . $templateId . trim($mobiles) . json_encode($params, JSON_UNESCAPED_UNICODE) . $config->get('sign') . $config->get('addSerial');
return strtolower(md5($macstr));
}
public function getName(): string
{
return 'yidongmasblacktemplate';
}
}
namespace common;
use Overtrue\EasySms\Strategies\OrderStrategy;
class SmsConfig
{
public static array $config = [
'timeout' => 5.0,
'default' => [
'strategy' => OrderStrategy::class,
'gateways' => [
'yidongmasblacktemplate'
],
],
'gateways' => [
'errorlog' => [
'file' => '/tmp/easy-sms.log',
],
'yidongmasblacktemplate' => [
'ecName' => '',
'secretKey' => '',
'apId' => '',
'sign' => '',
'addSerial' => '',
],
]
];
}
class MasTemplate
{
public static string $REGISTER = '';
public static string $EVENT_FALLBACK = '';
public static string $UPDATE_INFO = '';
}
$easySms = new EasySms(SmsConfig::$config);
$easySms->extend('yidongmasblacktemplate', function ($gatewayConfig) {
return new YidongmasblackTemplateGateway($gatewayConfig);
});
$easySms->send($mobile, new Message([
'template' => $templateId,
'data' => $data
]), [$gatewayName]);