Im.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace app\controller\api\user;
  3. use Easemob\Message;
  4. use Exception;
  5. use think\facade\Db;
  6. use Easemob\Auth;
  7. use Easemob\User;
  8. class Im
  9. {
  10. private $appKey = '1154240320150538#cxb-yhc';
  11. private $clientID = 'YXA6d1hICoWeQW6hzhQtSxqJ0A';
  12. private $clientSecret = 'YXA60XX-QAq1W2Lj2h1xE9WJAx9DzAM';
  13. //创建账号
  14. public function createImUser($account, $user_nickname, $password)
  15. {
  16. try {
  17. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  18. $user = new User($auth);
  19. $data = array(
  20. 'username' => $account,
  21. 'password' => $password,
  22. 'nickname' => $user_nickname ?? $account,
  23. );
  24. Db::name("log")->insert(array('data' => '创建环信账号请求:' . json_encode($data)));//日志记录
  25. $result = $user->create($data);
  26. Db::name("log")->insert(array('data' => '创建环信账号返回:' . json_encode($result)));//日志记录
  27. return $result['uuid'];
  28. } catch (Exception $e) {
  29. Db::name("log")->insert(array('data' => '创建环信账号出错' . $e->getMessage()));//日志记录
  30. }
  31. }
  32. /**
  33. * 获取环信用户
  34. * @param $username
  35. * @return array
  36. * @throws \Exception
  37. */
  38. public function getImUser($username)
  39. {
  40. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  41. $user = new User($auth);
  42. Db::name("log")->insert(array('data' => '获取环信账号请求:' . $username));//日志记录
  43. $result = $user->get($username);
  44. Db::name("log")->insert(array('data' => '获取环信账号返回:' . json_encode($result)));//日志记录
  45. return $result;
  46. }
  47. /**
  48. * updateUserPassword
  49. * @param $username
  50. * @return array
  51. * @throws \Exception
  52. */
  53. public function updateUserPassword($username,$password)
  54. {
  55. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  56. $user = new User($auth);
  57. $data = array(
  58. 'username' => $username,
  59. 'password' => $password ? $password : '123456',
  60. );
  61. Db::name("log")->insert(array('data' => '环信修改密码:' . json_encode($data)));//日志记录
  62. $result = $user->updateUserPassword($username, $data['password']);
  63. Db::name("log")->insert(array('data' => '环信修改密码返回:' . json_encode($result)));//日志记录
  64. return $result;
  65. }
  66. /**
  67. * 创建环信用户
  68. * @param $account
  69. * @param $password
  70. * @param $user_number
  71. * @return mixed|string
  72. */
  73. public function MerCreateImUser($account, $password, $user_nickname)
  74. {
  75. try {
  76. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  77. $user = new User($auth);
  78. $data = array(
  79. 'username' => $account,
  80. 'password' => $password ? $password : '123456',
  81. 'nickname' => $user_nickname ?? $account,
  82. );
  83. Db::name("log")->insert(array('data' => '创建环信账号请求:' . json_encode($data)));//日志记录
  84. $result = $user->create($data);
  85. Db::name("log")->insert(array('data' => '创建环信账号返回:' . json_encode($result)));//日志记录
  86. // if(isset($result['activated']) && $result['activated']==true){
  87. // Db::name('User') -> where(['account' => $account]) -> update(['im'=>1,'im_uuid'=>$result['uuid']]);
  88. // }
  89. return $result['uuid'];
  90. } catch (Exception $e) {
  91. Db::name("log")->insert(array('data' => '创建环信账号出错' . $e->getMessage()));//日志记录
  92. }
  93. return "";
  94. }
  95. /**
  96. * 发送文本消息
  97. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  98. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  99. * @param array $message 消息内容
  100. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  101. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  102. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  103. * @return array
  104. */
  105. public function sendText($target_type,
  106. $target,
  107. $message,
  108. $from = 'admin',
  109. $sync_device = false,
  110. $isOnline = false
  111. )
  112. {
  113. try {
  114. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  115. $messageObj = new Message($auth);
  116. $result = $messageObj->text($target_type,
  117. $target,
  118. $message,
  119. $from,
  120. $sync_device,
  121. $isOnline
  122. );
  123. Db::name("log")->insert(array('data' => '发送文本消息返回:' . json_encode($result)));//日志记录
  124. return $result;
  125. } catch (Exception $e) {
  126. Db::name("log")->insert(array('data' => '发送文本消息出错' . $e->getMessage()));//日志记录
  127. }
  128. }
  129. /**
  130. * 发送图片消息
  131. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  132. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  133. * @param array $message 消息内容
  134. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  135. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  136. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  137. * @return array
  138. */
  139. public function sendImage($target_type,
  140. $target,
  141. $message,
  142. $from = 'admin',
  143. $sync_device = false,
  144. $isOnline = false
  145. )
  146. {
  147. try {
  148. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  149. $messageObj = new Message($auth);
  150. $result = $messageObj->image($target_type,
  151. $target,
  152. $message,
  153. $from,
  154. $sync_device,
  155. $isOnline
  156. );
  157. Db::name("log")->insert(array('data' => '发送图片消息返回:' . json_encode($result)));//日志记录
  158. return $result;
  159. } catch (Exception $e) {
  160. Db::name("log")->insert(array('data' => '发送图片消息出错' . $e->getMessage()));//日志记录
  161. }
  162. }
  163. /**
  164. * 发送语音消息
  165. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  166. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  167. * @param array $message 消息内容
  168. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  169. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  170. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  171. * @return array
  172. */
  173. public function sendAudio($target_type,
  174. $target,
  175. $message,
  176. $from = 'admin',
  177. $sync_device = false,
  178. $isOnline = false
  179. )
  180. {
  181. try {
  182. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  183. $messageObj = new Message($auth);
  184. $result = $messageObj->audio($target_type,
  185. $target,
  186. $message,
  187. $from,
  188. $sync_device,
  189. $isOnline
  190. );
  191. Db::name("log")->insert(array('data' => '发送语音消息返回:' . json_encode($result)));//日志记录
  192. return $result;
  193. } catch (Exception $e) {
  194. Db::name("log")->insert(array('data' => '发送语音消息出错' . $e->getMessage()));//日志记录
  195. }
  196. }
  197. /**
  198. * 下载消息历史文件到本地
  199. * @param int $datetime 时间,每次只能获取一小时的消息,格式为 yyyyMMddHH 如 2018112717。
  200. * @return array|string
  201. */
  202. public function getHistoryAsUri(int $datetime)
  203. {
  204. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  205. $messageObj = new Message($auth);
  206. return $messageObj->getHistoryAsUri($datetime);
  207. }
  208. /**
  209. * 获取用户离线消息数
  210. * @param string $username 用户名
  211. * @return array|string
  212. */
  213. public function countMissedMessages(string $username)
  214. {
  215. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  216. $messageObj = new Message($auth);
  217. return $messageObj->countMissedMessages($username);
  218. }
  219. /**
  220. * 获取用户在线状态
  221. * @param string $username 要获取在线状态的用户名
  222. * @return array|string
  223. */
  224. public function isUserOnline(string $username)
  225. {
  226. $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
  227. $messageObj = new User($auth);
  228. return $messageObj->isUserOnline($username);
  229. }
  230. /**
  231. * 创建群组
  232. * @param $uid int 环信用户id
  233. * @param $groupname string 群名
  234. * @param $type string 群类型
  235. */
  236. public function createGroup($uid, $groupname, $type)
  237. {
  238. //p:商户群。d:代理商群
  239. try {
  240. $data['groupname'] = $groupname; //群名
  241. $data['desc'] = $type == 'p' ? '商户群' : '推广群'; //商户群,推广群
  242. switch ($type) {
  243. case 'p':
  244. $data['desc'] = '商户群';
  245. break;
  246. case 'd':
  247. $data['desc'] = '代理群';
  248. break;
  249. case 't':
  250. $data['desc'] = '推广群';
  251. break;
  252. }
  253. $data['public'] = true;
  254. $data['maxusers'] = 100; //群组成员最大数(包括群主),值为数值类型,默认值200
  255. $data['allowinvites'] = false;//不允许群成员邀请别人加入此群
  256. $data['members_only'] = false;
  257. $data['owner'] = $uid;//群主
  258. // $data['members'] = '';//群成员id
  259. // $data['custom'] = '';//群介绍
  260. $url = "http://im.bjtdba.com/api/im/create_group";
  261. $res = $this->posturl($url, $data);
  262. if ($res['code'] == 200) {
  263. Db::name('ImGroup')->insert([
  264. 'uid' => $uid,
  265. 'groupname' => $data['groupname'],
  266. 'desc' => $data['desc'],
  267. 'maxusers' => $data['maxusers'],
  268. 'allowinvites' => $data['allowinvites'],
  269. 'members_only' => $data['members_only'],
  270. 'owner' => $data['owner'],
  271. 'groupid' => $res['message'],
  272. 'type' => $type
  273. ]);
  274. return $res['message'];
  275. }
  276. } catch (Exception $e) {
  277. Db::name("log")->insert(array('data' => '创建环信群组出错' . $e->getMessage()));//日志记录
  278. }
  279. }
  280. /**
  281. * 加群
  282. * @param $group_id string 群组id
  283. * @param $user_id string 环信用户id
  284. * @param $uid int 幸福宝用户id
  285. */
  286. public function createGroupUser($group_id, $user_id, $uid)
  287. {
  288. try {
  289. $data['group_id'] = $group_id;
  290. $data['user_id'] = $user_id;
  291. $url = "http://im.bjtdba.com/api/im/create_group_user";
  292. $res = $this->posturl($url, $data);
  293. if ($res['code'] == 200) {
  294. Db::name('imGroupUsers')->insert([
  295. 'group_id' => $group_id,
  296. 'im_uid' => $user_id,
  297. 'uid' => $uid,
  298. 'time' => time()
  299. ]);
  300. }
  301. } catch (Exception $e) {
  302. Db::name("log")->insert(array('data' => '环信加群出错' . $e->getMessage()));//日志记录
  303. }
  304. }
  305. public function posturl($url, $data)
  306. {
  307. if (is_array($data))
  308. $data = http_build_query($data);
  309. $curl = curl_init();
  310. curl_setopt($curl, CURLOPT_URL, $url);
  311. // curl_setopt($curl,CURLOPT_HEADER, 1 ); // 过滤HTTP头
  312. // curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
  313. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
  314. curl_setopt($curl, CURLOPT_POST, true); // post传输数据
  315. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);// post传输数据
  316. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
  317. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//严格认证
  318. $output = curl_exec($curl);
  319. curl_close($curl);
  320. Db::name("log")->insert(array('data' => '环信请求结果' . $output));//日志记录
  321. return json_decode($output, true);
  322. }
  323. }