dongdavid/notify

消息通知发送组件

2.0.1 2021-03-10 13:57 UTC

This package is auto-updated.

Last update: 2024-09-13 10:14:04 UTC


README

Build Status StyleCI Scrutinizer Code Quality Total Downloads License

function offical($account)
{
    $data = [
        'access_token'=>$account['wechatoffical']['token'],
        'openid'=>$account['wechatoffical']['openid'],
        'template_id'=>$account['wechatoffical']['template_id'],
        'data'=>[
            'first'=>['value'=>'first is me'],
            'keyword1'=>['value'=>'哈哈哈'],
            'keyword2'=>['value'=>number_format(23.32,2)],
            'remark'=>['value'=>'备注信息'],
        ],

    ];


    $d = QuickSend::offical($data['access_token'],$data['openid'],$data['template_id'],$data['data']);
    var_dump($d);
}
function mini($account)
{
    $data = [
        'access_token'=>$account['miniprogram']['token'],
        'openid'=>$account['miniprogram']['openid'],
        'template_id'=>$account['miniprogram']['template_id'],
        'data'=>[
            'thing1'=>['value'=>'first is me'],
            'thing3'=>['value'=>'哈哈哈'],
            'time2'=>['value'=>23.32],
        ],

    ];

    $d = QuickSend::miniProgram($data['access_token'],$data['openid'],$data['template_id'],$data['data']);
    var_dump($d);
}
function email($account)
{
    // to、cc、bcc 可以支持字符串、数组,如果是二维数组, 则子数组必须要有键[email]保存邮箱地址
    // attachments 支持字符串、数组,如果是二维数组,则子数组必须要有键[filepath]来保存文件路径,会自动转换成绝对路径
    // config参考phpmailer,
    $data = [
        'config'=>$account['mail'],
        'subject'=>'测试邮件',
        'body'=>'邮件正文',
        'to'=>[
            'xxx@outlook.com',
        ],
        'cc'=>[
            ['email'=>'xxx@live.com','name'=>'收件人昵称'],
        ],
        'bcc'=>'xxx@qq.com',
        'attachments'=>[
            './aa.md',
            ['filepath'=>'./bb.md','filename'=>'说明文件.txt']
        ],
        //'attachments'=>'./aa.md',
    ];
    $d = QuickSend::mail($data['config'],$data['subject'],$data['body'],$data['to'],$data['attachments'],$data['cc'],$data['bcc']);
    var_dump($d);
}
function alisms($account)
{
    $data = [
        'config'=>[
            'accessKeyId'=>$account['alisms']['accessKeyId'],
            'accessKeySecret'=>$account['alisms']['accessKeySecret'],
            'SignName'=>$account['alisms']['SignName'],
            'template_code'=>$account['alisms']['template_code'],
        ],
        'phone'=>$account['alisms']['phone'],
        'template_param'=>[
            'name'=>'皮皮虾',
            'course_name'=>'如何上天',
        ],
    ];
    $d = QuickSend::alisms($data['config'],$data['phone'],$data['template_param']);
    var_dump($d);
}

需求

  • PHP >= 5.6
  • Redis PHP 扩展
  • guzzlehttp/guzzle ^6.3
  • phpmailer/phpmailer ^ 6.1

安装

$ composer require dongdavid/notify -vvv

使用方法

如使用微信公众号/企业微信/微信小程序消息通知,则需要配置redis缓存access_token

# 修改配置
\Dongdavid\Notify\cache(
    // 默认配置
    'host'     => '127.0.0.1',
    'port'     => '6379',
    'password' => '',
    'select'   => 6,
    'timeout'  => 3,
])

有两种使用方式,一种是直接将配置参数一并传入,另一种是定义config方法来获取配置,传入配置名称

第一种方式

邮件

$mail = [
    'type'   => 'email',
    'config' => [
        'type'       => 'email',
        'signature'  => 'email_tx',
        'host'       => 'smtp.exmail.qq.com',
        'port'       => '465',
        'username'   => 'noreply@xxx.com',
        'password'   => '', //腾讯的要用专用密码登陆
        'SMTPSecure' => 'ssl',
        'fromEmail'  => 'noreply@xxx.com',
        'fromName'   => 'name',
    ],
    'msg'    => [
        'subject'     => '邮件主旨',
        'body'        => '邮件内容',
        'touser'      => [
            [
                'emailAddress' => 'receiver@outlook.com',
                'name'         => '收件人名字',
            ],
        ],
        'cc'          => [],
        'attachments' => [
            './public/robots.txt',
        ],
    ],
];

Notify::send($mail);

企业微信

$qy = [
    'type'   => 'wechatwork',
    'config' => [
        'type'      => 'wechatwork',
        'signature' => 'work_xxx_1000002',
        'appid'     => 'xxx',
        'appsecret' => 'xxxxx',
        'agentid'   => '1000002',
    ],
    'msg'    => [
        'touser'   => '001',
        'msgtype'  => 'textcard',
        'textcard' => [
            'title'       => '测试通知',
            'description' => "我是通知内容\n第二行开始了\n加上一个链接<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
            'url'         => 'https://www.dongdavid.com',
        ],
    ],
];

