123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use Redis;
- use think\Db;
- /**
- * 环信接口
- */
- class Easemob extends Api
- {
- protected $noNeedLogin = ["callback"];
- protected $noNeedRight = ['*'];
- protected $appKey;
- public function __construct() {
- //初始化配置
- $easemob_config = config('easemob');
- $this->appKey = $easemob_config['appkey'];
- }
- /**
- * 回调
- * https://docs-im-beta.easemob.com/document/server-side/callback_configurations.html
- *
- * 群组和聊天室操作:
- * muc:kick 踢出聊天室
- * {"callId":"1101231101159883#demo_1215088856588617164","channel_channel":"1101231101159883#demo_9@easemob.com","eventType":"chat","channel_user":"1101231101159883#demo_231181574537218@admin.conference.easemob.com","chat_type":"muc","security":"83f9462c7769df1c979fd9dea88e7400","is_downgrade":false,"content_type":"muc:kick","payload":{"muc_id":"1101231101159883#demo_231181574537218@conference.easemob.com","reason":"chatroom kick offline user","is_chatroom":true,"operation":"kick","status":{"description":"","error_code":"ok"}},"group_id":"231181574537218","writed_channel":false,"host":"msync@ebs-ali-beijing-msync71","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"9","msg_id":"1215088856588617164","timestamp":1700474720591}
- * muc:absence 有成员离开了聊天室
- * {"callId":"1101231101159883#demo_1215088856710252128","channel_channel":"1101231101159883#demo_231181574537218@admin.conference.easemob.com","eventType":"chat","channel_user":"admin@easemob.com","chat_type":"muc","security":"ec7d29ea415ea5930c2236089dff3bc2","is_downgrade":false,"content_type":"muc:absence","payload":{"muc_id":"1101231101159883#demo_231181574537218@conference.easemob.com","is_chatroom":true,"operation":"absence"},"group_id":"231181574537218","writed_channel":false,"host":"msync@ebs-ali-beijing-msync108","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"231181574537218","msg_id":"1215088856710252128","timestamp":1700474720619}
- * muc:leave 成员主动退出聊天室
- *
- * 用户登出
- * {"callId":"1101231101159883#demo_ee82832b-3455-472d-9024-3706b1e519a7","reason":"logout","security":"6266f2a31bc3b603d8458f3d39a49d47","os":"android","ip":"182.37.138.94:39768","host":"msync@ebs-ali-beijing-msync60","session_id":"1215076057950983584","appkey":"1101231101159883#demo","user":"1101231101159883#demo_2@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837","version":"4.1.2","timestamp":1700472505682,"status":"offline"}
- *
- * 用户登出(被其他设备踢掉)
- * {"callId":"1101231101159883#demo_b2cdc4d9-a499-4e46-8aff-4503fb83a5c5","reason":"replaced","security":"1f4bb2ebb99c2f5da20048c7ca87f352","os":"android","ip":"182.37.138.94:41044","host":"msync@ebs-ali-beijing-msync62","appkey":"1101231101159883#demo","user":"1101231101159883#demo_9@easemob.com/android_fb260fdf-3935-4499-a09d-b270fdd88a0e","version":"4.1.2","timestamp":1700473675582,"status":"offline"}
- */
- public function callback() {
- $this->notify_log_start();
- $input = file_get_contents("php://input"); // 主题信息
- $input = json_decode($input,true);
- //验证加密
- //解析数据格式
- //用户登出
- //用户登出(被其他设备踢掉)
- if(isset($input['reason']) && ( $input['reason'] == 'logout' || $input['status'] == 'replaced') && isset($input['status']) && $input['status'] == 'offline'){
- //登出
- $user = $input['user'];
- $uid = $this->get_easemob_uid($user);//用户主键id
- //开始处理
- }
- //
- if(isset($input['chat_type']) && $input['chat_type'] == 'muc' && isset($input['eventType']) && $input['eventType'] == 'chat' ){
- $room_id = '';
- $is_chatroom = false;
- $operation = false;
- $error_code = false;
- if(isset($input['payload'])){
- $payload = $input['payload'];
- if(isset($payload['muc_id'])){
- $room_id = $this->get_easemob_uid($payload['muc_id']);
- }
- if(isset($payload['is_chatroom']) && $payload['is_chatroom'] == true){
- $is_chatroom = true;
- }
- if(isset($payload['operation']) && in_array($payload['operation'],['kick','absence','leave']) ){
- $operation = true;
- }
- if(isset($payload['status']['error_code']) && $payload['status']['error_code'] == 'ok'){
- $error_code = true;
- }
- }
- //用户
- $uid = 0;
- if(isset($input['from'])){
- $uid = $this->get_easemob_uid($user);
- }
- if($uid && $room_id && $is_chatroom && $operation && $error_code){
- //开始处理
- }
- }
- }
- //输入: 1101231101159883#demo_9@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837
- //输出: 9
- //输入: 1101231101159883#demo_231181574537218@admin.conference.easemob.com
- //输出: 231181574537218
- private function get_easemob_uid($user = ''){
- //去掉后半段
- $easemob = '@';
- $start = strpos($user,$easemob);
- $uid = substr($user,0,$start);
- //echo $uid;
- //echo '<br>';
- //去掉前缀
- $uid = substr($uid,strlen($this->appKey)+1);
- //echo $uid;
- return intval($uid);
- }
- //异步日志
- private function notify_log_start($paytype = 'easemob'){
- //记录支付回调数据
- ignore_user_abort(); // run script in background
- set_time_limit(30);
- // 日志文件 start
- $log_base_dir = '../paylog/'.$paytype.'/';
- if (!is_dir($log_base_dir))
- {
- mkdir($log_base_dir, 0770, true);
- @chmod($log_base_dir, 0770);
- }
- $notify_file = $log_base_dir.'notify.txt';
- if(!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- if(filesize($notify_file)>5242880)//大于5M自动切换
- {
- rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
- }
- if(!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- // 日志文件 end
- //开始写入
- $xml = file_get_contents("php://input");
- file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
- ini_set('display_errors','On');
- return $notify_file;
- }
- }
|