139 lines
3.5 KiB
PHP
139 lines
3.5 KiB
PHP
<?php
|
|
namespace app\model;
|
|
|
|
/**
|
|
* 朋友圈动态模型
|
|
* @property integer $id 主键(ID)
|
|
* @property integer $user_id 用户ID
|
|
* @property string $body 内容
|
|
* @property string $files 图片列表(JSON)
|
|
* @property integer $like_count 点赞数
|
|
* @property integer $comment_count 评论数
|
|
* @property integer $created_at 创建时间
|
|
* @property integer $updated_at 更新时间
|
|
* @property integer $status 状态(0:隐藏 1:正常)
|
|
*/
|
|
class FriendCircle extends Base
|
|
{
|
|
protected $name = 'friend_circle';
|
|
|
|
protected function getOptions(): array
|
|
{
|
|
return array_merge(parent::getOptions(), [
|
|
'insert' => [
|
|
'status' => 1,
|
|
'like_count' => 0,
|
|
'comment_count' => 0,
|
|
],
|
|
]);
|
|
}
|
|
public static function onAfterInsert($row){
|
|
$changeData = $row->getChangedData();
|
|
if(isset($changeData['files'])) {
|
|
$files = json_decode($changeData['files'],true);
|
|
Files::whereIn('path',$files)->inc('use_count');
|
|
};
|
|
}
|
|
|
|
public static function onAfterUpdate($row){
|
|
$OrgData = $row->getOrigin();
|
|
$changeData = $row->getChangedData();
|
|
if(isset($OrgData['files'])) {
|
|
if(is_array($OrgData['files'])){
|
|
\support\Log::alert('OrgData array');
|
|
}else{
|
|
\support\Log::info('OrgData string');
|
|
$files = json_decode($OrgData['files'],true);
|
|
Files::whereIn('path',$files)->dec('use_count');
|
|
}
|
|
};
|
|
if(isset($changeData['files'])) {
|
|
if(is_array($changeData['files'])){
|
|
\support\Log::alert('changeData array');
|
|
}else{
|
|
\support\Log::info('changeData string');
|
|
$files = json_decode($changeData['files'],true);
|
|
Files::whereIn('path',$files)->inc('use_count');
|
|
}
|
|
};
|
|
}
|
|
/**
|
|
* 关联用户
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo('\\app\\model\\User', 'user_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* 关联点赞
|
|
*/
|
|
public function likes()
|
|
{
|
|
return $this->hasMany('\\app\\model\\FriendCircleLike', 'circle_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* 关联评论
|
|
*/
|
|
public function comments()
|
|
{
|
|
return $this->hasMany('\\app\\model\\FriendCircleComment', 'circle_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* 获取图片列表
|
|
*/
|
|
public function getFilesAttr($value)
|
|
{
|
|
if (empty($value)) {
|
|
return [];
|
|
}
|
|
if(is_array($value)){
|
|
return $value;
|
|
}
|
|
$files = json_decode($value, true);
|
|
return is_array($files) ? $files : [];
|
|
}
|
|
|
|
/**
|
|
* 设置图片列表
|
|
*/
|
|
public function setFilesAttr($value)
|
|
{
|
|
if (empty($value)) {
|
|
return '[]';
|
|
}
|
|
if (is_array($value)) {
|
|
return json_encode($value, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
return $value;
|
|
}
|
|
/**
|
|
* 获取图片列表
|
|
*/
|
|
public function getAddressAttr($value)
|
|
{
|
|
if (empty($value)) {
|
|
return [];
|
|
}
|
|
$files = json_decode($value, true);
|
|
return is_array($files) ? $files : [];
|
|
}
|
|
|
|
/**
|
|
* 设置图片列表
|
|
*/
|
|
public function setAddressAttr($value)
|
|
{
|
|
if (empty($value)) {
|
|
return '{"chooseFlag":false}';
|
|
}
|
|
if (is_array($value)) {
|
|
return json_encode($value, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
return $value;
|
|
}
|
|
}
|
|
|