1129 lines
47 KiB
PHP
1129 lines
47 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
use App\Models\Live\Live;
|
|
use App\Models\Live\Anchor;
|
|
use App\Models\CommunityTopic;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Community;
|
|
use App\Models\CommunityMember;
|
|
use App\Models\User;
|
|
use App\Models\Matchmaker;
|
|
use App\Models\CommunityGroup;
|
|
use App\Models\CommunityGroupLink;
|
|
use App\Models\CommunityComplaintHistory;
|
|
use App\Models\CommunityMoment;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use App\Models\KeywordHistory;
|
|
use App\Models\HotKeyword;
|
|
use App\Models\CommunityMomentComplaint;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Response\ResponseJson;
|
|
use App\Services\IMService;
|
|
use App\Contracts\OrderContract;
|
|
use App\Contracts\UserContract;
|
|
|
|
class CommunityController extends Controller
|
|
{
|
|
use ResponseJson;
|
|
protected $orderCon;
|
|
protected $userCon;
|
|
public function __construct(OrderContract $orderCon, UserContract $userCon){
|
|
$this->orderCon = $orderCon;
|
|
$this->userCon = $userCon;
|
|
}
|
|
|
|
/**
|
|
* 社群首页
|
|
* @param Request $request [description]
|
|
* @param CommunityGroup $community_group [description]
|
|
* @param CommunityMoment $community_moment [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communityHome(Request $request, CommunityGroup $community_group, CommunityMoment $community_moment)
|
|
{
|
|
try {
|
|
$user = auth()->user();
|
|
if (empty($user)) {
|
|
$user = $this->authCheck();
|
|
}
|
|
$community_home = $this->newCommunityHome($request, $community_group, $community_moment, $user);
|
|
$community_groups = $community_home['community_groups'];
|
|
$community_moments = $community_home['community_moments'];
|
|
|
|
//我加入的社群
|
|
// $community_ids = CommunityMember::where('user_id', $user->id)->pluck('community_id');
|
|
// $my_communites = Community::whereIn('id', $community_ids)->get();
|
|
return $this->success('ok', compact('community_groups', 'community_moments'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 加入的社群列表
|
|
* @param [type] $user_id [description]
|
|
* @param integer $limit [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getJoinedCommunities($request, $user_id, $limit=0)
|
|
{
|
|
$community_ids = CommunityMember::where('user_id', $user_id)->where('status', 1);
|
|
if ($limit) {
|
|
$community_ids = $community_ids->limit($limit);
|
|
}
|
|
$community_ids = $community_ids->pluck('community_id');
|
|
$joined_communities = Community::whereIn('id', $community_ids);
|
|
$keyword = $request->input('keyword');
|
|
if ($keyword) {
|
|
$joined_communities = $joined_communities->where('title', 'like', '%'.$keyword.'%');
|
|
}
|
|
$joined_communities = $joined_communities->where('is_hided', 0)->select('id', 'title', 'logo')->get();
|
|
foreach ($joined_communities as $community) {
|
|
$community->moment_count = $community->moment()->count()?:0;
|
|
}
|
|
return $joined_communities;
|
|
}
|
|
|
|
/**
|
|
* 创建的社群列表
|
|
* @param [type] $user_id [description]
|
|
* @param integer $limit [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getStoreCommunities($request, $user_id, $limit=0)
|
|
{
|
|
$store_communities = Community::where('user_id', $user_id);
|
|
$keyword = $request->input('keyword');
|
|
if ($keyword) {
|
|
$store_communities = $store_communities->where('title', 'like', '%'.$keyword.'%');
|
|
}
|
|
if ($limit) {
|
|
$store_communities = $store_communities->limit($limit);
|
|
}
|
|
$store_communities = $store_communities->where('is_hided', 0)->select('id', 'title', 'logo')->get();
|
|
foreach ($store_communities as $community) {
|
|
$community->moment_count = $community->moment()->count()?:0;
|
|
}
|
|
return $store_communities;
|
|
}
|
|
|
|
/**
|
|
* 社群组列表
|
|
* @param Request $request [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communityGroups(Request $request)
|
|
{
|
|
try {
|
|
//我加入的社群
|
|
$joined_communities = $this->getJoinedCommunities($request, auth()->id(), 3);
|
|
//社群组列表
|
|
$groups = CommunityGroup::select('id', 'logo', 'title');
|
|
$keyword = $request->input('keyword');
|
|
if ($keyword) {
|
|
$groups = $groups->where('title', 'like', '%'.$keyword.'%');
|
|
}
|
|
$groups = $groups->where('is_show', 1)->paginate(32);
|
|
//已经加入的社群数
|
|
$joined_community_count = $this->getJoinedCommunities($request, auth()->id())->count();
|
|
return $this->success('ok', compact('groups', 'joined_communities', 'joined_community_count'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取社群组失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 我加入的社群列表
|
|
* @param Request $request [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function joinedCommunities(Request $request)
|
|
{
|
|
$community_ids = CommunityMember::where('user_id', auth()->id())->where('status', 1)->pluck('community_id');
|
|
$joined_communities = Community::whereIn('id', $community_ids);
|
|
$keyword = $request->input('keyword');
|
|
if ($keyword) {
|
|
$joined_communities = $joined_communities->where('title','like', '%'.$keyword.'%');
|
|
}
|
|
$joined_communities = $joined_communities->withCount('moment')->where('is_hided', 0)->paginate();
|
|
return $this->success('ok', $joined_communities);
|
|
}
|
|
|
|
/**
|
|
* 最新社群首页
|
|
* @param [type] $request [description]
|
|
* @param [type] $community_group [description]
|
|
* @param [type] $community_moment [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function newCommunityHome($request, $community_group, $community_moment, $user)
|
|
{
|
|
//社群组
|
|
//是否缓存首页群组
|
|
$community_groups = Cache::get('home_community_groups');
|
|
if (empty($community_groups)) {
|
|
$community_groups = $community_group->select('id', 'title', 'logo')->where('is_show', 1)->limit(7)->orderBy('num','desc')->get();
|
|
Cache::put('home_community_groups', 60);
|
|
}
|
|
//所有社群动态
|
|
$community_moments = $community_moment->with(['user:id,nickname,photo', 'topic:id,name', 'community:id,title']);
|
|
if ($request->header('app-version') === config('app.app_version') && $request->header('client-os') === 'IOS' && config('app.env') == 'production') {
|
|
$user_ids = [7808, 58859, 616];
|
|
$community_moments = $community_moments->whereIn('user_id', $user_ids);
|
|
}
|
|
$community_moments = $community_moments->orderBy('id', 'desc')->paginate();
|
|
$community_moments = $this->getCommunityMoments($user, $community_moments);
|
|
$community_home = [
|
|
'community_groups'=>$community_groups,
|
|
'community_moments'=>$community_moments
|
|
];
|
|
return $community_home;
|
|
}
|
|
|
|
/**
|
|
* 社群动态列表
|
|
* @param [type] $user [description]
|
|
* @param [type] $community_moments [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getCommunityMoments($user, $community_moments)
|
|
{
|
|
foreach ($community_moments as $community_moment) {
|
|
$this->getCommunityMoment($community_moment);
|
|
}
|
|
return $community_moments;
|
|
}
|
|
|
|
public function getCommunityMoment($community_moment)
|
|
{
|
|
$photos = json_decode($community_moment->photos, true);
|
|
$photo_arr = [];
|
|
if(is_array($photos)){
|
|
|
|
foreach ($photos as $photo) {
|
|
$photo = $photo.'?x-oss-process=style/scale1';
|
|
$photo_arr[] = $photo;
|
|
}
|
|
}
|
|
$user = auth()->user();
|
|
$community_moment->photos = $photo_arr;
|
|
//自己是否点赞
|
|
$isLkerMoment = $community_moment->isLikedBy($user)?1:0;
|
|
//是否收藏
|
|
$isFavorite = $user->hasFavorited($community_moment)?1:0;
|
|
//是否是自己的
|
|
$is_self = $community_moment->user_id == $user->id?1:0;
|
|
//是否点赞
|
|
$community_moment->is_llker = $isLkerMoment;
|
|
//是否收藏
|
|
$community_moment->is_favorite = $isFavorite;
|
|
//点赞数
|
|
$community_moment->momentLikerCount = $community_moment->likers()->get()->count()?:0;
|
|
//收藏数
|
|
$community_moment->favoriteCount = $community_moment->favoriters()->get()->count()?:0;
|
|
//评论数
|
|
$community_moment->momentCommentCount = $community_moment->totalCommentsCount();
|
|
//是否是自己的
|
|
$community_moment->is_self = $is_self;
|
|
//时间转换
|
|
$community_moment->last_time = \CommonUtilsService::getLastTime(strtotime($community_moment->created_at->toDateTimeString()));
|
|
return $community_moment;
|
|
|
|
}
|
|
|
|
/**
|
|
* 举报社群动态
|
|
* @return [type] [description]
|
|
*/
|
|
public function complaintCommunityMoment(Request $request, $moment_id)
|
|
{
|
|
try {
|
|
//动态
|
|
$moment = CommunityMoment::find($moment_id);
|
|
if (empty($moment)) {
|
|
throw new \Exception("社群动态不存在", 1);
|
|
}
|
|
//举报内容
|
|
$content = $request->input('content');
|
|
if (empty($content)) {
|
|
return $this->failure('请输入举报内容');
|
|
}
|
|
//类型
|
|
$type = $request->input('type');
|
|
//举报图片
|
|
$pics = $request->input('pics', []);
|
|
|
|
// if (!is_array($pics)) {
|
|
// throw new \Exception("举报图片类型错误", 1);
|
|
// }
|
|
// if (count($pics) > 6) {
|
|
// return $this->failure('举报图片内容过多');
|
|
// }
|
|
CommunityMomentComplaint::create([
|
|
'user_id'=>auth()->id(),
|
|
'community_moment_id'=>$moment_id,
|
|
'content'=>$content,
|
|
'pics'=>json_encode($pics),
|
|
'type'=>$type
|
|
]);
|
|
return $this->success('ok', $moment);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('举报社群动态失败,请稍后再试');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 社群组下的社群列表
|
|
* @param Request $request [description]
|
|
* @param CommunityGroup $group [description]
|
|
* @return [type] [description]
|
|
*/
|
|
|
|
public function communityGroup(Request $request, CommunityGroup $community_group, Community $community)
|
|
{
|
|
$query = $community_group->communities();
|
|
$keyword = $request->input('keyword');
|
|
if ($keyword) {
|
|
$query = $query->where('title', 'like', '%'.$keyword.'%');
|
|
}
|
|
$communities = $query->where('is_hided', 0)->withCount('moment')->paginate();
|
|
//轮播
|
|
$carousel = $query->whereHas('members', function($sql){
|
|
$sql->where('id', '>', 0);
|
|
})->select('communities.id', 'communities.title')->where('is_hided', 0)->get();
|
|
foreach ($carousel as $ca) {
|
|
$member_arr = [];
|
|
$members = CommunityMember::with('user:id,nickname,photo')->where('community_id', $ca->id)->limit(3)->select('user_id','community_id')->orderBy('id', 'desc')->get();
|
|
foreach ($members as $member) {
|
|
$member_arr[] = $member->user;
|
|
}
|
|
$ca->members = $member_arr;
|
|
|
|
unset($ca->pivot);
|
|
}
|
|
return $this->success('ok', compact('community_group', 'communities', 'carousel'));
|
|
}
|
|
|
|
/**
|
|
* 创建社群
|
|
* @param Request $request [description]
|
|
* @param CommunityGroup $community_group [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function storeCommunity(Request $request, CommunityGroup $community_group, CommunityGroupLink $link, CommunityMember $community_member)
|
|
{
|
|
try {
|
|
\DB::beginTransaction();
|
|
if (mb_strlen($request->intro) > 512) {
|
|
return $this->failure('群介绍内容过多');
|
|
}
|
|
//创建社群
|
|
$community_obj = $community_group->communities()->create([
|
|
'user_id'=>auth()->id(),
|
|
'logo'=>$request->logo,
|
|
'title'=>$request->title,
|
|
'intro'=>$request->intro,
|
|
'is_free'=>$request->input('is_free', 1),
|
|
'price'=>$request->input('price', 0),
|
|
'owner_name'=>auth()->user()->nickname,
|
|
'owner_photo'=>auth()->user()->photo,
|
|
'member_num'=>1,
|
|
]);
|
|
|
|
//自动入群
|
|
$community_member->create([
|
|
'user_id'=>auth()->id(),
|
|
'community_id'=>$community_obj->id,
|
|
'status'=>1,
|
|
'is_manager'=>1,
|
|
]);
|
|
//创建网易云信群组 todo
|
|
$result = $community_obj->createIMGroup();
|
|
if (empty($result)) {
|
|
throw new \Exception("创建网易云信群组失败", 1);
|
|
}
|
|
\DB::commit();
|
|
unset($community_obj->user);
|
|
return $this->success('创建成功', $community_obj);
|
|
} catch (\Exception $e) {
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return $this->failure('创建社群失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 修改社群
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function updateCommunity(Request $request, Community $community)
|
|
{
|
|
try {
|
|
$change = false;
|
|
if ($community->user_id != auth()->id()) {
|
|
return $this->failure('暂无权限修改');
|
|
}
|
|
if ($request->input('logo') && $request->logo != $community->logo) {
|
|
$community->logo = $request->logo;
|
|
$change = true;
|
|
}
|
|
if ($request->input('title') && $request->title != $community->title) {
|
|
$community->title = $request->title;
|
|
$change = true;
|
|
}
|
|
if ($request->input('intro') && $request->intro != $community->intro) {
|
|
$community->intro = $request->intro;
|
|
$change = true;
|
|
}
|
|
if ($request->has('is_free') && $request->is_free != $community->is_free) {
|
|
$community->is_free = $request->is_free;
|
|
}
|
|
if ($request->has('price') && $request->price != $community->price) {
|
|
$community->price = $request->price;
|
|
}
|
|
$community->save();
|
|
if ($change) {
|
|
$result = $community->updateIMGroup();
|
|
if (empty($result)) {
|
|
throw new \Exception("修改网易云信群组失败", 1);
|
|
}
|
|
}
|
|
unset($community->user);
|
|
return $this->success('ok', $community);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('修改社群信息失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社群搜索记录
|
|
* @param Request $request [description]
|
|
* @param HotKeyword $hot_keyword [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communitySearchHistories(Request $request, HotKeyword $hot_keyword)
|
|
{
|
|
try {
|
|
$user = auth()->user();
|
|
//我的搜索记录
|
|
$keyword_histories = $user->keywordHistories()->orderBy('updated_at', 'desc')->limit(10)->get();
|
|
//热搜
|
|
$hot_keywords = $hot_keyword->orderBy('num', 'desc')->limit(10)->paginate();
|
|
return $this->success('ok', compact('keyword_histories', 'hot_keywords'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 清空搜索记录
|
|
* @param Request $request [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function clearCommunitySearch(Request $request)
|
|
{
|
|
auth()->user()->keywordHistories()->delete();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
/**
|
|
* 社群搜索结果
|
|
* @param Request $request [description]
|
|
* @param CommunityGroup $community_group [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function seaarchCommunities(Request $request, CommunityGroup $community_group, Community $community, KeywordHistory $keyword_history)
|
|
{
|
|
try {
|
|
$keyword = $request->input('keyword');
|
|
if (!empty($keyword)) {
|
|
//添加搜索记录,更新搜索
|
|
$keyword_history->addKeywordHistory($keyword, auth()->user());
|
|
//查找社群组
|
|
$community_groups = $community_group->where('title', 'like', '%'.$keyword.'%')->where('is_show', 1)->paginate();
|
|
//查找社群
|
|
$communities = $community->where('title', 'like', '%'.$keyword.'%')->where('is_hided', 0)->paginate();
|
|
return $this->success('ok', compact('community_groups', 'communities'));
|
|
}else{
|
|
return $this->failure('请输入搜索词');
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 动态详情
|
|
* @param Request $request [description]
|
|
* @param CommunityMoment $community_moment [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communityMoment(Request $request, CommunityMoment $community_moment)
|
|
{
|
|
try {
|
|
$community_moment = $this->getCommunityMoment($community_moment);
|
|
$comments = $community_moment->comments()->orderBy('id', 'desc')->paginate();
|
|
foreach ($comments as $comment) {
|
|
$comment->user = User::where('id', $comment->commented_id)->select('id', 'nickname', 'photo', 'type')->first();
|
|
}
|
|
$community_moment->comments = $comments;
|
|
$community_moment->topic = $community_moment->topic()->select('id', 'name')->first();
|
|
$community_moment->user = $community_moment->user()->select('id', 'nickname', 'photo')->first();
|
|
//社群信息
|
|
$community_moment->community = $community_moment->community()->select('id', 'title')->first();
|
|
//点过赞的
|
|
$liker_users = $community_moment->likers()->select('id', 'nickname', 'photo')->get();
|
|
foreach ($liker_users as $user) {
|
|
unset($user->uid, $user->is_new, $user->name, $user->face_score, $user->card_num, $user->circle_avatar, $user->mobile, $user->email, $user->location_longitude, $user->location_latitude, $user->temp_member, $user->industry, $user->industry_sub,$user->from_user_id,$user->from_openid,$user->from_official_openid, $user->from_platform,$user->friend_count,$user->my_qrcode,$user->my_qrcode_rect,$user->my_share_rect,$user->my_share,$user->home_share,$user->is_admin,$user->approve_time,$user->is_real_approved,$user->approve_date,$user->hidden,$user->is_audited,$user->is_photo_audited,$user->system_info,$user->identification_photos,$user->scene,$user->last_visit,$user->tag_num,$user->negative_score,$user->info_complete_score,$user->is_update_score,$user->is_subscribe,$user->mark,$user->created_at,$user->updated_at,$user->deleted_at);
|
|
unset($user->pivot);
|
|
}
|
|
$community_moment->liker_users = $liker_users;
|
|
return $this->success('ok', $community_moment);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社群详情
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function community(Request $request, Community $community)
|
|
{
|
|
try {
|
|
//热门话题
|
|
$topics = $community->topic()->where('is_show',1)->limit(4)->orderBy('click_num', 'desc')->get();
|
|
//社群动态
|
|
$community_moments = $community->moment()->with(['user:id,nickname,photo', 'topic:id,name', 'community:id,title']);
|
|
if ($request->header('app-version') === config('app.app_version') && $request->header('client-os') === 'IOS' && config('app.env') == 'production') {
|
|
$user_ids = [7808, 58859, 616];
|
|
$community_moments = $community_moments->whereIn('user_id', $user_ids);
|
|
}
|
|
$community_moments = $community_moments->orderBy('id', 'desc')->paginate();
|
|
//成员数
|
|
$community_moments = $this->getCommunityMoments(auth()->user(), $community_moments);
|
|
//是否是群主
|
|
$community->is_owner = (auth()->id() == $community->user_id)?1:0;
|
|
//是否加入群
|
|
$community->is_member = $community->members()->where('user_id', auth()->id())->where('status',1)->count()?1:0;
|
|
//群组名
|
|
$link = $community->link;
|
|
$group = $link?$link->group:null;
|
|
$community->group_title = $group?$group->title:null;
|
|
$community->group_id = $group?$group->id:null;
|
|
//最新成员
|
|
$community->members = $community->members()->where('status',1)->with('user:id,photo')->whereHas('user', function($sql){
|
|
$sql->whereNotNull('photo');
|
|
})->orderBy('id', 'desc')->limit(3)->select('user_id')->get();
|
|
foreach ($community->members as $member) {
|
|
$member->photo = $member->user->photo;
|
|
unset($member->user);
|
|
}
|
|
unset($community->link);
|
|
return $this->success('ok', compact('community', 'community_moments', 'topics'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社群详情(主要展示成员列表)
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
*/
|
|
public function CommunityDetail(Request $request, $tid)
|
|
{
|
|
try {
|
|
$community = Community::with('members.user:id,nickname,photo')->where('tid', $tid)->select('id', 'tid', 'title', 'intro', 'qrcode')->first();
|
|
if (empty($community)) {
|
|
throw new \Exception("社群不存在, 社群id:".$tid, 1);
|
|
}
|
|
foreach ($community->members as $member) {
|
|
$member->nickname = $member->user?$member->user->nickname:null;
|
|
$member->photo = $member->user?$member->user->photo:null;
|
|
unset($member->user);
|
|
}
|
|
return $this->success('ok', $community);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取群信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 话题动态列表
|
|
* @param Request $request [description]
|
|
* @param CommunityTopic $community_topic [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function topicMoments(Request $request, CommunityTopic $community_topic)
|
|
{
|
|
try {
|
|
$community_moments = $community_topic->moment()->with(['user:id,nickname,photo', 'topic:id,name', 'community:id,title']);
|
|
if ($request->header('app-version') === config('app.app_version') && $request->header('client-os') === 'IOS' && config('app.env') == 'production') {
|
|
$user_ids = [7808, 58859, 616];
|
|
$community_moments = $community_moments->whereIn('user_id', $user_ids);
|
|
}
|
|
$community_moments = $community_moments->orderBy('id', 'desc')->paginate();
|
|
$community_moments = $this->getCommunityMoments(auth()->user(), $community_moments);
|
|
$community_topic->increment('click_num', 1);
|
|
return $this->success('ok', compact('community_moments', 'community_topic'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建社群话题
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @param CommunityTopic $community_topic [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function storeCommunityMoment(Request $request, Community $community)
|
|
{
|
|
try {
|
|
\DB::beginTransaction();
|
|
$topic_id = $request->topic_id?:0;
|
|
$topic_status = $request->input('topic_status');
|
|
if ($topic_status == 2 && empty($topic_id)) {
|
|
//创建话题
|
|
$topic = $community->topic()->create(['name'=>$request->name, 'creater_id'=>auth()->id()]);
|
|
$topic_id = $topic->id;
|
|
}
|
|
//创建动态
|
|
$moment = $community->moment()->create([
|
|
'user_id'=>auth()->id(),
|
|
'topic_id'=>$topic_id,
|
|
'content'=>$request->input('content'),
|
|
'photos'=>json_encode($request->input('photos', [])),
|
|
]);
|
|
\DB::commit();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return $this->failure('发布动态失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社群话题列表
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communityTopics(Request $request, Community $community)
|
|
{
|
|
try {
|
|
$nopage = $request->input('nopage', 0);
|
|
$topics = $community->topic()->where('is_show',1)->orderBy('click_num', 'desc');
|
|
if ($nopage) {
|
|
$topics = $topics->limit($request->input('limit', 20))->get();
|
|
}else{
|
|
$topics = $topics->paginate();
|
|
}
|
|
return $this->success('ok',$topics);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社群所有话题列表分页
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communityPageTopics(Request $request, Community $community)
|
|
{
|
|
try {
|
|
$topics = $community->topic()->withCount('moment')->where('is_show',1)->orderBy('is_hot', 'asc')->orderBy('click_num', 'desc')->paginate();
|
|
$community = $community->select('id', 'tid', 'user_id', 'logo', 'title', 'click_num')->where('id', $community->id)->first();
|
|
//成员数
|
|
$community->member_count = $community->members()->where('status', 1)->count();
|
|
//最新成员头像
|
|
$members = $community->members()->with('user:id,photo')->whereHas('user', function($sql){
|
|
$sql->whereNotNull('photo');
|
|
})->where('status', 1)->select('user_id')->orderBy('id', 'desc')->limit(3)->get();
|
|
$last_members = [];
|
|
foreach ($members as $member) {
|
|
|
|
$last_members[] = $member->user->photo;
|
|
}
|
|
$community->last_members = $last_members;
|
|
$community->community_group = $community->links()->first()->group()->value('title');
|
|
|
|
return $this->success('ok', compact('community', 'topics'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取社群话题列表失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 加入社群
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function joinCommunity(Request $request, Community $community)
|
|
{
|
|
\DB::beginTransaction();
|
|
try {
|
|
$order_pay = null;
|
|
$user = auth()->user();
|
|
$result = $community->members()->where('user_id', $user->id)->exists();
|
|
if ($result) {
|
|
return $this->failure('已是社群成员');
|
|
}
|
|
//是否是群成员
|
|
//收费群还是免费群
|
|
if ($community->is_free) {//免费
|
|
//判断是否申请过
|
|
$member = $community->members()->firstOrCreate([
|
|
'user_id'=>$user->id,
|
|
'status'=>1
|
|
]);
|
|
//添加会员数
|
|
$community->increment('member_num', 1);
|
|
//生成支付订单
|
|
$order_pay = $this->orderCon->makeAppOrder($request, $community->price, 'community', $community->id, '加入社群【'.$community->title.'】', $linkmen=[], $detail='入群申请');
|
|
if (empty($order_pay)) throw new \Exception("订单生成失败", 1);
|
|
//拉入群聊
|
|
$result = $community->addIntoIMGroup([auth()->id()]);
|
|
if (empty($result)) throw new \Exception("邀请加入社群失败", 1);
|
|
}else{
|
|
//生成支付订单
|
|
$order_pay = $this->orderCon->makeAppOrder($request, $community->price, 'community', $community->id, '加入社群【'.$community->title.'】',[],'入群申请');
|
|
if (empty($order_pay)) throw new \Exception("订单生成失败", 1);
|
|
}
|
|
\DB::commit();
|
|
return $this->success('申请成功', $order_pay);
|
|
} catch (\Exception $e) {
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return $this->failure('入群失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社群成员
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communityMembers(Request $request, $community_id)
|
|
{
|
|
try {
|
|
$community = Community::where('id', $community_id)->select('id', 'tid', 'title', 'user_id', 'logo','member_num', 'click_num')->first();
|
|
list($community, $members) = $this->getCommunityMembers($community);
|
|
if (empty($community)) throw new \Exception("社群不存在", 1);
|
|
return $this->success('ok', compact('members', 'community'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
public function getCommunityMembers($community)
|
|
{
|
|
try {
|
|
$link = $community->link;
|
|
$group = $link?$link->group:null;
|
|
$community->group_title = $group?$group->title:null;
|
|
$community->group_id = $group?$group->id:null;
|
|
$members = $community->members()->with('user:id,nickname,photo')->paginate(30);
|
|
$community->user = $community->user()->select('id', 'nickname', 'photo')->first();
|
|
unset($community->link);
|
|
return [$community, $members];
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return [0, 0];
|
|
}
|
|
|
|
}
|
|
|
|
public function communityMembersByTid(Request $request, $tid)
|
|
{
|
|
try {
|
|
$community = Community::where('tid', $tid)->select('id', 'tid', 'title', 'user_id', 'logo','member_num', 'click_num')->first();
|
|
if(empty($community)){
|
|
return $this->failure('社群不存在');
|
|
}
|
|
list($community, $members) = $this->getCommunityMembers($community);
|
|
if (empty($community)) throw new \Exception("社群不存在", 1);
|
|
return $this->success('ok', compact('members', 'community'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社群聊天记录
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function communityChatMessages(Request $request, Community $community)
|
|
{
|
|
try {
|
|
$begintime = $request->input('begintime');
|
|
$endtime = $request->input('endtime');
|
|
if (empty($begintime) || empty($endtime)) {
|
|
return $this->failure('请输入开始时间和结束时间');
|
|
}
|
|
$tid = $community->tid;
|
|
if (empty($tid)) {
|
|
$tid = $community->createIMGroup();
|
|
if (empty($tid)) {
|
|
throw new \Exception("创建网易云信群组失败", 1);
|
|
}
|
|
}
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$accid = $community->user->mobile;
|
|
$result = $im_service->queryGroupMsg($tid,$accid,$begintime,$endtime,$limit='100',$reverse='1');
|
|
if ($result['code'] != 200) {
|
|
throw new \Exception("获取网易云信群聊天记录失败", 1);
|
|
}
|
|
return $this->success('ok', $result);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取群聊天信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 发送群聊消息
|
|
* @param Request $request [description]
|
|
* @param Community $community [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function sendCommunityMessage(Request $request, Community $community)
|
|
{
|
|
try {
|
|
if (empty($community->tid)) {
|
|
$community->tid = $community->createIMGroup();
|
|
}
|
|
$mine = auth()->user();
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$body = ['msg'=>$request->input('content')];
|
|
$result = $im_service->sendMsg($mine->mobile,$ope=1,$community->tid,$type=0,$body,$option=array("push"=>false,"roam"=>true,"history"=>false,"sendersync"=>true, "route"=>false));
|
|
if ($result['code'] != 200) {
|
|
throw new \Exception("发送云信群聊天信息失败", 1);
|
|
}
|
|
return $this->success('发送群聊消息成功');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('发送群聊天信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 评论社群动态
|
|
* @param Request $request [description]
|
|
* @param CommunityMoment $community_moment [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function commentCommunityMoment(Request $request, CommunityMoment $community_moment)
|
|
{
|
|
try {
|
|
$comment = $request->input('comment');
|
|
if (empty($comment)) {
|
|
return $this->failure('请输入评论内容');
|
|
}
|
|
$user = auth()->user();
|
|
$user->comment($community_moment, $comment);
|
|
$content = '用户'.$user->nickname.'评论了你的社群动态';
|
|
$this->userCon->sendNotice($community_moment->user_id, $user->id, 'comment', $content, '', $community_moment->id);
|
|
//添加IM自定义消息
|
|
$user->sendIMAttachMsg($user->id, $community_moment->user_id, 'comment', $content);
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('评论社群动态失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 点赞社群动态
|
|
* @param Request $request [description]
|
|
* @param CommunityMoment $community_moment [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function likeCommunityMoment(Request $request, CommunityMoment $community_moment)
|
|
{
|
|
try {
|
|
$user = auth()->user();
|
|
if ($user->hasLiked($community_moment)) {
|
|
$user->unlike($community_moment);
|
|
$is_llker = 0;
|
|
}else{
|
|
$user->like($community_moment);
|
|
$is_llker = 1;
|
|
$content = '用户'.$user->nickname.'点赞了你的社群动态';
|
|
$this->userCon->sendNotice($community_moment->user_id, $user->id, 'like', $content, '', $community_moment->id);
|
|
//添加IM自定义消息
|
|
$user->sendIMAttachMsg($user->id, $community_moment->user_id, 'like', $content);
|
|
}
|
|
return $this->success('ok', compact('is_llker'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('点赞社群动态失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 收藏社群动态
|
|
* @param Request $request [description]
|
|
* @param CommunityMoment $community_moment [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function favoriteCommunityMoment(Request $request, CommunityMoment $community_moment)
|
|
{
|
|
try {
|
|
$user = auth()->user();
|
|
if ($user->hasFavorited($community_moment)) {
|
|
$user->unfavorite($community_moment);
|
|
$is_favorite = 0;
|
|
}else{
|
|
$user->favorite($community_moment);
|
|
$is_favorite = 1;
|
|
}
|
|
return $this->success('ok', compact('is_favorite'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('收藏社群动态失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户加入的社群+创建的社群+动态列表
|
|
* @param Request $request [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function userCommunityCenter(Request $request, $user_id)
|
|
{
|
|
//创建的社群
|
|
$store_communities = $this->getStoreCommunities($request, $user_id);
|
|
//加入的社群
|
|
$joined_communities = $this->getJoinedCommunities($request, $user_id);
|
|
//发表的动态
|
|
$moments = CommunityMoment::where('user_id', $user_id)->with(['user:id,nickname,photo', 'topic:id,name', 'community:id,title'])->orderBy('id', 'desc')->paginate();
|
|
$user = User::where('id', $user_id)->select('id', 'nickname', 'photo')->first();
|
|
$moments = $this->getCommunityMoments($user, $moments);
|
|
$user->is_friend = auth()->user()->isFriend($user)?1:0;
|
|
$user->is_self = auth()->id() == $user->id?1:0;
|
|
return $this->success('ok', compact('user', 'store_communities', 'joined_communities', 'moments'));
|
|
}
|
|
|
|
/**
|
|
* 修改社群昵称
|
|
* @param Request $request [description]
|
|
* @param [type] $member_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function updateCommunityNick(Request $request, $community_id)
|
|
{
|
|
try {
|
|
$user_id = auth()->id();
|
|
$member = CommunityMember::with('community:id,tid,user_id')->where('user_id', $user_id)->where('community_id', $community_id)->where('status', 1)->first();
|
|
if (empty($member)) {
|
|
throw new \Exception("user_id为".$user_id."的群成员不存在", 1);
|
|
}
|
|
if (empty($member->community)) {
|
|
throw new \Exception("成员的社群不存在", 1);
|
|
}
|
|
$nickname = $request->input('nickname');
|
|
if (empty($nickname)) {
|
|
return $this->failure('请输入社群昵称');
|
|
}
|
|
if (mb_strlen($nickname) >32) {
|
|
return $this->failure('修改失败,昵称过长');
|
|
}
|
|
if ($nickname != $member->nickname) {
|
|
$member->nickname = $nickname;
|
|
$result = $member->updateCommunityNick($member->community->tid, $member->community->user_id, $user_id, $nickname);
|
|
if (empty($result)) throw new \Exception("修改昵称失败", 1);
|
|
$member->save();
|
|
}
|
|
return $this->success('ok', $member);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('修改社群昵称失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 退社群
|
|
* @param Request $request [description]
|
|
* @param [type] $community_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function leaveCommunity(Request $request, $community_id)
|
|
{
|
|
try {
|
|
$user_id = auth()->id();
|
|
$member = CommunityMember::with('community:id,tid,user_id')->where('user_id', $user_id)->where('community_id', $community_id)->where('status', 1)->first();
|
|
if (empty($member)) {
|
|
throw new \Exception("user_id为".$user_id."的群成员不存在", 1);
|
|
}
|
|
if (empty($member->community)) {
|
|
throw new \Exception("成员的社群不存在", 1);
|
|
}
|
|
$community = $member->community;
|
|
$result = $member->leaveCommunity($community->tid, $user_id);
|
|
if (empty($result)) throw new \Exception("退出社群失败", 1);
|
|
$member->delete();
|
|
return $this->success('ok', $community);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('退群失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 踢出社群
|
|
* @param Request $request [description]
|
|
* @param [type] $community_id [description]
|
|
* @param [type] $user_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function kickFromCommunity(Request $request, $user_id, $community_id)
|
|
{
|
|
try {
|
|
$community = Community::find($community_id);
|
|
//社群
|
|
if (empty($community)) {
|
|
throw new \Exception("社群不存在", 1);
|
|
}
|
|
$member = $this->checkCommunityPermission($community, $user_id);
|
|
if (empty($member)) throw new \Exception("社群权限不够", 1);
|
|
//踢出IM群
|
|
$result = $community->kickFromIMGroup(auth()->id(), $user_id);
|
|
if (empty($result)) throw new \Exception("社群踢人出群失败", 1);
|
|
//踢出福恋社群
|
|
$member->delete();
|
|
return $this->success('ok', $community);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('踢出社群失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 任命管理员
|
|
* @param Request $request [description]
|
|
* @param [type] $user_id [description]
|
|
* @param [type] $community_id [description]
|
|
*/
|
|
public function addCommunityManager(Request $request, $user_id, $community_id)
|
|
{
|
|
try {
|
|
$community = Community::find($community_id);
|
|
//社群
|
|
if (empty($community)) {
|
|
throw new \Exception("社群不存在", 1);
|
|
}
|
|
$member = $this->checkCommunityPermission($community, $user_id, 'owner');
|
|
if (empty($member)) throw new \Exception("社群权限不够", 1);
|
|
//任命IM群管理员
|
|
$result = $community->addIMGroupManager([$user_id]);
|
|
if (empty($result)) throw new \Exception("社群任命管理员失败", 1);
|
|
//任命福恋社群管理员
|
|
$member->is_manager = 1;
|
|
$member->save();
|
|
return $this->success('ok', $community);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('任命管理员失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 移除社群管理员
|
|
* @param Request $request [description]
|
|
* @param [type] $user_id [description]
|
|
* @param [type] $community_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function removeCommunityManager(Request $request, $user_id, $community_id)
|
|
{
|
|
try {
|
|
$community = Community::find($community_id);
|
|
//社群
|
|
if (empty($community)) {
|
|
throw new \Exception("社群不存在", 1);
|
|
}
|
|
$member = $this->checkCommunityPermission($community, $user_id, 'owner');
|
|
if (empty($member)) throw new \Exception("社群权限不够", 1);
|
|
$result = $community->removeIMGroupManager([$user_id]);
|
|
if (empty($result)) throw new \Exception("社群移除管理员失败", 1);
|
|
$member->is_manager = 0;
|
|
$member->save();
|
|
return $this->success('ok', $community);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('移除管理员失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查社群权限
|
|
* @param [type] $community [description]
|
|
* @param [type] $user_id [description]
|
|
* @param string $type [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function checkCommunityPermission($community, $user_id, $type='manager')
|
|
{
|
|
try {
|
|
$mine_user_id = auth()->id();
|
|
$community_id = $community->id;
|
|
//是否是群成员
|
|
$member = CommunityMember::where('community_id', $community_id)->where('user_id', $user_id)->where('is_sync_im', 1)->first();
|
|
if (empty($member)) {
|
|
throw new \Exception("当前用户还不是福恋群成员", 1);
|
|
}
|
|
if ($type === 'manager') {
|
|
//是否是管理员
|
|
$mine_member = CommunityMember::where('community_id', $community_id)->where('user_id', $mine_user_id)->where('is_sync_im', 1)->first();
|
|
if (empty($mine_member) || empty($mine_member->is_manager)) {
|
|
throw new \Exception("用户id:".$mine_user_id."还不是社群管理员", 1);
|
|
}
|
|
}elseif ($type === 'owner') {
|
|
//是否是群主
|
|
$mine_member = CommunityMember::where('community_id', $community_id)->where('user_id', $mine_user_id)->where('is_sync_im', 1)->first();
|
|
if (empty($mine_member) || $community->user_id != $mine_user_id) {
|
|
throw new \Exception("用户id:".$mine_user_id."还不是社群群主", 1);
|
|
}
|
|
}
|
|
return $member;
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|