TencentImMessage.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Services;
  3. use App\Models\User;
  4. use App\Traits\TencentIm;
  5. use PHPUnit\Util\Exception;
  6. class TencentImMessage
  7. {
  8. use TencentIm;
  9. //查询单聊消息
  10. public function getroammsg($from,$to){
  11. if(empty($from)){
  12. throw new Exception("参数错误");
  13. }
  14. if(empty($to)){
  15. throw new Exception("参数错误");
  16. }
  17. $from = User::query()->find($from,['tencent_im_user_id']);
  18. $to = User::query()->find($to,['tencent_im_user_id']);
  19. $this->restApiName = "v4/openim/admin_getroammsg";
  20. $baseApiHost = $this->getTencentImRestApiBaseHost();
  21. $params = [
  22. 'From_Account' => $from->tencent_im_user_id,
  23. 'To_Account' => $to->tencent_im_user_id,
  24. 'MaxCnt' => 999,
  25. 'MinTime' => 1625068800,
  26. 'MaxTime' => 1751299200,
  27. ];
  28. $apiResult = $this->requestApi($baseApiHost, $params);
  29. self::verifyApiResult($apiResult);
  30. return $apiResult;
  31. }
  32. //单发单聊消息
  33. public function send_msg($data){
  34. $this->restApiName = "v4/openim/sendmsg";
  35. $baseApiHost = $this->getTencentImRestApiBaseHost();
  36. $content = array();
  37. $msg['MsgType'] = 'TIMTextElem';
  38. $msg['MsgContent']['Text'] = $data['msg'];
  39. $content[]=$msg;
  40. $params = [
  41. 'From_Account' => $data['from_user'],
  42. 'To_Account' => $data['to_user'],
  43. 'MsgRandom' => rand(100000,999999),
  44. "MsgBody"=> $content,
  45. ];
  46. $apiResult = $this->requestApi($baseApiHost, $params);
  47. self::verifyApiResult($apiResult);
  48. return $apiResult;
  49. }
  50. }