chatwo.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/7/7
  6. * Time: 下午11:29
  7. */
  8. namespace room;
  9. use Exception;
  10. use Log;
  11. use member_info;
  12. class chatwo
  13. {
  14. protected $mUserInfos;
  15. private $mod_room;
  16. private $mMapChat;
  17. public function __construct()
  18. {
  19. $this->mUserInfos = [];
  20. $this->mMapChat = [];
  21. $this->mod_room = Model('room');
  22. }
  23. public function messageOp($input)
  24. {
  25. $from = intval($input['from']);
  26. $to = intval($input['to']);
  27. $seq = $input['seq'];
  28. if($from <= 0 || $to <= 0) return false;
  29. $finfo = $this->find($from);
  30. $tinfo = $this->find($to);
  31. if($tinfo == false || $tinfo == false) return false;
  32. $type = proto_type::to_msgtype($input['type']);
  33. $content = $this->validate_content($input['content']);
  34. if($type == false || $content == false) return false;
  35. $msgid = $this->record_message($from,$to,$type,$content);
  36. $msg = ['from' => $finfo,'content' => $content, 'type' => $input['type'],'send_time' => time(),'msgid' => $msgid,'seq' => $seq];
  37. return $this->format_msg($from,$to,"message",$msg);
  38. }
  39. private function chatid($from,$to)
  40. {
  41. if($from > $to) {
  42. $hash = "{$to}_{$from}";
  43. } else {
  44. $hash = "{$from}_{$to}";
  45. }
  46. if(array_key_exists($hash,$this->mMapChat)) {
  47. return $this->mMapChat[$hash];
  48. }
  49. else
  50. {
  51. $chat_id = $this->mod_room->findChatid($from,$to);
  52. if($chat_id === false) {
  53. $chat_id = $this->mod_room->addChatwo($from,$to);
  54. }
  55. if($chat_id != false) {
  56. $this->mMapChat[$hash] = $chat_id;
  57. }
  58. return $chat_id;
  59. }
  60. }
  61. private function format_msg($from,$to,$op,$content)
  62. {
  63. $msg = [];
  64. $msg['msgtype'] = proto_type::msgtype_message;
  65. $msg['act'] = "room";
  66. $msg['op'] = "relay";
  67. $msg['relay_type'] = "users";
  68. if($from == $to) {
  69. $msg['receivers'] = [$to];
  70. } else {
  71. $msg['receivers'] = [$from,$to];
  72. }
  73. $msg['room'] = 0;
  74. $msg['receiver_type'] = proto_type::sroom_chatwo;
  75. $msg['body']['act'] = proto_type::act_chatwo;
  76. $msg['body']['op'] = $op;
  77. $msg['body']['content'] = $content;
  78. $msg['body']['msgtype'] = proto_type::msgtype_message;
  79. return $msg;
  80. }
  81. protected function find($userid)
  82. {
  83. try
  84. {
  85. if(array_key_exists($userid,$this->mUserInfos)) {
  86. return $this->mUserInfos[$userid];
  87. }
  88. else {
  89. $item = Model('member')->getMemberInfoByID($userid);
  90. $uinfo = new member_info($item);
  91. $result = ['nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => intval($userid)];
  92. $this->mUserInfos[$userid] = $result;
  93. return $result;
  94. }
  95. }
  96. catch (Exception $ex)
  97. {
  98. Log::record($ex->getMessage(),Log::ERR);
  99. return false;
  100. }
  101. }
  102. protected function validate_content($content)
  103. {
  104. return $content;
  105. }
  106. protected function record_message($from,$to,$type,$content)
  107. {
  108. $chat_id = $this->chatid($from,$to);
  109. if($chat_id != false) {
  110. return $this->mod_room->addRoomMsg(['room_id' => $chat_id,'member_id' => $from, 'type' => $type,'msg' => $content,'add_time' => time(),'msg_type' => 1]);
  111. }
  112. else {
  113. return false;
  114. }
  115. }
  116. }