Skip to content

Commit c2b092a

Browse files
committed
访客记录离线处理
1 parent 6bb8148 commit c2b092a

File tree

3 files changed

+71
-27
lines changed

3 files changed

+71
-27
lines changed

Bootstrap.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ public function bootstrap($app)
3030
$app->controllerMap['user'] = [
3131
'class' => 'yuncms\user\console\UserController',
3232
];
33-
}
34-
else if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) {
35-
if (class_exists('\xutl\wechat\Application') && $app instanceof \xutl\wechat\Application) {
36-
//微信过来的用户
33+
} else if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) {
34+
if (class_exists('\xutl\wechat\Application') && $app instanceof \xutl\wechat\Application) {//微信过来的用户
3735
Yii::$container->set('yii\web\User', [
3836
'enableAutoLogin' => true,
3937
'loginUrl' => ['/user/security/login'],
@@ -61,7 +59,7 @@ public function bootstrap($app)
6159
/** @var \yii\web\UserEvent $event */
6260
$app->on(\yii\web\Application::EVENT_AFTER_REQUEST, function ($event) use ($app) {
6361
if (!$app->user->isGuest && Yii::$app->has('queue')) {
64-
Yii::$app->queue->push(new LastVisitJob(['user_id' => $app->user->identity->id,'time'=>time()]));
62+
Yii::$app->queue->push(new LastVisitJob(['user_id' => $app->user->identity->id, 'time' => time()]));
6563
}
6664
});
6765
} elseif ($module instanceof \yuncms\user\frontend\Module) {//前台判断放最后
@@ -99,7 +97,7 @@ public function bootstrap($app)
9997
$app->on(\yii\web\Application::EVENT_AFTER_REQUEST, function ($event) use ($app) {
10098
if (!$app->user->isGuest && Yii::$app->has('queue')) {
10199
//记录最后活动时间
102-
Yii::$app->queue->push(new LastVisitJob(['user_id' => $app->user->identity->id,'time'=>time()]));
100+
Yii::$app->queue->push(new LastVisitJob(['user_id' => $app->user->identity->id, 'time' => time()]));
103101
}
104102
});
105103
}

frontend/controllers/SpaceController.php

+12-21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use yii\data\ActiveDataProvider;
1515
use yii\web\Response;
1616
use yuncms\doing\models\Doing;
17+
use yuncms\user\jobs\VisitJob;
1718
use yuncms\user\models\User;
1819
use yuncms\user\models\Visit;
1920
use yuncms\tag\models\Tag;
@@ -34,7 +35,7 @@ public function behaviors()
3435
'rules' => [
3536
[
3637
'allow' => true,
37-
'actions' => ['index','tag'],
38+
'actions' => ['index', 'tag'],
3839
'roles' => ['@']
3940
],
4041
[
@@ -70,16 +71,11 @@ public function actionIndex()
7071
public function actionShow($slug)
7172
{
7273
$model = $this->findModelBySlug($slug);
73-
if (!Yii::$app->user->isGuest && Yii::$app->user->id != $model->id) {
74-
//记录访客
75-
if (($visit = Visit::findOne(['user_id' => Yii::$app->user->id, 'source_id' => $model->id])) == null) {
76-
$visit = new Visit(['user_id' => Yii::$app->user->id, 'source_id' => $model->id]);
77-
$visit->save(false);
78-
//更新访客计数
79-
$model->extend->updateCounters(['views' => 1]);
80-
} else {
81-
$visit->updateAttributes(['updated_at' => time()]);
82-
}
74+
if (!Yii::$app->user->isGuest && Yii::$app->has('queue')) {
75+
Yii::$app->queue->push(new VisitJob([
76+
'user_id' => Yii::$app->user->id,
77+
'source_id' => $model->id
78+
]));
8379
}
8480
$dataProvider = $this->getDoingDataProvider($model->id);
8581
return $this->render('view', [
@@ -97,16 +93,11 @@ public function actionShow($slug)
9793
public function actionView($id)
9894
{
9995
$model = $this->findModel($id);
100-
if (!Yii::$app->user->isGuest && Yii::$app->user->id != $id) {
101-
//记录访客
102-
if (($visit = Visit::findOne(['user_id' => Yii::$app->user->id, 'source_id' => $id])) == null) {
103-
$visit = new Visit(['user_id' => Yii::$app->user->id, 'source_id' => $id]);
104-
$visit->save(false);
105-
//更新访客计数
106-
$model->extend->updateCounters(['views' => 1]);
107-
} else {
108-
$visit->updateAttributes(['updated_at' => time()]);
109-
}
96+
if (!Yii::$app->user->isGuest && Yii::$app->has('queue')) {
97+
Yii::$app->queue->push(new VisitJob([
98+
'user_id' => Yii::$app->user->id,
99+
'source_id' => $model->id
100+
]));
110101
}
111102

112103
$dataProvider = $this->getDoingDataProvider($model->id);

jobs/VisitJob.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace yuncms\user\jobs;
4+
5+
use Yii;
6+
use yii\base\Object;
7+
use yii\queue\RetryableJob;
8+
use yuncms\user\models\User;
9+
use yuncms\user\models\Visit;
10+
use yuncms\user\models\Extend;
11+
12+
/**
13+
* 记录访客
14+
* @package yuncms\user\jobs
15+
*/
16+
class VisitJob extends Object implements RetryableJob
17+
{
18+
public $user_id;
19+
20+
public $source_id;
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function execute($queue)
26+
{
27+
if ($this->user_id != $this->source_id) {
28+
//记录访客
29+
if (($visit = Visit::findOne(['user_id' => $this->user_id, 'source_id' => $this->source_id])) == null) {
30+
$visit = new Visit(['user_id' => $this->user_id, 'source_id' => $this->source_id]);
31+
$visit->save(false);
32+
//更新访客计数
33+
Extend::updateAllCounters(['views' => 1], ['user_id' => $this->source_id]);
34+
} else {
35+
$visit->updateAttributes(['updated_at' => time()]);
36+
}
37+
}
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function getTtr()
44+
{
45+
return 60;
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function canRetry($attempt, $error)
52+
{
53+
return $attempt < 3;
54+
}
55+
}

0 commit comments

Comments
 (0)