SpyController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 actionAuthorping()
  31. {
  32. $cache = Yii::$app->memcache;
  33. $user = Yii::$app->user->identity->profile->name;
  34. $id = Yii::$app->user->identity->profile->user_id;
  35. $mduser = $id;
  36. $uactive = $cache->get(self::keylist);
  37. $html = '';
  38. if( $uactive ){
  39. $uactive = unserialize($uactive);
  40. $uactive[$mduser] = ['name'=>$user, 'time' => time(), 'id' => $id];
  41. $cache->set( self::keylist, serialize($uactive), self::dead );
  42. $a = [];
  43. foreach($uactive as $userid => $data){
  44. if( time()-$data['time'] < self::kill ){
  45. $a[$userid] = $data;
  46. }
  47. }
  48. $uactive = $a;
  49. $html = $this->renderPartial('list',['users'=>$uactive, 'obj'=>$this, 'id'=>$id]);
  50. }else{
  51. $uactive = [];
  52. $uactive[$mduser] = ['name'=>$user, 'time' => time(), 'id' => $id];
  53. $cache->add( self::keylist, serialize($uactive), self::dead );
  54. }
  55. return json_encode( ['status'=>'ok', 'html'=>$html, 'count' =>count($uactive)] );
  56. }
  57. }