Tenim.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. //设置资料
  91. public function useredit($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/profile/portrait_set";
  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. $ProfileItem = [];
  105. if($nickname){
  106. $ProfileItem[] = [
  107. 'Tag' => 'Tag_Profile_IM_Nick',
  108. 'Value' => $nickname
  109. ];
  110. }
  111. if($avatar){
  112. $ProfileItem[] = [
  113. 'Tag' => 'Tag_Profile_IM_Image',
  114. 'Value' => $avatar
  115. ];
  116. }
  117. $data = [
  118. 'From_Account' => $userid,
  119. 'ProfileItem' => $ProfileItem
  120. ];
  121. $res = $tencentObj->toSend($data);
  122. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  123. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  124. return $error;
  125. }
  126. return true;
  127. }
  128. //注册用户到im
  129. public function register($userid,$nickname,$avatar) {
  130. $random = rand(10000000,99999999);
  131. $usersig = $this->usersig("administrator");
  132. //dump($usersig);
  133. // 获取配置信息
  134. $config = config("tencent_im");
  135. $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import";
  136. $url .= "?sdkappid=".$config["sdkappid"];
  137. $url .= "&identifier=administrator";
  138. $url .= "&usersig=".$usersig;
  139. $url .= "&random=".$random;
  140. $url .= "&contenttype=json";
  141. $tencentObj = new tencentim($url);
  142. $data = [
  143. 'UserID' => $userid,
  144. 'Nick' => $nickname,
  145. 'FaceUrl' => $avatar,
  146. ];
  147. $res = $tencentObj->toSend($data);
  148. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  149. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  150. return $error;
  151. }
  152. return true;
  153. }
  154. //某个用户是否im在线
  155. //https://cloud.tencent.com/document/product/269/2566
  156. /* QueryResult.Status
  157. 返回的用户状态,目前支持的状态有:
  158. 前台运行状态(Online):客户端登录后和即时通信 IM 后台有长连接
  159. 后台运行状态(PushOnline):iOS 和 Android 进程被 kill 或因网络问题掉线,进入 PushOnline 状态,此时仍然可以接收消息的离线推送。客户端切到后台,但是进程未被手机操作系统 kill 掉时,此时状态仍是 Online
  160. 未登录状态(Offline):客户端主动退出登录或者客户端自上一次登录起7天之内未登录过
  161. 如果用户是多终端登录,则只要有一个终端的状态是 Online ,该字段值就是 Online
  162. */
  163. public function is_online($userid){
  164. $random = rand(10000000,99999999);
  165. $usersig = $this->usersig("administrator");
  166. //dump($usersig);
  167. // 获取配置信息
  168. $config = config("tencent_im");
  169. $url = "https://console.tim.qq.com/v4/openim/query_online_status";
  170. $url .= "?sdkappid=".$config["sdkappid"];
  171. $url .= "&identifier=administrator";
  172. $url .= "&usersig=".$usersig;
  173. $url .= "&random=".$random;
  174. $url .= "&contenttype=json";
  175. $tencentObj = new tencentim($url);
  176. $data = [
  177. 'To_Account' => [$userid],
  178. ];
  179. $res = $tencentObj->toSend($data);
  180. if (!empty($res['ActionStatus']) && $res['ActionStatus'] == 'OK') {
  181. if( isset($res['QueryResult'][0]['To_Account'])
  182. && isset($res['QueryResult'][0]['Status'])
  183. && $res['QueryResult'][0]['To_Account'] == $userid
  184. && $res['QueryResult'][0]['Status'] == 'Online'){
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * 获取usersig签名-具体操作
  192. */
  193. public function usersig($user_id) {
  194. // 获取配置信息
  195. $config = config("tencent_im");
  196. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  197. $usersig = $usersigObj->genUserSig($user_id);
  198. return $usersig;
  199. }
  200. }