base_room.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 下午5:47
  7. */
  8. namespace room;
  9. use member_info;
  10. use Log;
  11. abstract class base_room extends base_info
  12. {
  13. protected $mRoomid;
  14. protected $mRoomkeys; //每个人都有一个唯一的roomkey
  15. protected $mRoomType;
  16. protected $mod_room;
  17. protected $mAccReq;
  18. protected $mCurRespMsgs;
  19. public function __construct($cinfos, $roomkeys = [])
  20. {
  21. parent::__construct($cinfos);
  22. $this->mAccReq = false;
  23. $this->mRoomid = $this->room_id();
  24. $this->mCurRespMsgs['return'] = [];
  25. $this->mCurRespMsgs['broadcast'] = [];
  26. $this->mRoomkeys = $roomkeys;
  27. $this->mod_room = Model('room');
  28. }
  29. public function inviteOp($input)
  30. {
  31. $this->clear();
  32. $this->mAccReq = false;
  33. $userid = intval($input['user']);
  34. if($userid > 0) {
  35. $room_key = $this->invite($userid);
  36. $ret = ['room_key' => $room_key,'room' => $this->mRoomid];
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. public function invite($userid)
  43. {
  44. $item = Model('member')->getMemberInfo(['member_id' => $userid]);
  45. if(empty($item)) return false;
  46. $uinfo = new member_info($item);
  47. $participant = $this->room_key($uinfo->unionid());
  48. $ret = $this->find($participant);
  49. if($ret == false)
  50. {
  51. $ret = $this->mod_room->invite($this->room_id(),$userid,$participant);
  52. if($ret == false) {
  53. Log::record(__METHOD__ . " invite error",Log::ERR);
  54. return false;
  55. }
  56. else {
  57. $this->mRoomkeys[$participant] = ['active' => false,
  58. 'nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => intval($userid)];
  59. }
  60. }
  61. return $participant;
  62. }
  63. public function reject($roomkey)
  64. {
  65. $ret = $this->find($roomkey);
  66. if($ret != false) {
  67. unset($this->mRoomkeys[$roomkey]);
  68. }
  69. return true;
  70. }
  71. protected function room_info($roomkey)
  72. {
  73. $result = [];
  74. $result['room'] = $this->mRoomid;
  75. $result['users'] = [];
  76. foreach ($this->mRoomkeys as $key => $item)
  77. {
  78. $result['users'][] = $item;
  79. if($key == $roomkey) {
  80. $result['me'] = $item['userid'];
  81. }
  82. }
  83. return $result;
  84. }
  85. public function joinOp($input)
  86. {
  87. $this->clear();
  88. $room_key = $input['room_key'];
  89. if(empty($room_key)) {
  90. return false;
  91. }
  92. $this->mAccReq = true;
  93. if($this->join($room_key))
  94. {
  95. $this->add_return([$room_key],'ret_join',$this->room_info($room_key));
  96. $this->add_broad('join',$this->mRoomkeys[$room_key]);
  97. return true;
  98. }
  99. else {
  100. return false;
  101. }
  102. }
  103. public function leaveOp($input)
  104. {
  105. $this->clear();
  106. $room_key = $input['room_key'];
  107. $userinfo = $this->find($room_key);
  108. if($userinfo == false) return false;
  109. $this->mAccReq = true;
  110. if($this->leave($room_key)) {
  111. $this->add_return([$room_key],'ret_leave',$userinfo);
  112. $this->add_broad('leave',$userinfo);
  113. return true;
  114. }
  115. else {
  116. return false;
  117. }
  118. }
  119. public function messageOp($input)
  120. {
  121. $this->clear();
  122. $room_key = $input['room_key'];
  123. $userinfo = $this->find($room_key);
  124. if($userinfo == false) return false;
  125. $type = $input['type'];
  126. $content = $input['content'];
  127. if(empty($content)) return false;
  128. $this->add_broad('message',['from' => $userinfo,'type' => $type,'content' => $content]);
  129. return true;
  130. }
  131. public function return_msgs()
  132. {
  133. return $this->mCurRespMsgs['return'];
  134. }
  135. public function broadcast_msgs()
  136. {
  137. return $this->mCurRespMsgs['broadcast'];
  138. }
  139. protected function clear()
  140. {
  141. $this->mCurRespMsgs['return'] = [];
  142. $this->mCurRespMsgs['broadcast'] = [];
  143. }
  144. protected function add_return(array $roomkeys,$op,$content)
  145. {
  146. $msg = [];
  147. $msg['receivers'] = ['type' => 'roomkey', 'users' => $roomkeys];
  148. $msg['room'] = $this->mRoomid;
  149. $msg['act'] = "room";
  150. $msg['op'] = $op;
  151. $msg['body']['act'] = 'room';
  152. $msg['body']['op'] = $op;
  153. $msg['body']['room'] = $this->mRoomid;
  154. $msg['body']['content'] = $content;
  155. $this->mCurRespMsgs['return'][] = $msg;
  156. }
  157. protected function add_broad($op,$content)
  158. {
  159. $msg = [];
  160. $msg['receivers'] = ['type' => 'broadcast'];
  161. $msg['room'] = $this->mRoomid;
  162. $msg['act'] = "room";
  163. $msg['op'] = $op;
  164. $msg['body']['act'] = 'room';
  165. $msg['body']['op'] = $op;
  166. $msg['body']['room'] = $this->mRoomid;
  167. $msg['body']['content'] = $content;
  168. $this->mCurRespMsgs['broadcast'][] = $msg;
  169. }
  170. public function acc_req() {
  171. return $this->mAccReq;
  172. }
  173. public function join($participant)
  174. {
  175. $ret = $this->find($participant);
  176. if($ret != false) {
  177. $this->mRoomkeys[$participant]['active'] = true;
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. }
  183. public function leave($participant)
  184. {
  185. $ret = $this->find($participant);
  186. if($ret != false) {
  187. $this->mRoomkeys[$participant]['active'] = false;
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. protected function find($participant)
  194. {
  195. if(empty($participant)) return false;
  196. if(array_key_exists($participant,$this->mRoomkeys)) {
  197. return $this->mRoomkeys[$participant];
  198. } else {
  199. return false;
  200. }
  201. }
  202. protected function room_key($unionid)
  203. {
  204. return md5("{$this->mRoomType}_{$this->mRoomid}_{$unionid}");
  205. }
  206. public function room_type() {
  207. return $this->mRoomType;
  208. }
  209. public function cur_msgs() {
  210. return $this->mCurRespMsgs;
  211. }
  212. }