74 lines
2.2 KiB
PHP
74 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Service\wx;
|
|
|
|
use App\BaseApp\BaseWxService;
|
|
use App\Models\business\CatalogueModel;
|
|
use App\Models\business\FavoriteModel;
|
|
use App\Service\common\MediaUrlService;
|
|
|
|
/**
|
|
* 商品收藏
|
|
*/
|
|
class WxFavoriteService extends BaseWxService
|
|
{
|
|
public function list(): array
|
|
{
|
|
$rows = FavoriteModel::with('catalogue')
|
|
->where('user_id', $this->userId)
|
|
->where('deleted_at', 0)
|
|
->orderByDesc('id')
|
|
->get()
|
|
->toArray();
|
|
$media = MediaUrlService::getInstance();
|
|
foreach ($rows as &$row) {
|
|
$cat = $row['catalogue'] ?? [];
|
|
$row['title'] = $cat['title'] ?? '';
|
|
$row['cover'] = $media->toPublic($cat['cover'] ?? '');
|
|
$row['price'] = $cat['price'] ?? '';
|
|
}
|
|
unset($row);
|
|
return $rows;
|
|
}
|
|
|
|
/**
|
|
* 切换收藏,返回当前是否已收藏
|
|
*/
|
|
public function toggle(int $catalogueId): array
|
|
{
|
|
if ($catalogueId <= 0) {
|
|
$this->utils->errorThrow('参数错误');
|
|
}
|
|
$exists = CatalogueModel::where('id', $catalogueId)->where('deleted_at', 0)->exists();
|
|
if (!$exists) {
|
|
$this->utils->errorThrow('商品不存在');
|
|
}
|
|
$row = FavoriteModel::where('user_id', $this->userId)
|
|
->where('catalogue_id', $catalogueId)
|
|
->where('deleted_at', 0)
|
|
->first();
|
|
if (!empty($row)) {
|
|
FavoriteModel::where('id', $row['id'])->update(['deleted_at' => time(), 'updated_at' => time()]);
|
|
return ['favorited' => false];
|
|
}
|
|
FavoriteModel::insert([
|
|
'user_id' => $this->userId,
|
|
'catalogue_id' => $catalogueId,
|
|
'created_at' => time(),
|
|
'updated_at' => time(),
|
|
]);
|
|
return ['favorited' => true];
|
|
}
|
|
|
|
public function isFavorited(int $catalogueId): bool
|
|
{
|
|
if ($this->userId <= 0 || $catalogueId <= 0) {
|
|
return false;
|
|
}
|
|
return FavoriteModel::where('user_id', $this->userId)
|
|
->where('catalogue_id', $catalogueId)
|
|
->where('deleted_at', 0)
|
|
->exists();
|
|
}
|
|
}
|