Files

119 lines
3.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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\wxchat\model;
use ywxapp\model\BaseModel;
use ywxapp\model\MemberUser as UserModel;
class WxchatMoment extends BaseModel
{
// 遗留完整表名(无 wxapp_ 前缀,须用 $table 显式声明,不能用 $name
protected $table = 'dynamics';
protected $autoWriteTimestamp = 'int';
protected $createTime = 'create_at';
protected $updateTime = 'update_at';
// 关联:发布用户
public function user()
{
return $this->belongsTo(UserModel::class, 'uid');
}
// 关联:媒体文件
public function media()
{
return $this->hasMany(WxchatMomentMedia::class, 'mid');
}
// 关联:评论
public function comments()
{
return $this->hasMany(WxchatMomentComment::class, 'mid');
}
// 关联:点赞用户
public function likeUsers()
{
return $this->belongsToMany(UserModel::class, WxchatMomentLike::class, 'uid', 'mid');
}
// 关联:收藏用户
public function collectUsers()
{
return $this->belongsToMany(UserModel::class, WxchatMomentCollect::class, 'uid', 'mid');
}
// 关联:话题标签
public function hashtags()
{
return $this->belongsToMany(WxchatMomentHashtag::class, WxchatMomentHashtagAb::class, 'hid', 'mid');
}
// 获取动态列表(带分页)
public static function getList($page = 1, $limit = 20, $userId = 0)
{
$query = self::with([
'member' =>
function($query) {
$query->field('id,username,nickname,avatar');
},
'media' =>
function($query) {
$query->field('id,dynamic_id,type,url,thumbnail_url')
->order('sort_order', 'asc')
->limit(9); // 最多显示9张
}
])->where('status', 1)
->order('create_at', 'desc');
$total = $query->count();
$moments = $query->page($page, $limit)->select();
// 格式化数据
$formattedDynamics = [];
foreach ($moments as $moment) {
$formattedDynamic = $moment->toArray();
// 添加互动状态
if ($userId) {
$formattedDynamic['liked'] = WxchatMomentLike::where('mid', $moment->id)
->where('uid', $userId)
->exists();
$formattedDynamic['collected'] = WxchatMomentCollect::where('mid', $moment->id)
->where('uid', $userId)
->exists();
} else {
$formattedDynamic['liked'] = false;
$formattedDynamic['collected'] = false;
}
$formattedDynamics[] = $formattedDynamic;
}
return [
'list' => $formattedDynamics,
'total' => $total,
'pages' => ceil($total / $limit)
];
}
}