Tenim.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\common\library;
  3. use getusersig\getusersig;
  4. use tencentim\tencentim;
  5. class Tenim
  6. {
  7. /**
  8. * 发送消息给某人-接口调用
  9. */
  10. public function sendToUser() {
  11. $from_user = '2';// 发送者
  12. $to_user = '294';// 接收者
  13. $message = 'hello许犇';// 接收者
  14. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  15. $this->sendMessageToUser($from_user,$to_user,$message);
  16. }
  17. /**
  18. * 发送消息给某人
  19. */
  20. //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  21. public function sendMessageToUser($from_user,$to_user,$message) {
  22. $random = rand(10000000,99999999);
  23. $usersig = $this->usersig("administrator");
  24. // 获取配置信息
  25. $config = config("tencent_im");
  26. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  27. $url .= "?sdkappid=".$config["sdkappid"];
  28. $url .= "&identifier=administrator";
  29. $url .= "&usersig=".$usersig;
  30. $url .= "&random=".$random;
  31. $url .= "&contenttype=json";
  32. $tencentObj = new tencentim($url);
  33. $data = [];
  34. $data["SyncOtherMachine"] = 1;
  35. $data["From_Account"] = (string)$from_user;
  36. $data["To_Account"] = (string)$to_user;
  37. $data["MsgRandom"] = rand(1000000,9999999);
  38. $data["MsgTimeStamp"] = time();
  39. $data["MsgBody"][] = [
  40. "MsgType" => "TIMTextElem",
  41. "MsgContent" => [
  42. "Text"=> $message
  43. ],
  44. ];
  45. $res = $tencentObj->toSend($data);
  46. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  47. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  48. return $error;
  49. }
  50. return true;
  51. }
  52. /**
  53. * 发送自定义消息给某人
  54. */
  55. public function sendCustomMessageToUser($from_user,$to_user,$message) {
  56. $random = rand(10000000,99999999);
  57. $usersig = $this->usersig("administrator");
  58. // 获取配置信息
  59. $config = config("tencent_im");
  60. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  61. $url .= "?sdkappid=".$config["sdkappid"];
  62. $url .= "&identifier=administrator";
  63. $url .= "&usersig=".$usersig;
  64. $url .= "&random=".$random;
  65. $url .= "&contenttype=json";
  66. $tencentObj = new tencentim($url);
  67. $data = [];
  68. $data["SyncOtherMachine"] = 1;//1=消息同步至发送方,2=消息不同步至发送方
  69. $data["From_Account"] = (string)$from_user;
  70. $data["To_Account"] = (string)$to_user;
  71. $data["MsgRandom"] = rand(1000000,9999999);
  72. $data["MsgTimeStamp"] = time();
  73. $data["MsgBody"][] = [
  74. "MsgType" => "TIMCustomElem",
  75. "MsgContent" => [
  76. "Data" => json_encode($message),
  77. "Desc" => $message['name'],
  78. "Ext" => $message['name'],
  79. "Sound"=> '',
  80. ],
  81. ];
  82. $data['CloudCustomData'] = $message['id'];
  83. $res = $tencentObj->toSend($data);
  84. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  85. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  86. return $error;
  87. }
  88. return true;
  89. }
  90. //注册用户到im
  91. public function register($userid,$nickname,$avatar) {
  92. $random = rand(10000000,99999999);
  93. $usersig = $this->usersig("administrator");
  94. //dump($usersig);
  95. // 获取配置信息
  96. $config = config("tencent_im");
  97. $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import";
  98. $url .= "?sdkappid=".$config["sdkappid"];
  99. $url .= "&identifier=administrator";
  100. $url .= "&usersig=".$usersig;
  101. $url .= "&random=".$random;
  102. $url .= "&contenttype=json";
  103. $tencentObj = new tencentim($url);
  104. $data = [
  105. 'UserID' => $userid,
  106. 'Nick' => $nickname,
  107. 'FaceUrl' => $avatar,
  108. ];
  109. $res = $tencentObj->toSend($data);
  110. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  111. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  112. return $error;
  113. }
  114. return true;
  115. }
  116. //某个用户是否im在线
  117. //https://cloud.tencent.com/document/product/269/2566
  118. /* QueryResult.Status
  119. 返回的用户状态,目前支持的状态有:
  120. 前台运行状态(Online):客户端登录后和即时通信 IM 后台有长连接
  121. 后台运行状态(PushOnline):iOS 和 Android 进程被 kill 或因网络问题掉线,进入 PushOnline 状态,此时仍然可以接收消息的离线推送。客户端切到后台,但是进程未被手机操作系统 kill 掉时,此时状态仍是 Online
  122. 未登录状态(Offline):客户端主动退出登录或者客户端自上一次登录起7天之内未登录过
  123. 如果用户是多终端登录,则只要有一个终端的状态是 Online ,该字段值就是 Online
  124. */
  125. public function is_online($userid){
  126. $random = rand(10000000,99999999);
  127. $usersig = $this->usersig("administrator");
  128. //dump($usersig);
  129. // 获取配置信息
  130. $config = config("tencent_im");
  131. $url = "https://console.tim.qq.com/v4/openim/query_online_status";
  132. $url .= "?sdkappid=".$config["sdkappid"];
  133. $url .= "&identifier=administrator";
  134. $url .= "&usersig=".$usersig;
  135. $url .= "&random=".$random;
  136. $url .= "&contenttype=json";
  137. $tencentObj = new tencentim($url);
  138. $data = [
  139. 'To_Account' => [$userid],
  140. ];
  141. $res = $tencentObj->toSend($data);
  142. if (!empty($res['ActionStatus']) && $res['ActionStatus'] == 'OK') {
  143. if( isset($res['QueryResult'][0]['To_Account'])
  144. && isset($res['QueryResult'][0]['Status'])
  145. && $res['QueryResult'][0]['To_Account'] == $userid
  146. && $res['QueryResult'][0]['Status'] == 'Online'){
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. /**
  153. * 获取usersig签名-具体操作
  154. */
  155. public function usersig($user_id) {
  156. // 获取配置信息
  157. $config = config("tencent_im");
  158. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  159. $usersig = $usersigObj->genUserSig($user_id);
  160. return $usersig;
  161. }
  162. }