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