312 lines
11 KiB
PHP
312 lines
11 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Contracts\UserContract;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Response\ResponseJson;
|
|
use App\Models\App\Team;
|
|
use App\Models\ServerQuestion;
|
|
use App\Models\TeamLive;
|
|
use App\Models\TeamLiveLog;
|
|
use App\Repositories\Eloquent\SmsRepository as Sms;
|
|
use App\Services\ImCallback\Message\ImCallbackMessageService;
|
|
use App\Services\ImCallback\MessageRecall\ImCallbackMessageRecallService;
|
|
use EasyWeChat\Factory;
|
|
use Illuminate\Http\Request;
|
|
use Log;
|
|
|
|
class ServerController extends Controller
|
|
{
|
|
use ResponseJson;
|
|
|
|
protected $sms;
|
|
protected $userCon;
|
|
protected $viewer;
|
|
protected $app;
|
|
protected $config;
|
|
|
|
public function __construct(Sms $sms, UserContract $userCon)
|
|
{
|
|
$this->config = [
|
|
'app_id' => config('wechat.official_account.new.app_id'),
|
|
'secret' => config('wechat.official_account.new.secret'),
|
|
'token' => config('wechat.official_account.new.token'),
|
|
'aes_key' => config('wechat.official_account.new.aes_key')
|
|
];
|
|
|
|
$this->app = Factory::officialAccount($this->config);
|
|
$this->sms = $sms;
|
|
$this->userCon = $userCon;
|
|
}
|
|
|
|
|
|
//获取公众号标签
|
|
public function getTag(Request $request){
|
|
$tags = $this->app->user_tag->list();
|
|
return $this->success('ok', $tags);
|
|
}
|
|
|
|
//新增标签
|
|
public function createTag(Request $request){
|
|
$this->app->user_tag->create($request->name);
|
|
return $this->success('ok');
|
|
}
|
|
|
|
/**
|
|
* 客服问题列表
|
|
* @param Request $request [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function serverQuestions(Request $request)
|
|
{
|
|
try {
|
|
$questions = ServerQuestion::with('keywords', 'answer')->orderBy('id', 'desc');
|
|
$type = $request->input('type');
|
|
if ($type) {
|
|
$questions = $questions->where('type', $type);
|
|
}
|
|
$questions = $questions->paginate();
|
|
return $this->success('ok', $questions);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取客服问题列表失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
public function addServerQuestion(Request $request)
|
|
{
|
|
try {
|
|
$name = $request->input('name');
|
|
if (empty($name)) return $this->failure('请输入客服问题内容');
|
|
$type = $request->input('type');
|
|
if (empty($type)) return $this->failure('请输入客服问题类型');
|
|
$question = new ServerQuestion;
|
|
$question->name = $name;
|
|
$question->type = $type;
|
|
$question->save();
|
|
//关键字
|
|
$this->addKeywords($question);
|
|
//答案
|
|
$this->addAnswer($question);
|
|
return $this->success('ok', $question);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('增加客服问题失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
public function addKeywords($question)
|
|
{
|
|
//关键字
|
|
$keywords = request()->input('keywords', []);
|
|
$data = [];
|
|
foreach ($keywords as $keyword) {
|
|
$arr['name'] = $keyword;
|
|
$arr['question_id'] = $question->id;
|
|
$arr['created_at'] = date('Y-m-d H:i:s');
|
|
$arr['updated_at'] = date('Y-m-d H:i:s');
|
|
$data[] = $arr;
|
|
}
|
|
if (count($data)) {
|
|
$question->keywords()->insert($data);
|
|
}
|
|
return ;
|
|
}
|
|
|
|
public function addAnswer($question)
|
|
{
|
|
$answer = request()->input('answer');
|
|
$answer_type = request()->input('answer_type');
|
|
$answer_path_type = request()->input('answer_path_type');
|
|
$answer_path = request()->input('answer_path');
|
|
$answer_obj = $question->answer;
|
|
if ($answer_obj && $answer) {
|
|
$question->answer()->update(['content'=>$answer, 'type'=>$answer_type, 'path_type'=>$answer_path_type, 'path'=>$answer_path]);
|
|
}elseif (empty($answer_obj) && $answer){
|
|
$question->answer()->create(['content'=>$answer, 'type'=>$answer_type, 'path_type'=>$answer_path_type, 'path'=>$answer_path]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function updateServerQuestion(Request $request, $id)
|
|
{
|
|
try {
|
|
$question = ServerQuestion::find($id);
|
|
if ($request->input('name') && $request->name != $question->name) {
|
|
$question->name = $request->name;
|
|
}
|
|
if ($request->input('type') && $request->type != $question->type) {
|
|
$question->type = $request->type;
|
|
}
|
|
$question->save();
|
|
$question->keywords()->delete();
|
|
//关键字
|
|
$this->addKeywords($question);
|
|
//答案
|
|
$this->addAnswer($question);
|
|
return $this->success('ok', $question);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('修改客服问题失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
public function serverQuestion(Request $request, $id)
|
|
{
|
|
try {
|
|
$question = ServerQuestion::find($id);
|
|
if (empty($question)) throw new \Exception("客服问题id不存在", 1);
|
|
$question->keywords;
|
|
$question->answer;
|
|
return $this->success('ok', $question);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('客服问题信息失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
public function deleteServerQuestion(Request $request, $id)
|
|
{
|
|
try {
|
|
$question = ServerQuestion::find($id);
|
|
$question->delete();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('删除客服问题失败,请稍后再试');
|
|
}
|
|
}
|
|
|
|
//网易云信消息抄送回调
|
|
public function wangyiImCallback(Request $request){
|
|
try {
|
|
Log::info("开始抄送IM信息");
|
|
Log::info($request->all());
|
|
//验证头部
|
|
$appsecret = env('IM_APP_SECRET');
|
|
$md5 = $request->header('md5');
|
|
$checksum = $request->header('checksum');
|
|
$curtime = $request->header('curtime');
|
|
//验证数据是否在传输过程中被篡改
|
|
if($checksum != sha1($appsecret.$md5.$curtime)){
|
|
return $this->failure('无效数据');
|
|
}
|
|
switch ($request->eventType){
|
|
case 1 ://消息处理
|
|
(new ImCallbackMessageService)->handleMessage($request->convType);
|
|
break;
|
|
case 8:// 消息撤回
|
|
(new ImCallbackMessageService)->recallMessage("TEAM");
|
|
break;
|
|
case 104://推流结束回调
|
|
$this->stopPushingStream($request);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return $this->success('ok');
|
|
}catch (\Exception $e){
|
|
$this->getError($e);
|
|
return $this->failure();
|
|
}
|
|
}
|
|
|
|
//用户离开房间
|
|
public function leaveLive($request){
|
|
try {
|
|
\DB::beginTransaction();
|
|
$team_live = TeamLive::where('channel_id', $request->channelId)->where('version', 2)->where('name', $request->channelName)->where('user_id', $request->uid)->where('in_live', 1)->first();
|
|
if (!empty($team_live)) {
|
|
$team_live->in_live = 0;
|
|
$team_live->save();
|
|
|
|
$team_live_log = TeamLiveLog::where('user_id', $request->uid)->where('live_id', $team_live->id)->first();
|
|
if(!empty($team_live_log)){
|
|
$team_live_log->end_time = date('Y-m-d H:i:s');
|
|
}else{
|
|
$team_live_log = new TeamLiveLog();
|
|
$team_live_log->user_id = $request->uid;
|
|
$team_live_log->live_id = $team_live->id;
|
|
$team_live_log->start_time = date('Y-m-d H:i:s');
|
|
$team_live_log->end_time = date('Y-m-d H:i:s');
|
|
}
|
|
$team_live_log->save();
|
|
|
|
$team = Team::where('tid', $team_live->team_id)->first();
|
|
if($team->custom){
|
|
if ($team->custom->in_live == 1) {
|
|
$custom = $team->custom;
|
|
$custom->in_live = 0;
|
|
$team->custom = json_encode($custom);
|
|
$team->save();
|
|
}
|
|
}
|
|
}
|
|
\DB::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//推流结束回调
|
|
public function stopPushingStream($request){
|
|
if($request->data){
|
|
$this->updateLiveStatue($request->data['channelId']);
|
|
}
|
|
}
|
|
|
|
//更新直播间状态
|
|
public function updateLiveStatue($channelId){
|
|
try {
|
|
\DB::beginTransaction();
|
|
$team_live = TeamLive::where('channel_id', $channelId)->where('version', 2)->where('in_live', 1)->first();
|
|
if (!empty($team_live)) {
|
|
$team_live->in_live = 0;
|
|
$team_live->save();
|
|
|
|
$team_live_log = TeamLiveLog::where('user_id', $team_live->user_id)->where('live_id', $team_live->id)->first();
|
|
if(!empty($team_live_log)){
|
|
$team_live_log->end_time = date('Y-m-d H:i:s');
|
|
}else{
|
|
$team_live_log = new TeamLiveLog();
|
|
$team_live_log->user_id = $team_live->user_id;
|
|
$team_live_log->live_id = $team_live->id;
|
|
$team_live_log->start_time = date('Y-m-d H:i:s');
|
|
$team_live_log->end_time = date('Y-m-d H:i:s');
|
|
}
|
|
$team_live_log->save();
|
|
|
|
$team = Team::where('tid', $team_live->team_id)->first();
|
|
if ($team->custom) {
|
|
$old_custom = $team->custom;
|
|
$old_custom->in_live = 0;
|
|
$old_custom->anchor_id = $team_live->user_id;
|
|
}else{
|
|
$old_custom['in_live'] = 0;
|
|
$old_custom['anchor_id'] = $team_live->user_id;
|
|
}
|
|
$data['custom'] = json_encode($old_custom);
|
|
$data['tid'] = $team->tid;
|
|
$data['owner'] = $team->owner;
|
|
$im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET"));
|
|
$url = "https://api.netease.im/nimserver/team/update.action";
|
|
$result = $im_service->postDataCurl($url,$data);
|
|
if ($result['code'] != 200) throw new \Exception("群扩展修改失败", 1);
|
|
$team->custom = $data['custom'];
|
|
$team->save();
|
|
}
|
|
\DB::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
\DB::rollback();
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|