Easemob.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. *
  52. array(2) {
  53. [0] => array(3) {
  54. ["id"] => string(15) "230909536174085"
  55. ["name"] => string(15) "聊天室名称"
  56. ["disabled"] => string(5) "false"
  57. }
  58. [1] => array(3) {
  59. ["id"] => string(15) "230909515202562"
  60. ["name"] => string(15) "888888的房间"
  61. ["disabled"] => string(5) "false"
  62. }
  63. }
  64. */
  65. public function room_listAllRoomsUserJoined($user_id){
  66. $user_id = '' . $user_id . '';
  67. $rs = $this->room->listAllRoomsUserJoined($user_id);
  68. return $rs;
  69. }
  70. //查询聊天室详情
  71. /*
  72. array(14) {
  73. ["id"] => string(15) "230909515202562"
  74. ["name"] => string(15) "888888的房间"
  75. ["description"] => string(15) "888888的房间"
  76. ["membersonly"] => bool(false)
  77. ["allowinvites"] => bool(false)
  78. ["maxusers"] => int(1000)
  79. ["owner"] => string(1) "2"
  80. ["created"] => int(1699610874211)
  81. ["custom"] => string(0) ""
  82. ["mute"] => bool(false)
  83. ["affiliations_count"] => int(1)
  84. ["disabled"] => bool(false)
  85. ["affiliations"] => array(1) {
  86. [0] => array(1) {
  87. ["owner"] => string(1) "2"
  88. }
  89. }
  90. ["public"] => bool(true)
  91. }
  92. */
  93. public function room_getRoom($roomId){
  94. $rs = $this->room->getRoom($roomId);
  95. return $rs;
  96. }
  97. //创建聊天室
  98. /*
  99. string(15) "230821166383116"
  100. */
  101. public function room_createRoom($name,$info,$user_id){
  102. $user_id = '' . $user_id . '';
  103. $room_id = $this->room->createRoom($name,$info,$user_id,[$user_id],10000);
  104. return $room_id;
  105. }
  106. //修改聊天室信息
  107. /*
  108. bool(true)
  109. */
  110. public function room_updateRoom($room_id,$name,$description,$maxusers = false){
  111. $data = [
  112. 'room_id' => $room_id,
  113. 'name' => $name,
  114. 'description'=>$description,
  115. ];
  116. if($maxusers !== false){
  117. $data['maxusers'] = $maxusers;
  118. }
  119. $rs = $this->room->updateRoom($data);
  120. return $rs;
  121. }
  122. //删除聊天室
  123. //获取聊天室公告
  124. //修改聊天室公告
  125. //设置聊天室自定义属性
  126. //获取聊天室自定义属性
  127. /*
  128. array(14) {
  129. ["seat4"] => string(3) "555"
  130. ["seat3"] => string(3) "555"
  131. ["seat2"] => string(3) "555"
  132. ["seat1"] => string(3) "555"
  133. ["seat8"] => string(3) "555"
  134. ["sate1"] => string(1) "5"
  135. ["seat7"] => string(3) "555"
  136. ["sate2"] => string(1) "6"
  137. ["seat6"] => string(3) "555"
  138. ["seat5"] => string(3) "555"
  139. ["sate3"] => string(1) "7"
  140. ["seat9"] => string(3) "555"
  141. ["sate4"] => string(1) "8"
  142. ["seat10"] => string(3) "555"
  143. }
  144. */
  145. public function room_getRoomCustomAttribute($room_id,$keys){
  146. $rs = $this->room->getRoomCustomAttribute($room_id,$keys);
  147. return $rs;
  148. }
  149. //删除聊天室自定义属性
  150. //强制设置聊天室自定义属性
  151. //初次设置新增,或修改都可用,一次请求的数组大小为10,可多次设置,共100个
  152. /*
  153. array(2) {
  154. ["successKeys"] => array(10) {
  155. [0] => string(5) "seat1"
  156. [1] => string(5) "seat2"
  157. [2] => string(5) "seat3"
  158. [3] => string(5) "seat4"
  159. [4] => string(5) "seat5"
  160. [5] => string(5) "seat6"
  161. [6] => string(5) "seat7"
  162. [7] => string(5) "seat8"
  163. [8] => string(5) "seat9"
  164. [9] => string(6) "seat10"
  165. }
  166. ["errorKeys"] => array(0) {
  167. }
  168. }
  169. */
  170. public function room_setRoomCustomAttributeForced($room_id,$owner_id,$matedata){
  171. $owner_id = ''.$owner_id.'';
  172. //先进入房间
  173. $rs = $this->room->addRoomMember($room_id,$owner_id);
  174. //dump($rs);
  175. //再设置
  176. $rs = $this->room->setRoomCustomAttributeForced($room_id,$owner_id,$matedata);
  177. return $rs;
  178. }
  179. //强制删除聊天室自定义属性
  180. /*
  181. array(2) {
  182. ["successKeys"] => array(1) {
  183. [0] => string(5) "sate1" //被删除的keys
  184. }
  185. ["errorKeys"] => array(0) {
  186. }
  187. }
  188. //再次删除同一个
  189. array(2) {
  190. ["successKeys"] => array(0) {
  191. }
  192. ["errorKeys"] => array(1) {
  193. ["sate1"] => string(33) "properties key sate1 is not exist"
  194. }
  195. }
  196. */
  197. public function room_deleteRoomCustomAttributeForced($room_id,$owner_id,$keys){
  198. $owner_id = ''.$owner_id.'';
  199. $rs = $this->room->deleteRoomCustomAttributeForced($room_id,$owner_id,$keys);
  200. return $rs;
  201. }
  202. ///////////////管理聊天室成员//////////////////
  203. //分页获取聊天室成员列表
  204. //添加单个聊天室成员
  205. //批量添加聊天室成员
  206. //移除单个聊天室成员
  207. //批量移除聊天室成员
  208. //获取聊天室管理员列表
  209. //添加聊天室管理员
  210. //移除聊天室管理员
  211. //////////////////管理黑名单///////////
  212. //////////////////管理白名单///////////
  213. //////////////////管理禁言///////////
  214. }