自己写的包才是最符合自己需求的,还是自己写一个用吧。
easywechat 官网:https://www.easywechat.com/
easywechat 官网文档:https://www.easywechat.com/docs/master/overview
eastwechat环境要求:
PHP >= 7.0
建议使用laradock
1.安装easywechat包:
composer require overtrue/laravel-wechat
2.发布配置文件
php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider"
内容如下:
```php
/*
* This file is part of the overtrue/laravel-wechat.
*
* (c) overtrue
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
return [
/*
* 默认配置,将会合并到各模块中*/
'defaults' => [
/*
* 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名*/
'response_type' => 'array',
/*
* 使用 Laravel 的缓存系统*/
'use_laravel_cache' => true,
/*
* 日志配置*
* level: 日志级别,可选为:* debug/info/notice/warning/error/critical/alert/emergency
* file:日志文件位置(绝对路径!!!),要求可写权限*/
'log' => [
'level' => env('WECHAT_LOG_LEVEL', 'debug'),
'file' => env('WECHAT_LOG_FILE', storage_path('logs/wechat.log')),
],
],
/*
* 路由配置*/
'route' => [
/*
* 开放平台第三方平台路由配置*/
// 'open_platform' => [
// 'uri' => 'serve',
// 'action' => Overtrue\LaravelWeChat\Controllers\OpenPlatformController::class,
// 'attributes' => [
// 'prefix' => 'open-platform',
// 'middleware' => null,
// ],
// ],
],
/*
* 公众号*/
'official_account' => [
'default' => [
'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID', 'your-app-id'), // AppID
'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET', 'your-app-secret'), // AppSecret
'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN', 'your-token'), // Token
'aes_key' => env('WECHAT_OFFICIAL_ACCOUNT_AES_KEY', ''), // EncodingAESKey
/*
* OAuth 配置*
* scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
* callback:OAuth授权完成后的回调页地址(如果使用中间件,则随便填写。。。)
*/
// 'oauth' => [
// 'scopes' => array_map('trim', explode(',', env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES', 'snsapi_userinfo'))),
// 'callback' => env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK', '/examples/oauth_callback.php'),
// ],
],
],
/*
* 开放平台第三方平台*/
// 'open_platform' => [
// 'default' => [
// 'app_id' => env('WECHAT_OPEN_PLATFORM_APPID', ''),
// 'secret' => env('WECHAT_OPEN_PLATFORM_SECRET', ''),
// 'token' => env('WECHAT_OPEN_PLATFORM_TOKEN', ''),
// 'aes_key' => env('WECHAT_OPEN_PLATFORM_AES_KEY', ''),
// ],
// ],
/*
* 小程序*/
// 'mini_program' => [
// 'default' => [
// 'app_id' => env('WECHAT_MINI_PROGRAM_APPID', ''),
// 'secret' => env('WECHAT_MINI_PROGRAM_SECRET', ''),
// 'token' => env('WECHAT_MINI_PROGRAM_TOKEN', ''),
// 'aes_key' => env('WECHAT_MINI_PROGRAM_AES_KEY', ''),
// ],
// ],
/*
* 微信支付*/
// 'payment' => [
// 'default' => [
// 'sandbox' => env('WECHAT_PAYMENT_SANDBOX', false),
// 'app_id' => env('WECHAT_PAYMENT_APPID', ''),
// 'mch_id' => env('WECHAT_PAYMENT_MCH_ID', 'your-mch-id'),
// 'key' => env('WECHAT_PAYMENT_KEY', 'key-for-signature'),
// 'cert_path' => env('WECHAT_PAYMENT_CERT_PATH', 'path/to/cert/apiclient_cert.pem'), // XXX: 绝对路径!!!!
// 'key_path' => env('WECHAT_PAYMENT_KEY_PATH', 'path/to/cert/apiclient_key.pem'), // XXX: 绝对路径!!!!
// 'notify_url' => 'http://example.com/payments/wechat-notify', // 默认支付结果通知地址
// ],
// // ...
// ],
/*
* 企业微信*/
// 'work' => [
// 'default' => [
// 'corp_id' => 'xxxxxxxxxxxxxxxxx',
// 'agent_id' => 100020,
// 'secret' => env('WECHAT_WORK_AGENT_CONTACTS_SECRET', ''),
// //...
// ],
// ],
];
```
3.修改env文件
WECHAT_OFFICIAL_ACCOUNT_APPID=你的APPID
WECHAT_OFFICIAL_ACCOUNT_SECRET=你的SECRET
WECHAT_OFFICIAL_ACCOUNT_TOKEN=
如何拿到APPID和SECRET呢 你需要有个认证的服务号
4. 登陆微信公众号平台
官网地址:https://mp.weixin.qq.com/
配置如下图
配置APPID丶SECRET和IP白名单配置齐活,开始写频道和通知啦!
5.创建频道驱动
WechatTemplateMessageChannel 位置:App\Channels\WechatTemplateMessageChannel
代码截图:
代码图片1 代码图片2代码如下:
namespace App\Channels;
use EasyWeChat\Factory;
use GuzzleHttp\Client;
use Illuminate\Notifications\Notification;
use PhpParser\Node\Expr\Array_;
class WechatTemplateMessageChannel
{
/**
* Send the given notification.
*
* 1.广播消息:当仅返回 (消息内容)时 ,触发广播行为,给所有用户发送(消息内容)广播。
* 2.指定用户:当完整返回openid,模板id,消息内容时,触发模板消息行为,使用模板id发送给指定openid用户以消息内容。
*
* 使用方法:
* notification 可以使用 toWechatTemplateMessage方法
*
* toWechatTemplateMessage方法中返回一个数组:( 消息内容(数组格式), 模板ID(字符串格式) )如:
*
* $allData=[$data,$template];
* return $allData;
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$app = app('wechat.official_account');
$allData = $notification->toWechatTemplateMessage($notifiable);
$data=$allData[0];
$template=$allData[1];
$openId=isset($notifiable->routes['WechatTemplateMessage'])?$notifiable->routes['WechatTemplateMessage']:null;
$broad=false;
if (!$openId) {
$broad=true;
}
if ($openId) {
$openId=is_array($openId)?$openId:array($openId);
}
if ($broad==false) {
foreach ($openId as $keys) {
$app->template_message->send(
[
'touser' => $keys,
'template_id' => $template,
"data"=>$data
]
);
}
}
// 没有指定用户,就广播
if ($broad) {
$app->broadcasting->sendText($data);
}
}
}
6.书写notification
代码图片代码如下:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use App\Channels\WechatTemplateMessageChannel;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Text;
class WechatTemplateMessageNotification extends Notification
{
use Queueable;
public function __construct($data, $template = null)
{
$this->data = $data;
$this->template = $template;
}
public function via($notifiable)
{
return [WechatTemplateMessageChannel::class];
}
public function toWechatTemplateMessage($notifiable)
{
$data=$this->data;
$template=$this->template;
$allData=[$data,$template];
return $allData;
}
}
7.书写 控制器测试代码:
代码图片namespace App\Http\Controllers;
use App\Notifications\WechatTemplateMessageNotification;
use App\Notifications\MailNotification;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
class CController extends Controller
{
//广播
public function a()
{
$data='谢谢关注';
Notification::route('WechatTemplateMessage', null)->notify(new WechatTemplateMessageNotification($data));
}
//指定用户,完整参数
public function b()
{
$ren=['odAYnxOVy7vS2YVipvmG4biBzGFQ','odAYnxEuuTCfR6LmpNWfov27cf4A'];
$template="iA2V1K45vS8IgUEvE8Z3KJYrlwMOEH3R-V-DdLWpzAw";
$data=[
"order_id"=>[
"value"=>"20200414234478934343",
"color"=>"#173177"
],
"package_id"=>[
"value"=>"SF4345454534",
"color"=>"#173177"
],
"remark"=>[
"value"=>'模板消息发送',
"color"=>"#173177"
]
];
Notification::route('WechatTemplateMessage', $ren)->notify(new WechatTemplateMessageNotification($data, $template));
}
}
网友评论