love_php/app/Http/Controllers/Admin/CustomerServiceController.php
2026-04-02 09:20:51 +08:00

254 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\AdminLog;
use App\Models\User;
use App\Services\IMService;
use App\Models\MessageLinkman;
use App\Models\ChatMessage;
use App\Models\Linking;
use Illuminate\Http\Request;
class CustomerServiceController extends Controller
{
//
public function sendMessage(Request $request)
{
try {
$mine = User::where('id',$request->server_id)->first();
$user = User::where('id',$request->user_id)->first();
if(!$user) return $this->fail('该用户不存在');
$content = $request->input('content', '');
//我的聯係人
$my_linkman = MessageLinkman::where('user_id', $mine->id)->where('other_user_id', $user->id)->first();
$other_linkman = MessageLinkman::where('user_id', $user->id)->where('other_user_id', $mine->id)->first();
if (empty($other_linkman)) {
$other_linkman = MessageLinkman::create([
'user_id'=>$user->id,
'other_user_id'=>$mine->id,
]);
}else{
$other_linkman->updated_at = date('Y-m-d H:i:s');
$other_linkman->save();
}
if (empty($my_linkman)) {
$my_linkman = MessageLinkman::create([
'user_id'=>$mine->id,
'other_user_id'=>$user->id,
]);
}else{
$my_linkman->updated_at = date('Y-m-d H:i:s');
$my_linkman->save();
}
$content_type = $request->input('content_type', 'text');
//创建聊天记录
ChatMessage::create([
'user_id'=>$mine->id,
'other_user_id'=>$user->id,
'content'=>$content,
'content_type'=>$content_type,
'system_type'=>$request->system_type??'',
]);
return $this->success('消息发送成功');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('发送聊天信息失败,请稍后再试');
}
}
//消息列表
public function linkmen(Request $request)
{
$user_id = $request->server_id;
if(!$user_id) return $this->failure('请提供用户信息!');
$types = $request->input('type') ? [$request->input('type')] : ['single', 'loveing', 'marriage'];
$linkmen = MessageLinkman::with('otherUser:id,nickname,photo,app_avatar,circle_avatar')->whereHas('otherUser', function ($sql) use ($types) {
$sql->where('id', '>', 0)
// ->where('hidden_profile', '<>', 'ALLSEX')
->whereIn('type', $types);
})/*->where('other_user_id', '<>', 1)*/->select('id', 'user_id', 'other_user_id')->where('user_id', $user_id);
$linkmen = $linkmen->orderBy('updated_at', 'desc')->paginate();
foreach ($linkmen as $linkman) {
//新消息数
$new_count = $this->newChatMessageCount($linkman->user_id, $linkman->other_user_id);
$linkman->new_count = $new_count;
$linkman->type = auth()->user()->type;
$other_user_id = $linkman->other_user_id;
$user_id = $linkman->user_id;
// \Log::info('other_user_id'. $other_user_id);
// \Log::info('user_id'. $user_id);
$last_message = ChatMessage::whereIn('user_id', [$user_id, $other_user_id])
->whereIn('other_user_id', [$user_id, $other_user_id])->select('user_id', 'other_user_id', 'content', 'content_type', 'created_at')->orderBy('id', 'desc')->first();
if ($last_message) {
$last_time = $this->getLastTime(strtotime($last_message->created_at->toDateTimeString()));
$last_time = (explode(' ', $last_time))[0];
$last_message->last_time = $last_time;
if ($last_message->user_id != $user_id) {
switch ($last_message->content_type) {
case 'source':
$content = "[你收到一条语音消息请在福恋APP上查看]";
break;
case 'video':
$content = "[你收到一个视频请在福恋APP上查看]";
break;
case 'image':
$content = "[你收到一张图片请在福恋APP上查看]";
break;
default:
$content = $last_message->content;
}
} else {
switch ($last_message->content_type) {
case 'source':
$content = "[你发送一条语音消息请在福恋APP上查看]";
break;
case 'video':
$content = "[你发送一个视频请在福恋APP上查看]";
break;
case 'image':
$content = "[你发送一张图片请在福恋APP上查看]";
break;
default:
$content = $last_message->content;
}
}
$last_message->content = $content;
}
$linkman->last_message = $last_message;
$linkman->otherUser->circle_avatar = $linkman->otherUser->userAvatar();
}
return $this->success('ok',$linkmen);
}
//获取网易云token
public function getImToken(Request $request)
{
$user_id = $request->user_id;
$user = User::where('id',$user_id)->first();
//判断是否已创建accid;
if(!$user) return $this->failure('获取用户信息失败');
$wyy_user = $user->IMUser;
$im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET"));
if (empty($wyy_user)) {
$wyy_user = $user->createIMUser();
} elseif (empty($wyy_user->token)) {
//更新网易云token
$result = $im_service->updateUserToken($user->id);
if ($result['code'] == 200) {
$user->IMUser()->update(['token' => $result['info']['token']]);
$wyy_user->token = $result['info']['token'];
}
}
$birthday = $user->profileCourtship ? $user->profileCourtship->birthday : null;
$avatar = $this->getUserValidPhoto($user);
$result = $im_service->updateUinfo($user->id, $user->nickname, $avatar, $sign = '', $user->email, $birthday, $user->mobile, $user->sex);
return $this->success('ok',$wyy_user);
}
/**获取好友的列表 */
public function myFriendsChat(Request $request){
$user_id = $request->server_id;
$keyword = $request->keyword;
$ids = MessageLinkman::where('user_id', $user_id)->pluck('other_user_id');
$other_ids = MessageLinkman::where('other_user_id', $user_id)->pluck('user_id');
$users = User::select('id','nickname','name','mobile','photo','app_avatar','circle_avatar')->where(function ($sql) use ($ids, $other_ids) {
$sql->whereIn('id', $ids)->orWhereIn('id', $other_ids);
});
if ($keyword) {
$keyword = trim($keyword);
$users = $users->where(function ($sql) use ($keyword) {
$sql->where('name', 'like', '%' . $keyword . '%')
->orWhere('nickname', 'like', '%' . $keyword . '%')
->orWhere('mobile', 'like', '%' . $keyword . '%');
});
}
$users = $users->orderBy('id', 'desc')->paginate();
return $this->success('ok',$users);
}
/**获取用户的可以用的头像 */
public function getUserValidPhoto($user){
try{
if ($user->app_avatar) {
$avatar = strstr($user->app_avatar,'?x-oss-process=style/scale1')?$user->app_avatar:$user->app_avatar.'?x-oss-process=style/scale1';
} elseif ($user->photo) {
$avatar = strstr($user->photo,'?x-oss-process=style/scale1')?$user->photo:$user->photo.'?x-oss-process=style/scale1';
} elseif ($user->circle_avatar) {
$avatar = $user->circle_avatar;
} elseif (empty($user->photo) && empty($user->app_avatar)) {
if ($user->sex == 1) {
$avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
}elseif ($user->sex == 2) {
$avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
}else{
$avatar = 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png';
}
}
return $avatar;
} catch (\Exception $e) {
\CommonUtilsService::HandleLogsAdd(3,'获取最适合的头像失败',[],'getUserValidPhoto');
return 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png';;
}
return 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png';;
}
/**
* 未查看某人的聊天数
* @return [type] [description]
*/
public function newChatMessageCount($user_id, $other_user_id)
{
$new_count = ChatMessage::where('status', 0)->where('other_user_id', $user_id)->where('user_id', $other_user_id)->count();
return $new_count ? $new_count : 0;
}
/**
* 获取已经过了多久
* PHP时间转换
* 刚刚、几分钟前、几小时前
* 今天昨天前天几天前
* @param string $targetTime 时间戳
* @return string
*/
public function getLastTime($targetTime)
{
// 今天最大时间
$todayLast = strtotime(date('Y-m-d 23:59:59'));
$agoTimeTrue = time() - $targetTime;
$agoTime = $todayLast - $targetTime;
$agoDay = floor($agoTime / 86400);
// dd($targetTime);
if ($agoTimeTrue < 60) {
$result = '刚刚';
} elseif ($agoTimeTrue < 3600) {
$result = (ceil($agoTimeTrue / 60)) . '分钟前';
} elseif ($agoTimeTrue < 3600 * 12) {
$result = (ceil($agoTimeTrue / 3600)) . '小时前';
} elseif ($agoDay == 0) {
$result = '今天 ' . date('H:i', $targetTime);
} elseif ($agoDay == 1) {
$result = '昨天 ' . date('H:i', $targetTime);
} elseif ($agoDay == 2) {
$result = '前天 ' . date('H:i', $targetTime);
} elseif ($agoDay > 2 && $agoDay < 16) {
$result = $agoDay . '天前 ' . date('H:i', $targetTime);
} else {
$format = date('Y') != date('Y', $targetTime) ? "Y-m-d H:i" : "m-d H:i";
$result = date($format, $targetTime);
}
return $result;
}
}