Tenim.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. //设置资料
  92. public function useredit($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/profile/portrait_set";
  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. $ProfileItem = [];
  106. if($nickname){
  107. $ProfileItem[] = [
  108. 'Tag' => 'Tag_Profile_IM_Nick',
  109. 'Value' => $nickname
  110. ];
  111. }
  112. if($avatar){
  113. $ProfileItem[] = [
  114. 'Tag' => 'Tag_Profile_IM_Image',
  115. 'Value' => $avatar
  116. ];
  117. }
  118. $data = [
  119. 'From_Account' => $this->prefix . (string)$userid,
  120. 'ProfileItem' => $ProfileItem
  121. ];
  122. $res = $tencentObj->toSend($data);
  123. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  124. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  125. return $error;
  126. }
  127. return true;
  128. }
  129. //注册用户到im
  130. public function register($userid,$nickname,$avatar) {
  131. $random = rand(10000000,99999999);
  132. $usersig = $this->usersig("administrator");
  133. //dump($usersig);
  134. // 获取配置信息
  135. $config = config("tencent_im");
  136. $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import";
  137. $url .= "?sdkappid=".$config["sdkappid"];
  138. $url .= "&identifier=administrator";
  139. $url .= "&usersig=".$usersig;
  140. $url .= "&random=".$random;
  141. $url .= "&contenttype=json";
  142. $tencentObj = new tencentim($url);
  143. $data = [
  144. 'UserID' => $this->prefix . $userid,
  145. 'Nick' => $nickname,
  146. 'FaceUrl' => $avatar,
  147. ];
  148. $res = $tencentObj->toSend($data);
  149. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  150. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  151. return $error;
  152. }
  153. return true;
  154. }
  155. //某个用户是否im在线
  156. public function is_online($userid){
  157. $random = rand(10000000,99999999);
  158. $usersig = $this->usersig("administrator");
  159. //dump($usersig);
  160. // 获取配置信息
  161. $config = config("tencent_im");
  162. $url = "https://console.tim.qq.com/v4/openim/query_online_status";
  163. $url .= "?sdkappid=".$config["sdkappid"];
  164. $url .= "&identifier=administrator";
  165. $url .= "&usersig=".$usersig;
  166. $url .= "&random=".$random;
  167. $url .= "&contenttype=json";
  168. $tencentObj = new tencentim($url);
  169. $data = [
  170. 'To_Account' => [$this->prefix . $userid],
  171. ];
  172. $res = $tencentObj->toSend($data);
  173. if (!empty($res['ActionStatus']) && $res['ActionStatus'] == 'OK') {
  174. if( isset($res['QueryResult'][0]['To_Account'])
  175. && isset($res['QueryResult'][0]['Status'])
  176. && $res['QueryResult'][0]['To_Account'] == $this->prefix . $userid
  177. && $res['QueryResult'][0]['Status'] == 'Online'){
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. /**
  184. * 获取usersig签名-具体操作
  185. */
  186. public function usersig($user_id) {
  187. // 获取配置信息
  188. $config = config("tencent_im");
  189. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  190. if($user_id != 'administrator'){
  191. $user_id = $this->prefix . $user_id;
  192. }
  193. $usersig = $usersigObj->genUserSig($user_id);
  194. return $usersig;
  195. }
  196. }