lyrasoft/favorite

Lyrasoft Favorite软件包

1.1.0 2024-01-26 08:54 UTC

This package is auto-updated.

Last update: 2024-08-26 10:09:04 UTC


README

安装

通过Composer安装

composer require lyrasoft/favorite

然后将文件复制到项目中

php windwalker pkg:install lyrasoft/favorite -t routes -t migrations

此包没有种子文件,您必须添加自己的种子文件。

语言文件

如果您不想覆盖语言,请将此行添加到admin & front中间件

$this->lang->loadAllFromVendor(\Windwalker\Language\LanguagePackage::class, 'ini');

或者运行此命令以复制语言文件

php windwalker pkg:install lyrasoft/favorite -t lang

种子文件

使用不同的type编写自己的种子文件

use Lyrasoft\Favorite\Entity\Favorite;

$mapper = $orm->mapper(Favorite::class);

foreach ($faker->randomElements($items, 5) as $item) {
    $favorite = new Favorite();
    $favorite->setType('item');
    $favorite->setUserId($user->getId());
    $favorite->setTargetId($item->getId());
    
    $mapper->createOne($favorite);
}

添加AJAX按钮

您可以在blade模板中添加按钮组件

<div class="card c-item-card">
    <x-favorite-button
        type="item"
        :id="$item->getId()"
        :added="$item->favorited"
        class="..."
    ></x-favorite-button>

    <div class="card-body">
        ...
    </div>
</div>

可用参数

AJAX类型保护

默认情况下,favorite软件包将不允许从浏览器发送任何类型。

您可以在配置文件中配置允许的类型

return [
    'favorite' => [
        // ...

        'ajax' => [
            'type_protect' => true,
            'allow_types' => [
                'article',
                '...' // <-- Add your new types here
            ]
        ],
    ]
];

您还可以将type_protect设置为FALSE,但我们不建议这样做。

AJAX事件

您可以在收藏操作后监听事件

// Select all favorite buttons, you can use your own class to select it.
const buttons = document.querySelectorAll('[uni-favorite-button]');

for (const button of buttons) {
  button.addEventListener('favorited', (e) => {
    u.notify(e.detail.message, 'success');
    
    // Available details
    e.detail.favorited;
    e.detail.type;
    e.detail.task;
    e.detail.message;
  });
}

或者全局监听

document.addEventListener('favorited', (e) => {
  if (e.detail.type === 'product') {
    if (e.detail.favorited) {
      u.notify('已收藏', 'success');
    } else {
      u.notify('已取消收藏', 'success');
    }
  }
});

将按钮添加到Vue应用

使用uni-favorite-button指令在Vue应用中自动启用按钮。

<a href="javascript://"
    uni-favorite-button
    :data-added="favorited"
    :data-type="type"
    data-class-active=""
    data-class-inactive=""
    data-icon-active="fas fa-heart"
    data-icon-inactive="far fa-heart"
    data-title-active="..."
    data-title-inactive="..."
>
    <i></i>
</a>

使用FavoriteRepository

加入列表

use Lyrasoft\Favorite\Repository\FavoriteRepository;

    // In any repository

    public function getFrontListSelector(?User $user = null): ListSelector
    {
        $selector = $this->getListSelector();

        if ($user && $user->isLogin()) {
            FavoriteRepository::joinFavorite(
                $selector,
                'item',
                $user->getId(),
                'item.id'
            );
        }
        
        // ...

在blade中

@foreach ($items of $item)
<div>
    ...
    <x-favorite-button
        type="item"
        :id="$item->getId()"
        :added="$item->favorited
        class="..."
    ></x-favorite-button>
    ...
</div>
@endforeach

使用FavoriteService

您可以使用FavoriteService来添加/删除或检查收藏项。

$favoriteService = $app->service(\Lyrasoft\Favorite\Service\FavoriteService::class);

$favoriteService->addFavorite($type, $user->getId(), $targetId);

$favoriteService->removeFavorite($type, $user->getId(), $targetId);

$favoriteService->isFavorited($type, $user->getId(), $targetId);