Easemob.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Redis;
  5. use think\Db;
  6. /**
  7. * 环信接口
  8. */
  9. class Easemob extends Api
  10. {
  11. protected $noNeedLogin = ["callback"];
  12. protected $noNeedRight = ['*'];
  13. protected $appKey;
  14. public function __construct() {
  15. //初始化配置
  16. $easemob_config = config('easemob');
  17. $this->appKey = $easemob_config['appkey'];
  18. }
  19. /**
  20. * 回调
  21. * https://docs-im-beta.easemob.com/document/server-side/callback_configurations.html
  22. *
  23. * 群组和聊天室操作:
  24. * muc:kick 踢出聊天室
  25. * {"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}
  26. * muc:absence 有成员离开了聊天室
  27. * {"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}
  28. * muc:leave 成员主动退出聊天室
  29. *
  30. * 用户登出
  31. * {"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"}
  32. *
  33. * 用户登出(被其他设备踢掉)
  34. * {"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"}
  35. */
  36. public function callback() {
  37. $this->notify_log_start();
  38. $input = file_get_contents("php://input"); // 主题信息
  39. $input = json_decode($input,true);
  40. //验证加密
  41. //解析数据格式
  42. //用户登出
  43. //用户登出(被其他设备踢掉)
  44. if(isset($input['reason']) && ( $input['reason'] == 'logout' || $input['status'] == 'replaced') && isset($input['status']) && $input['status'] == 'offline'){
  45. //登出
  46. $user = $input['user'];
  47. $uid = $this->get_easemob_uid($user);//用户主键id
  48. //开始处理
  49. }
  50. //
  51. if(isset($input['chat_type']) && $input['chat_type'] == 'muc' && isset($input['eventType']) && $input['eventType'] == 'chat' ){
  52. $room_id = '';
  53. $is_chatroom = false;
  54. $operation = false;
  55. $error_code = false;
  56. if(isset($input['payload'])){
  57. $payload = $input['payload'];
  58. if(isset($payload['muc_id'])){
  59. $room_id = $this->get_easemob_uid($payload['muc_id']);
  60. }
  61. if(isset($payload['is_chatroom']) && $payload['is_chatroom'] == true){
  62. $is_chatroom = true;
  63. }
  64. if(isset($payload['operation']) && in_array($payload['operation'],['kick','absence','leave']) ){
  65. $operation = true;
  66. }
  67. if(isset($payload['status']['error_code']) && $payload['status']['error_code'] == 'ok'){
  68. $error_code = true;
  69. }
  70. }
  71. //用户
  72. $uid = 0;
  73. if(isset($input['from'])){
  74. $uid = $this->get_easemob_uid($user);
  75. }
  76. if($uid && $room_id && $is_chatroom && $operation && $error_code){
  77. //开始处理
  78. }
  79. }
  80. }
  81. //输入: 1101231101159883#demo_9@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837
  82. //输出: 9
  83. //输入: 1101231101159883#demo_231181574537218@admin.conference.easemob.com
  84. //输出: 231181574537218
  85. private function get_easemob_uid($user = ''){
  86. //去掉后半段
  87. $easemob = '@';
  88. $start = strpos($user,$easemob);
  89. $uid = substr($user,0,$start);
  90. //echo $uid;
  91. //echo '<br>';
  92. //去掉前缀
  93. $uid = substr($uid,strlen($this->appKey)+1);
  94. //echo $uid;
  95. return intval($uid);
  96. }
  97. //异步日志
  98. private function notify_log_start($paytype = 'easemob'){
  99. //记录支付回调数据
  100. ignore_user_abort(); // run script in background
  101. set_time_limit(30);
  102. // 日志文件 start
  103. $log_base_dir = '../paylog/'.$paytype.'/';
  104. if (!is_dir($log_base_dir))
  105. {
  106. mkdir($log_base_dir, 0770, true);
  107. @chmod($log_base_dir, 0770);
  108. }
  109. $notify_file = $log_base_dir.'notify.txt';
  110. if(!file_exists($notify_file)) {
  111. @touch($notify_file);
  112. @chmod($notify_file, 0770);
  113. }
  114. if(filesize($notify_file)>5242880)//大于5M自动切换
  115. {
  116. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  117. }
  118. if(!file_exists($notify_file)) {
  119. @touch($notify_file);
  120. @chmod($notify_file, 0770);
  121. }
  122. // 日志文件 end
  123. //开始写入
  124. $xml = file_get_contents("php://input");
  125. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  126. ini_set('display_errors','On');
  127. return $notify_file;
  128. }
  129. }