Tenim.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. public function is_online($userid){
  118. $random = rand(10000000,99999999);
  119. $usersig = $this->usersig("administrator");
  120. //dump($usersig);
  121. // 获取配置信息
  122. $config = config("tencent_im");
  123. $url = "https://console.tim.qq.com/v4/openim/query_online_status";
  124. $url .= "?sdkappid=".$config["sdkappid"];
  125. $url .= "&identifier=administrator";
  126. $url .= "&usersig=".$usersig;
  127. $url .= "&random=".$random;
  128. $url .= "&contenttype=json";
  129. $tencentObj = new tencentim($url);
  130. $data = [
  131. 'To_Account' => [$userid],
  132. ];
  133. $res = $tencentObj->toSend($data);
  134. if (!empty($res['ActionStatus']) && $res['ActionStatus'] == 'OK') {
  135. if( isset($res['QueryResult'][0]['To_Account'])
  136. && isset($res['QueryResult'][0]['Status'])
  137. && $res['QueryResult'][0]['To_Account'] == $userid
  138. && $res['QueryResult'][0]['Status'] == 'Online'){
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * 获取usersig签名-具体操作
  146. */
  147. public function usersig($user_id) {
  148. // 获取配置信息
  149. $config = config("tencent_im");
  150. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  151. $usersig = $usersigObj->genUserSig($user_id);
  152. return $usersig;
  153. }
  154. }