PHP code example of xiaohuilam / laravel-wxapp-notification-channel

1. Go to this page and download the library: Download xiaohuilam/laravel-wxapp-notification-channel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xiaohuilam / laravel-wxapp-notification-channel example snippets


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


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' => '点击查看订单详情',
        ];
    }
}


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';
    }
}

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());
bash
php artisan migrate