Преглед на файлове

增加在线用户活跃用户功能

15954078560 преди 3 години
родител
ревизия
c09a6eaba3
променени са 2 файла, в които са добавени 76 реда и са изтрити 0 реда
  1. 60 0
      application/api/controller/User.php
  2. 16 0
      application/common/library/Auth.php

+ 60 - 0
application/api/controller/User.php

@@ -930,6 +930,19 @@ class User extends Api
                 $userInfo['wechat'] = '******';
             }
 
+            //查询在线状态和活跃状态
+            $user_info = Db::name('user_info')->where(['user_id' => $user_id])->find();
+            $asktime = explode(',', $user_info['asktime']);
+            if ($user_info && $asktime && time() - $asktime[count($asktime) - 1] <= 3600) {
+                $userInfo['is_online'] = 1; //0离线 1在线
+            } else {
+                $userInfo['is_online'] = 0; //0离线 1在线
+            }
+            if ($user_info && count($asktime) >= 20 && $asktime[count($asktime) - 1] - $asktime[0] <= 86400) {
+                $userInfo['is_active'] = 1; //活跃
+            } else {
+                $userInfo['is_active'] = 0; //不活跃
+            }
             // 获取已有标签以及数量
             $userInfo['tagUser'] = \app\common\model\TagUser::alias('a')
                 ->field('a.id,t.name,a.number')
@@ -978,6 +991,15 @@ class User extends Api
         $userInfo['wechat_auth_stauts'] = \app\common\model\WechatAuth::getAuthStatus($userInfo['id'],$userInfo['wechat_auth']);
         $userInfo['declaration_auth_stauts'] = $userInfo['declaration_auth'] !== '' ? \app\common\model\DeclarationAuth::getAuthStatus($userInfo['id'],$userInfo['declaration_auth']) : 1;
 
+        //查询在线状态和活跃状态
+        $user_info = Db::name('user_info')->where(['user_id' => $user_id])->find();
+        $asktime = explode(',', $user_info['asktime']);
+        $userInfo['is_online'] = time() - $asktime[count($asktime) - 1] > 3600 ? 0 : 1; //0离线 1在线
+        if (count($asktime) >= 20 && $asktime[count($asktime) - 1] - $asktime[0] <= 86400) {
+            $userInfo['is_active'] = 1; //活跃
+        } else {
+            $userInfo['is_active'] = 0; //不活跃
+        }
 
 //            $userInfo = $userInfo->toArray();
 //            $redis->hMSet('userInfo_'.$user_id,encodeArray($userInfo));
@@ -1355,4 +1377,42 @@ class User extends Api
 
         $this->success('设置成功', $status);
     }
+
+    //收集收集信息
+    public function collectmobileinfo() {
+        $system = input('system', '', 'trim'); //系统
+        $mobilemodel = input('mobilemodel', '', 'trim'); //手机型号
+        $mobilebrand = input('mobilebrand', '', 'trim'); //手机品牌
+
+        if (!$system || !$mobilemodel || !$mobilebrand) {
+            $this->error('参数缺失');
+        }
+        if (iconv_strlen($system, 'utf-8') > 255) {
+            $this->error('超出限制');
+        }
+        if (iconv_strlen($mobilemodel, 'utf-8') > 255) {
+            $this->error('超出限制');
+        }
+        if (iconv_strlen($mobilebrand, 'utf-8') > 255) {
+            $this->error('超出限制');
+        }
+
+        $data['system'] = $system;
+        $data['mobilemodel'] = $mobilemodel;
+        $data['mobilebrand'] = $mobilebrand;
+
+        $info = Db::name('user_info')->where(['user_id' => $this->auth->id])->find();
+        if (!$info) {
+            $data['user_id'] = $this->auth->id;
+            $rs = Db::name('user_info')->insertGetId($data);
+        } else {
+            $rs = Db::name('user_info')->where(['user_id' => $this->auth->id])->setField($data);
+        }
+
+        if ($rs === false) {
+            $this->error('收集失败');
+        }
+
+        $this->success('收集成功');
+    }
 }

+ 16 - 0
application/common/library/Auth.php

@@ -115,6 +115,22 @@ class Auth
             $this->_logined = true;
             $this->_token = $token;
 
+            //记录用户访问时间
+            $info = Db::name('user_info')->where(['user_id' => $user_id])->find();
+            if (!$info) {
+                Db::name('user_info')->insertGetId(['user_id' => $user_id, 'asktime' => time()]);
+            } else {
+                $asktime_before = explode(',', $info['asktime']);
+                if (count($asktime_before) < 20) {
+                    $asktime_now = $info['asktime'] . ',' . time();
+                } else {
+                    unset($asktime_before[0]);
+                    $asktime_now = join(',', $asktime_before) . ',' . time();
+                }
+
+                Db::name('user_info')->where(['user_id' => $user_id])->setField('asktime', $asktime_now);
+            }
+
             //初始化成功的事件
             Hook::listen("user_init_successed", $this->_user);