lyrasoft/favorite 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lyrasoft/favorite

Composer 安装命令:

composer require lyrasoft/favorite

包简介

Lyrasoft Favorite package

README 文档

README

Installation

Install from composer

composer require lyrasoft/favorite

Then copy files to project

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

This package has no seeders, you must add your own seeders.

Language Files

Add this line to admin & front middleware if you don't want to override languages:

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

Or run this command to copy languages files:

php windwalker pkg:install lyrasoft/favorite -t lang

Seeders

Write your own seeders with different 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);
}

Add AJAX Buttons

You can add button component in blade templates:

<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>

in page ts file

import { useFavoriteButton } from '@lyrasoft/favorite';

await useFavoriteButton();

Available params:

Name Type Description
type string The favorite type
id string or int The item ID
added bool or int Current is favorited or not in this page.
class-active string The button class if favorited.
class-inactive string The button class if not favorited.
icon-active string The icon class if favorited.
icon-inactive string The button class if not favorited.
title-active string The tooltip title if favorited.
title-inactive string The tooltip title if not favorited.

AJAX Type Protect

By default, favorite package will not allow any types sent from browser.

You can configre allowed types in config file:

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

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

You can also set the type_protect to FALSE but we don't recommend to do this.

AJAX Events

You can listen events after favorited actions:

// 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;
  });
}

Or listen globally:

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

Add Button to Vue App

Use uni-favorite-button directive to auto enale button in Vue app.

<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>

Use FavoriteRepository

Join to List

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'
            );
        }
        
        // ...

In blade:

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

Use FavoriteService

You can use FavoriteService to add/remove or check favorited items.

$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);

lyrasoft/favorite 适用场景与选型建议

lyrasoft/favorite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.75k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lyrasoft/favorite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lyrasoft/favorite 我们能提供哪些服务?
定制开发 / 二次开发

基于 lyrasoft/favorite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-28