base_room.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. use Exception;
  12. abstract class base_room
  13. {
  14. protected $mRoomid;
  15. protected $mParticipants;
  16. protected $mRoomType;
  17. protected $mod_room;
  18. protected $mRelayMsgs;
  19. protected $mRoomInfo;
  20. public function __construct($cinfos, $participants = [])
  21. {
  22. $this->mRoomInfo = new room_info($cinfos);
  23. $this->mRoomid = $this->room_id();
  24. $this->mRelayMsgs['reply'] = []; //转发回复包
  25. $this->mRelayMsgs['broadcast'] = []; //广播包
  26. $this->mParticipants = $participants;
  27. $this->mod_room = Model('room');
  28. }
  29. public function room_id() {
  30. return $this->mRoomInfo->room_id();
  31. }
  32. public function creator() {
  33. return $this->mRoomInfo->creator();
  34. }
  35. public function room_info() {
  36. return $this->mRoomInfo->format();
  37. }
  38. public function room_name() {
  39. return $this->mRoomInfo->name();
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. public function invite($inviter,$invitees,&$newusers)
  43. {
  44. $this->clear();
  45. if($this->verify_inviter($inviter))
  46. {
  47. $users = $this->add($inviter,$invitees,$newusers);
  48. if($users === false) {
  49. $ret = ['room' => $this->mRoomid,'invitees' => [],'newusers' => []];
  50. }
  51. else
  52. {
  53. $ret = ['room' => $this->mRoomid,'invitees' => $users,'newusers' => $newusers];
  54. if(!empty($newusers)) {
  55. $content = $this->format_invite($inviter,$newusers);
  56. if($content !== false)
  57. {
  58. $type = proto_type::to_msgtype(proto_type::msg_stype_plain);
  59. $msgid = $this->record_message(0,$type,$content);
  60. $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $content,'send_time' => time()]);
  61. }
  62. }
  63. }
  64. return $ret;
  65. }
  66. else {
  67. return false;
  68. }
  69. }
  70. public function usercount() {
  71. return count($this->mParticipants);
  72. }
  73. //返回被删除的所有userid 数组
  74. public function kickout($manager,$users)
  75. {
  76. $this->clear();
  77. $fAdmin = $this->mRoomInfo->isAdmin($manager);
  78. if($fAdmin == false) return false;
  79. $uids = [];
  80. $kicks = [];
  81. foreach ($users as $user) {
  82. $fAdmin = $this->mRoomInfo->isAdmin($user);
  83. if($fAdmin || $manager == $user) continue;
  84. $userinfo = $this->find($user);
  85. if($userinfo == false) continue;
  86. $kicks[] = $userinfo;
  87. unset($this->mParticipants[$user]);
  88. $this->mod_room->kickout($this->room_id(),$user);
  89. $uids[] = $user;
  90. }
  91. if(empty($kicks)) {
  92. return false;
  93. } else {
  94. $content = $this->format_kickout($manager,$kicks);
  95. $type = proto_type::to_msgtype(proto_type::msg_stype_plain);
  96. $msgid = $this->record_message(0,$type,$content);
  97. $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $content,'send_time' => time()]);
  98. return $uids;
  99. }
  100. }
  101. private function format_kickout($user, $kickinfos)
  102. {
  103. $user = $this->userinfos($user);
  104. $str = "<font color='#3c78d8'>{$user['nickname']}</font>将";
  105. $contents = [];
  106. foreach ($kickinfos as $user) {
  107. $contents[] = "<font color='#3c78d8'>{$user['nickname']}</font>";
  108. }
  109. $str .= implode('、',$contents);
  110. $str .= "移除群聊";
  111. return $str;
  112. }
  113. private function format_invite($inviter,$invitees)
  114. {
  115. $invitees = $this->userinfos($invitees);
  116. $inviter = $this->userinfos($inviter);
  117. if( count($invitees)==1 && $invitees[0]['userid']==$inviter['userid']){
  118. return false;
  119. }
  120. $str = "<font color='#3c78d8'>{$inviter['nickname']}</font>邀请";
  121. $contents = [];
  122. foreach ($invitees as $user) {
  123. $contents[] = "<font color='#3c78d8'>{$user['nickname']}</font>";
  124. }
  125. $str .= implode('、',$contents);
  126. $str .= "加入群聊";
  127. return $str;
  128. }
  129. private function donate_msg($user,$steps,$amount)
  130. {
  131. try
  132. {
  133. $minfo = $this->userinfos($user);
  134. $str = "{$minfo['nickname']}贡献了{$amount}元助力共享基金,兑换步数{$steps}步。<font color='#4A90E2'>一起参与助力吧!</font>";
  135. return $str;
  136. }
  137. catch (Exception $ex) {
  138. return false;
  139. }
  140. }
  141. private function spend_msg($user,$amount)
  142. {
  143. try
  144. {
  145. $minfo = $this->userinfos($user);
  146. $str = "{$minfo['nickname']}使用了{$amount}元共享基金,成功购物。<font color='#4A90E2'>一起参与助力吧!</font>";
  147. return $str;
  148. }
  149. catch (Exception $ex) {
  150. return false;
  151. }
  152. }
  153. private function verify_inviter($inviter)
  154. {
  155. if($this->creator() == $inviter) {
  156. return true;
  157. }
  158. else {
  159. $ret = $this->find($inviter);
  160. return ($ret === false) ? false : true;
  161. }
  162. }
  163. private function add($inviter,$invitees,&$newusers)
  164. {
  165. $newusers = [];
  166. $invitees = array_unique($invitees);
  167. $items = Model('member')->getMemberList(['member_id' => ['in',$invitees]]);
  168. if(empty($items)) return false;
  169. $users = [];
  170. foreach ($items as $item) {
  171. $userid = intval($item['member_id']);
  172. $users[$userid] = $item;
  173. }
  174. $result = [];
  175. foreach ($invitees as $invitee)
  176. {
  177. try
  178. {
  179. if(!array_key_exists($invitee,$users)) continue;
  180. $result[] = $invitee;
  181. $ret = $this->find($invitee);
  182. if($ret !== false) continue;
  183. $ret = $this->mod_room->invite($this->room_id(),$invitee,$inviter);
  184. if($ret == false) {
  185. continue;
  186. } else {
  187. $user = $users[$invitee];
  188. $uinfo = new member_info($user);
  189. $this->mParticipants[$invitee] = ['nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => intval($invitee)];
  190. $newusers[] = $invitee;
  191. }
  192. }
  193. catch (Exception $ex) {
  194. Log::record($ex->getMessage(),Log::ERR);
  195. }
  196. }
  197. return empty($result) ? false : $result;
  198. }
  199. public function change()
  200. {
  201. $this->clear();
  202. $mod_room = Model('room');
  203. $item = $mod_room->getRoom($this->room_id());
  204. if(empty($item)) return false;
  205. $this->mRoomInfo = new room_info($item);
  206. return true;
  207. }
  208. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  209. public function leave($userid)
  210. {
  211. $this->clear();
  212. $ret = $this->find($userid);
  213. if($ret != false) {
  214. unset($this->mParticipants[$userid]);
  215. $this->mod_room->leave($this->room_id(),$userid);
  216. $content = "<font color='#3c78d8'>{$ret['nickname']}</font>退出群聊";
  217. $type = proto_type::to_msgtype(proto_type::msg_stype_plain);
  218. $msgid = $this->record_message(0,$type,$content);
  219. $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $content,'send_time' => time()]);
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. }
  225. public function notice($type,$content)
  226. {
  227. $this->clear();
  228. if($type == proto_type::msg_stype_donate) {
  229. $userid = $content['user'];
  230. $steps = $content['steps'];
  231. $amount = $content['amount'];
  232. $msg = $this->donate_msg($userid,$steps,$amount);
  233. }
  234. elseif($type == proto_type::msg_stype_spend) {
  235. $userid = $content['user'];
  236. $amount = $content['amount'];
  237. $msg = $this->spend_msg($userid,$amount);
  238. }
  239. else {
  240. return false;
  241. }
  242. if($msg == false) return false;
  243. $type = proto_type::to_msgtype($type);
  244. $msgid = $this->record_message(0,$type,$msg,json_encode($content));
  245. $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $msg,'send_time' => time()]);
  246. return true;
  247. }
  248. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  249. public function messageOp($input)
  250. {
  251. $this->clear();
  252. $user = $input['user'];
  253. $userinfo = $this->find($user);
  254. if($userinfo == false) {
  255. return false;
  256. }
  257. $type = proto_type::to_msgtype($input['type']);
  258. $content = $this->validate_content($input['content']);
  259. $seq = $input['seq'];
  260. if(empty($seq)) $seq = "";
  261. if($type == false || $content == false) {
  262. return false;
  263. }
  264. $msgid = $this->record_message($userinfo['userid'],$type,$content);
  265. if($msgid > 0) {
  266. $this->relay_broadcast('message',['msgid' => $msgid, 'from' => $userinfo,'type' => $input['type'],'content' => $content,'send_time' => time(),'seq' => $seq]);
  267. return ['act' => 'room','op' => 'message','room' => $this->room_id()];
  268. }
  269. else {
  270. return false;
  271. }
  272. }
  273. protected function validate_content($content)
  274. {
  275. return $content;
  276. }
  277. protected function record_message($userid,$type,$content,$orgmsg = '')
  278. {
  279. $mod_room = Model('room');
  280. if($type == proto_type::msg_type_goods) {
  281. $data = json_decode($content,true);
  282. $goods_id = $data['goods_id'];
  283. return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content, 'orgmsg' => $goods_id, 'add_time' => time(),'msg_type' => 0]);
  284. }
  285. else {
  286. return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content, 'orgmsg' => $orgmsg, 'add_time' => time(),'msg_type' => 0]);
  287. }
  288. }
  289. public function relay_reply_msgs()
  290. {
  291. return $this->mRelayMsgs['reply'];
  292. }
  293. public function relay_broadcast_msgs()
  294. {
  295. return $this->mRelayMsgs['broadcast'];
  296. }
  297. protected function clear()
  298. {
  299. $this->mRelayMsgs['reply'] = [];
  300. $this->mRelayMsgs['broadcast'] = [];
  301. }
  302. protected function relay_reply($user, $op, $content)
  303. {
  304. $msg = [];
  305. $msg['act'] = "room";
  306. $msg['op'] = "relay";
  307. $msg['msgtype'] = "message";
  308. $msg['relay_type'] = "roomusers";
  309. $msg['receivers'] = [$user];
  310. $msg['room'] = $this->mRoomid;
  311. $msg['receiver_type'] = $this->room_type();
  312. $msg['body']['act'] = 'room';
  313. $msg['body']['op'] = $op;
  314. $msg['body']['msgtype'] = "message";
  315. $msg['body']['room'] = $this->mRoomid;
  316. $msg['body']['content'] = $content;
  317. $this->mRelayMsgs['reply'][] = $msg;
  318. }
  319. protected function relay_users(array $users, $op, $content)
  320. {
  321. $msg = [];
  322. $msg['act'] = "room";
  323. $msg['op'] = "relay";
  324. $msg['msgtype'] = "message";
  325. $msg['relay_type'] = "roomusers";
  326. $msg['receivers'] = $users;
  327. $msg['room'] = $this->mRoomid;
  328. $msg['receiver_type'] = $this->room_type();
  329. $msg['body']['act'] = 'room';
  330. $msg['body']['op'] = $op;
  331. $msg['body']['msgtype'] = "message";
  332. $msg['body']['room'] = $this->mRoomid;
  333. $msg['body']['content'] = $content;
  334. $this->mRelayMsgs['broadcast'][] = $msg;
  335. }
  336. protected function relay_broadcast($op, $content)
  337. {
  338. $msg = [];
  339. $msg['act'] = "room";
  340. $msg['op'] = "relay";
  341. $msg['msgtype'] = "message";
  342. $msg['relay_type'] = "room";
  343. $msg['room'] = $this->mRoomid;
  344. $msg['receivers'] = [];
  345. $msg['receiver_type'] = $this->room_type();
  346. $msg['body']['act'] = 'room';
  347. $msg['body']['op'] = $op;
  348. $msg['body']['msgtype'] = "message";
  349. $msg['body']['room'] = $this->mRoomid;
  350. $msg['body']['content'] = $content;
  351. $this->mRelayMsgs['broadcast'][] = $msg;
  352. }
  353. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  354. protected function find($userid)
  355. {
  356. if($userid <= 0) return false;
  357. if(array_key_exists($userid,$this->mParticipants)) {
  358. return $this->mParticipants[$userid];
  359. } else {
  360. return false;
  361. }
  362. }
  363. public function room_type() {
  364. return $this->mRoomType;
  365. }
  366. public function userinfos($users)
  367. {
  368. if(is_array($users))
  369. {
  370. $result = [];
  371. foreach ($users as $user)
  372. {
  373. $info = $this->find($user);
  374. if($info == false) continue;
  375. $result[] = $info;
  376. }
  377. return $result;
  378. }
  379. else {
  380. return $this->find($users);
  381. }
  382. }
  383. protected function times($userid)
  384. {
  385. $userinfo = $this->find($userid);
  386. if($userinfo == false) {
  387. return 0;
  388. }
  389. else {
  390. return intval($this->mParticipants[$userid]['times']);
  391. }
  392. }
  393. protected function increase_times($userid)
  394. {
  395. $userinfo = $this->find($userid);
  396. if($userinfo != false) {
  397. $times = intval($this->mParticipants[$userid]['times']) + 1;
  398. $this->mParticipants[$userid]['times'] = $times;
  399. }
  400. }
  401. }