Giter Site home page Giter Site logo

huo-zi / laravel-wechat-notification Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 1.0 34 KB

Laravel 消息通知扩展 支持公众号模板消息、小程序消息、企微应用消息

License: MIT License

PHP 100.00%
laravel-notification-channels laravel-notifications laravel-notify laravel-wechat wechat weixin wxwork

laravel-wechat-notification's Introduction

laravel-wechat-notification

这是一个laravel框架下基于laravel-wechat使用微信/企微应用消息通知作为notification通道的composer包,使用前请先熟读laravel-消息通知
目前已支持:

  • 公众号模板消息
  • 小程序模板消息
  • 开放平台公众号模板消息
  • 开放平台小程序模板消息
  • 企业微信消息
  • 企业微信开放平台消息

使用方式:

0. 添加composer包:

composer require "huo-zi/laravel-wechat-notification"

1. 在继承了notification的通知类中可以使用如下方法:

public function via($notifiable)
{
    /*
     * 支持的via列表
     * official_account:公众号
     * mini_program:小程序
     * open_official_account:开放平台公众号
     * open_mini_program:开放平台小程序
     * work:企业微信
     * open_work:开放平台企业微信
     */
    return ['official_account', 'mini_program'];
}
  • 使用公众号发送模板消息:
public function toOfficialAccount()
{
    return WechatMessage::officialAccount('app_name')->template('templateId')->url($url)->data(['fisrt'=>'...']);
}
  • 使用小程序发送模板消息:
public function toMiniProgram()
{
    return WechatMessage::miniProgram('app_name')->template($templateId)->formId($formId)->data([
        'first' => ''
        //
    ]);
}
  • 使用开放平台公众号发送模板消息:
public function toOpenOfficialAccount()
{
    return WechatMessage::openFlatform($name)->officateAccount($appId, $refreshToken, $accessToken)->template('templateId');
}
// 或使用闭包创建开放平台的公众号对象
public function toOpenOfficialAccount()
{
    return WechatMessage::openFlatform($name)->officateAccount(function ($open) {
        return $open->officateAccount($appId, $refreshToken, $accessToken);
    })->template('templateId');
}
// 或创建好开放平台对象后使用
public function toOpenOfficialAccount()
{
    return (new WechatPlatform($openPlatform))->officateAccount($appId, $refreshToken, $accessToken)->template($templateId);
}
  • 使用开放平台小程序发送模板消息:
public function toOpenMiniProgram()
{
    return WechatMessage::openFlatform($name)->miniProgram($appId, $refreshToken, $accessToken)->template($templateId);
}
  • 使用企业微信发送消息:
public function toWork()
{
    return WechatMessage::work($name)->message($message)->ofAgent($agentId);
}
  • 使用开放平台企业微信发送消息:
public function toOpenWork()
{
    return WechatMessage::openWork($name)->work($authCorpId, $permanentCode)->message($message)->ofAgent($agentId);
    // 同样也支持创建好开放平台对象后使用及闭包创建work对象
    $messenger = (new WechatOpenWork($openWork))->work(function($openWork) {
        return $openWork->work($authCorpId, $permanentCode);
    });
    return $messenger->message($message)->ofAgent($agentid);
}

2. 在使用了triat Notifiable的模型里增加获取对应openid/userid的方法:

可以参考官方文档里发送邮件通知时自定义收件人

  • 获取公众号openid
public function routeNotificationForOfficialAccount($notification)
{
    // 返回当前model的公众号openid
    return $this->openid;
}
  • 获取小程序openid
public function routeNotificationForMiniProgram($notification)
{
    // 返回当前model的小程序openid
}
  • 获取开放平台公众号openid
public function routeNotificationForOpenOfficialAccount($notification)
{
    // 返回对应开放平台公众号用户的openid
}
  • 获取开放平台小程序openid
public function routeNotificationForOpenMiniProgram($notification)
{
    // 返回对应开放平台小程序用户的openid
}
  • 获取企业微信userid
public function routeNotificationForWork($notification)
{
    // 返回当前model的企业微信userid
    return ;
}
  • 获取开放平台企业微信userid
public function routeNotificationForOpenWork($notification)
{
    // 返回对应开放平台企业用户userid
    return ;
}

3. 可参考官方文档:发送通知

使用Notifiable特性的notify方法可以给用户实例发送通知

use App\Notifications\WorkNotify;

$user->notify(new WorkNotify(new Text('你好啊~')));

使用Notificationfacade 给多个可接收通知的实体发送通知

Notification::send($users, new WorkNotify(new Markdown('#你好啊~')));

License

Licensed under The MIT License (MIT).

laravel-wechat-notification's People

Contributors

huozi1024 avatar lio990527 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

shebaoting

laravel-wechat-notification's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.