短信扩展

目前已有一号通短信、阿里云短信、腾讯云短信扩展

扩展入口文件

文件目录 crmeb\services\sms\Sms.php 默认驱动类型为:一号通


namespace crmeb\services\sms;

use crmeb\basic\BaseManager;
use crmeb\services\AccessTokenServeService;
use crmeb\services\sms\storage\yihaotong;
use think\Container;
use think\facade\Config;


/**
 * Class Sms1
 * @package crmeb\services\sms
 * @mixin yihaotong
 */
class Sms extends BaseManager
{
    //...
}

扩展配置文件

文件目录config/sms.php
stores 内的配置可以填写也可以留空,留空的话,需要在调取服务的时候从其他地方获取;

return [
    //默认扩展
    'default' => 'yihaotong',
    //单个手机每日发送上限
    'maxPhoneCount' => 10,
    //验证码每分钟发送上线
    'maxMinuteCount' => 20,
    //单个IP每日发送上限
    'maxIpCount' => 50,
    //驱动模式
    'stores' => [
        //一号通
        'yihaotong' => [
            'sms_account' => '',
            'sms_token' => ''
        ],
        //阿里云
        'aliyun' => [
            'sign_name' => '',
            'aliyun_AccessKeyId' => '',
            'aliyun_AccessKeySecret' => '',
            'aliyun_RegionId' => '',
        ],
        //腾讯云
        'tencent' => [
            'tencent_sms_app_id' => '',
            'tencent_sms_secret_id' => '',
            'tencent_sms_secret_key' => '',
            'tencent_sms_sign_name' => '',
            'tencent_sms_region' => '',
        ]
    ]
];

使用短信扩展

use crmeb\services\sms\Sms;

$sms = new Sms();

$phone = '手机号';
$templateId = '短信模板id';
$data  = [];//短信模板变量内容

$res = $sms->send($phone, $templateId, $data = []);

if (false === $res) {
    var_dump($sms=>getError());
} else {
    //发送成功
}

切换其他扩展发送

use crmeb\services\sms\Sms;

//使用腾讯云短信发送
$sms = new Sms('tencent',[
    'tencent_sms_app_id'=>'应用id',
    'tencent_sms_secret_id'=>'secret_id',
    'tencent_sms_secret_key'=>'secret_key',
    'tencent_sms_sign_name'=>'签名',
    'tencent_sms_region'=>'地域',
]);

$phone = '手机号';
$templateId = '短信模板id';
$data  = [];//短信模板变量内容

$res = $sms->send($phone, $templateId, $data = []);

if (false === $res) {
    var_dump($sms=>getError());
} else {
    //发送成功
}

使用内置服务发送

use app\services\serve\ServeServices;

/** @var ServeServices  $serve */
$serve = app()->make(ServeServices::class);

//从数据库获取用户当前发送的扩展方式
$type = sys_config('sms_type', null);

$phone = '手机号';
$templateId = '短信模板id';
$data  = [];//短信模板变量内容

//发送短信
//sms()方法内部自动从配置获取参数名,根据配置情况获取配置参数
$res = $serve->sms($type)->send($phone, $templateId, $data);

if (false === $res) {
    var_dump($sms=>getError());
} else {
    //发送成功
}

举例增加:创蓝短信发送扩展

增加文件 crmeb\services\storage\Chuanglan.php
可以根据需求完成以下方法内的逻辑 send方法是必须要完成的

namespace crmeb\services\sms\storage;

use crmeb\services\sms\BaseSms;
use crmeb\exceptions\AdminException;
use think\facade\Config;


/**
 * Class Chuanglan
 * @package crmeb\services\sms\storage
 */
class Chuanglan extends BaseSms
{

    //初始化
    protected function initialize(array $config = []){};

    //开通服务
    public function open(){};

    //修改签名
    public function modify(string $sign = null, string $phone, string $code){};

    //获取用户信息
    public function info(){};

    //获取短信模板
    public function temps(int $page = 0, int $limit = 10, int $type = 1){};

    //申请模版
    public function apply(string $title, string $content, int $type){};

    //申请模板记录
    public function applys(int $tempType, int $page, int $limit){};

    //f发送短信
    public function send(string $phone, string $templateId, array $data = []){};

    //发送记录
    public function record($record_id){};

}

使用创蓝短信扩展发送短信

use crmeb\services\sms\Sms;

//使用创蓝驱动
$sms = new Sms('chuanglan');

$phone = '手机号';
$templateId = '短信模板id';
$data  = [];//短信模板变量内容

$res = $sms->send($phone, $templateId, $data = []);

if (false === $res) {
    var_dump($sms=>getError());
} else {
    //发送成功
}