1047 lines
43 KiB
PHP
1047 lines
43 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Events\News;
|
|
use App\Jobs\App\SendAttachMsg;
|
|
use App\Jobs\SendTemplateMsg;
|
|
use App\Models\App\MsgHistory;
|
|
use App\Models\App\Team;
|
|
use App\Models\App\UserTeam;
|
|
use App\Models\User;
|
|
use App\Models\Video;
|
|
use App\Models\Wechat;
|
|
use App\Services\IMService;
|
|
use App\Repositories\Eloquent\SmsRepository as Sms;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Contracts\UserContract;
|
|
use App\Http\Response\ResponseJson;
|
|
use App\Utils\Http;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ImController extends Controller
|
|
{
|
|
use ResponseJson;
|
|
protected $sms;
|
|
protected $userCon;
|
|
|
|
public function __construct(Sms $sms, UserContract $userCon)
|
|
{
|
|
$this->sms = $sms;
|
|
$this->userCon = $userCon;
|
|
}
|
|
|
|
//创建聊天群
|
|
public function createTeam(Request $request)
|
|
{
|
|
if (!$request->tname) {
|
|
return $this->failure('群名不能为空');
|
|
}
|
|
if (!$request->owner) {
|
|
return $this->failure('群主id不能为空');
|
|
}
|
|
$members = $request->members ?: [];
|
|
$msg = $request->msg ?: '邀请加入';
|
|
$custom = [];
|
|
if ($request->remark) {
|
|
$custom['remark'] = $request->remark;
|
|
}
|
|
try {
|
|
//请求网易群聊接口
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$url = 'https://api.netease.im/nimserver/team/create.action';
|
|
$data['members'] = json_encode($members);
|
|
$data['tname'] = $request->tname;
|
|
$data['sort'] = $request->sort ? $request->sort : 0;
|
|
$data['owner'] = $request->owner;
|
|
$data['msg'] = $msg;
|
|
$data['magree'] = $request->magree ?: 0;
|
|
$data['joinmode'] = $request->joinmode ?: 0;
|
|
if ($request->announcement) {
|
|
$data['announcement'] = $request->announcement;
|
|
}
|
|
if ($request->intro) {
|
|
$data['intro'] = $request->intro;
|
|
}
|
|
if ($request->desc) {
|
|
$data['desc'] = $request->desc;
|
|
}
|
|
if ($request->logo) {
|
|
$data['logo'] = $request->logo;
|
|
}
|
|
if ($request->official) {
|
|
$data['official'] = $request->official;
|
|
}
|
|
if ($request->type) {
|
|
$data['type'] = $request->type;
|
|
}
|
|
if (!empty($custom)) {
|
|
$data['custom'] = json_encode($custom);
|
|
}
|
|
if ($request->icon) {
|
|
$data['icon'] = $request->icon;
|
|
}
|
|
if ($request->beinvitemode) {
|
|
$data['beinvitemode'] = $request->beinvitemode;
|
|
}
|
|
if ($request->invitemode) {
|
|
$data['invitemode'] = $request->invitemode;
|
|
}
|
|
if ($request->uptinfomode) {
|
|
$data['uptinfomode'] = $request->uptinfomode;
|
|
}
|
|
if ($request->upcustommode) {
|
|
$data['upcustommode'] = $request->upcustommode;
|
|
}
|
|
if ($request->teamMemberLimit) {
|
|
$data['teamMemberLimit'] = $request->teamMemberLimit;
|
|
}
|
|
$result = $im_service->postDataCurl($url, $data);
|
|
if ($result['code'] != 200 || !$result['tid']) {
|
|
switch ($result['code']) {
|
|
case 806:
|
|
return $this->failure('该用户创建群聊数量达到100个上限');
|
|
break;
|
|
default:
|
|
return $this->failure('创建群聊失败');
|
|
break;
|
|
}
|
|
}
|
|
|
|
\DB::beginTransaction();
|
|
$team = new Team();
|
|
$team->id = $result['tid'];
|
|
$team->tid = $result['tid'];
|
|
$team->tname = $request->tname;
|
|
$team->owner = $request->owner ?: 0;
|
|
$team->announcement = $request->announcement;
|
|
$team->sort = $request->sort ? $request->sort : 0;
|
|
$team->intro = $request->intro;
|
|
$team->logo = $request->logo;
|
|
$team->desc = $request->desc;
|
|
$team->official = $request->official;
|
|
$team->type = $request->type;
|
|
$team->msg = $request->msg;
|
|
$team->magree = $request->owner ?: 0;
|
|
$team->joinmode = $request->joinmode ?: 0;
|
|
$team->custom = json_encode($custom);
|
|
$team->icon = $request->icon;
|
|
$team->beinvite_mode = $request->beinvite_mode ?: 0;
|
|
$team->invite_mode = $request->invite_mode ?: 0;
|
|
$team->uptinfo_mode = $request->uptinfo_mode ?: 0;
|
|
$team->upcustom_mode = $request->upcustom_mode ?: 0;
|
|
$team->team_member_limit = $request->team_member_limit ?: 200;
|
|
$team->save();
|
|
|
|
$insert[0] = [
|
|
'user_id' => $request->owner,
|
|
'team_id' => $result['tid'],
|
|
'type' => 1,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
if (!empty($request->members)) {
|
|
// SendAttachMsg::dispatch($request->members, 'team', $team->id, '你已被邀请加入'.$team->tname.'群聊')->onQueue('love');
|
|
$this->send($request->members, 'team', $team->id, '你已被邀请加入' . $team->tname . '群聊');
|
|
foreach ($request->members as $val) {
|
|
$arr = [
|
|
'user_id' => $val,
|
|
'team_id' => $result['tid'],
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
$insert[] = $arr;
|
|
}
|
|
}
|
|
UserTeam::insert($insert);
|
|
\DB::commit();
|
|
return $this->success('ok', ['id' => $result['tid']]);
|
|
} catch (\Exception $e) {
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return $this->failure('创建群聊失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
//修改群聊
|
|
public function updateTeam(Request $request, $team_id)
|
|
{
|
|
$team = Team::find($team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
try {
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$url = 'https://api.netease.im/nimserver/team/update.action';
|
|
//同步资料
|
|
if ($request->remark) {
|
|
if (empty($team->custom)) {
|
|
$custom['remark'] = $request->remark;
|
|
$team->custom = json_encode($custom);
|
|
$data['custom'] = json_encode($custom);
|
|
} else {
|
|
if (empty($team->custom->remark)) {
|
|
$custom = $team->custom;
|
|
$custom->remark = $request->remark;
|
|
$team->custom = json_encode($custom);
|
|
$data['custom'] = json_encode($custom);
|
|
} else {
|
|
if ($request->remark != $team->custom->remark) {
|
|
$custom = $team->custom;
|
|
$custom->remark = $request->remark;
|
|
$team->custom = json_encode($custom);
|
|
$data['custom'] = json_encode($custom);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// if($request->play_url){
|
|
// $play_url = $request->play_url;
|
|
// if(empty($team->custom)){
|
|
// $custom['play_url'] = $play_url;
|
|
// $team->custom = json_encode($custom);
|
|
// $data['custom'] = json_encode($custom);
|
|
// }else{
|
|
// if(empty($team->custom->play_url)){
|
|
// $custom = $team->custom;
|
|
// $custom->play_url = $play_url;
|
|
// $team->custom = json_encode($custom);
|
|
// $data['custom'] = json_encode($custom);
|
|
// }else{
|
|
// if($play_url != $team->custom->play_url){
|
|
// $custom = $team->custom;
|
|
// $custom->play_url = $play_url;
|
|
// $team->custom = json_encode($custom);
|
|
// $data['custom'] = json_encode($custom);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
$data['tid'] = $team->tid;
|
|
$data['owner'] = $team->owner;
|
|
if ($request->tname && $request->tname != $team->tname) {
|
|
$data['tname'] = $request->tname;
|
|
$team->tname = $request->tname;
|
|
}
|
|
if ($request->announcement && $request->announcement != $team->announcement) {
|
|
$data['announcement'] = $request->announcement;
|
|
$team->announcement = $request->announcement;
|
|
}
|
|
if ($request->intro && $request->intro != $team->intro) {
|
|
$data['intro'] = $request->intro;
|
|
$team->intro = $request->intro;
|
|
}
|
|
if ($request->logo && $request->logo != $team->logo) {
|
|
$data['logo'] = $request->logo;
|
|
$team->logo = $request->logo;
|
|
}
|
|
if ($request->desc && $request->desc != $team->desc) {
|
|
$data['desc '] = $request->desc;
|
|
$team->desc = $request->desc;
|
|
}
|
|
if (is_numeric($request->sort) && $request->sort != $team->sort) {
|
|
$data['sort '] = $request->sort;
|
|
$team->sort = $request->sort;
|
|
}
|
|
if ($request->official && $request->official != $team->official) {
|
|
$data['official '] = $request->official;
|
|
$team->official = $request->official;
|
|
}
|
|
if (is_numeric($request->joinmode) && $request->joinmode != $team->joinmode) {
|
|
$data['joinmode'] = $request->joinmode;
|
|
$team->joinmode = $request->joinmode;
|
|
}
|
|
// if($custom && json_encode($custom) != $team->custom){
|
|
// $data['custom'] = json_encode($custom);
|
|
// $team->custom = json_encode($custom);
|
|
// }
|
|
|
|
if ($request->icon && $request->icon != $team->icon) {
|
|
$data['icon'] = $request->icon;
|
|
$team->icon = $request->icon;
|
|
}
|
|
if ($request->type && $request->type != $team->type) {
|
|
$data['type'] = $request->type;
|
|
$team->type = $request->type;
|
|
}
|
|
if (is_numeric($request->beinvitemode) && $request->beinvitemode != $team->beinvitemode) {
|
|
$data['beinvitemode'] = $request->beinvitemode;
|
|
$team->beinvitemode = $request->beinvitemode;
|
|
}
|
|
if (is_numeric($request->invitemode) && $request->invitemode != $team->invitemode) {
|
|
$data['invitemode'] = $request->invitemode;
|
|
$team->invitemode = $request->invitemode;
|
|
}
|
|
if (is_numeric($request->upcustommode) && $request->upcustommode != $team->upcustommode) {
|
|
$data['upcustommode'] = $request->upcustommode;
|
|
$team->upcustommode = $request->upcustommode;
|
|
}
|
|
if (is_numeric($request->teamMemberLimit) && $request->teamMemberLimit != $team->team_member_limit) {
|
|
$data['teamMemberLimit'] = $request->teamMemberLimit;
|
|
$team->team_member_limit = $request->teamMemberLimit;
|
|
}
|
|
|
|
// $result = $im_service->postDataCurl($url,$data);
|
|
$result['code'] = 200;
|
|
// Log::info($result);
|
|
\DB::beginTransaction();
|
|
if ($result['code'] == 200) {
|
|
if ($request->tname && $team->tname != $request->tname) {
|
|
$team->tname = $request->tname;
|
|
}
|
|
|
|
//更换群主
|
|
if ($request->owner && $team->owner != $request->owner) {
|
|
//是否加入
|
|
$is_join = UserTeam::where('team_id', $team_id)->where('user_id', $request->owner)->count();
|
|
if (!$is_join) {
|
|
//拉人
|
|
$result = $im_service->addIntoGroup($team_id, $team->owner, [(string) $request->owner]);
|
|
// $result = $im_service->addIntoSuperGroup($team_id, $team->owner, [$request->owner]);
|
|
Log::info("拉人" . $request->owner);
|
|
Log::info($result);
|
|
if ($result['code'] == 200) {
|
|
UserTeam::insert([
|
|
'user_id' => $request->owner,
|
|
'team_id' => (string) $team_id,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
]);
|
|
} else {
|
|
DB::rollBack();
|
|
throw new \Exception("拉人入群失败 群主 " . $team->owner . json_encode($result));
|
|
}
|
|
}
|
|
//设置为群主
|
|
$result = $im_service->changeGroupOwner($team_id, $team->owner, $request->owner);
|
|
// $result = $im_service->changeSuperGroupOwner($team_id, $team->owner, $request->owner);
|
|
Log::info("修改群主");
|
|
Log::info($result);
|
|
if ($result['code'] == 200) {
|
|
UserTeam::where('team_id', $team_id)->where('user_id', $team->owner)->update(['type' => 0]);
|
|
$team->owner = $request->owner;
|
|
$team->save();
|
|
UserTeam::where('team_id', $team_id)->where('user_id', $request->owner)->update(['type' => 1]);
|
|
} else {
|
|
DB::rollBack();
|
|
throw new \Exception("修改群主失败 " . json_encode($result));
|
|
}
|
|
}
|
|
|
|
$team->save();
|
|
}
|
|
|
|
$notice_user_ids = [];
|
|
//增删群成员
|
|
$user_ids = UserTeam::where('team_id', $team_id)->pluck('user_id')->toArray();
|
|
\Log::info("是否拉人入群");
|
|
\Log::info($request->has('members') && !empty($request->members) && array_diff($request->members, $user_ids));
|
|
if ($request->has('members') && !empty($request->members) && array_diff($request->members, $user_ids)) {
|
|
//网易端拉人
|
|
$result = $im_service->addIntoGroup($team->id, $team->owner, $request->members);
|
|
|
|
if ($result['code'] == 200) {
|
|
$this->send($request->members, 'team', $team->id, '你已被邀请加入' . $team->tname . '群聊');
|
|
$insert = [];
|
|
$members = array_unique($request->members);
|
|
foreach ($members as $val) {
|
|
if (!in_array($val, $user_ids)) {
|
|
$arr = [
|
|
'user_id' => $val,
|
|
'team_id' => (string) $team->id,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
$insert[] = $arr;
|
|
$notice_user_ids[] = $val;
|
|
}
|
|
}
|
|
UserTeam::insert($insert);
|
|
} else {
|
|
\Log::info('addIntoGroup fail:' . json_encode($result));
|
|
}
|
|
}
|
|
\DB::commit();
|
|
//公众号通知
|
|
$notice_openid_map = Wechat::whereIn('user_id', $notice_user_ids)
|
|
->whereNotNull('official_openid')
|
|
->pluck('official_openid')
|
|
->toArray();
|
|
foreach ($notice_openid_map as $openid) {
|
|
//模板消息通知
|
|
$data['touser'] = $openid;
|
|
$data['template_id'] = config('wechat.tpls.start_message_notice');
|
|
$data['url'] = '';
|
|
$data['miniprogram'] = [
|
|
'appid' => config('wechat.mini_program.app_id'),
|
|
'pagepath' => '/pages/tabBar/news'
|
|
];
|
|
$data['data'] = [
|
|
"first" => "你收到新的群消息",
|
|
'keyword1' => date('Y-m-d H:i:s'),
|
|
'keyword2' => "您被邀请进入群聊{$request->tname}",
|
|
"remark" => "点击查看"
|
|
];
|
|
SendTemplateMsg::dispatch($data)->onQueue('start_message');
|
|
}
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return $this->failure('修改群聊失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
//群聊列表
|
|
public function teams(Request $request)
|
|
{
|
|
$teams = Team::withCount('members')->with('owner:id,name,nickname,app_avatar,photo,circle_avatar')->orderBy('created_at', 'desc');
|
|
if ($request->keyword) {
|
|
$teams = $teams->where('tname', 'like', '%' . $request->keyword . '%')->orWhere('tid', $request->keyword)->orWhere('owner', $request->keyword);
|
|
}
|
|
$teams = $teams->paginate();
|
|
return $this->success('ok', $teams);
|
|
}
|
|
|
|
//群聊详情
|
|
public function team(Request $request, $team_id)
|
|
{
|
|
$team = Team::with('owner:id,name,nickname,app_avatar,photo,circle_avatar,mobile')->where('id', $team_id)->first();
|
|
\DB::enableQueryLog();
|
|
$members = $team->members();
|
|
if ($request->keyword) {
|
|
$keyword = $request->keyword;
|
|
$members = $members->where(function ($query) use ($keyword) {
|
|
$query->where('nickname', 'like', '%' . $keyword . '%')->orWhere('mobile', 'like', '%' . $keyword . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->has('city') && $request->city) {
|
|
$city = $request->city;
|
|
$members = $members->whereHas('profileCourtship', function ($query) use ($city) {
|
|
$query->where('city', 'like', '%' . $city . '%');
|
|
});
|
|
}
|
|
if ($request->has('province') && $request->province) {
|
|
$province = $request->province;
|
|
$members = $members->whereHas('profileCourtship', function ($query) use ($province) {
|
|
$query->where('province', 'like', '%' . $province . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->has('resident_city') && $request->resident_city) {
|
|
$resident_city = $request->resident_city;
|
|
$members = $members->whereHas('profileCourtship', function ($query) use ($resident_city) {
|
|
$query->where('resident_city', 'like', '%' . $resident_city . '%');
|
|
});
|
|
}
|
|
if ($request->has('resident_province') && $request->resident_province) {
|
|
$resident_province = $request->resident_province;
|
|
$members = $members->whereHas('profileCourtship', function ($query) use ($resident_province) {
|
|
$query->where('resident_province', 'like', '%' . $resident_province . '%');
|
|
});
|
|
}
|
|
|
|
if (!empty($request->birthday[0]) && is_array($request->birthday)) {
|
|
$birthday = $request->birthday;
|
|
$members = $members->whereHas('profileCourtship', function ($query) use ($birthday) {
|
|
$query->whereBetween('birthday', $birthday);
|
|
});
|
|
}
|
|
$members = $members->paginate();
|
|
if (!empty($members)) {
|
|
foreach ($members as $val) {
|
|
$val->birthday = !empty($val->profileCourtship->birthday) ? $val->profileCourtship->birthday : '';
|
|
$val->mute = $val->pivot->mute;
|
|
$val->is_owner = $team->owner == $val->id ? 1 : 0;
|
|
$custom = !empty($val->pivot->custom) ? json_decode($val->pivot->custom) : '';
|
|
$val->pivot->class = !empty($custom->class) ? $custom->class : '';
|
|
$val->pivot->is_anchor = !empty($custom->is_anchor) ? $custom->is_anchor : '';
|
|
}
|
|
}
|
|
$team->members = $members;
|
|
|
|
return $this->success('ok', $team);
|
|
}
|
|
|
|
//聊天记录
|
|
public function teamMsg(Request $request, $team_id)
|
|
{
|
|
$keyword = $request->keyword;
|
|
$msg_history = MsgHistory::with('user:id,mobile,name,nickname,photo,app_avatar,circle_avatar')->where('team_id', $team_id);
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$msg_history = $msg_history->whereHas('user', function ($sql) use ($keyword) {
|
|
$sql->where('id', $keyword)->orWhere('nickname', 'like', '%' . $keyword . '%')->orWhere('mobile', 'like', '%' . $keyword . '%');
|
|
});
|
|
}
|
|
$msg_history = $msg_history->orderBy('id', 'desc')->paginate();
|
|
return $this->success('ok', $msg_history);
|
|
}
|
|
|
|
//删除聊天记录
|
|
public function delMsg(Request $request, $msg_id)
|
|
{
|
|
$msg_history = MsgHistory::where('id', $msg_id)->delete();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//解散群聊
|
|
public function disbandTeam(Request $request, $team_id)
|
|
{
|
|
$team = Team::find($team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$result = $im_service->removeGroup($team->tid, $team->owner);
|
|
if ($result['code'] != 200) {
|
|
return $this->failure('解散失败');
|
|
}
|
|
$team->delete();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//踢人
|
|
public function kickFromTeam(Request $request, $team_id)
|
|
{
|
|
if (empty($request->user_id)) {
|
|
return $this->failure('请输入用户id');
|
|
}
|
|
|
|
$is_join = UserTeam::where('team_id', $team_id)->where('user_id', $request->user_id)->count();
|
|
if (!$is_join) {
|
|
return $this->failure('该用户不在群聊');
|
|
}
|
|
$team = Team::find($team_id);
|
|
if ($team->owner == $request->user_id) {
|
|
return $this->failure('不能踢群主');
|
|
}
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$result = $im_service->kickFromGroup($team->tid, $team->owner, $request->user_id, '');
|
|
if ($result['code'] == 200) {
|
|
UserTeam::where('user_id', $request->user_id)->where('team_id', $team_id)->delete();
|
|
}
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//禁言/解除禁言
|
|
public function muteFromTeam(Request $request, $team_id)
|
|
{
|
|
if (empty($request->user_id)) {
|
|
return $this->failure('请输入用户id');
|
|
}
|
|
$is_join = UserTeam::where('team_id', $team_id)->where('user_id', $request->user_id)->count();
|
|
if (!$is_join) {
|
|
return $this->failure('该用户不在群聊');
|
|
}
|
|
$team = Team::find($team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
if ($team->owner == $request->user_id) {
|
|
return $this->failure('不能禁言群主');
|
|
}
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$result = $im_service->mute($team->tid, $team->owner, $request->user_id, $request->mute);
|
|
if ($result['code'] == 200) {
|
|
UserTeam::where('team_id', $request->team_id)->where('user_id', $request->user_id)->update(['mute' => $request->mute]);
|
|
} else {
|
|
return $this->failure('禁言失败');
|
|
}
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//禁止加入群聊
|
|
public function setJoinLive(Request $request, $team_id)
|
|
{
|
|
if (empty($request->user_id)) {
|
|
return $this->failure('请输入用户id');
|
|
}
|
|
$is_join = UserTeam::where('team_id', $team_id)->where('user_id', $request->user_id)->count();
|
|
if (!$is_join) {
|
|
return $this->failure('该用户不在群聊');
|
|
}
|
|
$team = Team::find($team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
if ($team->owner == $request->user_id) {
|
|
return $this->failure('不能禁言群主');
|
|
}
|
|
UserTeam::where('team_id', $team_id)->where('user_id', $request->user_id)->update(['join_live' => $request->join_live]);
|
|
return $this->success('ok');
|
|
}
|
|
|
|
public function send($user_id, $type, $type_id, $content)
|
|
{
|
|
switch ($type) {
|
|
case 'team':
|
|
//\Log::info('SendAttachMsg.team');
|
|
$team = Team::find($type_id);
|
|
break;
|
|
default:
|
|
//\Log::info('SendAttachMsg.default');
|
|
break;
|
|
}
|
|
if (!empty($team)) {
|
|
foreach ($user_id as $id) {
|
|
$result = $this->sendAttachMsg($type_id, 'team', $content, $team->owner, $id, $team->icon);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//发送系统通知
|
|
public function sendAttachMsg($type_id, $type, $content, $from, $to, $image)
|
|
{
|
|
$body = "点击查看>>";
|
|
//发送评论自定义系统消息
|
|
$im_service = new IMService();
|
|
$attach = ["myattach" => $content, $type . '_id' => $type_id];
|
|
$payload = [
|
|
"apsField" => [
|
|
"alert" => [
|
|
"title" => $content,
|
|
"body" => $body,
|
|
],
|
|
"mutable-content" => 1
|
|
],
|
|
"hwField" => [
|
|
"click_action" => [
|
|
"type" => 1,
|
|
"intent" => "intent://com.huawei.codelabpush/deeplink?id=" . $type_id . "&customType=" . $type . "#Intent;scheme=pushscheme;launchFlags=0x4000000;end",
|
|
],
|
|
"title" => $content,
|
|
"body" => $body,
|
|
],
|
|
"oppoField" => [
|
|
"click_action_type" => 1,
|
|
"click_action_activity" => "com.oppo.codelabpush.intent.action.test",
|
|
"action_parameters" => [
|
|
"id" => $type_id,
|
|
"customType" => "moment",
|
|
],
|
|
],
|
|
"apnsText" => $body,
|
|
"pushTitle" => $content,
|
|
"customType" => $type,
|
|
"content" => $body,
|
|
"media_image" => $image,
|
|
"media_type" => 'image',
|
|
"id" => $type_id,
|
|
"type" => $type,
|
|
'passThrough' => 1,
|
|
];
|
|
$result = $im_service->sendAttachMsg($from, 0, $to, json_encode($attach), $content, $payload);
|
|
return true;
|
|
}
|
|
|
|
//修改群成员信息
|
|
public function updateTeamNick(Request $request, $team_id)
|
|
{
|
|
if (empty($request->user_id)) {
|
|
return $this->failure('请输入用户id');
|
|
}
|
|
$user_team = UserTeam::where('team_id', $team_id)->where('user_id', $request->user_id)->first();
|
|
if (!$user_team) {
|
|
return $this->failure('该用户不在群聊');
|
|
}
|
|
$team = Team::find($team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
//扩展字段
|
|
$custom = [];
|
|
if ($request->class) {
|
|
$custom['class'] = $request->class;
|
|
}
|
|
if ($request->has('is_anchor')) {
|
|
$custom['is_anchor'] = $request->is_anchor;
|
|
//生成群主播记录
|
|
if ($request->is_anchor) {//直播
|
|
$team->anchors()->firstOrCreate(['user_id' => $request->user_id, 'team_id' => $team_id]);
|
|
//任命管理员
|
|
// $im_service= new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
// $result = $im_service->addGroupManager($team_id, $team->owner, [$request->user_id]);
|
|
// \Log::info($result);
|
|
} else {
|
|
$team->anchors()->where('user_id', $request->user_id)->where('team_id', $team_id)->delete();
|
|
}
|
|
}
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$url = 'https://api.netease.im/nimserver/team/updateTeamNick.action';
|
|
$data['tid'] = $team_id;
|
|
$data['owner'] = $team->owner;
|
|
$data['accid'] = $request->user_id;
|
|
if ($user_team->nick != $request->nick) {
|
|
$user_team->nick = $request->nick;
|
|
$data['nick'] = $request->nick;
|
|
}
|
|
if ($user_team->custom != json_encode($custom)) {
|
|
$user_team->custom = json_encode($custom);
|
|
$data['custom'] = json_encode($custom);
|
|
}
|
|
$result = $im_service->postDataCurl($url, $data);
|
|
if ($result['code'] != 200) {
|
|
return $this->failure('更新群成员信息失败');
|
|
}
|
|
$user_team->save();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
|
|
public function teamInfo(Request $request, $team_id)
|
|
{
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$result = $im_service->queryDetailGroup($team_id);
|
|
return $this->success($result['tinfo']);
|
|
$teams = Team::get();
|
|
foreach ($teams as $team) {
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$result = $im_service->queryDetailGroup($team->id);
|
|
if ($result['code'] == 200) {
|
|
$wangyi_members = array_column($result['tinfo']['members'], 'accid');
|
|
$wangyi_owner = array_column($result['tinfo']['owner'], 'accid');
|
|
$wangyi_admins = array_column($result['tinfo']['admins'], 'accid');
|
|
$wangyi_members = array_merge($wangyi_members, $wangyi_admins, $wangyi_owner);
|
|
$wangyi_members[] = $team->owner;
|
|
$local_members = UserTeam::where('team_id', $team->id)->pluck('user_id')->toArray();
|
|
$delete_members = array_diff($local_members, $wangyi_members);
|
|
$insert_members = array_diff($wangyi_members, $local_members);
|
|
$insert = [];
|
|
foreach ($insert_members as $user_id) {
|
|
$new_arr = [
|
|
'user_id' => $user_id,
|
|
'team_id' => $team->id,
|
|
'created_at' => date("Y-m-d H:i:s"),
|
|
'updated_at' => date("Y-m-d H:i:s"),
|
|
];
|
|
$insert[] = $new_arr;
|
|
}
|
|
if (!empty($delete_members)) {
|
|
foreach ($delete_members as $delete_member) {
|
|
UserTeam::where('user_id', $delete_member)->where('team_id', $team->id)->delete();
|
|
}
|
|
}
|
|
if (!empty($insert)) {
|
|
UserTeam::insert($insert);
|
|
}
|
|
}
|
|
}
|
|
return $this->success('ok', $result);
|
|
}
|
|
|
|
//设置群聊管理员
|
|
public function addManager(Request $request, $team_id)
|
|
{
|
|
if (empty($request->user_id)) {
|
|
return $this->failure('请输入用户id');
|
|
}
|
|
$user_team = UserTeam::where('team_id', $team_id)->where('user_id', $request->user_id)->first();
|
|
if (!$user_team) {
|
|
return $this->failure('该用户不在群聊');
|
|
}
|
|
$team = Team::find($team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
if ($user_team->type != 0) {
|
|
return $this->failure('要设置的管理员不是普通成员');
|
|
}
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$result = $im_service->addGroupManager($team->tid, $team->owner, [$request->user_id]);
|
|
if ($result['code'] === 200) {
|
|
$user_team->type = 2;
|
|
$user_team->save();
|
|
}
|
|
return $this->success('ok', $user_team);
|
|
}
|
|
|
|
|
|
|
|
//移除管理员
|
|
public function removeManager(Request $request, $team_id)
|
|
{
|
|
if (empty($request->user_id)) {
|
|
return $this->failure('请输入用户id');
|
|
}
|
|
$user_team = UserTeam::where('team_id', $team_id)->where('user_id', $request->user_id)->first();
|
|
if (!$user_team) {
|
|
return $this->failure('该用户不在群聊');
|
|
}
|
|
$team = Team::find($team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
if ($user_team->type != 2) {
|
|
return $this->failure('该用户还不是管理员');
|
|
}
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$result = $im_service->removeGroupManager($team->tid, $team->owner, [$request->user_id]);
|
|
if ($result['code'] == 200) {
|
|
$user_team->type = 0;
|
|
$user_team->save();
|
|
}
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//群聊周消息数统计
|
|
public function weekMsgStat(Request $request, $team_id)
|
|
{
|
|
if ($request->start_time) {
|
|
$start_time = $request->start_time;
|
|
} else {
|
|
$start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
|
|
}
|
|
$date[] = $start_time;
|
|
for ($i = 1; $i < 7; $i++) {
|
|
$date[] = date("Y-m-d 00:00:00", strtotime("+$i day", strtotime($start_time)));
|
|
}
|
|
$all_msg_count = 0;
|
|
foreach ($date as $start_time) {
|
|
$end_time = date("Y-m-d 23:59:59", strtotime($start_time));
|
|
$msg_count = MsgHistory::where('team_id', $team_id)->whereBetween('created_at', [$start_time, $end_time])->count();
|
|
|
|
$arr['msg_count'] = $msg_count;
|
|
$date_arr[] = $start_time;
|
|
$all_msg_count += $msg_count;
|
|
$count[] = $msg_count;
|
|
}
|
|
return $this->success('ok', compact('all_msg_count', 'count', 'date_arr'));
|
|
}
|
|
|
|
//上传群聊视频
|
|
public function addVideo(Request $request)
|
|
{
|
|
$request->validate([
|
|
'aliyun_video_id' => 'required',
|
|
'title' => 'required',
|
|
'intro' => 'required',
|
|
]);
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
|
if (empty($result))
|
|
throw new \Exception("获取播放地址失败", 1);
|
|
$video = new Video();
|
|
if ($request->has('team_id')) {
|
|
$video->model = 'Team';
|
|
$video->model_id = $request->team_id;
|
|
}
|
|
$video->aliyun_video_id = $request->aliyun_video_id;
|
|
$video->title = $request->title;
|
|
$video->intro = $request->intro;
|
|
$video->cover_url = $result['VideoBase']['CoverURL'] ?? '';
|
|
$video->play_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
|
$video->duration = round($result['VideoBase']['Duration']);
|
|
$video->height = $result['PlayInfoList']['PlayInfo'][0]['Height'];
|
|
$video->width = $result['PlayInfoList']['PlayInfo'][0]['Width'];
|
|
$video->save();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//删除群聊视频
|
|
public function delVideo(Request $request, $video_id)
|
|
{
|
|
Video::where('id', $video_id)->delete();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//修改群聊视频
|
|
public function updateVideo(Request $request, $video_id)
|
|
{
|
|
$video = Video::find($video_id);
|
|
if ($request->title && $video->title != $request->title) {
|
|
$video->title = $request->title;
|
|
}
|
|
if ($request->intro && $video->intro != $request->intro) {
|
|
$video->intro = $request->intro;
|
|
}
|
|
if ($request->aliyun_video_id && $video->aliyun_video_id != $request->aliyun_video_id) {
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
|
if (empty($result))
|
|
throw new \Exception("获取播放地址失败", 1);
|
|
$video->aliyun_video_id = $request->aliyun_video_id;
|
|
$video->cover_url = $result['VideoBase']['CoverURL'];
|
|
$video->play_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
|
$video->duration = round($result['VideoBase']['Duration']);
|
|
$video->height = $result['PlayInfoList']['PlayInfo'][0]['Height'];
|
|
$video->width = $result['PlayInfoList']['PlayInfo'][0]['Width'];
|
|
}
|
|
$video->save();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//视频列表
|
|
public function Videos(Request $request)
|
|
{
|
|
$videos = Video::orderBy('id', 'desc');
|
|
if ($request->has('team_id')) {
|
|
$videos = $videos->where('model', 'Team')->where('model_id', $request->team_id);
|
|
}
|
|
if ($request->keyword) {
|
|
$keyword = $request->keyword;
|
|
$videos = $videos->where(function ($query) use ($keyword) {
|
|
$query->where('title', 'like', '%' . $keyword . '%')->orWhere('intro', 'like', '%' . $keyword . '%');
|
|
});
|
|
}
|
|
$videos = $videos->paginate();
|
|
foreach ($videos as $video) {
|
|
$video->duration = gmstrftime('%H:%M:%S', $video->duration);
|
|
}
|
|
return $this->success('ok', $videos);
|
|
}
|
|
|
|
//批量更换群主(高级群)
|
|
public function updateTeamOwner(Request $request)
|
|
{
|
|
try {
|
|
$owner_id = $request->owner_id;
|
|
$change_owner_id = $request->change_owner_id;
|
|
$teams = Team::where('owner', $owner_id)->get();
|
|
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$team_ids = [];
|
|
$false_owner = [];
|
|
|
|
foreach ($teams as $team) {
|
|
//更换群主
|
|
//是否加入
|
|
$is_join = UserTeam::where('team_id', $team->id)->where('user_id', $change_owner_id)->count();
|
|
if (!$is_join) {
|
|
//拉人
|
|
$result = $im_service->addIntoGroup($team->id, $team->owner, [$request->owner, $change_owner_id]);
|
|
// dd($result);
|
|
if ($result['code'] == 200) {
|
|
UserTeam::insert([
|
|
'user_id' => $change_owner_id,
|
|
'team_id' => (string) $team->id,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
]);
|
|
} else {
|
|
Log::info("加入群");
|
|
Log::info($result);
|
|
$team_ids[] = $team->id;
|
|
}
|
|
}
|
|
//设置为群主
|
|
$result = $im_service->changeGroupOwner($team->id, $team->owner, $change_owner_id);
|
|
|
|
// dd($result);
|
|
if ($result['code'] == 200) {
|
|
UserTeam::where('team_id', $team->id)->where('user_id', $team->owner)->update(['type' => 0]);
|
|
$team->owner = $change_owner_id;
|
|
$team->save();
|
|
UserTeam::where('team_id', $team->id)->where('user_id', $change_owner_id)->update(['type' => 1]);
|
|
} else {
|
|
Log::info("改群主");
|
|
Log::info($result);
|
|
$false_owner[] = $team->id;
|
|
}
|
|
$team->save();
|
|
}
|
|
// \DB::commit();
|
|
return $this->success('ok', $team_ids);
|
|
} catch (\Exception $e) {
|
|
// \DB::rollback();
|
|
$this->getError($e);
|
|
return $this->failure(compact('team_ids', 'false_owner'));
|
|
}
|
|
}
|
|
public function teamDetail(Request $request)
|
|
{
|
|
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
|
$url = 'https://api.netease.im/nimserver/team/queryDetail.action';
|
|
|
|
$team_ids = [$request->tid];
|
|
// $result = $im_service->postDataCurl($url,$data);
|
|
|
|
|
|
// $team_ids = [3837279398,3837285691,3837288698,3837702166];
|
|
// $team_ids = Team::where('owner',79675)->pluck('tid')->toArray();
|
|
|
|
$team_members = [];
|
|
$false_owner = [];
|
|
foreach ($team_ids as $team_id) {
|
|
$data['tid'] = $team_id;
|
|
$result = $im_service->postDataCurl($url, $data);
|
|
if ($result['code'] == 200) {
|
|
// $team_members [] = $result['tinfo']['owner']['accid'];
|
|
$team_members[] = $result['tinfo'];
|
|
|
|
} else {
|
|
$false_owner[] = $team_id;
|
|
}
|
|
}
|
|
return $this->success('ok', compact('team_members', 'false_owner'));
|
|
}
|
|
|
|
public function updateTeamOwnerBatch(Request $request)
|
|
{
|
|
$teams = Team::where('owner', 79675)->get();
|
|
foreach ($teams as $team) {
|
|
$team->owner = 61848;
|
|
$team->save();
|
|
}
|
|
return $this->success('ok');
|
|
}
|
|
|
|
/**
|
|
* 加入群聊二维码
|
|
* @param Request $request
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View
|
|
*/
|
|
public function teamJoinQrcode(Request $request)
|
|
{
|
|
try {
|
|
$team_id = $request->input('team_id');
|
|
if (!$team_id) {
|
|
return $this->failure('参数错误');
|
|
}
|
|
$team = Team::find($team_id);
|
|
if (!$team) {
|
|
return $this->failure('群聊不存在');
|
|
}
|
|
$cache_key = "group:chat:team_id{$team_id}_join_qrcode";
|
|
$qrcode = Cache::get($cache_key);
|
|
if (empty($qrcode)) {
|
|
// $app = \EasyWeChat::miniProgram();
|
|
// $token = $app->access_token->getToken();
|
|
// $access_token = $token['access_token'];
|
|
$access_token = "";
|
|
$resp = Http::get("https://love.ufutx.com/go/api/mp/access_token?token=go_love");
|
|
if ($resp) {
|
|
if ($resp) {
|
|
$res = json_decode($resp, true);
|
|
$access_token = $res["data"]['access_token'];
|
|
}
|
|
}
|
|
if (empty($access_token)) {
|
|
return $this->failure("缺少access_token");
|
|
}
|
|
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
|
|
Log::info($url);
|
|
$data = [
|
|
'page' => 'pages/news/groupChitchatDetail',
|
|
'scene' => "id={$team_id}",
|
|
];
|
|
Log::info($data);
|
|
$uniqid = uniqid();
|
|
$result = Http::post($url, json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
log::info($result);
|
|
$path = storage_path("qrcode/team{$team_id}_join_qrcode_{$uniqid}.png");
|
|
file_put_contents($path, $result);
|
|
$qrcode = $this->uploadFile($path);
|
|
unlink($path);
|
|
Cache::put($cache_key, $qrcode, 60 * 24); //有效期一天
|
|
}
|
|
return $this->success('ok', compact('qrcode'));
|
|
} catch (\Exception $e) {
|
|
return $this->failure($e->getMessage());
|
|
}
|
|
}
|
|
}
|