123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace manager\controllers;
- use Yii;
- class SpyController extends BaseController
- {
- const sleep = 60*5;
- const dead = 60*15;
- const kill = 60*25;
- const keylist = 'uactive';
- public function actionIndex()
- {
- return $this->redirect("news/list");
- }
- public function actionView()
- {
- $cache = Yii::$app->memcache;
- $user = Yii::$app->user->identity->profile->name;
- $id = Yii::$app->user->identity->profile->user_id;
- $mduser = $id;
- $uactive = $cache->get(self::keylist);
- $html = '';
- if( $uactive ){
- $uactive = unserialize($uactive);
- $html = $this->renderPartial('_item',['users'=>$uactive, 'obj'=>$this, 'id'=>$id]);
- }else{
- $html = '';
- }
- return $this->render("view", ['html'=>$html]);
- }
- public function actionTime($id)
- {
- return $this->render("time", ['id'=>$id, 'obj'=>$this]);
- }
- public function actionAuthorping()
- {
- $cache = Yii::$app->memcache;
- $user = Yii::$app->user->identity->profile->name;
- $id = Yii::$app->user->identity->profile->user_id;
- $mduser = $id;
- $ukey ='userlog_'.$id;
- $uactive = $cache->get(self::keylist);
- $html = '';
- $stat = 'start';
- if( $uactive ){
- $uactive = unserialize($uactive);
- if( isset($uactive[$mduser])){
- $old = $uactive[$mduser];
- }else{
- $old = null;
- }
- $uactive[$mduser] = ['name'=>$user, 'time' => time(), 'id' => $id];
- $cache->set( self::keylist, serialize($uactive), self::kill );
- $a = [];
- foreach($uactive as $userid => $data){
- if( time()-$data['time'] < self::kill ){
- $a[$userid] = $data;
- }else{
- $xlog = $cache->get( 'userlog_'.$userid );
- if( $xlog ){
- $xlog = unserialize($xlog);
- $old = end($xlog);
- if( $old['stat'] != 'dead' ){
- $xlog[time()] = ['stat' => 'dead', 'time' => time()];
- $cache->set( 'userlog_'.$userid, serialize($xlog), 86400 );
- }
- }
- }
- }
- $uactive = $a;
- $html = $this->renderPartial('list',['users'=>$uactive, 'obj'=>$this, 'id'=>$id]);
- }else{
- $uactive = [];
- $uactive[$mduser] = ['name'=>$user, 'time' => time(), 'id' => $id];
- $cache->add( self::keylist, serialize($uactive), self::kill );
- }
- if( $old ){
- if( time()-$old['time'] >= self::kill ){
- $stat = 'dead';
- }
- if( time()-$old['time'] < self::kill && time()-$old['time'] >= self::dead){
- $stat = 'sleep';
- }
- if( time()-$old['time'] < self::sleep ){
- $stat = 'good';
- }
- }
- $ulog = $cache->get( $ukey );
- if( $ulog ){
- $ulog = unserialize($ulog);
- $old = end($ulog);
- if( $old['stat'] != $stat ){
- $ulog[time()] = ['stat' => $stat, 'time' => time()];
- $cache->set( $ukey, serialize($ulog), 86400 );
- }
- }else{
- $ulog[time()] = ['stat' => 'start', 'time' => time()];
- $cache->add( $ukey, serialize($ulog), 86400 );
- }
- return json_encode( ['status'=>'ok', 'html'=>$html, 'count' =>count($uactive)] );
- }
- }
|