403 lines
17 KiB
PHP
403 lines
17 KiB
PHP
|
<?php
|
|||
|
|
|||
|
namespace app\common\controller;
|
|||
|
|
|||
|
use alioss\alioss;
|
|||
|
use app\common\model\auth\Admin;
|
|||
|
use app\common\model\Company;
|
|||
|
use app\common\model\im\UserImMessage;
|
|||
|
use app\common\model\user\User;
|
|||
|
use GatewayClient\Gateway;
|
|||
|
use think\App;
|
|||
|
use think\facade\Db;
|
|||
|
use think\response\Json;
|
|||
|
|
|||
|
class ImController extends BaseLikeAdminController
|
|||
|
{
|
|||
|
public function __construct(App $app)
|
|||
|
{
|
|||
|
parent::__construct($app);
|
|||
|
Gateway::$registerAddress = "172.19.97.179:1256";
|
|||
|
}
|
|||
|
|
|||
|
//获取场景值
|
|||
|
private function sceneText($scene): string
|
|||
|
{
|
|||
|
if($scene == 0){
|
|||
|
$scene = 'app';
|
|||
|
}else{
|
|||
|
$scene = 'web';
|
|||
|
}
|
|||
|
return $scene;
|
|||
|
}
|
|||
|
|
|||
|
//获取发送者信息
|
|||
|
private function getFromUserInfo($uid,$scene): array
|
|||
|
{
|
|||
|
//如果是从app端发送的消息,发送者为app端的user用户
|
|||
|
//如果是从web端发送的消息,发送者为web端的admin用户
|
|||
|
if($scene == 0){
|
|||
|
//app端 获取user表数据
|
|||
|
$data = User::field('id,nickname as name,avatar,register_id')->where('id',$uid)->findOrEmpty()->toArray();
|
|||
|
}elseif($scene == 1){
|
|||
|
//web端 获取admin表数据
|
|||
|
$data = Admin::field('id,name,avatar')->where('id',$uid)->findOrEmpty()->toArray();
|
|||
|
|
|||
|
}else{
|
|||
|
$data = [];
|
|||
|
}
|
|||
|
return $data;
|
|||
|
}
|
|||
|
|
|||
|
//获取接收者信息
|
|||
|
private function getToUserInfo($uid,$scene): array
|
|||
|
{
|
|||
|
//如果是从app端发送的消息,接收者为web端的admin用户
|
|||
|
//如果是从web端发送的消息,接收者为app端的user用户
|
|||
|
if($scene == 0){
|
|||
|
//app端 获取admin表数据
|
|||
|
$data = Admin::field('id,name,avatar')->where('id',$uid)->findOrEmpty()->toArray();
|
|||
|
}elseif($scene == 1){
|
|||
|
//web端 获取user表数据
|
|||
|
$data = User::field('id,nickname as name,avatar,register_id')->where('id',$uid)->findOrEmpty()->toArray();
|
|||
|
}else{
|
|||
|
$data = [];
|
|||
|
}
|
|||
|
return $data;
|
|||
|
}
|
|||
|
|
|||
|
/*****************************************************************************************************************************************************************************/
|
|||
|
|
|||
|
//绑定uid
|
|||
|
public function doBindUid(): Json
|
|||
|
{
|
|||
|
//验证请求方式
|
|||
|
if(!$this->request->isPost()){
|
|||
|
return $this->fail('请求方式错误');
|
|||
|
}
|
|||
|
//获取请求参数
|
|||
|
$params = $this->request->post(['client_id','user_id','scene']);
|
|||
|
if(empty($params['client_id']) || empty($params['user_id']) || !isset($params['scene'])){
|
|||
|
return $this->fail('参数错误');
|
|||
|
}
|
|||
|
if(!in_array($params['scene'],[0,1])){
|
|||
|
return $this->fail('场景错误');
|
|||
|
}
|
|||
|
Gateway::bindUid($params['client_id'], $this->sceneText($params['scene']).'_'.$params['user_id']);
|
|||
|
return $this->success('绑定成功',['msg'=>$this->sceneText($params['scene']).'_'.$params['user_id']]);
|
|||
|
}
|
|||
|
|
|||
|
//发送消息
|
|||
|
public function sendTextMsg(): Json
|
|||
|
{
|
|||
|
//验证请求方式
|
|||
|
if(!$this->request->isPost()){
|
|||
|
return $this->fail('请求方式错误');
|
|||
|
}
|
|||
|
//获取请求参数
|
|||
|
$params = $this->request->post(['from_user_id','to_user_id','msg_id','content','type','scene']);
|
|||
|
//验证请求参数
|
|||
|
if(empty($params['from_user_id']) || empty($params['to_user_id']) || empty($params['content']) || empty($params['msg_id']) || empty($params['type']) || !isset($params['scene'])){
|
|||
|
return $this->fail('参数错误');
|
|||
|
}
|
|||
|
if($params['type'] !== 'text'){
|
|||
|
return $this->fail('消息类型错误');
|
|||
|
}
|
|||
|
if(!in_array($params['scene'],[0,1])){
|
|||
|
return $this->fail('消息场景错误');
|
|||
|
}
|
|||
|
//获取发送者信息
|
|||
|
$fromUserInfo = $this->getFromUserInfo($params['from_user_id'],$params['scene']);
|
|||
|
if(empty($fromUserInfo)){
|
|||
|
return $this->fail('发送者信息错误');
|
|||
|
}
|
|||
|
//获取接收者信息
|
|||
|
$toUserInfo = $this->getToUserInfo($params['to_user_id'],$params['scene']);
|
|||
|
if(empty($toUserInfo)){
|
|||
|
return $this->fail('接收者信息错误');
|
|||
|
}
|
|||
|
//存储信息
|
|||
|
$saveData = [
|
|||
|
'from_user_id' => $params['from_user_id'],
|
|||
|
'to_user_id' => $params['to_user_id'],
|
|||
|
'content' => $params['content'],
|
|||
|
'msg_id' => $params['msg_id'],
|
|||
|
'type' => $params['type'],
|
|||
|
'is_read' => 0,
|
|||
|
'is_undo' => 0,
|
|||
|
'is_delete' => 0,
|
|||
|
'create_time' => time(),
|
|||
|
'extends' => null,
|
|||
|
];
|
|||
|
//保存消息
|
|||
|
try {
|
|||
|
$model = new UserImMessage();
|
|||
|
$model->save($saveData);
|
|||
|
if(empty($model->id)){
|
|||
|
return $this->fail('发送失败');
|
|||
|
}
|
|||
|
//发送消息
|
|||
|
$sendData = [
|
|||
|
'id' => $model->id,
|
|||
|
'from_user_id' => $params['from_user_id'],
|
|||
|
'from_user_name' => $fromUserInfo['name'],
|
|||
|
'from_user_avatar' => $fromUserInfo['avatar'],
|
|||
|
'to_user_id' => $params['to_user_id'],
|
|||
|
'to_user_name' => $toUserInfo['name'],
|
|||
|
'to_user_avatar' => $toUserInfo['avatar'],
|
|||
|
'content' => $params['content'],
|
|||
|
'msg_id' => $params['msg_id'],
|
|||
|
'type' => $params['type'],
|
|||
|
'is_read' => 0,
|
|||
|
'create_time' => $saveData['create_time'],
|
|||
|
'extends' => null,
|
|||
|
];
|
|||
|
if($params['scene'] == 0) {
|
|||
|
//给admin用户发
|
|||
|
Gateway::sendToUid($this->sceneText(1).'_'.$params['to_user_id'], json_encode($sendData));
|
|||
|
}else{
|
|||
|
//给user用户发
|
|||
|
Gateway::sendToUid($this->sceneText(0).'_'.$params['to_user_id'], json_encode($sendData));
|
|||
|
//极光推送
|
|||
|
if(isset($toUserInfo['register_id']) && !empty($toUserInfo['register_id'])){
|
|||
|
push_message($toUserInfo['register_id'],$fromUserInfo['name'].':'.$params['content']);
|
|||
|
}
|
|||
|
}
|
|||
|
return $this->success('发送成功',$sendData);
|
|||
|
}catch (\Exception $e) {
|
|||
|
return $this->fail($e->getMessage());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//上传文件
|
|||
|
public function sendFileMsg(): Json
|
|||
|
{
|
|||
|
//验证请求方式
|
|||
|
if(!$this->request->isPost()){
|
|||
|
return $this->fail('请求方式错误');
|
|||
|
}
|
|||
|
//获取请求参数
|
|||
|
$params = $this->request->post(['from_user_id','to_user_id','msg_id','type','scene']);
|
|||
|
//获取参数
|
|||
|
$file = $this->request->file('file');
|
|||
|
if(empty($file) || empty($params['from_user_id']) || empty($params['to_user_id']) || empty($params['msg_id']) || empty($params['type']) || !isset($params['scene'])){
|
|||
|
return $this->fail('参数错误');
|
|||
|
}
|
|||
|
if(!in_array($params['type'],['image','video'])){
|
|||
|
return $this->fail('消息类型错误');
|
|||
|
}
|
|||
|
if(!in_array($params['scene'],[0,1])){
|
|||
|
return $this->fail('消息场景错误');
|
|||
|
}
|
|||
|
//获取发送者信息
|
|||
|
$fromUserInfo = $this->getFromUserInfo($params['from_user_id'],$params['scene']);
|
|||
|
if(empty($fromUserInfo)){
|
|||
|
return $this->fail('发送者信息错误');
|
|||
|
}
|
|||
|
//获取接收者信息
|
|||
|
$toUserInfo = $this->getToUserInfo($params['to_user_id'],$params['scene']);
|
|||
|
if(empty($toUserInfo)){
|
|||
|
return $this->fail('接收者信息错误');
|
|||
|
}
|
|||
|
$filePath =$file->getRealPath();
|
|||
|
$fileType = $file->extension();
|
|||
|
$fileSize = $file->getSize();
|
|||
|
$ali_oss = new alioss();
|
|||
|
switch ($params['type']) {
|
|||
|
case 'image':
|
|||
|
$result = $ali_oss -> uploadImg($filePath,$fileType,$fileSize);
|
|||
|
break;
|
|||
|
case 'video':
|
|||
|
$result = $ali_oss -> uploadVideo($filePath,$fileType,$fileSize);
|
|||
|
break;
|
|||
|
default:
|
|||
|
return $this->fail('文件类型错误~');
|
|||
|
}
|
|||
|
if(!$result || $result['code'] == 0){
|
|||
|
return $this->fail($result['msg']);
|
|||
|
}
|
|||
|
//存储信息
|
|||
|
$saveData = [
|
|||
|
'from_user_id' => $params['from_user_id'],
|
|||
|
'to_user_id' => $params['to_user_id'],
|
|||
|
'content' => $result['data'],
|
|||
|
'msg_id' => $params['msg_id'],
|
|||
|
'type' => $params['type'],
|
|||
|
'is_read' => 0,
|
|||
|
'is_undo' => 0,
|
|||
|
'is_delete' => 0,
|
|||
|
'create_time' => time(),
|
|||
|
'extends' => $params['type'] == 'video' ? json_encode(['poster_img'=>$result['data'].'?x-oss-process=video/snapshot,t_1000,m_fast,w_800,f_png']) : null,
|
|||
|
];
|
|||
|
try {
|
|||
|
$model = new UserImMessage();
|
|||
|
$model->save($saveData);
|
|||
|
if(empty($model->id)){
|
|||
|
return $this->fail('发送失败');
|
|||
|
}
|
|||
|
//发送消息
|
|||
|
$sendData = [
|
|||
|
'id' => $model->id,
|
|||
|
'from_user_id' => $params['from_user_id'],
|
|||
|
'from_user_name' => $fromUserInfo['name'],
|
|||
|
'from_user_avatar' => $fromUserInfo['avatar'],
|
|||
|
'to_user_id' => $params['to_user_id'],
|
|||
|
'to_user_name' => $toUserInfo['name'],
|
|||
|
'to_user_avatar' => $toUserInfo['avatar'],
|
|||
|
'content' => $result['data'],
|
|||
|
'msg_id' => $params['msg_id'],
|
|||
|
'type' => $params['type'],
|
|||
|
'is_read' => 0,
|
|||
|
'create_time' => $saveData['create_time'],
|
|||
|
'extends' => $params['type'] == 'video' ? json_encode(['poster_img'=>$result['data'].'?x-oss-process=video/snapshot,t_1000,m_fast,w_800,f_png']) : null,
|
|||
|
];
|
|||
|
if($params['scene'] == 0) {
|
|||
|
//给admin用户发
|
|||
|
Gateway::sendToUid($this->sceneText(1).'_'.$params['to_user_id'], json_encode($sendData));
|
|||
|
}else{
|
|||
|
//给user用户发
|
|||
|
Gateway::sendToUid($this->sceneText(0).'_'.$params['to_user_id'], json_encode($sendData));
|
|||
|
//极光推送
|
|||
|
if(isset($toUserInfo['register_id']) && !empty($toUserInfo['register_id'])){
|
|||
|
$push_msg = $params['type'] == 'image' ? '[图片]' : '[视频]';
|
|||
|
push_message($toUserInfo['register_id'],$fromUserInfo['name'].':'.$push_msg);
|
|||
|
}
|
|||
|
}
|
|||
|
return $this->success('发送成功',$sendData );
|
|||
|
}catch (\Exception $e) {
|
|||
|
return $this->fail($e->getMessage());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//消息列表
|
|||
|
public function msgList(): Json
|
|||
|
{
|
|||
|
//验证请求方式
|
|||
|
if(!$this->request->isPost()){
|
|||
|
return $this->fail('请求方式错误');
|
|||
|
}
|
|||
|
//获取请求参数
|
|||
|
$params = $this->request->post(['from_user_id','to_user_id','scene','page_no','page_size']);
|
|||
|
if(empty($params['from_user_id']) || empty($params['to_user_id']) || !isset($params['scene']) || empty($params['page_no']) || empty($params['page_size'])){
|
|||
|
return $this->fail('参数错误');
|
|||
|
}
|
|||
|
if(!in_array($params['scene'],[0,1])){
|
|||
|
return $this->fail('消息场景错误');
|
|||
|
}
|
|||
|
//获取发送者信息
|
|||
|
$fromUserInfo = $this->getFromUserInfo($params['from_user_id'],$params['scene']);
|
|||
|
if(empty($fromUserInfo)){
|
|||
|
return $this->fail('发送者信息错误');
|
|||
|
}
|
|||
|
//获取接收者信息
|
|||
|
$toUserInfo = $this->getToUserInfo($params['to_user_id'],$params['scene']);
|
|||
|
if(empty($toUserInfo)){
|
|||
|
return $this->fail('接收者信息错误');
|
|||
|
}
|
|||
|
//将消息设置为已读
|
|||
|
UserImMessage::where([['from_user_id','=',$params['to_user_id']],['to_user_id','=',$params['from_user_id']]])->update(['is_read' => 1]);
|
|||
|
//获取用户消息
|
|||
|
$msgData = Db::name('user_im_message')->field('id,from_user_id,to_user_id,content,type,is_read,create_time,extends')
|
|||
|
->whereRaw("(from_user_id = {$params['from_user_id']} AND to_user_id = {$params['to_user_id']}) OR (from_user_id = {$params['to_user_id']} AND to_user_id = {$params['from_user_id']})")
|
|||
|
->order('create_time desc')
|
|||
|
->page($params['page_no'],$params['page_size'])
|
|||
|
->select()->toArray();
|
|||
|
foreach($msgData as $k=>$v){
|
|||
|
if($v['from_user_id'] == $params['from_user_id']){
|
|||
|
$msgData[$k]['from_user_name'] = $fromUserInfo['name'];
|
|||
|
$msgData[$k]['from_user_avatar'] = $fromUserInfo['avatar'];
|
|||
|
}else{
|
|||
|
$msgData[$k]['from_user_name'] = $toUserInfo['name'];
|
|||
|
$msgData[$k]['from_user_avatar'] = $toUserInfo['avatar'];
|
|||
|
}
|
|||
|
if($v['to_user_id'] == $params['to_user_id']){
|
|||
|
$msgData[$k]['to_user_name'] = $toUserInfo['name'];
|
|||
|
$msgData[$k]['to_user_avatar'] = $toUserInfo['avatar'];
|
|||
|
}else{
|
|||
|
$msgData[$k]['to_user_name'] = $fromUserInfo['name'];
|
|||
|
$msgData[$k]['to_user_avatar'] = $fromUserInfo['avatar'];
|
|||
|
}
|
|||
|
}
|
|||
|
//返回数据
|
|||
|
return $this->success('请求成功',$msgData);
|
|||
|
}
|
|||
|
|
|||
|
//获取联系人列表
|
|||
|
public function contactList(): Json{
|
|||
|
//验证请求方式
|
|||
|
if(!$this->request->isPost()){
|
|||
|
return $this->fail('请求方式错误');
|
|||
|
}
|
|||
|
//获取请求参数
|
|||
|
$params = $this->request->post(['user_id','keywords']);
|
|||
|
if(empty($params['user_id'])){
|
|||
|
return $this->fail('参数错误');
|
|||
|
}
|
|||
|
//获取用户信息
|
|||
|
$userInfo = Admin::where('id',$params['user_id'])->findOrEmpty();
|
|||
|
//验证用户信息
|
|||
|
if($userInfo->isEmpty()){
|
|||
|
return $this->fail('未找到用户信息');
|
|||
|
}
|
|||
|
//验证用户身份
|
|||
|
if(empty($userInfo['role_id']) || $userInfo['role_id'][0] != 8){
|
|||
|
return $this->fail('用户角色错误');
|
|||
|
}
|
|||
|
//获取片区经理创建的公司
|
|||
|
$company = Company::field('id')->where('area_manager',$userInfo['id'])->column('id');
|
|||
|
//获取公司的成员
|
|||
|
$params['keywords'] = !empty($params['keywords']) ? $params['keywords'] : '';
|
|||
|
$users = User::field('id,nickname as name,avatar')->where('company_id','in',$company)->where('nickname','like','%'.$params['keywords'].'%')->select()->each(function($item) use($params) {
|
|||
|
//获取消息
|
|||
|
$msg_no_read = UserImMessage::field('id')->where('from_user_id',$item['id'])->where('to_user_id',$params['user_id'])->where('is_read',0)->count();
|
|||
|
$last_msg = Db::name('user_im_message')->field('id,content,type,create_time')
|
|||
|
->whereRaw("(from_user_id = {$item['id']} AND to_user_id = {$params['user_id']}) OR (from_user_id = {$params['user_id']} AND to_user_id = {$item['id']})")
|
|||
|
->order('create_time desc')->limit(1)->findOrEmpty();
|
|||
|
$item['no_read_num'] = $msg_no_read;
|
|||
|
$item['last_msg_content'] = empty($last_msg) ? '' : $last_msg['content'];
|
|||
|
$item['last_msg_type'] = empty($last_msg) ? '' : $last_msg['type'];
|
|||
|
$item['last_msg_time'] = empty($last_msg) ? '' : $last_msg['create_time'];
|
|||
|
})->toArray();
|
|||
|
$key = array_column($users,'last_msg_time');
|
|||
|
array_multisort($key,SORT_DESC,$users);
|
|||
|
//返回数据
|
|||
|
return $this->success('请求成功',$users);
|
|||
|
}
|
|||
|
|
|||
|
//获取片区经理用户id
|
|||
|
public function getAreaManager(): Json
|
|||
|
{
|
|||
|
//验证请求方式
|
|||
|
if(!$this->request->isPost()){
|
|||
|
return $this->fail('请求方式错误');
|
|||
|
}
|
|||
|
//获取请求参数
|
|||
|
$params = $this->request->post(['user_id']);
|
|||
|
if(empty($params['user_id'])){
|
|||
|
return $this->fail('参数错误');
|
|||
|
}
|
|||
|
//获取其用户的公司id
|
|||
|
$userInfo = User::field('id,company_id')->where('id',$params['user_id'])->findOrEmpty();
|
|||
|
if(empty($userInfo)){
|
|||
|
return $this->fail('未获取到用户信息');
|
|||
|
}
|
|||
|
//获取公司信息
|
|||
|
$companyInfo = Company::field('area_manager')->where('id',$userInfo['company_id'])->findOrEmpty();
|
|||
|
if(empty($companyInfo)){
|
|||
|
return $this->fail('用户信息错误');
|
|||
|
}
|
|||
|
if($params['user_id'] == $companyInfo['area_manager']){
|
|||
|
return $this->fail('用户身份错误');
|
|||
|
}
|
|||
|
//获取用户信息
|
|||
|
$adminInfo = Admin::field('id,name,avatar')->where('id',$companyInfo['area_manager'])->findOrEmpty();
|
|||
|
//获取片区经理给用户发送的未读消息数
|
|||
|
$msg_num = UserImMessage::field('id')->where([
|
|||
|
['from_user_id','=',$adminInfo['id']],
|
|||
|
['to_user_id','=',$params['user_id']],
|
|||
|
['is_read','=',0]
|
|||
|
])->count();
|
|||
|
//返回
|
|||
|
return $this->success('请求成功',['to_user_id'=>$adminInfo['id'],'manager_name'=>$adminInfo['name'],'manager_avatar'=>$adminInfo['avatar'],'msg_num'=>$msg_num]);
|
|||
|
}
|
|||
|
}
|