Easy SMS短信发送

git地址

扩展移动云的模板发送短信

<?php

/*
 * This file is part of the overtrue/easy-sms.
 *
 * (c) overtrue <i@overtrue.me>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

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 YidongmasblackGateway.
 * 移动MAS黑名单模式(模板模式).
 *
 * @author houang <mail@houang.cn>
 *
 * @see https://mas.10086.cn
 */
class YidongmasblackTemplateGateway extends Gateway
{
    public const ENDPOINT_URL = 'http://112.33.46.17:37891/sms/tmpsubmit';

    public const ENDPOINT_METHOD = 'send';

    /**
     * @param PhoneNumberInterface $to
     * @param MessageInterface $message
     * @param Config $config
     * @return ResponseInterface
     *
     * @throws GatewayErrorException
     * @throws GuzzleException
     */
    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());

		// 我这个事基于swoole的hyperf里面使用,所以用了协程版的guzzle
		$clientFactory = new ClientFactory(container());
        $client = $clientFactory->create();
		/*
			如果不是swoole可以使用use \Overtrue\EasySms\Traits\HasHttpRequest;里面方法
		*/
        
        $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;
    }

    /**
     * 构建mac字符串
     * @param Config $config
     * @param string $mobiles 手机号(逗号隔开)
     * @param array $params 参数
     * @param string $templateId 模板id
     * @return string
     * @author 张志强
     * @date 2025-03-17
     */
    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 = [
        // HTTP 请求的超时时间(秒)
        'timeout' => 5.0,

        // 默认发送配置
        'default' => [
            // 网关调用策略,默认:顺序调用
            'strategy' => OrderStrategy::class,
            // 默认可用的发送网关
            'gateways' => [
                'yidongmasblacktemplate'
            ],
        ],
        // 可用的网关配置
        'gateways' => [
            'errorlog' => [
                'file' => '/tmp/easy-sms.log',
            ],
            'yidongmasblacktemplate' => [
                'ecName' => '', // 机构名称
                'secretKey' => '', // 密钥
                'apId' => '', // 应用ID
                'sign' => '', // 签名
                'addSerial' => '', // 通道号默认空
            ],
        ]
    ];
}
// 模板文件
class MasTemplate
{
    /**
     * 注册
     * @var string
     */
    public static string $REGISTER = '';
    /**
     * 事件退回
     * @var string
     */
    public static string $EVENT_FALLBACK = '';
    /**
     * 修改信息
     * @var string
     */
    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]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值