Tenim.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $res = $tencentObj->toSend($data);
  83. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  84. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  85. return $error;
  86. }
  87. return true;
  88. }
  89. //注册用户到im
  90. public function register($userid,$nickname,$avatar) {
  91. $random = rand(10000000,99999999);
  92. $usersig = $this->usersig("administrator");
  93. //dump($usersig);
  94. // 获取配置信息
  95. $config = config("tencent_im");
  96. $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import";
  97. $url .= "?sdkappid=".$config["sdkappid"];
  98. $url .= "&identifier=administrator";
  99. $url .= "&usersig=".$usersig;
  100. $url .= "&random=".$random;
  101. $url .= "&contenttype=json";
  102. $tencentObj = new tencentim($url);
  103. $data = [
  104. 'UserID' => $userid,
  105. 'Nick' => $nickname,
  106. 'FaceUrl' => $avatar,
  107. ];
  108. $res = $tencentObj->toSend($data);
  109. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  110. $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
  111. return $error;
  112. }
  113. return true;
  114. }
  115. /**
  116. * 获取usersig签名-具体操作
  117. */
  118. public function usersig($user_id) {
  119. // 获取配置信息
  120. $config = config("tencent_im");
  121. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  122. $usersig = $usersigObj->genUserSig($user_id);
  123. return $usersig;
  124. }
  125. }