更新若干功能
This commit is contained in:
73
app/Service/wx/WxFavoriteService.php
Normal file
73
app/Service/wx/WxFavoriteService.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user