Easemob.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace app\common\library;
  3. use Easemob\Auth;
  4. use Easemob\Group;
  5. use Easemob\User;
  6. use Easemob\Room;
  7. class Easemob {
  8. protected $auth;
  9. protected $room;
  10. protected $user;
  11. protected $group;
  12. public function __construct() {
  13. //初始化配置
  14. $easemob_config = config('easemob');
  15. $appKey = $easemob_config['appkey'];
  16. $clientIdOrAppID = $easemob_config['client_id'];
  17. $clientSecretOrAppCertificate = $easemob_config['client_secret'];
  18. $this->auth = new Auth($appKey,$clientIdOrAppID,$clientSecretOrAppCertificate);
  19. $this->room = new Room($this->auth);
  20. $this->user = new User($this->auth);
  21. $this->group = new Group($this->auth);
  22. }
  23. /////////////////////////////////管理用户/////////////////////////////
  24. //注册用户
  25. /*
  26. array(6) {
  27. ["uuid"] => string(36) "9783ddc0-7eeb-11ee-bbeb-913291843983"
  28. ["type"] => string(4) "user"
  29. ["created"] => int(1699526081953)
  30. ["modified"] => int(1699526081953)
  31. ["username"] => string(2) "12"
  32. ["activated"] => bool(true)
  33. }
  34. */
  35. public function user_create($user_id){
  36. $user_id = '' . $user_id . '';
  37. $register_data = ['username'=>$user_id,'password'=>123456];
  38. $rs = $this->user->create($register_data);
  39. //dump($rs);
  40. if(isset($rs['code'])){
  41. return false;
  42. }
  43. if(isset($rs['uuid'])){
  44. return true;
  45. }
  46. //默认
  47. return false;
  48. }
  49. /////////////////////////////////管理群组/////////////////////////////
  50. //创建群组,返回群组id
  51. public function group_createPublicGroup($user_id,$guild_name,$guild_desc,$maxusers){
  52. $user_id = ''.$user_id.'';
  53. $rs = $this->group->createPublicGroup($user_id,$guild_name,$guild_desc,[],$maxusers);
  54. return $rs;
  55. }
  56. //添加一个群组成员
  57. //$rs === true;
  58. public function group_addGroupMember($groupId,$user_id){
  59. $rs = $this->group->addGroupMember($groupId,$user_id);
  60. return $rs;
  61. }
  62. //移除一个群组成员
  63. public function group_removeGroupMember($groupId,$user_id)
  64. {
  65. $rs = $this->group->removeGroupMember($groupId,$user_id);
  66. return $rs;
  67. }
  68. //添加一个管理员
  69. public function group_addGroupAdmin($groupId,$user_id){
  70. $rs = $this->group->addGroupAdmin($groupId,$user_id);
  71. return $rs;
  72. }
  73. //移除一个管理员
  74. public function group_removeGroupAdmin($groupId,$user_id){
  75. $rs = $this->group->removeGroupAdmin($groupId,$user_id);
  76. return $rs;
  77. }
  78. /////////////////////////////////管理聊天室/////////////////////////////
  79. //https://docs-im-beta.easemob.com/document/server-side/chatroom.html
  80. //获取 app 中的聊天室
  81. /**
  82. * 获取用户所加入的聊天室
  83. *
  84. array(2) {
  85. [0] => array(3) {
  86. ["id"] => string(15) "230909536174085"
  87. ["name"] => string(15) "聊天室名称"
  88. ["disabled"] => string(5) "false"
  89. }
  90. [1] => array(3) {
  91. ["id"] => string(15) "230909515202562"
  92. ["name"] => string(15) "888888的房间"
  93. ["disabled"] => string(5) "false"
  94. }
  95. }
  96. */
  97. public function room_listAllRoomsUserJoined($user_id){
  98. $user_id = '' . $user_id . '';
  99. $rs = $this->room->listAllRoomsUserJoined($user_id);
  100. return $rs;
  101. }
  102. //查询聊天室详情
  103. /*
  104. array(14) {
  105. ["id"] => string(15) "230909515202562"
  106. ["name"] => string(15) "888888的房间"
  107. ["description"] => string(15) "888888的房间"
  108. ["membersonly"] => bool(false)
  109. ["allowinvites"] => bool(false)
  110. ["maxusers"] => int(1000)
  111. ["owner"] => string(1) "2"
  112. ["created"] => int(1699610874211)
  113. ["custom"] => string(0) ""
  114. ["mute"] => bool(false)
  115. ["affiliations_count"] => int(1)
  116. ["disabled"] => bool(false)
  117. ["affiliations"] => array(1) {
  118. [0] => array(1) {
  119. ["owner"] => string(1) "2"
  120. }
  121. }
  122. ["public"] => bool(true)
  123. }
  124. */
  125. public function room_getRoom($roomId){
  126. $rs = $this->room->getRoom($roomId);
  127. return $rs;
  128. }
  129. //创建聊天室
  130. /*
  131. string(15) "230821166383116"
  132. */
  133. public function room_createRoom($name,$info,$user_id){
  134. $user_id = '' . $user_id . '';
  135. $room_id = $this->room->createRoom($name,$info,$user_id,[$user_id],10000);
  136. return $room_id;
  137. }
  138. //修改聊天室信息
  139. /*
  140. bool(true)
  141. */
  142. public function room_updateRoom($room_id,$name,$description,$maxusers = false){
  143. $data = [
  144. 'room_id' => $room_id,
  145. 'name' => $name,
  146. 'description'=>$description,
  147. ];
  148. if($maxusers !== false){
  149. $data['maxusers'] = $maxusers;
  150. }
  151. $rs = $this->room->updateRoom($data);
  152. return $rs;
  153. }
  154. //删除聊天室
  155. //获取聊天室公告
  156. //修改聊天室公告
  157. //设置聊天室自定义属性
  158. //获取聊天室自定义属性
  159. /*
  160. array(14) {
  161. ["seat4"] => string(3) "555"
  162. ["seat3"] => string(3) "555"
  163. ["seat2"] => string(3) "555"
  164. ["seat1"] => string(3) "555"
  165. ["seat8"] => string(3) "555"
  166. ["sate1"] => string(1) "5"
  167. ["seat7"] => string(3) "555"
  168. ["sate2"] => string(1) "6"
  169. ["seat6"] => string(3) "555"
  170. ["seat5"] => string(3) "555"
  171. ["sate3"] => string(1) "7"
  172. ["seat9"] => string(3) "555"
  173. ["sate4"] => string(1) "8"
  174. ["seat10"] => string(3) "555"
  175. }
  176. */
  177. public function room_getRoomCustomAttribute($room_id,$keys){
  178. $rs = $this->room->getRoomCustomAttribute($room_id,$keys);
  179. return $rs;
  180. }
  181. //删除聊天室自定义属性
  182. //强制设置聊天室自定义属性
  183. //初次设置新增,或修改都可用,一次请求的数组大小为10,可多次设置,共100个
  184. /*
  185. array(2) {
  186. ["successKeys"] => array(10) {
  187. [0] => string(5) "seat1"
  188. [1] => string(5) "seat2"
  189. [2] => string(5) "seat3"
  190. [3] => string(5) "seat4"
  191. [4] => string(5) "seat5"
  192. [5] => string(5) "seat6"
  193. [6] => string(5) "seat7"
  194. [7] => string(5) "seat8"
  195. [8] => string(5) "seat9"
  196. [9] => string(6) "seat10"
  197. }
  198. ["errorKeys"] => array(0) {
  199. }
  200. }
  201. */
  202. public function room_setRoomCustomAttributeForced($room_id,$owner_id,$matedata){
  203. $owner_id = ''.$owner_id.'';
  204. //先进入房间
  205. $rs = $this->room->addRoomMember($room_id,$owner_id);
  206. //dump($rs);
  207. //再设置
  208. $rs = $this->room->setRoomCustomAttributeForced($room_id,$owner_id,$matedata);
  209. return $rs;
  210. }
  211. //强制删除聊天室自定义属性
  212. /*
  213. array(2) {
  214. ["successKeys"] => array(1) {
  215. [0] => string(5) "sate1" //被删除的keys
  216. }
  217. ["errorKeys"] => array(0) {
  218. }
  219. }
  220. //再次删除同一个
  221. array(2) {
  222. ["successKeys"] => array(0) {
  223. }
  224. ["errorKeys"] => array(1) {
  225. ["sate1"] => string(33) "properties key sate1 is not exist"
  226. }
  227. }
  228. */
  229. public function room_deleteRoomCustomAttributeForced($room_id,$owner_id,$keys){
  230. $owner_id = ''.$owner_id.'';
  231. $rs = $this->room->deleteRoomCustomAttributeForced($room_id,$owner_id,$keys);
  232. return $rs;
  233. }
  234. ///////////////管理聊天室成员//////////////////
  235. //分页获取聊天室成员列表
  236. public function room_listRoomMembers($roomId, $pageSize = 10, $pageNum = 1){
  237. $rs = $this->room->listRoomMembers($roomId, $pageSize, $pageNum);
  238. return $rs;
  239. }
  240. //添加单个聊天室成员
  241. //批量添加聊天室成员
  242. //移除单个聊天室成员
  243. //批量移除聊天室成员
  244. public function room_removeRoomMembers($roomId, $usernames){
  245. $rs = $this->room->removeRoomMembers($roomId, $usernames);
  246. return $rs;
  247. }
  248. //获取聊天室管理员列表
  249. //添加聊天室管理员
  250. public function room_promoteRoomAdmin($roomId, $newadmin){
  251. $newadmin = ''.$newadmin.'';
  252. $rs = $this->room->promoteRoomAdmin($roomId, $newadmin);
  253. //dump($rs);
  254. return $rs;
  255. }
  256. //移除聊天室管理员
  257. public function room_demoteRoomAdmin($roomId, $oldadmin){
  258. $oldadmin = ''.$oldadmin.'';
  259. $rs = $this->room->demoteRoomAdmin($roomId, $oldadmin);
  260. //dump($rs);
  261. return $rs;
  262. }
  263. //////////////////管理黑名单///////////
  264. //////////////////管理白名单///////////
  265. //////////////////管理禁言///////////
  266. }