pusher.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/9/7
  6. * Time: 上午10:06
  7. */
  8. namespace room;
  9. use Exception;
  10. use Log;
  11. use member_info;
  12. class pusher
  13. {
  14. private static $stInstance;
  15. private function __construct()
  16. {
  17. }
  18. public static function instance()
  19. {
  20. if(self::$stInstance == null) {
  21. self::$stInstance = new pusher();
  22. }
  23. return self::$stInstance;
  24. }
  25. public function new_friend($receiver,$user)
  26. {
  27. try
  28. {
  29. $minfo = new member_info($user);
  30. $from = ['nickname' => $minfo->nickname(),'avatar' => $minfo->avatar(),'userid' => intval($user)];
  31. $content = ['type' => 'new_friend','from' => $from];
  32. factory_client::instance()->notice_users([$receiver],proto_type::push_command,$content,'');
  33. }
  34. catch (Exception $ex) {
  35. Log::record($ex->getMessage(),Log::ERR);
  36. }
  37. }
  38. public function kick_room($room,$kicks)
  39. {
  40. try
  41. {
  42. $content = ['type' => 'kick_room','room' => $room];
  43. factory_client::instance()->notice_users($kicks,proto_type::push_command,$content,'');
  44. }
  45. catch (Exception $ex) {
  46. Log::record($ex->getMessage(),Log::ERR);
  47. }
  48. }
  49. //state = pending,accept,refuse
  50. public function apply_friend($applicant,$friend,$note)
  51. {
  52. try
  53. {
  54. $minfo = new member_info($applicant);
  55. $from = ['nickname' => $minfo->nickname(),'avatar' => $minfo->avatar(),'userid' => intval($applicant)];
  56. $content = ['type' => 'apply_friend','from' => $from,'note' => $note,'state' => 'pending'];
  57. $msg = "{$minfo->nickname()} 请求加您为好友.";
  58. factory_client::instance()->notice_users([$friend],proto_type::push_apply,$content,$msg);
  59. }
  60. catch (Exception $ex) {
  61. Log::record($ex->getMessage(),Log::ERR);
  62. }
  63. }
  64. //state = pending,accept,refuse
  65. public function apply_room($applicant,$roomid,$note)
  66. {
  67. try
  68. {
  69. $minfo = new member_info($applicant);
  70. $from = ['nickname' => $minfo->nickname(),'avatar' => $minfo->avatar(),'userid' => intval($applicant)];
  71. $content = ['type' => 'apply_room','from' => $from,'note' => $note,'room_id' => $roomid,'state' => 'pending'];
  72. $room = $this->get_room($roomid);
  73. if($room == false) return;
  74. $msg = "{$minfo->nickname()} 申请加入 {$room->name()}";
  75. factory_client::instance()->notice_users([$room->creator()],proto_type::push_apply,$content,$msg);
  76. }
  77. catch (Exception $ex) {
  78. Log::record($ex->getMessage(),Log::ERR);
  79. }
  80. }
  81. public function cert_notice($roomid, $state, $note)
  82. {
  83. try
  84. {
  85. $room = $this->get_room($roomid);
  86. if($room == false) return;
  87. $name = \util::ellipsis($room->name());
  88. if($state) {
  89. $msg = "群: {$name} 申请的认证被通过";
  90. $state = "accept";
  91. } else {
  92. $msg = "群: {$name} 申请的认证被拒绝";
  93. $state = "refuse";
  94. }
  95. $from = ['nickname' => $name,'avatar' => $room->avatar(),'room_id' => intval($roomid)];
  96. $content = ['type' => 'cert_notice','from'=>$from,'note' => $note,'room_id' => $roomid,"state"=>$state];
  97. factory_client::instance()->notice_users([$room->owner()],proto_type::push_apply,$content,$msg);
  98. }
  99. catch (Exception $ex) {
  100. Log::record($ex->getMessage(),Log::ERR);
  101. }
  102. }
  103. private function get_room($roomid)
  104. {
  105. $roomid = intval($roomid);
  106. if($roomid <= 0) return false;
  107. $mod_room = Model('room');
  108. $item = $mod_room->getRoom($roomid);
  109. if(empty($item)) return false;
  110. return new room_info($item);
  111. }
  112. }