TencentIm.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. namespace app\utils\Service\Tencent;
  3. use app\utils\CurlUtil;
  4. class TencentIm extends Module
  5. {
  6. private $config;
  7. private $host = "https://console.tim.qq.com";
  8. public function __construct()
  9. {
  10. // 获取配置信息
  11. $this->config = [
  12. 'appid' => config('tencent_im.sdkappid'),
  13. 'identifier' => config('tencent_im.identifier'),
  14. 'key' => config('tencent_im.key'),
  15. ];
  16. }
  17. /**
  18. * 注册im
  19. * @param string $userid
  20. * @param string $nickname
  21. * @param string $avatar
  22. * @return bool
  23. */
  24. public function register(string $userid, string $nickname, string $avatar = '')
  25. {
  26. $data = [
  27. 'UserID' => $userid,
  28. 'Nick' => $nickname,
  29. 'FaceUrl' => $avatar
  30. ];
  31. $res = $this->postJson('/v4/im_open_login_svc/account_import', $data);
  32. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  33. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  34. }
  35. return $this->success('操作成功', $res);
  36. }
  37. /**
  38. * 设置用户资料
  39. * @param string $userid
  40. * @param array $params
  41. * @return bool
  42. */
  43. public function set_info(string $userid,array $params = [])
  44. {
  45. $data = [];
  46. foreach ($params as $key => $value) {
  47. $data[] = [
  48. 'Tag' => $key,
  49. 'Value' => $value,
  50. ];
  51. }
  52. $data = [
  53. 'From_Account' => $userid,
  54. 'ProfileItem' => $data
  55. ];
  56. $res = $this->postJson('/v4/profile/portrait_set', $data);
  57. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  58. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  59. }
  60. return $this->success('操作成功', $res);
  61. }
  62. /**
  63. * 发送消息给某人
  64. * @param $from_user
  65. * @param $to_user
  66. * @param $message
  67. * @param $type //0普通消息 1自定义消息
  68. * @return bool
  69. */
  70. public function sendMessageToUser($from_user, $to_user, $message, $type = 0)
  71. {
  72. $data = [
  73. 'SyncOtherMachine' => 1,
  74. 'From_Account' => (string)$from_user,
  75. 'To_Account' => (string)$to_user,
  76. 'MsgRandom' => rand(1000000, 9999999),
  77. 'MsgTimeStamp' => time(),
  78. ];
  79. if ($type == 1) { //自定义消息
  80. $body_message = '{"businessID":"custom_tips_message","receiverShow":true,"senderId":"' . $from_user . '","text":"' . $message . '","tipType":257,"version":0}';
  81. $data["MsgBody"][] = [
  82. "MsgType" => "TIMCustomElem",
  83. "MsgContent" => [
  84. "Data" => $body_message,
  85. "Desc" => $body_message,
  86. "Ext" => 'custom_tips_message',
  87. "Sound" => ''
  88. ],
  89. ];
  90. } elseif ($type == 2) {
  91. // 完全自定义
  92. $data['MsgBody'] = $message['MsgBody'];
  93. $data['CloudCustomData'] = $message['CloudCustomData'];
  94. } elseif ($type == 3) {
  95. // 客服自定义消息
  96. $data["MsgBody"][] = [
  97. "MsgType" => "TIMCustomElem",
  98. "MsgContent" => [
  99. "Data" => json_encode($message,JSON_UNESCAPED_UNICODE),
  100. "Desc" => json_encode($message,JSON_UNESCAPED_UNICODE),
  101. "Ext" => 'custom_tips_message',
  102. "Sound" => ''
  103. ],
  104. ];
  105. } else {
  106. $data["MsgBody"][] = [
  107. "MsgType" => "TIMTextElem",
  108. "MsgContent" => [
  109. "Text" => $message
  110. ],
  111. ];
  112. }
  113. $res = $this->postJson('/v4/openim/sendmsg', $data);
  114. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  115. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  116. }
  117. return $this->success('操作成功', $res);
  118. }
  119. /**
  120. * 批量发送消息
  121. * @param $from_user
  122. * @param array $to_user
  123. * @param $message
  124. * @param $type //0普通消息 1自定义消息
  125. * @return bool
  126. */
  127. public function sendMessageToUsers($from_user, array $to_user, $message, $type = 0)
  128. {
  129. $data = [
  130. 'SyncOtherMachine' => 2,
  131. 'From_Account' => (string)$from_user,
  132. 'To_Account' => $to_user,
  133. 'MsgRandom' => rand(1000000, 9999999),
  134. 'MsgTimeStamp' => time(),
  135. ];
  136. if ($type == 1) { //自定义消息
  137. $body_message = '{"businessID":"custom_tips_message","receiverShow":true,"senderId":"' . $from_user . '","text":"' . $message . '","tipType":257,"version":0}';
  138. $data["MsgBody"][] = [
  139. "MsgType" => "TIMCustomElem",
  140. "MsgContent" => [
  141. "Data" => $body_message,
  142. "Desc" => $body_message,
  143. "Ext" => 'custom_tips_message',
  144. "Sound" => ''
  145. ],
  146. ];
  147. } elseif ($type == 2) {
  148. // 完全自定义
  149. $data['MsgBody'] = $message['MsgBody'];
  150. $data['CloudCustomData'] = $message['CloudCustomData'];
  151. } else {
  152. $data["MsgBody"][] = [
  153. "MsgType" => "TIMTextElem",
  154. "MsgContent" => [
  155. "Text" => $message
  156. ],
  157. ];
  158. }
  159. $res = $this->postJson('/v4/openim/sendmsg', $data);
  160. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  161. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  162. }
  163. return $this->success('操作成功', $res);
  164. }
  165. /**
  166. * 创建群组
  167. * @param $user_id
  168. * @param $room_no
  169. * @param $room_name
  170. * @param $type
  171. * @param $params // 其他参数
  172. * @return bool
  173. */
  174. public function create_group($user_id, $room_no, $room_name, $type = 'AVChatRoom', $params = [])
  175. {
  176. $this->config['identifier'] = (string)$user_id;// 群主 ID
  177. $data = [
  178. 'Owner_Account' => (string)$user_id, // 群主 ID
  179. 'Type' => $type, // 群组形态,包括 Public(陌生人社交群),Private(即 Work,好友工作群),ChatRoom(即 Meeting,会议群),AVChatRoom(直播群),Community(社群)
  180. 'GroupId' => (string)$room_no, // 自定义群组 ID
  181. 'Name' => $room_name,
  182. // 申请加群处理方式。包含 FreeAccess(自由加入),NeedPermission(需要验证),DisableApply(禁止加群),如果不填,具体默认项可参见 加群方式差异;
  183. // 仅当创建支持申请加群的 群组 时,该字段有效。
  184. // 社群目前不支持此字段。
  185. //'ApplyJoinOption' => 'NeedPermission',// 申请加群处理方式。
  186. // 邀请加群处理方式,包含 FreeAccess (直接邀请用户进群,不需要审批等操作), NeedPermission 需要群管理员或者群主审批, DisableInvite 不支持 SDK 邀请进群, 该选项 AVChatRoom 群类型不支持
  187. //'InviteJoinOption' => 'NeedPermission',// 申请加群处理方式。
  188. ];
  189. $res = $this->postJson('/v4/group_open_http_svc/create_group', array_merge($data, $params));
  190. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  191. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  192. }
  193. return $this->success('创建成功', $res);
  194. }
  195. // 解散群组
  196. public function close_group($room_no)
  197. {
  198. $data = [
  199. 'GroupId' => (string)$room_no, // 自定义群组 ID
  200. ];
  201. $res = $this->postJson('/v4/group_open_http_svc/destroy_group', $data);
  202. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  203. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  204. }
  205. return $this->success('解散成功', $res);
  206. }
  207. /**
  208. * 创建群组
  209. * @param $user_id
  210. * @param $room_no
  211. * @param $room_name
  212. * @param $type
  213. * @param $params // 其他参数
  214. * @return bool
  215. */
  216. public function modify_group_base_info($room_no, $room_name, $face_url = '', $params = [])
  217. {
  218. $data = [
  219. 'GroupId' => (string)$room_no, // 自定义群组 ID
  220. 'Name' => $room_name,
  221. 'FaceUrl' => $face_url,
  222. ];
  223. $res = $this->postJson('/v4/group_open_http_svc/modify_group_base_info', array_merge($data, $params));
  224. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  225. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  226. }
  227. return $this->success('创建成功', $res);
  228. }
  229. // 发送群系统消息
  230. public function send_group_system_message($room_no,$content)
  231. {
  232. $data = [
  233. 'GroupId' => (string)$room_no, // 自定义群组 ID
  234. 'Content' => $content, // 消息内容
  235. ];
  236. $res = $this->postJson('/v4/group_open_http_svc/send_group_system_notification', $data);
  237. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  238. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  239. }
  240. return $this->success('创建成功', $res);
  241. }
  242. // 发送群普通消息
  243. public function send_group_message($room_no,$content)
  244. {
  245. $data = [
  246. 'GroupId' => (string)$room_no, // 自定义群组 ID
  247. 'Content' => $content, // 消息内容
  248. ];
  249. $res = $this->postJson('/v4/group_open_http_svc/send_group_system_notification', $data);
  250. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  251. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  252. }
  253. return $this->success('创建成功', $res);
  254. }
  255. /**
  256. * 转让群主
  257. * @param $group_chat_id
  258. * @param $chat_id
  259. * @return bool
  260. */
  261. public function change_owner($group_chat_id, $chat_id)
  262. {
  263. $data = [
  264. 'GroupId' => (string)$group_chat_id, // 要被转移的群 ID(必填)
  265. 'NewOwner_Account' => (string)$chat_id, // 新群主 ID(必填)
  266. ];
  267. $res = $this->postJson('/v4/group_open_http_svc/change_group_owner', $data);
  268. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  269. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  270. }
  271. return $this->success('操作成功', $res);
  272. }
  273. /**
  274. * 增加群成员
  275. * @param $group_chat_id
  276. * @param array $chat_ids
  277. * @return bool
  278. */
  279. public function add_group_member($group_chat_id, array $chat_ids)
  280. {
  281. $MemberList = [];
  282. foreach ($chat_ids as $value) {
  283. $MemberList[] = [
  284. 'Member_Account' => $value,// 要添加的群成员ID(必填)
  285. ];
  286. }
  287. $data = [
  288. 'GroupId' => (string)$group_chat_id, // 要被转移的群 ID(必填)
  289. 'MemberList' => $MemberList, // 一次最多添加300个成员
  290. ];
  291. $res = $this->postJson('/v4/group_open_http_svc/add_group_member', $data);
  292. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  293. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  294. }
  295. return $this->success('操作成功', $res);
  296. }
  297. /**
  298. * 解散群组
  299. * @param $room_no
  300. * @return bool
  301. */
  302. public function destroy_group($room_no)
  303. {
  304. $data = [
  305. 'GroupId' => (string)$room_no
  306. ];
  307. $res = $this->postJson('/v4/group_open_http_svc/destroy_group', $data);
  308. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  309. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  310. }
  311. return $this->success('获取成功', $res);
  312. }
  313. /**
  314. * 在群组中发送系统通知
  315. * @param $room_no
  316. * @param $type // 雷松:10=点赞通知 20=pk通知
  317. * @param $content
  318. * @return bool
  319. */
  320. public function send_group_system_notification($room_no, $type, $content)
  321. {
  322. $data = [
  323. 'GroupId' => (string)$room_no,
  324. 'Content' => json_encode([
  325. 'type' => $type,
  326. 'content' => $content,
  327. ]),
  328. ];
  329. $res = $this->postJson('/v4/group_open_http_svc/send_group_system_notification', $data);
  330. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  331. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  332. }
  333. return $this->success('获取成功', $res);
  334. }
  335. /**
  336. * 封禁用户
  337. * @param $room_no
  338. * @param $account
  339. * @param $time
  340. * @param $remark
  341. * @return bool
  342. */
  343. public function ban_group_member($room_no, $account, $time, $remark = '违规操作')
  344. {
  345. $data = [
  346. 'GroupId' => (string)$room_no,
  347. 'Members_Account' => [$account],
  348. 'Duration' => $time, // 封禁时长,单位:秒
  349. 'Description' => $remark// 封禁信息
  350. ];
  351. $res = $this->postJson('/v4/group_open_http_svc/ban_group_member', $data);
  352. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  353. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  354. }
  355. return $this->success('获取成功', $res);
  356. }
  357. /**
  358. * 解封用户
  359. * @param $room_no
  360. * @param $account
  361. * @return bool
  362. */
  363. public function unban_group_member($room_no, $account)
  364. {
  365. $data = [
  366. 'GroupId' => (string)$room_no,
  367. 'Members_Account' => [$account]
  368. ];
  369. $res = $this->postJson('/v4/group_open_http_svc/unban_group_member', $data);
  370. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  371. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  372. }
  373. return $this->success('获取成功', $res);
  374. }
  375. // 加入黑名单
  376. public function black_add($from_user, array $to_users = [])
  377. {
  378. $data = [
  379. 'From_Account' => (string)$from_user,
  380. 'To_Account' => $to_users
  381. ];
  382. $res = $this->postJson('/v4/sns/black_list_add', $data);
  383. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  384. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  385. }
  386. return $this->success('获取成功', $res);
  387. }
  388. // 加入黑名单
  389. public function black_del($from_user, array $to_users = [])
  390. {
  391. $data = [
  392. 'From_Account' => (string)$from_user,
  393. 'To_Account' => $to_users
  394. ];
  395. $res = $this->postJson('/v4/sns/black_list_delete', $data);
  396. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  397. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  398. }
  399. return $this->success('获取成功', $res);
  400. }
  401. /**
  402. * 移除房间
  403. * @param $room_no
  404. * @param $account
  405. * @return bool
  406. */
  407. public function delete_group_member($room_no, array $accounts)
  408. {
  409. $data = [
  410. 'GroupId' => (string)$room_no,
  411. 'MemberToDel_Account' => $accounts
  412. ];
  413. $res = $this->postJson('/v4/group_open_http_svc/delete_group_member', $data);
  414. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  415. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  416. }
  417. return $this->success('获取成功', $res);
  418. }
  419. /**
  420. * 禁言 or 解除
  421. * @param $room_no
  422. * @param $account
  423. * @param $time // time = 0 解除
  424. * @return bool
  425. */
  426. public function forbid_send_msg($room_no, $account, $time = 0)
  427. {
  428. $data = [
  429. 'GroupId' => (string)$room_no,
  430. 'Members_Account' => [$account],
  431. 'MuteTime' => $time
  432. ];
  433. $res = $this->postJson('/v4/group_open_http_svc/forbid_send_msg', $data);
  434. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  435. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  436. }
  437. return $this->success('获取成功', $res);
  438. }
  439. /**
  440. * 获取全部群组
  441. * @return bool
  442. */
  443. public function get_appid_group_list()
  444. {
  445. $data = [
  446. 'Limit' => 1000,// 群主 ID
  447. 'Next' => 0
  448. ];
  449. $res = $this->postJson('/v4/group_open_http_svc/get_appid_group_list', $data);
  450. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  451. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  452. }
  453. return $this->success('获取成功', $res);
  454. }
  455. /**
  456. * 获取直播推流地址
  457. * @param string $domain 您用来推流的域名
  458. * @param string $streamName 您用来区别不同推流地址的唯一流名称
  459. * @param $key 安全密钥
  460. * @param $time 过期时间 sample 2016-11-12 12:00:00
  461. * @return string
  462. */
  463. public function getLivePushUrl(string $domain,string $streamName,$key = null, $time = null)
  464. {
  465. if($key && $time){
  466. $txTime = strtoupper(base_convert(strtotime($time),10,16));
  467. //txSecret = MD5( KEY + streamName + txTime )
  468. $txSecret = md5($key.$streamName.$txTime);
  469. $ext_str = "?".http_build_query(["txSecret"=> $txSecret, "txTime"=> $txTime]);
  470. }
  471. return "rtmp://".$domain."/live/".$streamName . ($ext_str ?? "");
  472. }
  473. /**
  474. * 获取直播播放地址
  475. * @param string $domain 您用来推流的域名
  476. * @param string $streamName 您用来区别不同推流地址的唯一流名称
  477. * @return string
  478. */
  479. public function getLivePlayUrl(string $domain,string $streamName)
  480. {
  481. return "rtmp://".$domain."/live/".$streamName;
  482. }
  483. /**
  484. * 设置群属性
  485. * @param int $room_no
  486. * @param array $attr
  487. * @return bool
  488. */
  489. public function modify_group_attr( $room_no, array $attr)
  490. {
  491. $data = [
  492. 'GroupId' => (string)$room_no,// 群组 ID
  493. 'GroupAttr' => $attr
  494. ];
  495. $res = $this->postJson('/v4/group_open_http_svc/modify_group_attr', $data);
  496. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  497. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  498. }
  499. return $this->success('获取成功', $res);
  500. }
  501. /**
  502. * 获取群属性
  503. * @param int $room_no
  504. */
  505. public function get_group_attr( $room_no)
  506. {
  507. $data = [
  508. 'GroupId' => (string)$room_no,// 群组 ID
  509. ];
  510. $res = $this->postJson('/v4/group_open_attr_http_svc/get_group_attr', $data);
  511. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  512. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  513. }
  514. return $this->success('获取成功', $res);
  515. }
  516. /**
  517. * 删除群属性
  518. * @param int $room_no
  519. */
  520. public function delete_group_attr( $room_no,$key)
  521. {
  522. $data = [
  523. 'GroupId' => (string)$room_no,// 群组 ID
  524. 'GroupAttr' => [[
  525. 'key' => $key,//属性 key
  526. ]],
  527. ];
  528. $res = $this->postJson('/v4/group_open_http_svc/delete_group_attr', $data);
  529. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  530. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  531. }
  532. return $this->success('获取成功', $res);
  533. }
  534. // 下载最近消息记录
  535. public function get_history($MsgTime,$ChatType = 1)
  536. {
  537. $data = [
  538. 'ChatType' => $ChatType == 1 ? 'Group' : 'C2C',// 消息类型,C2C 表示单发消息 Group 表示群组消息
  539. 'MsgTime' => $MsgTime
  540. ];
  541. $res = $this->postJson('/v4/open_msg_svc/get_history', $data);
  542. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  543. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  544. }
  545. return $this->success('获取成功', $res);
  546. }
  547. //下载远程文件 到指定目录
  548. public function downloadFile($file_url, $path = '', $save_file_name = '')
  549. {
  550. $base_path = "uploads/im_file";
  551. if ($path) {
  552. $base_path = "{$base_path}/$path";
  553. }
  554. $dir_path = ROOT_PATH . "/public/{$base_path}/" . date('Ymd');
  555. if (!is_dir($dir_path)) {
  556. mkdir($dir_path, 0777, true);
  557. }
  558. $file = file_get_contents($file_url);
  559. //传入保存文件的名称
  560. $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
  561. $resource = fopen($dir_path. '/'. $filename, 'w');
  562. fwrite($resource, $file);
  563. fclose($resource);
  564. return $dir_path . '/' . $filename;
  565. }
  566. //解压
  567. public function jieyagz($gz_path = ''){
  568. $json_path = substr($gz_path,0,-3);
  569. if ($zp = gzopen($gz_path, 'r')) { // 打开压缩文件
  570. if ($fp = fopen($json_path, 'w')) { // 打开目标文件
  571. while (!gzeof($zp)) {
  572. fwrite($fp, gzread($zp, 1024 * 512)); // 逐块读取和解压缩后写入
  573. }
  574. fclose($fp);
  575. }
  576. gzclose($zp);
  577. }
  578. return $json_path;
  579. }
  580. //读取json并分析,group
  581. public function readjson_group($json_path = ''){
  582. $json_content = file_get_contents($json_path);
  583. $json_content = json_decode($json_content,true);
  584. $newMsgList = [];
  585. if(isset($json_content['MsgList']) && !empty($json_content['MsgList'])){
  586. $MsgList = $json_content['MsgList'];
  587. foreach($MsgList as $key => $val)
  588. {
  589. $MsgBody = isset($val['MsgBody']) ? json_encode($val['MsgBody'],JSON_UNESCAPED_UNICODE) : '';
  590. $newMsgList[] = [
  591. 'ClientIP' => $val['ClientIP'] ?? '',
  592. 'From_Account' => $val['From_Account'] ?? '',
  593. 'GroupId' => $val['GroupId'] ?? '',
  594. 'MsgBody' => $MsgBody,
  595. 'MsgFromPlatform' => $val['MsgFromPlatform'] ?? '',
  596. 'MsgSeq' => $val['MsgSeq'] ?? '',
  597. 'MsgTimestamp' => $val['MsgTimestamp'] ?? '',
  598. ];
  599. }
  600. }
  601. return $newMsgList;
  602. }
  603. //读取json并分析,group
  604. public function readjson_user($json_path = ''){
  605. $json_content = file_get_contents($json_path);
  606. $json_content = json_decode($json_content,true);
  607. $newMsgList = [];
  608. if(isset($json_content['MsgList']) && !empty($json_content['MsgList'])){
  609. $MsgList = $json_content['MsgList'];
  610. foreach($MsgList as $key => $val)
  611. {
  612. $MsgBody = isset($val['MsgBody']) ? json_encode($val['MsgBody'],JSON_UNESCAPED_UNICODE) : '';
  613. $newMsgList[] = [
  614. 'ClientIP' => $val['ClientIP'] ?? '',
  615. 'From_Account' => $val['From_Account'] ?? '',
  616. 'To_Account' => $val['To_Account'] ?? '',
  617. 'MsgBody' => $MsgBody,
  618. 'MsgFromPlatform' => $val['MsgFromPlatform'] ?? '',
  619. 'MsgRandom' => $val['MsgRandom'] ?? '',
  620. 'MsgSeq' => $val['MsgSeq'] ?? '',
  621. 'MsgTimestamp' => $val['MsgTimestamp'] ?? '',
  622. ];
  623. }
  624. }
  625. return $newMsgList;
  626. }
  627. public function get_usersig($user_id){
  628. return $this->usersig($user_id);
  629. }
  630. private function postJson(string $uri, array $params = [], array $header = [])
  631. {
  632. $random = rand(10000000, 99999999);
  633. $userSig = $this->usersig($this->config['identifier']);
  634. return CurlUtil::postJson($this->host . $uri . "?sdkappid={$this->config['appid']}&identifier={$this->config['identifier']}&usersig={$userSig}&random={$random}&contenttype=json", $params, $header);
  635. }
  636. /**
  637. * 获取usersig签名-具体操作
  638. */
  639. private function userSig($user_id)
  640. {
  641. // 获取配置信息
  642. $userSigObj = new GetUserSig($this->config["appid"], $this->config["key"]);
  643. return $userSigObj->genUserSig($user_id);
  644. }
  645. }