base_room.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. $userid = intval($input['user']);
  33. if($userid > 0) {
  34. $room_key = $this->invite($userid);
  35. $ret = ['room_key' => $room_key,'room' => $this->mRoomid];
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. public function invite($userid)
  42. {
  43. $item = Model('member')->getMemberInfo(['member_id' => $userid]);
  44. if(empty($item)) return false;
  45. $uinfo = new member_info($item);
  46. $participant = $this->room_key($uinfo->unionid());
  47. $ret = $this->find($participant);
  48. if($ret == false)
  49. {
  50. $ret = $this->mod_room->invite($this->room_id(),$userid,$participant);
  51. if($ret == false) {
  52. Log::record(__METHOD__ . " invite error",Log::ERR);
  53. return false;
  54. }
  55. else {
  56. $this->mRoomkeys[$participant] = ['active' => false,
  57. 'nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => intval($userid)];
  58. }
  59. }
  60. return $participant;
  61. }
  62. public function reject($roomkey)
  63. {
  64. $ret = $this->find($roomkey);
  65. if($ret != false) {
  66. unset($this->mRoomkeys[$roomkey]);
  67. }
  68. return true;
  69. }
  70. protected function room_info($roomkey)
  71. {
  72. $result = [];
  73. $result['room'] = $this->mRoomid;
  74. $result['users'] = [];
  75. foreach ($this->mRoomkeys as $key => $item)
  76. {
  77. $result['users'][] = $item;
  78. if($key == $roomkey) {
  79. $result['me'] = $item['userid'];
  80. }
  81. }
  82. return $result;
  83. }
  84. public function joinOp($input)
  85. {
  86. $this->clear();
  87. $room_key = $input['room_key'];
  88. if(empty($room_key)) {
  89. return false;
  90. }
  91. if($this->join($room_key))
  92. {
  93. $this->add_return([$room_key],'ret_join',$this->room_info($room_key));
  94. $this->add_broad('join',$this->mRoomkeys[$room_key]);
  95. return true;
  96. }
  97. else {
  98. return false;
  99. }
  100. }
  101. public function leaveOp($input)
  102. {
  103. $this->clear();
  104. $room_key = $input['room_key'];
  105. $userinfo = $this->find($room_key);
  106. if($userinfo == false) return false;
  107. if($this->leave($room_key)) {
  108. $this->add_return([$room_key],'ret_leave',$userinfo);
  109. $this->add_broad('leave',$userinfo);
  110. return true;
  111. }
  112. else {
  113. return false;
  114. }
  115. }
  116. public function messageOp($input)
  117. {
  118. $this->clear();
  119. $room_key = $input['room_key'];
  120. $userinfo = $this->find($room_key);
  121. if($userinfo == false) return false;
  122. $type = $this->validate_type($input['type']);
  123. $content = $this->validate_content($input['content']);
  124. if($type == false || $content == false) return false;
  125. $this->record_message($userinfo['userid'],$type,$content);
  126. $this->add_broad('message',['from' => $userinfo,'type' => $type,'content' => $content]);
  127. return true;
  128. }
  129. protected function validate_type($type,$content)
  130. {
  131. $stype = strtolower($type);
  132. if($stype == proto_type::msg_stype_text) {
  133. return proto_type::msg_type_text;
  134. }
  135. elseif($stype == proto_type::msg_stype_pic) {
  136. return proto_type::msg_type_text;
  137. }
  138. elseif($stype == proto_type::msg_stype_bargain) {
  139. return proto_type::msg_type_bargain;
  140. }
  141. else {
  142. return false;
  143. }
  144. }
  145. protected function validate_content($content)
  146. {
  147. return $content;
  148. }
  149. protected function record_message($userid,$type,$content)
  150. {
  151. $mod_room = Model('room');
  152. $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content,'add_time' => time()]);
  153. }
  154. public function return_msgs()
  155. {
  156. return $this->mCurRespMsgs['return'];
  157. }
  158. public function broadcast_msgs()
  159. {
  160. return $this->mCurRespMsgs['broadcast'];
  161. }
  162. protected function clear()
  163. {
  164. $this->mCurRespMsgs['return'] = [];
  165. $this->mCurRespMsgs['broadcast'] = [];
  166. }
  167. protected function add_return(array $roomkeys,$op,$content)
  168. {
  169. $msg = [];
  170. $msg['receivers'] = ['type' => 'roomkey', 'users' => $roomkeys];
  171. $msg['room'] = $this->mRoomid;
  172. $msg['act'] = "room";
  173. $msg['op'] = $op;
  174. $msg['body']['act'] = 'room';
  175. $msg['body']['op'] = $op;
  176. $msg['body']['room'] = $this->mRoomid;
  177. $msg['body']['content'] = $content;
  178. $this->mCurRespMsgs['return'][] = $msg;
  179. }
  180. protected function add_broad($op,$content)
  181. {
  182. $msg = [];
  183. $msg['receivers'] = ['type' => 'broadcast'];
  184. $msg['room'] = $this->mRoomid;
  185. $msg['act'] = "room";
  186. $msg['op'] = $op;
  187. $msg['body']['act'] = 'room';
  188. $msg['body']['op'] = $op;
  189. $msg['body']['room'] = $this->mRoomid;
  190. $msg['body']['content'] = $content;
  191. $this->mCurRespMsgs['broadcast'][] = $msg;
  192. }
  193. public function join($participant)
  194. {
  195. $ret = $this->find($participant);
  196. if($ret != false) {
  197. $this->mRoomkeys[$participant]['active'] = true;
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. }
  203. public function leave($participant)
  204. {
  205. $ret = $this->find($participant);
  206. if($ret != false) {
  207. $this->mRoomkeys[$participant]['active'] = false;
  208. return true;
  209. } else {
  210. return false;
  211. }
  212. }
  213. protected function find($participant)
  214. {
  215. if(empty($participant)) return false;
  216. if(array_key_exists($participant,$this->mRoomkeys)) {
  217. return $this->mRoomkeys[$participant];
  218. } else {
  219. return false;
  220. }
  221. }
  222. protected function room_key($unionid)
  223. {
  224. return md5("{$this->mRoomType}_{$this->mRoomid}_{$unionid}");
  225. }
  226. public function room_type() {
  227. return $this->mRoomType;
  228. }
  229. public function cur_msgs() {
  230. return $this->mCurRespMsgs;
  231. }
  232. }