├── .gitignore ├── README.md ├── composer.json └── src ├── MiniProgramChannel.php ├── OfficialAccountChannel.php ├── WeChatChannelServiceProvider.php └── WechatSubscribeChannel.php /.gitignore: -------------------------------------------------------------------------------- 1 | # phpstorm project files 2 | .idea 3 | 4 | # netbeans project files 5 | nbproject 6 | 7 | # zend studio for eclipse project files 8 | .buildpath 9 | .project 10 | .settings 11 | 12 | # windows thumbnail cache 13 | Thumbs.db 14 | 15 | # Mac DS_Store Files 16 | .DS_Store 17 | 18 | # phpunit itself is not needed 19 | phpunit.phar 20 | 21 | # local phpunit config 22 | /phpunit.xml 23 | 24 | # composer vendor dir 25 | /vendor 26 | 27 | # composer itself is not needed 28 | composer.phar 29 | composer.lock 30 | 31 | # Node.js modules 32 | node_modules/ 33 | package-lock.json 34 | 35 | # Bower components 36 | bower_components 37 | /package-lock.json 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # larva/laravel-wechat-notification-channel 2 | 3 | [![Packagist](https://img.shields.io/packagist/l/larva/laravel-wechat-notification-channel.svg?maxAge=2592000)](https://packagist.org/packages/larva/laravel-wechat-notification-channel) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/larva/laravel-wechat-notification-channel.svg?style=flat-square)](https://packagist.org/packages/larva/laravel-wechat-notification-channel) 5 | 6 | 适用于 Laravel 的微信模板消息推送通道适配器 7 | 8 | 该扩展是对 安正超 超哥的微信扩展的补充。 9 | 10 | ## 安装 11 | 12 | ```shell 13 | 14 | composer require "larva/laravel-wechat-notification-channel" 15 | ``` 16 | 17 | ## 配置 18 | 19 | 无需配置 20 | 21 | ## 使用 22 | 23 | 编写如下 通知类然后发出去就行了 24 | ```php 25 | namespace App\Models; 26 | 27 | class User { 28 | public function routeNotificationForWechat(){ 29 | return $this->wechatid; 30 | } 31 | 32 | public function routeNotificationForWechatMiniProgram(){ 33 | return $this->wechatminiid; 34 | } 35 | 36 | public function routeNotificationForWechatSubscribe(){ 37 | return $this->wechatminiid; 38 | // or 39 | // $notifiable->routeNotificationFor('wechatMiniProgram',$this) 通过小程序获取 不再设置此方法 40 | } 41 | } 42 | ``` 43 | 44 | ```php 45 | namespace App\Notifications; 46 | 47 | use Illuminate\Notifications\Notification; 48 | 49 | class WelcomeNotification extends Notification 50 | { 51 | /** 52 | * Get the notification's channels. 53 | * 54 | * @param mixed $notifiable 55 | * @return array|string 56 | */ 57 | public function via($notifiable) 58 | { 59 | return ['wechat','wechatMiniProgram']; 60 | } 61 | 62 | /** 63 | * Build the wechat representation of the notification. 64 | * 65 | * @param mixed $notifiable 66 | * @return array|false 67 | */ 68 | public function toWechat($notifiable) 69 | { 70 | if (!$toUser = $notifiable->routeNotificationFor('wechat',$this)) { 71 | return false; 72 | } 73 | return [ 74 | 'touser' => $toUser, 75 | 'template_id' => 'template-id', 76 | 'page' => 'index', 77 | 'form_id' => 'form-id', 78 | 'data' => [ 79 | 'keyword1' => 'VALUE', 80 | 'keyword2' => 'VALUE2', 81 | // ... 82 | ], 83 | ]; 84 | } 85 | 86 | /** 87 | * Build the MiniProgram representation of the notification. 88 | * 89 | * @param mixed $notifiable 90 | * @return array|false 91 | */ 92 | public function toWechatMiniProgram($notifiable) 93 | { 94 | if (!$toUser = $notifiable->routeNotificationFor('wechatMiniProgram',$this)) { 95 | return false; 96 | } 97 | return [ 98 | 'touser' => $toUser, 99 | 'template_id' => 'template-id', 100 | 'page' => 'index', 101 | 'form_id' => 'form-id', 102 | 'data' => [ 103 | 'keyword1' => 'VALUE', 104 | 'keyword2' => 'VALUE2', 105 | // ... 106 | ], 107 | ]; 108 | } 109 | 110 | /** 111 | * Build the WechatSubscribeChannel representation of the notification. 112 | * 113 | * @param mixed $notifiable 114 | * @return array|false 115 | */ 116 | public function toWechatSubscribe($notifiable) 117 | { 118 | if (!$toUser = $notifiable->routeNotificationFor('wechatSubscribe',$this)) { 119 | return false; 120 | } 121 | return [ 122 | 'touser' => $toUser, 123 | 'template_id' => 'template-id', 124 | 'page' => 'pages/index/index', 125 | 'miniprogram_state' => env('APP_DEBUG', false) ? 'trial' : 'formal', 126 | // 跳转小程序类型:developer 为开发版;trial为体验版;formal为正式版;默认为正式版 127 | 'data' => [ 128 | 'name1' => 'name1', 129 | 'thing2' => 'thing2', 130 | //... 131 | ], 132 | 133 | ]; 134 | } 135 | } 136 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "larva/laravel-wechat-notification-channel", 3 | "description": "This is a Laravel expansion for the wechat Push.", 4 | "keywords": [ 5 | "Larva", 6 | "laravel", 7 | "wechat" 8 | ], 9 | "type": "library", 10 | "license": "MIT", 11 | "require": { 12 | "illuminate/support": "^9.0 || ^10.0", 13 | "illuminate/notifications": "^9.0 || ^10.0", 14 | "overtrue/laravel-wechat": "^6.0 || ^7.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "Larva\\WeChat\\Notifications\\": "src" 19 | } 20 | }, 21 | "extra": { 22 | "branch-alias": { 23 | "dev-master": "1.0-dev" 24 | }, 25 | "laravel": { 26 | "providers": [ 27 | "Larva\\WeChat\\Notifications\\WeChatChannelServiceProvider" 28 | ] 29 | } 30 | }, 31 | "prefer-stable": true 32 | } 33 | -------------------------------------------------------------------------------- /src/MiniProgramChannel.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class MiniProgramChannel 20 | { 21 | /** 22 | * Send the given notification. 23 | * 24 | * @param mixed $notifiable 25 | * @param Notification $notification 26 | * @return void 27 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException 28 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException 29 | * @throws \GuzzleHttp\Exception\GuzzleException 30 | */ 31 | public function send($notifiable, Notification $notification) 32 | { 33 | /** @var array $message */ 34 | $message = $notification->toWechatMiniProgram($notifiable); 35 | if ($message) { 36 | WeChat::miniProgram()->template_message->send($message); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/OfficialAccountChannel.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class OfficialAccountChannel 20 | { 21 | /** 22 | * Send the given notification. 23 | * 24 | * @param mixed $notifiable 25 | * @param Notification $notification 26 | * @return void 27 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException 28 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException 29 | * @throws \GuzzleHttp\Exception\GuzzleException 30 | */ 31 | public function send($notifiable, Notification $notification) 32 | { 33 | /** @var array $message */ 34 | $message = $notification->toWechat($notifiable); 35 | if ($message) { 36 | WeChat::officialAccount()->template_message->send($message); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/WeChatChannelServiceProvider.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class WeChatChannelServiceProvider extends ServiceProvider 20 | { 21 | /** 22 | * Register the service provider. 23 | * 24 | * @return void 25 | */ 26 | public function register() 27 | { 28 | $channels = [ 29 | 'wechat' => OfficialAccountChannel::class, 30 | 'wechatMiniProgram' => MiniProgramChannel::class, 31 | 'wechatSubscribe' => WechatSubscribeChannel::class, 32 | ]; 33 | foreach ($channels as $name => $className) { 34 | Notification::extend($name, function () use ($className) { 35 | return new $className; 36 | }); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/WechatSubscribeChannel.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class WechatSubscribeChannel 20 | { 21 | /** 22 | * Send the given notification. 23 | * 24 | * @param mixed $notifiable 25 | * @param Notification $notification 26 | * @return void 27 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException 28 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException 29 | * @throws \GuzzleHttp\Exception\GuzzleException 30 | */ 31 | public function send($notifiable, Notification $notification) 32 | { 33 | /** @var array $message */ 34 | $message = $notification->toWechatSubscribe($notifiable); 35 | if ($message) { 36 | WeChat::miniProgram()->subscribe_message->send($message); 37 | } 38 | } 39 | } 40 | --------------------------------------------------------------------------------