Browse Source

朋友圈 我的列表,删除帖子,查看详情

author
wanghongjun 8 months ago
parent
commit
dcf022f356
  1. 104
      app/enterprise/controller/Posts.php
  2. 25
      app/enterprise/model/Comment.php
  3. 9
      app/enterprise/model/Friend.php
  4. 23
      app/enterprise/model/Likes.php
  5. 16
      app/enterprise/model/PostsFile.php
  6. 72
      public/sql/database.sql

104
app/enterprise/controller/Posts.php

@ -68,49 +68,16 @@ class Posts extends BaseController
$item['location_address'] = trim(implode(" ", array_unique(\Ip::find($item['location'])))); $item['location_address'] = trim(implode(" ", array_unique(\Ip::find($item['location']))));
} }
$fileWhere = [ $item['files'] = PostsFile::getPostsFile($item['id']);
'posts_id' => $item['id'],
'delete_time' => 0
];
$files = (new PostsFile())
->where($fileWhere)
->field('file_id,type')
->order('sort asc')
->select()
->toArray();
foreach ($files as $fileKey => $fileValue) {
$files[$fileKey]['src'] = File::where(['file_id' => $fileValue['file_id']])->value('src');
}
$item['files'] = $files;
$userIdArr = $friendIds; $userIdArr = $friendIds;
$userIdArr[] = $this->uid; $userIdArr[] = $this->uid;
$playWhere = [
['type', '=', 1],
['relevance_id', '=', $item['id']],
['user_id', 'in', $userIdArr]
];
$item['is_like'] = 0; $likesData = Likes::getPostsLikes($item['id'], $this->uid, $userIdArr);
$likes = Likes::where($playWhere)->field('user_id')->select(); $item['likes'] = $likesData['likes'];
foreach ($likes as $likesKey => $likesValue) { $item['is_like'] = $likesData['is_like'];
if ($likesValue['user_id'] == $this->uid) {
$item['is_like'] = 1; $item['comment'] = Comment::getPostsComment($item['id'], $this->uid, $userIdArr);
}
$likes[$likesKey]['nickname'] = Friend::getFriendName($this->uid, $likesValue['user_id']);
}
$item['likes'] = $likes;
$comment = Comment::where($playWhere)->field('id,content,user_id,reply_user_id')->select();
foreach ($comment as $commentKey => $commentValue) {
$comment[$commentKey]['nickname'] = Friend::getFriendName($this->uid, $commentValue['user_id']);
$comment[$commentKey]['reply_user_name'] = '';
if ($commentValue['reply_user_id']) {
$comment[$commentKey]['reply_user_name'] = Friend::getFriendName($this->uid, $commentValue['reply_user_id']);
}
unset($comment[$commentKey]['reply_user_id']);
}
$item['comment'] = $comment;
} }
} }
return success('', $data,$list->total(),$list->currentPage()); return success('', $data,$list->total(),$list->currentPage());
@ -120,7 +87,7 @@ class Posts extends BaseController
{ {
$where = [ $where = [
['user_id', '=', $this->uid], ['user_id', '=', $this->uid],
['privacy', '>', 0], ['privacy', 'in', [1,2,3,4]],
['delete_time', '=', 0] ['delete_time', '=', 0]
]; ];
@ -134,12 +101,69 @@ class Posts extends BaseController
$data = $list->toArray()['data']; $data = $list->toArray()['data'];
foreach ($data as &$item) { foreach ($data as &$item) {
$item['files'] = PostsFile::getPostsFile($item['id']);
} }
} }
return success('', $data,$list->total(),$list->currentPage()); return success('', $data,$list->total(),$list->currentPage());
} }
public function details()
{
$posts_id = $this->request->param('posts_id');
if (empty($posts_id)) {
return warning(lang('system.error'));
}
$where = [
'id' => $posts_id,
'user_id' => $this->uid,
'delete_time' => 0
];
$field = 'id,content,location,type,user_id,create_time';
$data = (new PostsModel())->where($where)->field($field)->find();
if (!$data) {
return warning(lang('system.error'));
}
$data['files'] = PostsFile::getPostsFile($data['id']);
$friendIds = Friend::getFriendIds($this->uid);
$friendIds[] = $this->uid;
$likesData = Likes::getPostsLikes($data['id'], $this->uid, $friendIds);
$data['likes'] = $likesData['likes'];
$data['is_like'] = $likesData['is_like'];
$data['comment'] = Comment::getPostsComment($data['id'], $this->uid, $friendIds);
return success('', $data);
}
public function del()
{
try {
$posts_id = $this->request->param('posts_id');
if (empty($posts_id)) {
throw new \Exception('');
}
$existsWhere = [
'id' => $posts_id,
'user_id' => $this->uid,
'delete_time' => 0
];
$exists = (new PostsModel())->where($existsWhere)->find();
if (!$exists) {
throw new \Exception('');
}
$exists->delete_time = time();
$exists->save();
return success(lang('system.success'));
} catch (\Exception $e) {
return warning(lang('system.error'));
}
}
public function add() public function add()
{ {
$content = $this->request->param('content', ''); $content = $this->request->param('content', '');

25
app/enterprise/model/Comment.php

@ -6,5 +6,30 @@ use think\Model;
class Comment extends Model class Comment extends Model
{ {
public static function getPostsComment($relevance_id, $uid, $userIdArr = [])
{
$playWhere = [
['type', '=', 1],
['relevance_id', '=', $relevance_id],
['user_id', 'in', $userIdArr]
];
$comment = self::where($playWhere)->field('id,content,user_id,reply_user_id')->select();
foreach ($comment as &$commentValue) {
$friend = Friend::getFriendName($uid, $commentValue['user_id']);
$commentValue['nickname'] = $friend['nickname'];
$commentValue['reply_user_name'] = '';
if ($commentValue['reply_user_id']) {
$friend = Friend::getFriendName($uid, $commentValue['reply_user_id']);
$commentValue['reply_user_name'] = $friend['nickname'];
}
unset($commentValue['reply_user_id']);
}
return $comment;
}
} }

9
app/enterprise/model/Friend.php

@ -34,10 +34,15 @@ class Friend extends BaseModel
public static function getFriendName($uid, $friend_user_id) public static function getFriendName($uid, $friend_user_id)
{ {
$friendNickname = Friend::where(['create_user' => $uid, 'friend_user_id' => $friend_user_id, 'status' => 1])->value('nickname'); $friendNickname = Friend::where(['create_user' => $uid, 'friend_user_id' => $friend_user_id, 'status' => 1])->value('nickname');
$avatar = '';
if (empty($friendNickname)) { if (empty($friendNickname)) {
$user = User::getUserInfo(['user_id' => $friend_user_id, 'status' => 1], 'realname'); $user = User::getUserInfo(['user_id' => $friend_user_id, 'status' => 1], 'user_id,realname,avatar');
$avatar = $user['avatar'] ?: avatarUrl($user['avatar'], $user['realname'], $user['user_id'], 120);
$friendNickname = $user['realname']; $friendNickname = $user['realname'];
} }
return $friendNickname; return [
'nickname' => $friendNickname,
'avatar' => $avatar
];
} }
} }

