SpyController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace manager\controllers;
  3. use Yii;
  4. class SpyController extends BaseController
  5. {
  6. const sleep = 60*5;
  7. const dead = 60*15;
  8. const kill = 60*25;
  9. const keylist = 'uactive';
  10. public function actionIndex()
  11. {
  12. return $this->redirect("news/list");
  13. }
  14. public function actionView()
  15. {
  16. $cache = Yii::$app->memcache;
  17. $user = Yii::$app->user->identity->profile->name;
  18. $id = Yii::$app->user->identity->profile->user_id;
  19. $mduser = $id;
  20. $uactive = $cache->get(self::keylist);
  21. $html = '';
  22. if( $uactive ){
  23. $uactive = unserialize($uactive);
  24. $html = $this->renderPartial('_item',['users'=>$uactive, 'obj'=>$this, 'id'=>$id]);
  25. }else{
  26. $html = '';
  27. }
  28. return $this->render("view", ['html'=>$html]);
  29. }
  30. public function actionTime($id)
  31. {
  32. return $this->render("time", ['id'=>$id, 'obj'=>$this]);
  33. }
  34. public function actionAuthorping()
  35. {
  36. $cache = Yii::$app->memcache;
  37. $user = Yii::$app->user->identity->profile->name;
  38. $id = Yii::$app->user->identity->profile->user_id;
  39. $mduser = $id;
  40. $ukey ='userlog_'.$id;
  41. $uactive = $cache->get(self::keylist);
  42. $html = '';
  43. $stat = 'start';
  44. if( $uactive ){
  45. $uactive = unserialize($uactive);
  46. if( isset($uactive[$mduser])){
  47. $old = $uactive[$mduser];
  48. }else{
  49. $old = null;
  50. }
  51. $uactive[$mduser] = ['name'=>$user, 'time' => time(), 'id' => $id];
  52. $cache->set( self::keylist, serialize($uactive), self::kill );
  53. $a = [];
  54. foreach($uactive as $userid => $data){
  55. if( time()-$data['time'] < self::kill ){
  56. $a[$userid] = $data;
  57. }else{
  58. $xlog = $cache->get( 'userlog_'.$userid );
  59. if( $xlog ){
  60. $xlog = unserialize($xlog);
  61. $old = end($xlog);
  62. if( $old['stat'] != 'dead' ){
  63. $xlog[time()] = ['stat' => 'dead', 'time' => time()];
  64. $cache->set( 'userlog_'.$userid, serialize($xlog), 86400 );
  65. }
  66. }
  67. }
  68. }
  69. $uactive = $a;
  70. $html = $this->renderPartial('list',['users'=>$uactive, 'obj'=>$this, 'id'=>$id]);
  71. }else{
  72. $uactive = [];
  73. $uactive[$mduser] = ['name'=>$user, 'time' => time(), 'id' => $id];
  74. $cache->add( self::keylist, serialize($uactive), self::kill );
  75. }
  76. if( $old ){
  77. if( time()-$old['time'] >= self::kill ){
  78. $stat = 'dead';
  79. }
  80. if( time()-$old['time'] < self::kill && time()-$old['time'] >= self::dead){
  81. $stat = 'sleep';
  82. }
  83. if( time()-$old['time'] < self::sleep ){
  84. $stat = 'good';
  85. }
  86. }
  87. $ulog = $cache->get( $ukey );
  88. if( $ulog ){
  89. $ulog = unserialize($ulog);
  90. $old = end($ulog);
  91. if( $old['stat'] != $stat ){
  92. $ulog[time()] = ['stat' => $stat, 'time' => time()];
  93. $cache->set( $ukey, serialize($ulog), 86400 );
  94. }
  95. }else{
  96. $ulog[time()] = ['stat' => 'start', 'time' => time()];
  97. $cache->add( $ukey, serialize($ulog), 86400 );
  98. }
  99. return json_encode( ['status'=>'ok', 'html'=>$html, 'count' =>count($uactive)] );
  100. }
  101. }