Tenim.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\common\library;
  3. use getusersig\getusersig;
  4. use tencentim\tencentim;
  5. class Tenim
  6. {
  7. protected $prefix = 'xl';
  8. /**
  9. * 发送消息给某人-接口调用
  10. */
  11. public function sendToUser() {
  12. $from_user = '2';// 发送者
  13. $to_user = '294';// 接收者
  14. $message = 'hello许犇';// 接收者
  15. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  16. $this->sendMessageToUser($from_user,$to_user,$message);
  17. }
  18. /**
  19. * 发送消息给某人
  20. */
  21. //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  22. public function sendMessageToUser($from_user,$to_user,$message) {
  23. $random = rand(10000000,99999999);
  24. $usersig = $this->usersig("administrator");
  25. // 获取配置信息
  26. $config = config("tencent_im");
  27. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  28. $url .= "?sdkappid=".$config["sdkappid"];
  29. $url .= "&identifier=administrator";
  30. $url .= "&usersig=".$usersig;
  31. $url .= "&random=".$random;
  32. $url .= "&contenttype=json";
  33. $tencentObj = new tencentim($url);
  34. $data = [];
  35. $data["SyncOtherMachine"] = 1;
  36. $data["From_Account"] = $this->prefix . (string)$from_user;
  37. $data["To_Account"] = $this->prefix . (string)$to_user;
  38. $data["MsgRandom"] = rand(1000000,9999999);
  39. $data["MsgTimeStamp"] = time();
  40. $data["MsgBody"][] = [
  41. "MsgType" => "TIMTextElem",
  42. "MsgContent" => [
  43. "Text"=> $message
  44. ],
  45. ];
  46. $res = $tencentObj->toSend($data);
  47. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  48. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  49. return $error;
  50. }
  51. return true;
  52. }
  53. /**
  54. * 发送自定义消息给某人
  55. */
  56. public function sendCustomMessageToUser($from_user,$to_user,$message) {
  57. $random = rand(10000000,99999999);
  58. $usersig = $this->usersig("administrator");
  59. // 获取配置信息
  60. $config = config("tencent_im");
  61. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  62. $url .= "?sdkappid=".$config["sdkappid"];
  63. $url .= "&identifier=administrator";
  64. $url .= "&usersig=".$usersig;
  65. $url .= "&random=".$random;
  66. $url .= "&contenttype=json";
  67. $tencentObj = new tencentim($url);
  68. $data = [];
  69. $data["SyncOtherMachine"] = 1;//1=消息同步至发送方,2=消息不同步至发送方
  70. $data["From_Account"] = $this->prefix . (string)$from_user;
  71. $data["To_Account"] = $this->prefix . (string)$to_user;
  72. $data["MsgRandom"] = rand(1000000,9999999);
  73. $data["MsgTimeStamp"] = time();
  74. $data["MsgBody"][] = [
  75. "MsgType" => "TIMCustomElem",
  76. "MsgContent" => [
  77. "Data" => json_encode($message),
  78. "Desc" => $message['name'],
  79. "Ext" => $message['name'],
  80. "Sound"=> '',
  81. ],
  82. ];
  83. $data['CloudCustomData'] = $message['id'];
  84. $res = $tencentObj->toSend($data);
  85. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  86. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  87. return $error;
  88. }
  89. return true;
  90. }
  91. //注册用户到im
  92. public function register($userid,$nickname,$avatar) {
  93. $random = rand(10000000,99999999);
  94. $usersig = $this->usersig("administrator");
  95. //dump($usersig);
  96. // 获取配置信息
  97. $config = config("tencent_im");
  98. $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import";
  99. $url .= "?sdkappid=".$config["sdkappid"];
  100. $url .= "&identifier=administrator";
  101. $url .= "&usersig=".$usersig;
  102. $url .= "&random=".$random;
  103. $url .= "&contenttype=json";
  104. $tencentObj = new tencentim($url);
  105. $data = [
  106. 'UserID' => $this->prefix . $userid,
  107. 'Nick' => $nickname,
  108. 'FaceUrl' => $avatar,
  109. ];
  110. $res = $tencentObj->toSend($data);
  111. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  112. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  113. return $error;
  114. }
  115. return true;
  116. }
  117. //某个用户是否im在线
  118. public function is_online($userid){
  119. $random = rand(10000000,99999999);
  120. $usersig = $this->usersig("administrator");
  121. //dump($usersig);
  122. // 获取配置信息
  123. $config = config("tencent_im");
  124. $url = "https://console.tim.qq.com/v4/openim/query_online_status";
  125. $url .= "?sdkappid=".$config["sdkappid"];
  126. $url .= "&identifier=administrator";
  127. $url .= "&usersig=".$usersig;
  128. $url .= "&random=".$random;
  129. $url .= "&contenttype=json";
  130. $tencentObj = new tencentim($url);
  131. $data = [
  132. 'To_Account' => [$this->prefix . $userid],
  133. ];
  134. $res = $tencentObj->toSend($data);
  135. if (!empty($res['ActionStatus']) && $res['ActionStatus'] == 'OK') {
  136. if( isset($res['QueryResult'][0]['To_Account'])
  137. && isset($res['QueryResult'][0]['Status'])
  138. && $res['QueryResult'][0]['To_Account'] == $this->prefix . $userid
  139. && $res['QueryResult'][0]['Status'] == 'Online'){
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * 获取usersig签名-具体操作
  147. */
  148. public function usersig($user_id) {
  149. // 获取配置信息
  150. $config = config("tencent_im");
  151. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  152. $usersig = $usersigObj->genUserSig($this->prefix . $user_id);
  153. return $usersig;
  154. }
  155. }