diff --git a/app/enterprise/controller/Posts.php b/app/enterprise/controller/Posts.php new file mode 100644 index 0000000..614a036 --- /dev/null +++ b/app/enterprise/controller/Posts.php @@ -0,0 +1,358 @@ +uid; + + $friendIds = Friend::getFriendIds($this->uid); + + if ($friendIds) { + $friendIdStr = implode(',',$friendIds); + $whereOr .= ' or ( p.privacy = 1 and p.user_id in (' . $friendIdStr . '))'; + } + + $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 + ))"; + + // 不可见 + $whereOr .= " or ( p.privacy = 4 and not exists ( + select 1 from {$prefix}posts_privacy_users peu + where peu.posts_id = p.id and peu.user_id = p.user_id and peu.type = 2 + ))"; + + $field = 'p.id,p.content,p.location,p.type,p.user_id,p.create_time'; + $order = 'p.create_time desc'; + $model = new PostsModel(); + $list = $this->paginate($model->alias('p')->where($where)->whereOr($whereOr)->field($field)->order($order)); + + $data = []; + if ($list) { + $data = $list->toArray()['data']; + foreach ($data as &$item) { + + $friendNickname = Friend::where(['create_user' => $this->uid, 'friend_user_id' => $item['user_id'], 'status' => 1])->value('nickname'); + $user = User::getUserInfo(['user_id' => $item['user_id'], 'status' => 1], 'realname, avatar, user_id'); + if (empty($friendNickname)) { + $friendNickname = $user['realname']; + } + $item['user'] = [ + 'user_id' => $item['user_id'], + 'avatar' => $user['avatar'] ?: avatarUrl($user['avatar'], $user['realname'], $user['user_id'], 120), + 'nickname' => $friendNickname + ]; + + $item['location_address'] = ''; + if ($item['location']) { + $item['location_address'] = trim(implode(" ", array_unique(\Ip::find($item['location'])))); + } + + $fileWhere = [ + '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[] = $this->uid; + $playWhere = [ + ['type', '=', 1], + ['relevance_id', '=', $item['id']], + ['user_id', 'in', $userIdArr] + ]; + + $item['is_like'] = 0; + $likes = Likes::where($playWhere)->field('user_id')->select(); + foreach ($likes as $likesKey => $likesValue) { + if ($likesValue['user_id'] == $this->uid) { + $item['is_like'] = 1; + } + $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()); + } + + public function myPosts() + { + $where = [ + ['user_id', '=', $this->uid], + ['privacy', '>', 0], + ['delete_time', '=', 0] + ]; + + $field = 'id,content,location,type,user_id,create_time'; + $order = 'create_time desc'; + $model = new PostsModel(); + + $list = $this->paginate($model->where($where)->field($field)->order($order)); + $data = []; + if ($list) { + $data = $list->toArray()['data']; + foreach ($data as &$item) { + + + } + } + return success('', $data,$list->total(),$list->currentPage()); + } + + public function add() + { + $content = $this->request->param('content', ''); + $privacy = $this->request->param('privacy', ''); + $location = $this->request->param('location', ''); + $video_file_id = $this->request->param('file_id', ''); + $imgArr = $this->request->param('img_arr', []); + $posts_id = $this->request->param('posts_id', ''); + $user_ids = $this->request->param('user_ids', []); + + $privacy_user_arr = [2,4]; + if (in_array($privacy, $privacy_user_arr)) { + if (empty($user_ids) || !is_array($user_ids)) { + return error(lang('posts.user_empty')); + } + } + + $type = ''; + if (!empty($content)) { + $type = '1'; + } + if (!empty($imgArr)) { + $type .= '2'; + } elseif (!empty($video_file_id)) { + $type .= '3'; + } + $exists = []; + if ($posts_id) { + $existsWhere = [ + 'id' => $posts_id, + 'privacy' => 0, + 'user_id' => $this->uid + ]; + $exists = (new PostsModel())->where($existsWhere)->find()->toArray(); + if (!$exists) { + return error(lang('posts.exists')); + } + } + + Db::startTrans(); + try { + $info = [ + 'user_id' => $this->uid, + 'type' => $type, + 'content' => $content, + 'privacy' => $privacy, + 'location' => $location ?? '' + ]; + if ($posts_id) { + $info['update_time'] = time(); + PostsModel::update($info,['id' => $posts_id]); + PostsFile::update(['delete_time' => time()],['posts_id' => $posts_id]); + + if (in_array($exists['privacy'], $privacy_user_arr)) { + (new PostsPrivacyUsers())->where(['posts_id' => $posts_id])->delete(); + } + } else { + $info['create_time'] = time(); + $posts_id = (new PostsModel())->insertGetId($info); + } + + $fileSaveArr = []; + foreach ($imgArr as $key => $img_file_id) { + $fileSaveArr[] = [ + 'posts_id' => $posts_id, + 'type' => 1, + 'file_id' => $img_file_id, + 'sort' => $key + 1 + ]; + } + + if (!empty($video_file_id)) { + $fileSaveArr[] = [ + 'posts_id' => $posts_id, + 'type' => 2, + 'file_id' => $video_file_id, + 'sort' => 1 + ]; + } + + if ($fileSaveArr) { + (new PostsFile())->saveAll($fileSaveArr); + } + if (!empty($user_ids) && in_array($info['privacy'], $privacy_user_arr)) { + $user_type = $info['privacy'] == 2 ? 1 : 2; + $privacy_users = []; + foreach ($user_ids as $user_id) { + $privacy_users[] = [ + 'posts_id' => $posts_id, + 'user_id' => $user_id, + 'type' => $user_type + ]; + } + if ($privacy_users) { + (new PostsPrivacyUsers())->saveAll($privacy_users); + } + } + + Db::commit(); + return success(lang('system.addOk')); + } catch (\Exception $e) { + Db::rollback(); + return warning(lang('system.error')); + } + } + + public function getLastPosts() + { + $existsWhere = ['user_id' => $this->uid, 'privacy' => 0, 'delete_time' => 0]; + $field = 'id as posts_id,content,type,location'; + $data = PostsModel::where($existsWhere)->field($field)->find()->toArray(); + if ($data) { + if ($data['location']) { + $data['location_address'] = trim(implode(" ", array_unique(\Ip::find($data['location'])))); + } + $fileWhere = [ + 'posts_id' => $data['posts_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'); + } + $data['files'] = $files; + } + return success('', $data); + } + + public function like() + { + $posts_id = $this->request->param('posts_id'); + if (empty($posts_id)) { + return error(lang('system.fail')); + } + + $info = [ + 'user_id' => $this->uid, + 'type' => 1, + 'relevance_id' => $posts_id + ]; + $exists_id = Likes::where($info)->value('id'); + if ($exists_id) { + (new Likes())->where('id', $exists_id)->delete(); + + return success(lang('posts.cancel_like')); + } + + $info['create_time'] = time(); + (new Likes())->save($info); + + return success(lang('posts.success_like')); + } + + public function comment() + { + $posts_id = $this->request->param('posts_id'); + if (empty($posts_id)) { + return error(lang('system.fail')); + } + $content = $this->request->param('content'); + if (empty($content)) { + return error(lang('system.fail')); + } + + $pid = $this->request->param('pid', 0); + $reply_user_id = 0; + if (!empty($pid)) { + $reply_user_id = Comment::where('id', $pid)->value('user_id'); + } + + $info = [ + 'user_id' => $this->uid, + 'content' => $content, + 'type' => 1, + 'relevance_id' => $posts_id, + 'reply_user_id' => $reply_user_id, + 'pid' => $pid, + 'create_time' => time() + ]; + Comment::create($info); + return success(lang('system.success')); + } + + public function delComment() + { + $posts_id = $this->request->param('posts_id'); + if (empty($posts_id)) { + return error(lang('system.fail')); + } + $comment_id = $this->request->param('comment_id'); + if (empty($comment_id)) { + return error(lang('system.fail')); + } + + $existsWhere = [ + 'id' => $comment_id, + 'user_id' => $this->uid, + 'type' => 1, + 'relevance_id' => $posts_id + ]; + + $exists = (new Comment())->where($existsWhere)->find('id'); + if (!$exists) { + return error(lang('system.fail')); + } + $exists->delete_time = time(); + $exists->save(); + + return success(lang('system.success')); + } +} \ No newline at end of file diff --git a/app/enterprise/model/Comment.php b/app/enterprise/model/Comment.php new file mode 100644 index 0000000..1710b1e --- /dev/null +++ b/app/enterprise/model/Comment.php @@ -0,0 +1,10 @@ + $uid, 'delete_time' => 0, 'status' => 1]; + return self::where($where)->column('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'); + if (empty($friendNickname)) { + $user = User::getUserInfo(['user_id' => $friend_user_id, 'status' => 1], 'realname'); + $friendNickname = $user['realname']; + } + return $friendNickname; + } } \ No newline at end of file diff --git a/app/enterprise/model/Likes.php b/app/enterprise/model/Likes.php new file mode 100644 index 0000000..befcb36 --- /dev/null +++ b/app/enterprise/model/Likes.php @@ -0,0 +1,10 @@ +find(); + $data = self::where($map)->field($field)->find(); if ($data) { $data = $data->toArray(); } diff --git a/app/lang/en_us.php b/app/lang/en_us.php index 5dd163a..a3cbbc2 100644 --- a/app/lang/en_us.php +++ b/app/lang/en_us.php @@ -152,5 +152,11 @@ return [ ], 'scan' => [ 'failure' => 'QR code has expired' + ], + 'posts' => [ + 'exists' => 'Save failed', + 'user_empty' => 'Restricting users to not be empty', + 'success_like' => 'Liked successfully', + 'cancel_like' => 'Cancel likes' ] ]; \ No newline at end of file diff --git a/app/lang/zh_cn.php b/app/lang/zh_cn.php index 7e83a88..9860398 100644 --- a/app/lang/zh_cn.php +++ b/app/lang/zh_cn.php @@ -151,5 +151,11 @@ return [ ], 'scan'=>[ 'failure'=>'二维码已失效' + ], + 'posts'=>[ + 'exists' => '保存失败', + 'user_empty' => '限制用户不能为空', + 'success_like' => '点赞成功', + 'cancel_like' => '取消点赞' ] ]; \ No newline at end of file