Notify::send($qy);

微信公众号

$wechat = [
    'type'   => 'wechatoffical',
    'config' => [
        'type'      => 'wechatoffical',
        'signature' => 'offical_xxx',
        'appid'     => 'xxx',
        'appsecret' => 'xxxxx',
    ],
    'msg'    => [
        'touser'      => 'openid',
        'template_id' => 'template_id',
        'url'         => 'https://www.dongdavid.com',
        'miniprogram' => [
            'appid'    => '',
            'pagepath' => '',
        ],
        'data'        => [
            'first'    => [
                'value' => '哈喽,我是first one',
            ],
            'keyword1' => [
                'value' => '我是关键词一号',
                'color' => '#17317',
            ],
            'keyword2' => [
                'value' => '我是关键词二号',
            ],
            'remark'   => [
                'value' => '我是备注了啊',
            ],
        ],
    ],
];
Notify::send($notify);

微信小程序

$wechat = [
    'type'   => 'miniprogram',
    'config' => [
        'type'      => 'miniprogram',
        'signature' => 'mini_xxx',
        'appid'     => 'xxx',
        'appsecret' => 'xxxx',
    ],
    'msg'    => [
        'touser'            => 'oBEIa0cr36R6FTCppAvgKLoKG9FY',
        'template_id'       => '0OHAj375XtCEVwJaASmRv79c4KhlqzN_mtsmNn6qHGg',
        'page'              => '',
        'miniprogram_state' => 'developer', //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
        'data'              => [
            'thing1' => [
                'value' => '哈喽,我是first one',
            ],
            'time2'  => [
                'value' => date('Y-m-d H:i'),
            ],
        ],
    ],
];
Notify::send($notify);

第二种方式

在框架中使用,或者自定义一个config方法来获取配置

helper.php中会使用\Dongdavid\Notify\config('notify_config')来获取所有的消息发送配置,所以需要确保你能够通过config('notify_config')['config_name']来获取到对应的配置

if (!function_exists('config')) {
    function config($name, $value = null)
    {
        if (null !== $value) {
            \Dongdavid\Notify\cache('notify-config', $value);
        } else {
            return \Dongdavid\Notify\cache('notify-config');
        }
    }
} else {
    function config($name, $value = null)
    {
        return \config($name, $value);
    }
}
# 预定义一个config方法用于获取配置
function config($config_name)
{
    $config = [
        'notify_config'=>[
            'work_xxx_1000002' => [
                'type'      => 'wechatwork',
                'signature' => 'work_xxx_1000002',
                'appid'     => 'xxx',
                'appsecret' => 'xxxxx',
                'agentid'   => '1000002',
            ],
            'offical_xxx'      => [
                'type'      => 'wechatoffical',
                'signature' => 'offical_xxx',
                'appid'     => 'xxx',
                'appsecret' => 'xxx',
            ],
            'email_tx'                        => [
                'type'       => 'email',
                'signature'  => 'email_tx',
                'host'       => 'smtp.exmail.qq.com',
                'port'       => '465',
                'username'   => 'noreply@xxx.com',
                'password'   => '', //腾讯的要用专用密码登陆
                'SMTPSecure' => 'ssl',
                'fromEmail'  => 'noreply@xxx.com',
                'fromName'   => 'name',

            ],
            'mini_xxx'         => [
                'type'      => 'miniprogram',
                'signature' => 'mini_xxx',
                'appid'     => 'xxx',
                'appsecret' => 'xxxxx',
            ],
        ]
    ];
    return $config[$config_name];
}
# 动态注入配置 
\Dongdavid\Notify\config('notify_config',$configs);

$data = [
    'type'   => 'email',
    'config' => 'email_tx',
    'msg'    => [
        .
        .
        .
    ],
];

$data = [
    'type'   => 'wechatwork',
    'config' => 'work_xxx_1000002',
    'msg'    => [
        .
        .
        .
    ],
];
$data = [
    'type'   => 'wechatoffical',
    'config' => 'offical_xxx',
    'msg'    => [
        .
        .
        .
    ],
];
$data = [
    'type'   => 'miniprogram',
    'config' => 'mini_xxx',
    'msg'    => [
        .
        .
        .
    ],
];

Notify::send($data);

测试

#本地调试
mkdir notify-test
cd notify-test
composer init  
composer config repositories.notify path ../notify  
composer require dongdavid/notify:dev-master
touch index.php

待办事项

  • 增加单元测试 - 我可能把单元测试写成了功能测试了,我得先去学一下怎么做单元测试😭

示例

结合think-queue实现消息发送队列教程

贡献

你可以通过以下三种方式之一进行贡献

  1. 使用 问题跟踪器 提交错误报告。
  2. 问题跟踪器 上回答问题或修复错误。
  3. 贡献新功能或更新wiki。

代码贡献过程并不非常正式。你只需要确保遵循PSR-0、PSR-1和PSR-2编码规范。任何新的代码贡献都必须附有适用的单元测试。

许可证

MIT