Easemob.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\common\library;
  3. use Easemob\Auth;
  4. use Easemob\User;
  5. use Easemob\Room;
  6. class Easemob {
  7. protected $auth;
  8. protected $room;
  9. protected $user;
  10. public function __construct() {
  11. //初始化配置
  12. $easemob_config = config('easemob');
  13. $appKey = $easemob_config['appkey'];
  14. $clientIdOrAppID = $easemob_config['client_id'];
  15. $clientSecretOrAppCertificate = $easemob_config['client_secret'];
  16. $this->auth = new Auth($appKey,$clientIdOrAppID,$clientSecretOrAppCertificate);
  17. $this->room = new Room($this->auth);
  18. $this->user = new User($this->auth);
  19. }
  20. /////////////////////////////////管理用户/////////////////////////////
  21. //注册用户
  22. /*
  23. array(6) {
  24. ["uuid"] => string(36) "9783ddc0-7eeb-11ee-bbeb-913291843983"
  25. ["type"] => string(4) "user"
  26. ["created"] => int(1699526081953)
  27. ["modified"] => int(1699526081953)
  28. ["username"] => string(2) "12"
  29. ["activated"] => bool(true)
  30. }
  31. */
  32. public function user_create($user_id){
  33. $user_id = '' . $user_id . '';
  34. $register_data = ['username'=>$user_id,'password'=>123456];
  35. $rs = $this->user->create($register_data);
  36. //dump($rs);
  37. if(isset($rs['code'])){
  38. return false;
  39. }
  40. if(isset($rs['uuid'])){
  41. return true;
  42. }
  43. //默认
  44. return false;
  45. }
  46. /////////////////////////////////管理聊天室/////////////////////////////
  47. //https://docs-im-beta.easemob.com/document/server-side/chatroom.html
  48. //获取 app 中的聊天室
  49. //获取用户加入的聊天室
  50. //查询聊天室详情
  51. public function room_getRoom($roomId){
  52. $rs = $this->room->getRoom($roomId);
  53. return $rs;
  54. }
  55. //创建聊天室
  56. /*
  57. string(15) "230821166383116"
  58. */
  59. public function room_createRoom($name,$info,$user_id){
  60. $user_id = '' . $user_id . '';
  61. $room_id = $this->room->createRoom($name,$info,$user_id,[$user_id]);
  62. return $room_id;
  63. }
  64. //修改聊天室信息
  65. /*
  66. bool(true)
  67. */
  68. public function room_updateRoom($room_id,$name,$description,$maxusers = false){
  69. $data = [
  70. 'room_id' => $room_id,
  71. 'name' => $name,
  72. 'description'=>$description,
  73. ];
  74. if($maxusers !== false){
  75. $data['maxusers'] = $maxusers;
  76. }
  77. $rs = $this->room->updateRoom($data);
  78. return $rs;
  79. }
  80. //删除聊天室
  81. //获取聊天室公告
  82. //修改聊天室公告
  83. //设置聊天室自定义属性
  84. //获取聊天室自定义属性
  85. /*
  86. array(14) {
  87. ["seat4"] => string(3) "555"
  88. ["seat3"] => string(3) "555"
  89. ["seat2"] => string(3) "555"
  90. ["seat1"] => string(3) "555"
  91. ["seat8"] => string(3) "555"
  92. ["sate1"] => string(1) "5"
  93. ["seat7"] => string(3) "555"
  94. ["sate2"] => string(1) "6"
  95. ["seat6"] => string(3) "555"
  96. ["seat5"] => string(3) "555"
  97. ["sate3"] => string(1) "7"
  98. ["seat9"] => string(3) "555"
  99. ["sate4"] => string(1) "8"
  100. ["seat10"] => string(3) "555"
  101. }
  102. */
  103. public function room_getRoomCustomAttribute($room_id,$keys){
  104. $rs = $this->room->getRoomCustomAttribute($room_id,$keys);
  105. return $rs;
  106. }
  107. //删除聊天室自定义属性
  108. //强制设置聊天室自定义属性
  109. //初次设置新增,或修改都可用,一次请求的数组大小为10,可多次设置,共100个
  110. /*
  111. array(2) {
  112. ["successKeys"] => array(10) {
  113. [0] => string(5) "seat1"
  114. [1] => string(5) "seat2"
  115. [2] => string(5) "seat3"
  116. [3] => string(5) "seat4"
  117. [4] => string(5) "seat5"
  118. [5] => string(5) "seat6"
  119. [6] => string(5) "seat7"
  120. [7] => string(5) "seat8"
  121. [8] => string(5) "seat9"
  122. [9] => string(6) "seat10"
  123. }
  124. ["errorKeys"] => array(0) {
  125. }
  126. }
  127. */
  128. public function room_setRoomCustomAttributeForced($room_id,$owner_id,$matedata){
  129. $owner_id = ''.$owner_id.'';
  130. $rs = $this->room->setRoomCustomAttributeForced($room_id,$owner_id,$matedata);
  131. return $rs;
  132. }
  133. //强制删除聊天室自定义属性
  134. /*
  135. array(2) {
  136. ["successKeys"] => array(1) {
  137. [0] => string(5) "sate1" //被删除的keys
  138. }
  139. ["errorKeys"] => array(0) {
  140. }
  141. }
  142. //再次删除同一个
  143. array(2) {
  144. ["successKeys"] => array(0) {
  145. }
  146. ["errorKeys"] => array(1) {
  147. ["sate1"] => string(33) "properties key sate1 is not exist"
  148. }
  149. }
  150. */
  151. public function room_deleteRoomCustomAttributeForced($room_id,$owner_id,$keys){
  152. $owner_id = ''.$owner_id.'';
  153. $rs = $this->room->deleteRoomCustomAttributeForced($room_id,$owner_id,$keys);
  154. return $rs;
  155. }
  156. ///////////////管理聊天室成员//////////////////
  157. //分页获取聊天室成员列表
  158. //添加单个聊天室成员
  159. //批量添加聊天室成员
  160. //移除单个聊天室成员
  161. //批量移除聊天室成员
  162. //获取聊天室管理员列表
  163. //添加聊天室管理员
  164. //移除聊天室管理员
  165. //////////////////管理黑名单///////////
  166. //////////////////管理白名单///////////
  167. //////////////////管理禁言///////////
  168. }