Giter Site home page Giter Site logo

laravel-wxapp-notification-channel's Introduction

Laravel 微信小程序模板消息通知


安装

composer require xiaohuilam/laravel-wxapp-notification-channel -vvv

发布

执行以下命令创建 formids 数据表

php artisan migrate

模型改动

打开 app\User.php (如果你修改过模型位置,请以自己项目实际位置为准)

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Xiaohuilam\Laravel\WxappNotificationChannel\Traits\UserTrait;
use Xiaohuilam\Laravel\WxappNotificationChannel\Interfaces\Formidable;

class User extends Authenticatable implements Formidable // 实现 Formidable
{
    use UserTrait; // 使用 UserTrait

配置

WECHAT_MINI_PROGRAM_APPID=#小程序的appid
WECHAT_MINI_PROGRAM_SECRET=#小程序的secret

使用

定义消息模板

新建 app/Notifications/WechatTemplateTestNotification.php

根据不通类型走不同 notification 类

  • 公众号模板通知
  • 小程序模板通知

公众号模板通知

<?php
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Xiaohuilam\Laravel\WxappNotificationChannel\Types\MiniprogramType;
use Xiaohuilam\Laravel\WxappNotificationChannel\Interfaces\WechatOfficialNotificationable;

class WechatTemplateTestNotification extends Notification implements WechatOfficialNotificationable
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['wechat-official'];
    }

    /**
     * 获取模板id
     *
     * @return string
     */
    public function getTemplateId()
    {
        return 'iL4c0FHJFIIUIDfNH-gMXgkGHRwlP-lvh1Emfl4d3pg';
    }

    /**
     * 获取模板消息跳转链接
     *
     * @return string
     */
    public function getTemplateMessageUrl()
    {
        return 'https://www.baidu.com/';
    }

    /**
     * 跳转到小程序, 与getTemplateMessageUrl二选一
     *
     * @return \Xiaohuilam\Laravel\WxappNotificationChannel\Types\MiniprogramType
     */
    public function miniprogram()
    {
        return new MiniprogramType('APPID...', 'PATH路径...');
    }

    /**
     * 获取模板消息数据
     *
     * @return array
     */
    public function getTemplateMessageData()
    {
        return [
            'keyword1' => '审核通过',
            'keyword2' => 'ORDER-9112212',
            'keyword3' => '点击查看订单详情',
        ];
    }
}

小程序模板通知

<?php
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Xiaohuilam\Laravel\WxappNotificationChannel\Interfaces\WechatAppNotificationable;

class WechatTemplateTestNotification extends Notification implements WechatAppNotificationable
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['wechat-app'];
    }

    /**
     * 获取模板id
     *
     * @return string
     */
    public function getTemplateId()
    {
        return 'iL4c0FHJFIIUIDfNH-gMXgkGHRwlP-lvh1Emfl4d3pg';
    }

    /**
     * 获取模板消息跳转链接
     *
     * @return string
     */
    public function getTemplateMessagePath()
    {
        return '/app/order/detail?id=11';
    }

    /**
     * 获取模板消息数据
     *
     * @return array
     */
    public function getTemplateMessageData()
    {
        return [
            'keyword1' => '审核通过',
            'keyword2' => 'ORDER-9112212',
            'keyword3' => '点击查看订单详情',
        ];
    }

    /**
     * 需要放大的词
     *
     * @return string
     */
    public function getTemplateMessageEmphasisKeyword()
    {
        return 'keyword1.DATA';
    }
}

如果是小程序,还需要开API来记录 FormId

use Xiaohuilam\Laravel\WxappNotificationChannel\Models\Formid;

// ...
$formid = request()->input('formid'); // 小程序前端post 过来的 formid

$user = User::find(1);
$user->formids()->saveMany([new FormId(['formid' => $formid])]);

发送模板消息

use App\Notifications\FriendshipRequestNotification;

$user = User::find(1);
$user->notify(new WechatTemplateTestNotification());

授权

MIT

鸣谢

laravel-wxapp-notification-channel's People

Contributors

xiaohuilam avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

fossabot

laravel-wxapp-notification-channel's Issues

本项目目前存在的问题

本项目目前存在的问题

虽然本项目用起来挺方便,但其实还是有设计上的问题

  • 小程序模板消息需要 FormID,而公众号不需要,因此在接口设计上和migration发布可选择性上,存在理解逻辑死角。

  • 用户表无 openid 字段,或者一个用户有多个 openid 字段的情况无法使用:

    • 一个用户有小程序 openid 和公众号 openid 时。
    • 一个用户有多个公众号 openid 时。
  • 对 easywechat 的版本约束太宽。导致低版本 easywechat 没有 wechat.mini_program 等对象时候不支持

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.