Tenim.php 7.4 KB

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