154 lines
4.5 KiB
PHP
154 lines
4.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace app\api\controller;
|
||
|
|
|
||
|
|
use app\common\controller\Api;
|
||
|
|
use app\common\model\FriendRelation;
|
||
|
|
use fast\Easemob;
|
||
|
|
use think\Hook;
|
||
|
|
|
||
|
|
class Huanxin extends Api
|
||
|
|
{
|
||
|
|
|
||
|
|
protected $noNeedRight = ['*'];
|
||
|
|
|
||
|
|
/*
|
||
|
|
* 注册环信
|
||
|
|
*/
|
||
|
|
public function register()
|
||
|
|
{
|
||
|
|
$user_info = $this->auth->getUserinfo();
|
||
|
|
|
||
|
|
$Easemob = new Easemob();
|
||
|
|
$result = $Easemob->creatuser($user_info);
|
||
|
|
if ($result['code'] <= 0){
|
||
|
|
$this->error($result['msg']);
|
||
|
|
}else{
|
||
|
|
$this->success('注册成功');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
* 获取环信token
|
||
|
|
*/
|
||
|
|
public function getToken()
|
||
|
|
{
|
||
|
|
$user_info = $this->auth->getUserinfo();
|
||
|
|
$Easemob = new Easemob();
|
||
|
|
$result = $Easemob->getUserToken($user_info['user_number'],$user_info['id']);
|
||
|
|
if (isset($result['code']) && $result['code'] <= 0) {
|
||
|
|
$this->error($result['msg']);
|
||
|
|
}
|
||
|
|
$this->success('获取成功',$result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* 发送消息
|
||
|
|
*/
|
||
|
|
public function send(){
|
||
|
|
$to_user_id = $this->request->post('to_user_id');
|
||
|
|
$send_type = $this->request->post('send_type');
|
||
|
|
$message = $this->request->post('message');
|
||
|
|
$uuid = $this->request->post('uuid','');
|
||
|
|
$thumb_uuid = $this->request->post('thumb_uuid','');
|
||
|
|
$url = $this->request->post('url','');
|
||
|
|
$length = $this->request->post('length',0);
|
||
|
|
$from_user_id = $this->auth->id;
|
||
|
|
|
||
|
|
//验证是不是好友
|
||
|
|
$is_friend = FriendRelation::isFriend($from_user_id,$to_user_id);
|
||
|
|
if (!$is_friend) $this->error('不是好友,不能发送消息');
|
||
|
|
|
||
|
|
//获取亲密度等级
|
||
|
|
$level_info = FriendRelation::getLevel($from_user_id,$to_user_id);
|
||
|
|
if (empty($level_info)) $this->error('不是好友,不能发送消息');
|
||
|
|
$level = $level_info['intimacy_level'];
|
||
|
|
$status = true;
|
||
|
|
$intimacy_value = (config('site')['intimacy_text'])??'2';
|
||
|
|
|
||
|
|
switch ($send_type){
|
||
|
|
case 'image':
|
||
|
|
if ($level < 3 ) $status = false;
|
||
|
|
break;
|
||
|
|
case 'audio':
|
||
|
|
if ($level < 1 ) $status = false;
|
||
|
|
break;
|
||
|
|
case 'video':
|
||
|
|
if ($level < 5 ) $status = false;
|
||
|
|
break;
|
||
|
|
case 'file':
|
||
|
|
if ($level < 6 ) $status = false;
|
||
|
|
break;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!$status) $this->error('亲密度不够,不能发送此消息');
|
||
|
|
|
||
|
|
$Easemob = new Easemob();
|
||
|
|
$Easemob->sendMessage($from_user_id,$to_user_id,$send_type,$message,$uuid,$url,$length,$thumb_uuid);
|
||
|
|
|
||
|
|
//增加亲密度
|
||
|
|
FriendRelation::addFriendDensity($from_user_id, $to_user_id, $intimacy_value,$send_type);
|
||
|
|
//监听亲密度升级问题
|
||
|
|
$data=['user_id'=>$from_user_id,'friend_id'=>$to_user_id,'intimacy_value'=>$intimacy_value];
|
||
|
|
Hook::listen('intimacy_uplevel', $data);
|
||
|
|
|
||
|
|
$this->success('发送成功');
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function online(){
|
||
|
|
$user_ids = $this->request->post('user_ids');
|
||
|
|
$Easemob = new Easemob();
|
||
|
|
$result = $Easemob->getOnlineUsers($user_ids);
|
||
|
|
$this->success('获取成功',$result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* 通话时间
|
||
|
|
*/
|
||
|
|
public function calltime()
|
||
|
|
{
|
||
|
|
$to_user_id = $this->request->post('to_user_id');
|
||
|
|
$minute = $this->request->post('minute');
|
||
|
|
$send_type = 'online_video';
|
||
|
|
$from_user_id = $this->auth->id;
|
||
|
|
|
||
|
|
$intimacy_value = (config('site')['intimacy_voice'])??'1';
|
||
|
|
$minute = floor($minute);
|
||
|
|
$intimacy_value = floor($minute * $intimacy_value);
|
||
|
|
$intimacy_value = $intimacy_value <=0 ? 1 : $intimacy_value;
|
||
|
|
//增加亲密度
|
||
|
|
FriendRelation::addFriendDensity($from_user_id, $to_user_id, $intimacy_value,$send_type);
|
||
|
|
//监听亲密度升级问题
|
||
|
|
$data=['user_id'=>$from_user_id,'friend_id'=>$to_user_id,'intimacy_value'=>$intimacy_value];
|
||
|
|
Hook::listen('intimacy_uplevel', $data);
|
||
|
|
$this->success('发送成功');
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
$content = $this->request->post('content');
|
||
|
|
$Easemob = new Easemob();
|
||
|
|
$data = $Easemob->uploadFile($content);
|
||
|
|
|
||
|
|
$data = array(
|
||
|
|
'username' => '18800000004',
|
||
|
|
'nickname' => '18800000004',
|
||
|
|
'id' => '8',
|
||
|
|
);
|
||
|
|
|
||
|
|
$result = $Easemob->creatuser($data);
|
||
|
|
var_dump($result);
|
||
|
|
die;
|
||
|
|
|
||
|
|
$result = $Easemob->getUserToken('18800000005','1234567');
|
||
|
|
|
||
|
|
var_dump($result);
|
||
|
|
die;
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|