dongdavid/notify

消息通知发送组件

2.0.1 2021-03-10 13:57 UTC

This package is auto-updated.

Last update: 2024-11-13 10:34:56 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);
}

Requirements

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

Installing

$ composer require dongdavid/notify -vvv

Usage

如使用微信公众号/企业微信/微信小程序消息通知,则需要配置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);

Test

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

TODO

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

例子

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

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

License

MIT