TencentIMController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Http\Controllers\Callbacks;
  3. use App\Events\ImSendMsg;
  4. use App\Http\Controllers\Controller;
  5. use App\Managers\IMManager;
  6. use App\Models\User\UserModel;
  7. use App\Services\IM\CustomElem;
  8. use App\Utils\ImUtil;
  9. use Illuminate\Http\Request;
  10. class TencentIMController extends Controller
  11. {
  12. protected $imManager;
  13. public function __construct(IMManager $imManager)
  14. {
  15. $this->imManager = $imManager;
  16. }
  17. /**
  18. * 单聊消息发送后
  19. * @param Request $request
  20. * @return array
  21. */
  22. public function singleMessageSent(Request $request)
  23. {
  24. $data = $request->all();
  25. $errorCode = 0;
  26. switch ($data['CallbackCommand']) {
  27. case 'C2C.CallbackBeforeSendMsg':
  28. $fromAccount = $data['From_Account'];
  29. $toAccount = $data['To_Account'];
  30. if (!$this->imManager->canIm($fromAccount, $toAccount)) {
  31. $errorCode = 1;
  32. }
  33. break;
  34. case 'C2C.CallbackAfterSendMsg':
  35. $fromAccount = $data['From_Account'];
  36. $toAccount = $data['To_Account'];
  37. $msgRandom = $data['MsgRandom'];
  38. $msgTime = $data['MsgTime'];
  39. $msgSeq = $data['MsgSeq'];
  40. $discardedMsgType = array(
  41. 'EventReceiveSystemMsg',
  42. 'EventFriendApply',
  43. 'EventAgreeFriendApply',
  44. 'EventRefuseRefuseFriendApply',
  45. 'EventPairApply',
  46. 'EventAgreePairApply',
  47. 'EventRefuseRefusePairApply',
  48. 'EventBeContact',
  49. 'EventBeFriend'
  50. );
  51. $messageElem = $data['MsgBody'][0];
  52. $msgType = $messageElem['MsgType'];
  53. if ('TIMCustomElem' == $msgType) {
  54. $customMsgType = $messageElem['Data']['MsgType'];
  55. if (in_array($customMsgType, $discardedMsgType)) {
  56. break;
  57. }
  58. }
  59. event(new ImSendMsg($fromAccount, $toAccount, floor($msgTime / 1000), $data));
  60. $this->imManager->createUserLatestMessageByAccount(
  61. $fromAccount,
  62. $fromAccount,
  63. $toAccount,
  64. $msgTime,
  65. $msgRandom,
  66. $msgSeq,
  67. $data,
  68. true
  69. );
  70. $this->imManager->createUserLatestMessageByAccount(
  71. $toAccount,
  72. $fromAccount,
  73. $toAccount,
  74. $msgTime,
  75. $msgRandom,
  76. $msgSeq,
  77. $data
  78. );
  79. break;
  80. }
  81. return [
  82. 'ActionStatus' => 'OK',
  83. 'ErrorInfo' => '',
  84. 'ErrorCode' => $errorCode,
  85. ];
  86. // [2019-09-28 19:28:20] release.INFO: array (
  87. // 'MsgBody' =>
  88. // array (
  89. // 0 =>
  90. // array (
  91. // 'MsgType' => 'TIMSoundElem',
  92. // 'MsgContent' =>
  93. // array (
  94. // 'Size' => 62351,
  95. // 'Second' => 1,
  96. // 'Url' => 'https://1234-5678187359-1253735226.cos.ap-shanghai.myqcloud.com/abc123/c9be9d32c05bfb77b3edafa4312c6c7d',
  97. // 'Download_Flag' => 2,
  98. // ),
  99. // ),
  100. // 1 =>
  101. // array (
  102. // 'MsgType' => 'TIMTextElem',
  103. // 'MsgContent' =>
  104. // array (
  105. // 'Text' => 'xxxx',
  106. // ),
  107. // ),
  108. // 2 =>
  109. // array(
  110. // "MsgType": "TIMCustomElem",
  111. // "MsgContent": {
  112. // "Data": "message",
  113. // "Desc": "notification",
  114. // "Ext": "url",
  115. // "Sound": "dingdong.aiff"
  116. // }
  117. // )
  118. // ),
  119. // 'CallbackCommand' => 'C2C.CallbackAfterSendMsg',
  120. // 'From_Account' => 'test',
  121. // 'To_Account' => 'test1',
  122. // 'MsgRandom' => 9499893,
  123. // 'MsgSeq' => 1547306453,
  124. // 'MsgTime' => 1569670099,
  125. // 'ClientIP' => '183.159.197.167',
  126. // 'OptPlatform' => 'RESTAPI',
  127. // 'SdkAppid' => '1400263610',
  128. // 'contenttype' => 'json',
  129. // )
  130. }
  131. /**
  132. * IM推送系统消息RPC
  133. * @param Request $request
  134. * @throws \Exception
  135. */
  136. public function sendSystemMessage(Request $request)
  137. {
  138. $data = $request->all();
  139. $uid = $data['toUid'];
  140. $type = $data['type'];
  141. switch ($type) {
  142. case 5:
  143. $fromAccount = "sys_notice_xcx";
  144. break;
  145. case 20:
  146. $fromAccount = "sys_notice_app";
  147. break;
  148. case 13:
  149. case 14:
  150. default:
  151. return;
  152. }
  153. $imUtil = new ImUtil();
  154. $sendUser = UserModel::whereUid($uid)->first(['uid', 'im_account']);
  155. if ($sendUser && $sendUser->im_account) {
  156. $message = new CustomElem('EventReceiveSystemMsg');
  157. $imUtil->sendMessage($fromAccount, $sendUser->im_account, $message->toArray());
  158. }
  159. }
  160. }