统一发送消息队列
composer require quansitech/send-msg
php artisan migrate# 微信公众号
WX_APPID=
WX_APPSECRET=# 启瑞云短信
QIRUI_API_KEY=
QIRUI_API_SECRET=
# 签名(即短信开头)
QIRUI_SIGN=方案2,采用其它api,详细配置请查看 easysms配置
在/app/Common/Conf/config.php中加入以下代码
'SEND_MSG_SMS_GATEWAY'=> [
//云片
'yunpian'=>[
'api_key'=>'27f03185cb9b6e1a4f274b*******',
'sign_text'=>'【云片签名】'
],
],在后台中新增消息模板,地址 http://[host]:[port]/admin/MsgTpl/add 或写迁移文件进行添加,建议写迁移文件
启动队列wx(微信)和sms(短信)
php resque start --queue=wx #微信消息
php resque start --queue=sms #短信消息class MsgTemplateParse extends BaseMsgTemplateParse {
static protected $_var_func = array(
'项目名称' => 'parseProjectName',
);
static public function parseProjectName($args){
$project_id = $args['project_id'];
return D('Project')->where(['id'=>$project_id])->getField('title');
}
}//MsgTemplateParse为上一步所定义的类
$builder=\QsSendMsg\SendMsgJobBuilder::getInstance()->setTemplateParser(MsgTemplateParse::class);/** @var $builder \QsSendMsg\SendMsgJobBuilder */
$builder->setMsgTpl('test_msg',['project_id'=>1])
->addSmsMsgJob('13100000000','发送短信')
->addWxMsgJob('[openid]','[消息模板id]','[消息内容对象]','[跳转的url]','发送微信消息','[内容填充字段(默认为"first")]');ps."test_msg"为数据库中'qs_msg_tpl'的数据
ps."消息内容对象"示例
$msgData = [
'first' => ['value' => 'test', 'color' => '#173177'],
'keyword1' => ['value' => 'keyword1', 'color' => '#173177'],
'keyword2' => ['value' => 'keyword2', 'color' => '#173177'],
'remark' => ['value' => 'remark', 'color' => '#173177'],
];/** @var $builder \QsSendMsg\SendMsgJobBuilder 第4步的$builder*/
$builder->dispatchAll();/** @var $builder \QsSendMsg\SendMsgJobBuilder 第4步的$builder*/
$builder->dispatch();根据队列用法1.1步骤,1.2步骤配置后即可使用
$job = new \QsSendMsg\Job\SendWxMsgJob();
$r=$job->send([
'open_id'=>'微信openid',
'template_id'=>'微信模板消息ID',
'url'=>'跳转地址',
'data'=>'消息内容'
]);$job = new \QsSendMsg\Job\SendSmsMsgJob();
$r=$job->send([
'mobile'=>'手机号',
'content'=>'短信内容',
//...其它参数
]);发送失败时$r为false,可查看$job->error错误信息
在.env文件下增加配置项
# 启瑞云短信
QIRUI_API_KEY=
QIRUI_API_SECRET=
# 签名(即短信开头)
QIRUI_SIGN=需要在config文件中加入以下内容
'SEND_MSG_SMS_GATEWAY'=> [
//云片
'yunpian'=>[
'api_key'=>'27f03185cb9b6e1a4f274b*******',
'sign_text'=>'【云片签名】'
],
],