23
app/enterprise/model/Likes.php

@ -7,4 +7,27 @@ use think\Model;
class Likes extends Model class Likes extends Model
{ {
public static function getPostsLikes($relevance_id, $uid, $userIdArr = [])
{
$playWhere = [
['type', '=', 1],
['relevance_id', '=', $relevance_id],
['user_id', 'in', $userIdArr]
];
$is_like = 0;
$likes = self::where($playWhere)->field('user_id')->select();
foreach ($likes as &$likesValue) {
if ($likesValue['user_id'] == $uid) {
$is_like = 1;
}
$friend = Friend::getFriendName($uid, $likesValue['user_id']);
$likesValue['nickname'] = $friend['nickname'];
$likesValue['avatar'] = $friend['avatar'];
}
return [
'likes' => $likes,
'is_like' => $is_like
];
}
} }

16
app/enterprise/model/PostsFile.php

@ -7,4 +7,20 @@ use think\Model;
class PostsFile extends Model class PostsFile extends Model
{ {
public static function getPostsFile($posts_id)
{
$fileWhere = [
'posts_id' => $posts_id,
'delete_time' => 0
];
$files = self::where($fileWhere)
->field('file_id,type')
->order('sort asc')
->select()
->toArray();
foreach ($files as $fileKey => $fileValue) {
$files[$fileKey]['src'] = File::where(['file_id' => $fileValue['file_id']])->value('src');
}
return $files;
}
} }

72
public/sql/database.sql

@ -335,3 +335,75 @@ ALTER TABLE `yu_user`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=21; MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=21;
COMMIT; COMMIT;
--
-- 表的结构 `yu_posts`
--
CREATE TABLE `yu_posts` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL DEFAULT '0' COMMENT '用户id',
`content` TEXT COMMENT '文字内容',
`type` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '1文字 2图片 3视频 12文字图片 13文字视频',
`privacy` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '0草稿 1公开 2部分可见 3私密 4不给谁看',
`location` CHAR(15) DEFAULT NULL COMMENT '发布地址',
`create_time` INT(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` INT(11) NOT NULL DEFAULT '0' COMMENT '更新时间',
`delete_time` INT(11) NOT NULL DEFAULT '0' COMMENT '删除时间',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT='帖子';
--
-- 表的结构 `yu_posts_file`
--
CREATE TABLE `yu_posts_file` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`posts_id` INT(11) NOT NULL COMMENT '朋友圈id',
`file_id` INT(11) NOT NULL COMMENT '文件id',
`sort` INT(11) NOT NULL DEFAULT '1' COMMENT '排序',
`delete_time` INT(11) NOT NULL DEFAULT '0' COMMENT '删除时间',
`type` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '1图片 2视频',
PRIMARY KEY (`id`),
KEY `posts_id` (`posts_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='帖子文件关联表';
--
-- 表的结构 `yu_posts_privacy_users`
--
CREATE TABLE `yu_posts_privacy_users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`posts_id` INT(10) UNSIGNED NOT NULL COMMENT '帖子id',
`user_id` INT(10) UNSIGNED NOT NULL COMMENT '用户id',
`type` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT '1可见用户 2排除用户',
PRIMARY KEY (`id`),
KEY `ix_posts_type` (`posts_id`,`type`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='帖子隐私关联用户';
--
-- 表的结构 `yu_likes`
--
CREATE TABLE `yu_likes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL DEFAULT '0' COMMENT '用户id',
`type` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '1 朋友圈',
`relevance_id` INT(11) NOT NULL DEFAULT '0' COMMENT '根据type关联',
`create_time` INT(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='点赞表';
--
-- 表的结构 `yu_comment`
--
CREATE TABLE `yu_comment` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL DEFAULT '0' COMMENT '用户id',
`content` VARCHAR(500) NOT NULL DEFAULT '' COMMENT '评论内容',
`type` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '1 朋友圈',
`relevance_id` INT(11) NOT NULL DEFAULT '0' COMMENT '根据type关联',
`reply_user_id` INT(11) NOT NULL DEFAULT '0' COMMENT '回复用户',
`pid` INT(11) NOT NULL DEFAULT '0' COMMENT '回复id',
`create_time` INT(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
`delete_time` INT(11) NOT NULL DEFAULT '0' COMMENT '删除时间',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT='评论表';

Loading…
Cancel
Save