meloflavio/notificacao-bundle

为 symfony 4 服务的通知。

安装: 21

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

1.3.3 2020-06-29 20:04 UTC

This package is auto-updated.

Last update: 2024-09-29 05:42:45 UTC


README

symfony flex 和 mercure bundle 的集成通知包。

安装

####1. 使用 Composer 需求

    composer require meloflavio/notificacao-bundle  

####2. 添加环境变量

MERCURE_PUBLISH_URL=https:///mercure/.well-known/mercure
MERCURE_JWT_TOKEN=Token
MERCURE_SECRET_KEY=CHANGEMEKey

Token 可以在 https://jwt.net.cn/ 上创建

####3. 在 notificacao.yaml 中添加,如果您想更改默认用户类,则是 App/UFT/UserBundle/Entity/Usuario

meloflavio_notificacao:
  user:
    class: App/UFT/UserBundle/Entity/Usuario

####4. 在 mercure.yaml 中添加

parameters:
   mercure_secret_key: '%env(MERCURE_SECRET_KEY)%'
   mercure_url: '%env(MERCURE_PUBLISH_URL)%'
   mercure_token: '%env(MERCURE_JWT_TOKEN)%'
mercure:
   enable_profiler: '%kernel.debug%'
   hubs:
       default:
           url: '%env(MERCURE_PUBLISH_URL)%'
           jwt: '%env(MERCURE_JWT_TOKEN)%'

####5. 创建通知实体

<?php

namespace App\Entity;


use Doctrine\ORM\Mapping as ORM;
use MeloFlavio\NotificacaoBundle\Entity\NotificacaoBase;

/**
 * Class Base
 * @ORM\MappedSuperclass()
 */
abstract class Notificacao extends NotificacaoBase
{
    /**
     * @ORM\Column(type="string", length=1500, nullable=true)
     */
    private $texto;

    

     /**
     *
     * @Gedmo\Blameable(on="create")
     * @ORM\ManyToOne(targetEntity="Sonata\UserBundle\Model\UserInterface")
     * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
     */
    protected $createdBy;
    
...

}

####6. 配置路由,创建文件 config/routes/notificacao.yaml

notificacao_discover:
  resource: '@MeloFlavioNotificacaoBundle/Resources/config/routing/discover.xml'

...
####7.  Exemplo de como utilizar
Adicione ao templete substituindo {{ settings.data_alvo.id }} pelo id da mensagem

```js
<script>
        document.addEventListener('DOMContentLoaded',function () {

            
            function addMessage(data){
               
                 let html = "<div " +
                     "                <div " +
                     "                    <span><strong class=\"autor\">"+data.createdBy.username+"</strong> fez um comentário</span>\n" +
                     "                </div>\n" +
                     "                <div class=\"message-content\">\n"+data.texto+
                     "                </div>\n" +
                "                </div>\n" +
                "            </div>";
                document.querySelector("#message-board").firstElementChild.insertAdjacentHTML("afterend",html);
            }
            fetch("{{ path('discover') }}").then(result => {
                const hubUrl = result.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1];

                const url  = new URL(hubUrl);
                // let url = new URL("https://:9090/.well-known/mercure");
                url.searchParams.append('topic', '/notificcao_base/{{app.user.username}}')
                const eventSource = new EventSource(url,{
                    withCredentials: true
                });
                eventSource.onmessage = (event) => {
                    var data = JSON.parse(event.data);
                    addMessage(data);
                };
            });
        })

    </script>