85 lines
2.2 KiB
PHP
85 lines
2.2 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Author: ywxapp<admin@ywxapp.cn>
|
||
// +----------------------------------------------------------------------
|
||
declare (strict_types = 1);
|
||
|
||
namespace addon\blog\model;
|
||
|
||
use ywxapp\model\BaseModel;
|
||
use ywxapp\model\MemberUser;
|
||
|
||
|
||
class BlogLike extends BaseModel
|
||
{
|
||
// 设置数据表名
|
||
protected $name = 'blog_like';
|
||
|
||
// 设置主键
|
||
protected $pk = 'id';
|
||
|
||
// 自动写入时间戳(int 型 Unix 时间戳,匹配 create_at int 列)
|
||
protected $autoWriteTimestamp = 'int';
|
||
protected $createTime = 'create_at';
|
||
protected $updateTime = false;
|
||
|
||
// 定义允许写入的字段
|
||
protected $allowField = [
|
||
'uid', 'aid'
|
||
];
|
||
|
||
// 设置字段类型
|
||
protected $type = [
|
||
'id' => 'int',
|
||
'uid' => 'int',
|
||
'aid' => 'int',
|
||
'create_at' => 'int'
|
||
];
|
||
|
||
// 定义关联 - 用户(博客用户即主系统会员,统一使用 wxapp_user,主键 uid)
|
||
|
||
public function user()
|
||
{
|
||
return $this->belongsTo(MemberUser::class, 'uid', 'uid');
|
||
}
|
||
|
||
// 定义关联 - 文章
|
||
|
||
public function article()
|
||
{
|
||
return $this->belongsTo(BlogArticle::class, 'aid');
|
||
}
|
||
|
||
// 检查用户是否已点赞
|
||
|
||
public static function hasLiked($userId, $articleId)
|
||
{
|
||
return self::where('uid', $userId)
|
||
->where('aid', $articleId)
|
||
->count() > 0;
|
||
}
|
||
|
||
// 切换点赞状态
|
||
|
||
public static function toggle($userId, $articleId)
|
||
{
|
||
$like = self::where('uid', $userId)
|
||
->where('aid', $articleId)
|
||
->find();
|
||
|
||
if ($like) {
|
||
$like->delete();
|
||
return false; // 取消点赞
|
||
} else {
|
||
$like = new self();
|
||
$like->uid = $userId;
|
||
$like->aid = $articleId;
|
||
$like->save();
|
||
return true; // 添加点赞
|
||
}
|
||
}
|
||
} |