Browse Source

朋友的朋友圈列表,朋友朋友圈详情接口

author
wanghongjun 9 months ago
parent
commit
6442a71bd7
  1. 89
      app/enterprise/controller/Posts.php

89
app/enterprise/controller/Posts.php

@ -31,7 +31,6 @@ class Posts extends BaseController
$where = '(p.delete_time = 0 and p.privacy > 0 and p.status = 1) '; $where = '(p.delete_time = 0 and p.privacy > 0 and p.status = 1) ';
$where .= ' and ( p.user_id = ' . $this->uid; $where .= ' and ( p.user_id = ' . $this->uid;
$prefix = config('database.connections.mysql.prefix');
$friendIds = Friend::getFriendIds($this->uid); $friendIds = Friend::getFriendIds($this->uid);
if ($friendIds) { if ($friendIds) {
@ -39,18 +38,7 @@ class Posts extends BaseController
$where .= ' or ( p.privacy = 1 and p.user_id in (' . $friendIdStr . '))'; $where .= ' or ( p.privacy = 1 and p.user_id in (' . $friendIdStr . '))';
// 可见 // 可见
$where .= " or ( p.privacy = 2 and exists ( $where = $this->getPostsWhereOr($where);
select 1 from {$prefix}posts_privacy_users pvu
where pvu.posts_id = p.id and pvu.user_id = p.user_id
and pvu.type = 1 and pvu.user_id in ({$this->uid})
))";
// 不可见
$where .= " or ( p.privacy = 4 and not exists (
select 1 from {$prefix}posts_privacy_users peu
where peu.posts_id = p.id
and peu.type = 2 and peu.user_id = {$this->uid}
))";
} }
$where .= ')'; $where .= ')';
@ -96,21 +84,62 @@ class Posts extends BaseController
]; ];
} }
// 好友可见、不可见查询条件
protected function getPostsWhereOr($whereOr):string
{
$prefix = config('database.connections.mysql.prefix');
$whereOr .= " or ( p.privacy = 2 and exists (
select 1 from {$prefix}posts_privacy_users pvu
where pvu.posts_id = p.id and pvu.user_id = p.user_id
and pvu.type = 1 and pvu.user_id in ({$this->uid})
))";
// 不可见
$whereOr .= " or ( p.privacy = 4 and not exists (
select 1 from {$prefix}posts_privacy_users peu
where peu.posts_id = p.id
and peu.type = 2 and peu.user_id = {$this->uid}
))";
return $whereOr;
}
// 我的朋友圈列表 // 我的朋友圈列表
public function myPosts() public function myPosts()
{ {
$where = [ $user_id = $this->uid;
['user_id', '=', $this->uid], // 好有朋友圈
['privacy', 'in', [1,2,3,4]], $friendUserId = $this->request->param('friend_user_id', '');
['status', '=', 1],
['delete_time', '=', 0] $whereAnd = " ( p.status = 1 and p.delete_time = 0";
]; $whereOr = '';
if ($friendUserId) {
// 验证是否为当前用户的好友
if (!is_numeric($friendUserId)) {
return warning(lang('system.error'));
}
$friendIds = Friend::getFriendIds($this->uid);
if (!in_array($friendUserId, $friendIds)) {
return warning(lang('system.error'));
}
$user_id = $friendUserId;
$field = 'id,content,location,address,type,user_id,create_time,privacy,avatar'; $whereOr .= ' and ( p.privacy = 1 ';
$whereOr = $this->getPostsWhereOr($whereOr);
$whereOr .= ')';
} else {
$whereAnd .= ' and p.privacy in (1,2,3,4)';
}
$whereAnd .= " and p.user_id = {$user_id} )";
$where = $whereAnd . $whereOr;
$field = 'p.id,p.content,p.location,p.address,p.type,p.user_id,p.create_time,p.privacy,p.avatar';
$order = 'create_time desc'; $order = 'create_time desc';
$model = new PostsModel(); $model = new PostsModel();
$list = $this->paginate($model->where($where)->field($field)->order($order)); $list = $this->paginate($model->alias('p')->where($where)->field($field)->order($order));
$data = []; $data = [];
if ($list) { if ($list) {
$data = $list->toArray()['data']; $data = $list->toArray()['data'];
@ -139,17 +168,31 @@ class Posts extends BaseController
public function details() public function details()
{ {
$posts_id = $this->request->param('posts_id'); $posts_id = $this->request->param('posts_id');
$friendUserId = $this->request->param('friend_user_id', '');
if (empty($posts_id)) { if (empty($posts_id)) {
return warning(lang('system.error')); return warning(lang('system.error'));
} }
$where = [ $where = [
['id', '=', $posts_id], ['id', '=', $posts_id],
['user_id', '=', $this->uid],
['privacy', 'in', [1,2,3,4]],
['status', '=', 1], ['status', '=', 1],
['delete_time', '=', 0] ['delete_time', '=', 0]
]; ];
if ($friendUserId) {
// 验证是否为当前用户的好友
if (!is_numeric($friendUserId)) {
return warning(lang('system.error'));
}
$friendIds = Friend::getFriendIds($this->uid);
if (!in_array($friendUserId, $friendIds)) {
return warning(lang('system.error'));
}
$where[] = ['user_id', '=', $friendUserId];
$where[] = ['privacy', 'in', [1,2,4]];
} else {
$where[] = ['user_id', '=', $this->uid];
$where[] = ['privacy', 'in', [1,2,3,4]];
}
$field = 'id,content,location,address,type,user_id,create_time,privacy'; $field = 'id,content,location,address,type,user_id,create_time,privacy';
$data = (new PostsModel())->where($where)->field($field)->find(); $data = (new PostsModel())->where($where)->field($field)->find();
if (!$data) { if (!$data) {

Loading…
Cancel
Save