base_room.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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($stype, $content,$user)
  226. {
  227. $this->clear();
  228. try
  229. {
  230. $user = intval($user);
  231. if($user > 0) {
  232. $uinfo = new member_info($user);
  233. $from = ['nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => $user];
  234. }
  235. else {
  236. $from = false;
  237. }
  238. }
  239. catch (Exception $ex) {
  240. $from = false;
  241. }
  242. if($stype == proto_type::msg_stype_donate) {
  243. $userid = $content['user'];
  244. $steps = $content['steps'];
  245. $amount = $content['amount'];
  246. $msg = $this->donate_msg($userid,$steps,$amount);
  247. $fplain = true;
  248. }
  249. elseif($stype == proto_type::msg_stype_spend) {
  250. $userid = $content['user'];
  251. $amount = $content['amount'];
  252. $msg = $this->spend_msg($userid,$amount);
  253. $fplain = true;
  254. }
  255. elseif($stype == proto_type::msg_stype_text)
  256. {
  257. $msg = $content;
  258. $fplain = false;
  259. }
  260. else {
  261. return false;
  262. }
  263. if($msg == false) return false;
  264. $type = proto_type::to_msgtype($stype);
  265. if($from != false)
  266. {
  267. $msgid = $this->record_message($user,$type,$msg,json_encode($content,JSON_UNESCAPED_UNICODE));
  268. if($fplain) {
  269. $this->relay_broadcast('message',['msgid' => $msgid,'from' => $from,'type' => proto_type::msg_stype_plain,'content' => $msg,'send_time' => time(),'seq' => 0]);
  270. } else {
  271. $this->relay_broadcast('message',['msgid' => $msgid,'from' => $from,'type' => $stype,'content' => $msg,'send_time' => time(),'seq' => 0]);
  272. }
  273. }
  274. else
  275. {
  276. $msgid = $this->record_message(0,$type,$msg,json_encode($content,JSON_UNESCAPED_UNICODE));
  277. if($fplain) {
  278. $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $msg,'send_time' => time()]);
  279. } else {
  280. $this->relay_broadcast('message',['msgid' => $msgid,'type' => $stype,'content' => $msg,'send_time' => time()]);
  281. }
  282. }
  283. return true;
  284. }
  285. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  286. public function messageOp($input)
  287. {
  288. $this->clear();
  289. $user = $input['user'];
  290. $userinfo = $this->find($user);
  291. if($userinfo == false) {
  292. return false;
  293. }
  294. $type = proto_type::to_msgtype($input['type']);
  295. $content = $this->validate_content($input['content']);
  296. $seq = $input['seq'];
  297. if(empty($seq)) $seq = "";
  298. if($type == false || $content == false) {
  299. return false;
  300. }
  301. $msgid = $this->record_message($userinfo['userid'],$type,$content);
  302. if($msgid > 0) {
  303. $this->relay_broadcast('message',['msgid' => $msgid, 'from' => $userinfo,'type' => $input['type'],'content' => $content,'send_time' => time(),'seq' => $seq]);
  304. return ['act' => 'room','op' => 'message','room' => $this->room_id()];
  305. }
  306. else {
  307. return false;
  308. }
  309. }
  310. protected function validate_content($content)
  311. {
  312. return $content;
  313. }
  314. protected function record_message($userid,$type,$content,$orgmsg = '')
  315. {
  316. $mod_room = Model('room');
  317. if($type == proto_type::msg_type_goods) {
  318. $data = json_decode($content,true);
  319. $goods_id = $data['goods_id'];
  320. return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content, 'orgmsg' => $goods_id, 'add_time' => time(),'msg_type' => 0]);
  321. }
  322. else {
  323. return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content, 'orgmsg' => $orgmsg, 'add_time' => time(),'msg_type' => 0]);
  324. }
  325. }
  326. public function relay_reply_msgs()
  327. {
  328. return $this->mRelayMsgs['reply'];
  329. }
  330. public function relay_broadcast_msgs()
  331. {
  332. return $this->mRelayMsgs['broadcast'];
  333. }
  334. protected function clear()
  335. {
  336. $this->mRelayMsgs['reply'] = [];
  337. $this->mRelayMsgs['broadcast'] = [];
  338. }
  339. protected function relay_reply($user, $op, $content)
  340. {
  341. $msg = [];
  342. $msg['act'] = "room";
  343. $msg['op'] = "relay";
  344. $msg['msgtype'] = "message";
  345. $msg['relay_type'] = "roomusers";
  346. $msg['receivers'] = [$user];
  347. $msg['room'] = $this->mRoomid;
  348. $msg['receiver_type'] = $this->room_type();
  349. $msg['body']['act'] = 'room';
  350. $msg['body']['op'] = $op;
  351. $msg['body']['msgtype'] = "message";
  352. $msg['body']['room'] = $this->mRoomid;
  353. $msg['body']['content'] = $content;
  354. $this->mRelayMsgs['reply'][] = $msg;
  355. }
  356. protected function relay_users(array $users, $op, $content)
  357. {
  358. $msg = [];
  359. $msg['act'] = "room";
  360. $msg['op'] = "relay";
  361. $msg['msgtype'] = "message";
  362. $msg['relay_type'] = "roomusers";
  363. $msg['receivers'] = $users;
  364. $msg['room'] = $this->mRoomid;
  365. $msg['receiver_type'] = $this->room_type();
  366. $msg['body']['act'] = 'room';
  367. $msg['body']['op'] = $op;
  368. $msg['body']['msgtype'] = "message";
  369. $msg['body']['room'] = $this->mRoomid;
  370. $msg['body']['content'] = $content;
  371. $this->mRelayMsgs['broadcast'][] = $msg;
  372. }
  373. protected function relay_broadcast($op, $content)
  374. {
  375. $msg = [];
  376. $msg['act'] = "room";
  377. $msg['op'] = "relay";
  378. $msg['msgtype'] = "message";
  379. $msg['relay_type'] = "room";
  380. $msg['room'] = $this->mRoomid;
  381. $msg['receivers'] = [];
  382. $msg['receiver_type'] = $this->room_type();
  383. $msg['body']['act'] = 'room';
  384. $msg['body']['op'] = $op;
  385. $msg['body']['msgtype'] = "message";
  386. $msg['body']['room'] = $this->mRoomid;
  387. $msg['body']['content'] = $content;
  388. $this->mRelayMsgs['broadcast'][] = $msg;
  389. }
  390. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  391. protected function find($userid)
  392. {
  393. if($userid <= 0) return false;
  394. if(array_key_exists($userid,$this->mParticipants)) {
  395. return $this->mParticipants[$userid];
  396. } else {
  397. return false;
  398. }
  399. }
  400. public function room_type() {
  401. return $this->mRoomType;
  402. }
  403. public function userinfos($users)
  404. {
  405. if(is_array($users))
  406. {
  407. $result = [];
  408. foreach ($users as $user)
  409. {
  410. $info = $this->find($user);
  411. if($info == false) continue;
  412. $result[] = $info;
  413. }
  414. return $result;
  415. }
  416. else {
  417. return $this->find($users);
  418. }
  419. }
  420. protected function times($userid)
  421. {
  422. $userinfo = $this->find($userid);
  423. if($userinfo == false) {
  424. return 0;
  425. }
  426. else {
  427. return intval($this->mParticipants[$userid]['times']);
  428. }
  429. }
  430. protected function increase_times($userid)
  431. {
  432. $userinfo = $this->find($userid);
  433. if($userinfo != false) {
  434. $times = intval($this->mParticipants[$userid]['times']) + 1;
  435. $this->mParticipants[$userid]['times'] = $times;
  436. }
  437. }
  438. }