ImUtil.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. namespace App\Utils;
  3. use GuzzleHttp\Client;
  4. use Ixudra\Curl\Facades\Curl;
  5. use Tencent\TLSSigAPI;
  6. class ImUtil
  7. {
  8. /** @var string $url */
  9. protected $url = 'https://console.tim.qq.com';
  10. /** @var string $version */
  11. protected $version = 'v4';
  12. /** @var string $usersig */
  13. protected $usersig = 'eJxlj1FPgzAYRd-5FYRXjftoaZ2*sUXjpChEnIMXUqFjFaFYus1p-O9GXCKJ9-Wc3Jv7adm27STs4YwXhdq2JjeHTjj2pe2Ac-oHu06WOTc51uU-KN47qUXO10boAbqEEAQwdmQpWiPX8mjwspGt7I3mRumR1pd1Pmz99ngAiGLqjpt6WQ0wvErni3ieIAgDvA9u2AyWKppw3Wf7pygWEZt0dSCqa5XGsA3Tqb-Y*HfVZsU*3hTzm8NJMrt1i2nDXnfZ42p5n70QEsTnFD8LDV49mjSyEcdj9IJ6mCI0ojuhe6naQUDgEhdh*IljfVnf3wtf6Q__';
  14. /** @var string $identifier */
  15. protected $identifier = 'administrator';
  16. /** @var string $sdkAppId */
  17. protected $sdkAppId = '1400263610';
  18. /** @var string $contentType */
  19. protected $contentType = 'json';
  20. /** @var Client $httpClient */
  21. protected $httpClient;
  22. public function __construct()
  23. {
  24. $this->httpClient = new Client(['verify' => false]);
  25. }
  26. /**
  27. * @param string $identifier
  28. * @return string
  29. * @throws \Exception
  30. */
  31. protected function generateSign($identifier)
  32. {
  33. $tencentIM = new TLSSigAPI();
  34. $tencentIM->SetAppid($this->sdkAppId);
  35. $private = file_get_contents(resource_path('certs/tencent_im_private'));
  36. $tencentIM->SetPrivateKey($private);
  37. return $tencentIM->genSig($identifier, 24 * 3600 * 365 * 10000);
  38. }
  39. protected function getUrl($type)
  40. {
  41. $url = $this->url . '/' . $this->version;
  42. switch ($type) {
  43. case 'sendMessage':
  44. $url .= '/openim/sendmsg';
  45. break;
  46. case 'register':
  47. $url .= '/im_open_login_svc/account_import';
  48. break;
  49. case 'update':
  50. $url .= '/profile/portrait_set';
  51. break;
  52. case 'fetchUser':
  53. $url .= '/profile/portrait_get';
  54. break;
  55. case 'download-messages':
  56. $url .= '/open_msg_svc/get_history';
  57. break;
  58. case 'dirty-word-add':
  59. $url .= '/openim_dirty_words/add';
  60. break;
  61. case 'get-dirty-words':
  62. $url .= '/openim_dirty_words/get';
  63. break;
  64. case 'delete-dirty-words':
  65. $url .= '/openim_dirty_words/delete';
  66. break;
  67. default:
  68. break;
  69. }
  70. return $url . "?" . http_build_query([
  71. 'usersig' => $this->usersig,
  72. 'identifier' => $this->identifier,
  73. 'sdkAppid' => $this->sdkAppId,
  74. 'random' => rand(10000, 99999),
  75. 'contenttype' => $this->contentType,
  76. ]);
  77. }
  78. public function getDirtyWords()
  79. {
  80. $url = $this->getUrl('get-dirty-words');
  81. $res = $this->httpClient->request('get', $url);
  82. return json_decode($res->getBody(), true);
  83. }
  84. public function dirtyWordAdd(array $words)
  85. {
  86. $url = $this->getUrl('dirty-word-add');
  87. $res = $this->httpClient->request("POST", $url, [
  88. 'json' => [
  89. 'DirtyWordsList' => $words,
  90. ]
  91. ]);
  92. return json_decode($res->getBody(), true);
  93. }
  94. public function deleteDirtyWords(array $words)
  95. {
  96. $url = $this->getUrl('delete-dirty-words');
  97. $res = $this->httpClient->request("POST", $url, [
  98. 'json' => [
  99. 'DirtyWordsList' => $words,
  100. ]
  101. ]);
  102. return json_decode($res->getBody(), true);
  103. }
  104. /**
  105. * 注册用户/重新获取sig
  106. * @param string $imAccount 自己生成的IM_Account
  107. * @param string $name 用户名字
  108. * @param string $avatar 头像
  109. * @param int $gender
  110. * @param int $type
  111. * @return string
  112. * @throws \GuzzleHttp\Exception\GuzzleException
  113. * @throws \Exception
  114. */
  115. public function registerUser($imAccount, $name, $avatar, $gender = 1, $type = 0)
  116. {
  117. $sign = $this->generateSign($imAccount);
  118. $url = $this->getUrl('register');
  119. $this->httpClient->request('POST', $url, [
  120. 'json' => [
  121. 'Identifier' => "$imAccount",
  122. 'Nick' => "$name",
  123. 'FaceUrl' => "$avatar",
  124. 'Type' => $type, // 值 0 表示普通帐号,1 表示机器人帐号
  125. ]
  126. ]);
  127. $this->updateUser($imAccount, $name, $avatar, $sign, $gender);
  128. return $sign;
  129. }
  130. /**
  131. * @param $name
  132. * @return mixed|\Psr\Http\Message\ResponseInterface
  133. * @throws \GuzzleHttp\Exception\GuzzleException
  134. */
  135. public function fetchUser($name)
  136. {
  137. $url = $this->getUrl('fetchUser');
  138. $response = $this->httpClient->request('POST', $url, [
  139. 'json' => [
  140. 'To_Account' => [
  141. $name
  142. ],
  143. 'TagList' => [
  144. "Tag_Profile_IM_Nick",
  145. ]
  146. ]
  147. ]);
  148. return $response;
  149. }
  150. /**
  151. * 发送消息
  152. * @param $identifier
  153. * @param $toAccount
  154. * @param $msgBody
  155. * @return \Psr\Http\Message\StreamInterface
  156. * @throws \Exception
  157. */
  158. public function sendMessage($identifier, $toAccount, $msgBody)
  159. {
  160. $sig = $this->generateSign($identifier);
  161. $this->identifier = $identifier;
  162. $this->usersig = $sig;
  163. $url = $this->getUrl('sendMessage');
  164. $response = $this->httpClient->post($url, [
  165. 'json' => [
  166. 'SyncOtherMachine' => 2,
  167. 'From_Account' => $identifier,
  168. 'To_Account' => $toAccount,
  169. 'MsgLifeTime' => 86400 * 7,
  170. 'MsgRandom' => rand(1000000, 9999999),
  171. 'MsgBody' => $msgBody
  172. // "OfflinePushInfo": {
  173. // "PushFlag": 0,
  174. // "Desc": "离线推送内容",
  175. // "Ext": "这是透传的内容",
  176. // "AndroidInfo": {
  177. // "Sound": "android.mp3"
  178. // },
  179. // "ApnsInfo": {
  180. // "Sound": "apns.mp3",
  181. // "BadgeMode": 1, // 这个字段缺省或者为 0 表示需要计数,为 1 表示本条消息不需要计数,即右上角图标数字不增加
  182. // "Title":"apns title", // apns title
  183. // "SubTitle":"apns subtitle", // apns subtitle
  184. // "Image":"www.image.com" // image url
  185. // }
  186. // }
  187. //}
  188. ]
  189. ]);
  190. return $response->getBody();
  191. }
  192. /**
  193. * 修改头像
  194. * @param string $identifier
  195. * @param string $name
  196. * @param string $avatar
  197. * @param int $sex
  198. * @throws \GuzzleHttp\Exception\GuzzleException
  199. * @throws \Exception
  200. */
  201. public function updateAvatar($identifier, $name, $avatar, $sex = 0)
  202. {
  203. $sign = $this->generateSign($identifier);
  204. $url = $this->getUrl('update');
  205. $this->httpClient->request("POST", $url, [
  206. 'json' => [
  207. "From_Account" => "$identifier",
  208. "ProfileItem" => [
  209. [
  210. 'Tag' => 'Tag_Profile_IM_Nick',
  211. 'Value' => "$name",
  212. ],
  213. [
  214. 'Tag' => 'Tag_Profile_IM_Gender',
  215. 'Value' => $this->getGenderString($sex),
  216. ],
  217. [
  218. 'Tag' => 'Tag_Profile_IM_Image',
  219. 'Value' => "$avatar",
  220. ],
  221. [
  222. 'Tag' => 'Tag_Profile_Custom_Password',
  223. 'Value' => "$sign",
  224. ],
  225. ]
  226. ]
  227. ]);
  228. }
  229. public function downloadMessages($msgTime)
  230. {
  231. $url = $this->getUrl('download-messages');
  232. $response = $this->httpClient->request("POST", $url, [
  233. 'json' => [
  234. 'ChatType' => 'C2C',
  235. 'MsgTime' => $msgTime,
  236. ]
  237. ]);
  238. return $response->getBody()->getContents();
  239. }
  240. /**
  241. * 获取性别字符串
  242. * @param int $gender
  243. * @return string
  244. */
  245. protected function getGenderString($gender)
  246. {
  247. switch ($gender) {
  248. case 1:
  249. return "Gender_Type_Male";
  250. case 2:
  251. return "Gender_Type_Female";
  252. default:
  253. return "Gender_Type_Unknown";
  254. }
  255. }
  256. /**
  257. * @param string $identifier
  258. * @param string $name
  259. * @param string $avatar
  260. * @param string $sign
  261. * @param int $gender
  262. * @throws \GuzzleHttp\Exception\GuzzleException
  263. */
  264. public function updateUser($identifier, $name, $avatar, $sign, $gender = 0)
  265. {
  266. $url = $this->getUrl('update');
  267. $this->httpClient->request("POST", $url, [
  268. 'json' => [
  269. "From_Account" => "$identifier",
  270. "ProfileItem" => [
  271. [
  272. 'Tag' => 'Tag_Profile_IM_Nick',
  273. 'Value' => "$name",
  274. ],
  275. [
  276. 'Tag' => 'Tag_Profile_IM_Gender',
  277. 'Value' => $this->getGenderString($gender),
  278. ],
  279. [
  280. 'Tag' => 'Tag_Profile_IM_Image',
  281. 'Value' => "$avatar",
  282. ],
  283. [
  284. 'Tag' => 'Tag_Profile_Custom_Password',
  285. 'Value' => "$sign",
  286. ],
  287. ]
  288. ]
  289. ]);
  290. }
  291. /**
  292. * 重新设置资料
  293. * @param string $identifier
  294. * @param string $name
  295. * @param string $avatar
  296. * @param int $gender
  297. * @throws \GuzzleHttp\Exception\GuzzleException
  298. * @throws \Exception
  299. */
  300. public function updates($identifier, $name, $avatar, $gender = 0)
  301. {
  302. $url = $this->getUrl('update');
  303. $sign = $this->generateSign($identifier);
  304. $this->httpClient->request("POST", $url, [
  305. 'json' => [
  306. "From_Account" => "$identifier",
  307. "ProfileItem" => [
  308. [
  309. 'Tag' => 'Tag_Profile_IM_Nick',
  310. 'Value' => "$name",
  311. ],
  312. [
  313. 'Tag' => 'Tag_Profile_IM_Gender',
  314. 'Value' => $this->getGenderString($gender),
  315. ],
  316. [
  317. 'Tag' => 'Tag_Profile_IM_Image',
  318. 'Value' => "$avatar",
  319. ],
  320. [
  321. 'Tag' => 'Tag_Profile_Custom_Password',
  322. 'Value' => "$sign",
  323. ],
  324. ]
  325. ]
  326. ]);
  327. }
  328. }