-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBootstrap.php
84 lines (80 loc) · 3.19 KB
/
Bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\user;
use Yii;
use yii\web\GroupUrlRule;
use yii\i18n\PhpMessageSource;
use yii\base\BootstrapInterface;
use yuncms\user\jobs\LastVisitJob;
/**
* Class Bootstrap
* @package yuncms/user
*/
class Bootstrap implements BootstrapInterface
{
/**
* 初始化
* @param \yii\base\Application $app
* @throws \yii\base\InvalidConfigException
*/
public function bootstrap($app)
{
if ($app instanceof \yii\console\Application) {
$app->controllerMap['user'] = [
'class' => 'yuncms\user\console\UserController',
];
} else if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) {
if ($module instanceof \yuncms\user\frontend\Module) {//前台判断放最后
Yii::$container->set('yii\web\User', [
'enableAutoLogin' => true,
'loginUrl' => ['/user/security/login'],
'identityClass' => 'yuncms\user\models\User',
'identityCookie' => ['name' => '_identity_frontend', 'httpOnly' => true],
'idParam' => '_user',
]);
$configUrlRule = [
'prefix' => $module->urlPrefix,
'rules' => $module->urlRules,
];
if ($module->urlPrefix != 'user') {
$configUrlRule['routePrefix'] = 'user';
}
$app->urlManager->addRules([new GroupUrlRule($configUrlRule)], false);
//监听用户登录事件
/** @var \yii\web\UserEvent $event */
$app->user->on(\yii\web\User::EVENT_AFTER_LOGIN, function ($event) {
//记录最后登录时间记录最后登录IP记录登录次数
$event->identity->resetLoginData();
});
//设置用户所在时区
// $app->on(\yii\web\Application::EVENT_BEFORE_REQUEST, function ($event) use ($app) {
// if (!$app->user->isGuest && $app->user->identity->profile->timezone) {
// $app->setTimeZone($app->user->identity->profile->timezone);
// }
// });
}
//监听用户活动时间
/** @var \yii\web\UserEvent $event */
$app->on(\yii\web\Application::EVENT_AFTER_REQUEST, function ($event) use ($app) {
if (!$app->user->isGuest && Yii::$app->has('queue')) {
//记录最后活动时间
Yii::$app->queue->push(new LastVisitJob(['user_id' => $app->user->identity->id, 'time' => time()]));
}
});
}
/**
* 注册语言包
*/
if (!isset($app->get('i18n')->translations['user*'])) {
$app->get('i18n')->translations['user*'] = [
'class' => PhpMessageSource::className(),
'sourceLanguage' => 'en-US',
'basePath' => __DIR__ . '/messages',
];
}
}
}