Easemob.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace fast\service;
  3. use fast\Http;
  4. use think\Cache;
  5. use think\Config;
  6. use think\Log;
  7. /**
  8. * 环信消息类
  9. */
  10. class Easemob
  11. {
  12. const URL = 'https://a1.easemob.com';
  13. static $_config = null;
  14. /**
  15. * 创建用户
  16. *
  17. * @param string $username 用户名
  18. * @param string $password 密码
  19. * @param string $nickname 昵称
  20. * @param boolean $token 是否认证模式
  21. * @return array
  22. */
  23. public static function register($username, $password, $nickname = '', $token = TRUE)
  24. {
  25. $params = ['username' => $username, 'password' => $password];
  26. if ($nickname)
  27. {
  28. $params['nickname'] = $nickname;
  29. }
  30. return self::api('users', $params, $token);
  31. }
  32. /**
  33. * 创建聊天室
  34. *
  35. * @param string $name 名称
  36. * @param string $description 描述
  37. * @param string $owner 创建人
  38. * @param int $maxusers 最多参与者
  39. * @param array $members 成员
  40. * @return array
  41. */
  42. public static function createRoom($name, $description, $owner, $maxusers = 5000, $members = [])
  43. {
  44. $owner = (string) $owner;
  45. $params = [
  46. "name" => $name, //聊天室名称,此属性为必须的
  47. "description" => $description, //聊天室描述,此属性为必须的
  48. "maxusers" => $maxusers, //聊天室成员最大数(包括群主),值为数值类型,默认值200,最大值5000,此属性为可选的
  49. "owner" => $owner, //聊天室的管理员,此属性为必须的
  50. ];
  51. if ($members)
  52. {
  53. if (!in_array($owner, $members))
  54. {
  55. $members[] = $owner;
  56. }
  57. $params['members'] = $members;
  58. }
  59. return self::api('chatrooms', $params, TRUE);
  60. }
  61. /**
  62. * 创建群组
  63. *
  64. * @param string $name 名称
  65. * @param string $description 描述
  66. * @param string $owner 管理员
  67. * @param int $maxusers 最大成员数量
  68. * @param array $members 成员列表
  69. * @param boolean $public 是否公开
  70. * @param boolean $approval 加入是否审核
  71. * @return array
  72. */
  73. public static function createGroup($name, $description, $owner, $maxusers = 2000, $members = [], $public = TRUE, $approval = FALSE)
  74. {
  75. $owner = (string) $owner;
  76. $params = [
  77. "groupname" => $name,
  78. "desc" => $description,
  79. "public" => (bool) $public,
  80. "maxusers" => $maxusers,
  81. "approval" => (bool) $approval,
  82. "owner" => $owner,
  83. ];
  84. if ($members)
  85. {
  86. if (!in_array($owner, $members))
  87. {
  88. $members[] = $owner;
  89. }
  90. $params['members'] = $members;
  91. }
  92. return self::api('chatgroups', $params, TRUE);
  93. }
  94. /**
  95. * 修改群组信息
  96. *
  97. * @param int $group_id
  98. * @param string $name
  99. * @param string $description
  100. * @param int $maxusers
  101. * @return array
  102. */
  103. public static function editGroup($group_id, $name, $description, $maxusers)
  104. {
  105. $params = [
  106. "groupname" => $name,
  107. "desc" => $description,
  108. "maxusers" => $maxusers,
  109. ];
  110. return self::api('chatgroups/' . $group_id, $params, TRUE, 'PUT');
  111. }
  112. /**
  113. * 获取好友列表
  114. */
  115. public static function getFiends($owner_username)
  116. {
  117. return self::api("users/{$owner_username}/contacts/users", [], TRUE, 'GET');
  118. }
  119. /**
  120. * 删除群组
  121. *
  122. * @param int $group_id
  123. * @return array
  124. */
  125. public static function deleteGroup($group_id)
  126. {
  127. $params = [];
  128. return self::api('chatgroups/' . $group_id, $params, TRUE, 'DELETE');
  129. }
  130. /**
  131. * 发送消息
  132. *
  133. * @param string $from 发件人
  134. * @param string $to 收件人
  135. * @param mixed $msg 消息内容
  136. * @param string $target_type 消息类型 users/chatgroups/chatrooms
  137. * @return array
  138. */
  139. public static function sendMessage($from, $to, $msg, $target_type = 'users')
  140. {
  141. if (!is_array($msg))
  142. {
  143. $msg = [
  144. 'type' => 'txt',
  145. 'msg' => $msg
  146. ];
  147. }
  148. $data = [
  149. 'target_type' => $target_type,
  150. 'target' => is_array($to) ? $to : [$to],
  151. 'from' => $from,
  152. ];
  153. if (isset($msg['ext']))
  154. {
  155. $data['ext'] = $msg['ext'];
  156. }
  157. unset($msg['ext']);
  158. $data['msg'] = $msg;
  159. return self::api('messages', $data);
  160. }
  161. /**
  162. * 获取离线消息记录条数
  163. * @param string $owner_username
  164. * @return array
  165. */
  166. public static function getOfflineMsgCount($owner_username)
  167. {
  168. return self::api("users/{$owner_username}/offline_msg_count", [], TRUE, 'GET');
  169. }
  170. /**
  171. * 群组添加成员
  172. * @param int $chatroom_id
  173. * @param array $usernames
  174. * @param array
  175. */
  176. public static function addChatRoomMembers($chatroom_id, $usernames)
  177. {
  178. return self::api("chatgroups/{$chatroom_id}/users", $usernames, TRUE);
  179. }
  180. /**
  181. * 添加单个成员POST
  182. */
  183. public static function addOneChatRoomMember($chatroom_id, $username)
  184. {
  185. //return $chatroom_id;
  186. return self::api("chatgroups/{$chatroom_id}/users/{$username}", [], TRUE);
  187. }
  188. /**
  189. * 群组删除成员
  190. * @param int $chatroom_id
  191. * @param string $usernames
  192. * @return array
  193. */
  194. public static function minusChatRoomMembers($chatroom_id, $usernames)
  195. {
  196. return self::api("chatgroups/{$chatroom_id}/users/{$usernames}", [], TRUE, 'DELETE');
  197. }
  198. /**
  199. * 添加好友
  200. */
  201. public static function addFriends($owner_username, $friend_username)
  202. {
  203. return self::api("users/{$owner_username}/contacts/users/{$friend_username}", [], TRUE);
  204. }
  205. /**
  206. * 删除好友
  207. */
  208. public static function minusFriends($owner_username, $friend_username)
  209. {
  210. return self::api("users/{$owner_username}/contacts/users/{$friend_username}", [], TRUE, 'DELETE');
  211. }
  212. /**
  213. * 查看用户参与的所有群组
  214. * @param type $owner_username
  215. * @return type
  216. */
  217. public static function joinedChatgroups($owner_username)
  218. {
  219. return self::api("users/{$owner_username}/joined_chatgroups", [], TRUE, 'GET');
  220. }
  221. /**
  222. * 调用API接口
  223. *
  224. * @param string $api 接口
  225. * @param array $params request head参数
  226. * @param boolean $token 是否认证模式
  227. * @param string $method 请求方法 POST/GET
  228. * @param array $options 扩展配置
  229. * @return array
  230. */
  231. public static function api($api, $params = [], $token = TRUE, $method = 'POST', $options = [])
  232. {
  233. $header = ['Content-Type:application/json'];
  234. if ($token)
  235. {
  236. $header[] = self::getNewToken();
  237. }
  238. $config = Config::get('service.easemob');
  239. $options[CURLOPT_HTTPHEADER] = $header;
  240. $url = self::URL . '/' . $config['org_name'] . '/' . $config['app_name'] . '/' . $api;
  241. //return $url;
  242. $ret = Http::sendRequest($url, json_encode($params), $method, $options);
  243. if ($ret['ret'] && $ret['msg'])
  244. {
  245. $msg = json_decode($ret['msg'], TRUE);
  246. if (isset($msg['error']))
  247. {
  248. Log::error($ret['msg']);
  249. }
  250. return isset($msg['error']) ? [] : $msg;
  251. }
  252. else
  253. {
  254. return [];
  255. }
  256. }
  257. private static function getToken()
  258. {
  259. $tokendata = Cache::get('easemobtoken');
  260. if ($tokendata && $tokendata['expiretime'] > time())
  261. {
  262. return $tokendata['access_token'];
  263. }
  264. else
  265. {
  266. $config = Config::get('service.easemob');
  267. $data = self::api('token', [
  268. 'grant_type' => 'client_credentials',
  269. 'client_id' => $config['client_id'],
  270. 'client_secret' => $config['client_secret'],
  271. ], FALSE, 'POST');
  272. if ($data)
  273. {
  274. $data['expiretime'] = time() + $data['expires_in'];
  275. Cache::set('easemobtoken', $data, $data['expires_in'] - 10);
  276. return $data['access_token'];
  277. }
  278. else
  279. {
  280. return '';
  281. }
  282. }
  283. }
  284. }