Skip to content

Commit 8117092

Browse files
author
qiang.sun
committed
用户积分及Swoole扩展包
1 parent 0fab76e commit 8117092

23 files changed

+18495
-612
lines changed

.phpstorm.meta.php

Lines changed: 672 additions & 540 deletions
Large diffs are not rendered by default.

_ide_helper.php

Lines changed: 15557 additions & 0 deletions
Large diffs are not rendered by default.

app/Http/Controllers/Auth/LoginController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Http\Controllers\Controller;
6+
use App\User;
7+
use GuzzleHttp\Client;
68
use Illuminate\Foundation\Auth\AuthenticatesUsers;
79
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\DB;
811

912
class LoginController extends Controller
1013
{
@@ -62,4 +65,11 @@ public function credentials(Request $request)
6265
}
6366
return $credentials;
6467
}
68+
69+
public function personal()
70+
{
71+
$user = User::where('name', '学院君')->first();
72+
$token = $user->createToken('Users')->accessToken;
73+
dd($token);
74+
}
6575
}

app/Http/Kernel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class Kernel extends HttpKernel
6161
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
6262
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
6363
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
64+
'client' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
65+
'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
66+
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,
6467
];
6568

6669
/**

app/Listeners/UserEventSubscriber.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use App\Events\UserDeleted;
66
use App\Events\UserDeleting;
7+
use App\PointLog;
8+
use Carbon\Carbon;
9+
use Illuminate\Auth\Events\Login;
10+
use Illuminate\Auth\Events\Registered;
11+
use Illuminate\Auth\Events\Verified;
712
use Illuminate\Support\Facades\Log;
813

914
class UserEventSubscriber
@@ -22,6 +27,65 @@ public function onUserDeleted($event) {
2227
Log::info('用户已经删除[' . $event->user->id . ']:' . $event->user->name);
2328
}
2429

30+
/**
31+
* 处理用户注册事件
32+
* @param $event
33+
*/
34+
public function onUserRegistered($event) {
35+
// 用户注册成功后初始积分为30
36+
$event->user->point += PointLog::$OPT_POINT[PointLog::OPT_USER_REGISTER];
37+
$event->user->save();
38+
// 保存积分变更日志
39+
$pointLog = new PointLog();
40+
$pointLog->type = PointLog::OPT_USER_REGISTER;
41+
$pointLog->value = PointLog::$OPT_POINT[PointLog::OPT_USER_REGISTER];
42+
$event->user->pointLogs()->save($pointLog);
43+
}
44+
45+
/**
46+
* 处理验证邮箱事件
47+
* @param $event
48+
*/
49+
public function onEmailVerified($event) {
50+
// 用户验证邮箱后增加20积分
51+
$event->user->point += PointLog::$OPT_POINT[PointLog::OPT_EMAIL_VERIFY];
52+
$event->user->save();
53+
// 保存积分变更日志
54+
$pointLog = new PointLog();
55+
$pointLog->type = PointLog::OPT_EMAIL_VERIFY;
56+
$pointLog->value = PointLog::$OPT_POINT[PointLog::OPT_EMAIL_VERIFY];
57+
$event->user->pointLogs()->save($pointLog);
58+
}
59+
60+
/**
61+
* 处理用户登录事件
62+
* @param $event
63+
*/
64+
public function onUserLogin($event) {
65+
$pointLog = PointLog::where('user_id', $event->user->id)->where('type', PointLog::OPT_USER_LOGIN)->orderBy('created_at', 'desc')->first();
66+
$firstLoginToday = false;
67+
if (!$pointLog) {
68+
// 注册后首次登录
69+
$firstLoginToday = true;
70+
} else {
71+
$lastLoginTime = new Carbon($pointLog->created_at);
72+
if ($lastLoginTime->isYesterday()) {
73+
// 上次登录时间是昨天
74+
$firstLoginToday = true;
75+
}
76+
}
77+
if ($firstLoginToday) {
78+
// 用户每日首次登录成功后增加5积分
79+
$event->user->point += PointLog::$OPT_POINT[PointLog::OPT_USER_LOGIN];
80+
$event->user->save();
81+
// 保存积分变更日志
82+
$pointLog = new PointLog();
83+
$pointLog->type = PointLog::OPT_USER_LOGIN;
84+
$pointLog->value = PointLog::$OPT_POINT[PointLog::OPT_USER_LOGIN];
85+
$event->user->pointLogs()->save($pointLog);
86+
}
87+
}
88+
2589
/**
2690
* 为订阅者注册监听器
2791
*
@@ -38,5 +102,20 @@ public function subscribe($events)
38102
UserDeleted::class,
39103
UserEventSubscriber::class . '@onUserDeleted'
40104
);
105+
106+
$events->listen(
107+
Registered::class,
108+
UserEventSubscriber::class . '@onUserRegistered'
109+
);
110+
111+
$events->listen(
112+
Verified::class,
113+
UserEventSubscriber::class . '@onEmailVerified'
114+
);
115+
116+
$events->listen(
117+
Login::class,
118+
UserEventSubscriber::class . '@onUserLogin'
119+
);
41120
}
42121
}

app/PointLog.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class PointLog extends Model
8+
{
9+
const OPT_USER_REGISTER = 1; // 用户注册
10+
const OPT_EMAIL_VERIFY = 2; // 邮箱验证
11+
const OPT_USER_LOGIN = 3; // 用户登录
12+
13+
// 不同操作对应积分映射关系
14+
public static $OPT_POINT = [
15+
self::OPT_USER_REGISTER => 30,
16+
self::OPT_EMAIL_VERIFY => 20,
17+
self::OPT_USER_LOGIN => 5
18+
];
19+
}

app/Providers/AuthServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ public function boot()
3333
return new EloquentUserProvider($app->make('hash'), $config['model']);
3434
});
3535

36+
// OAuth 相关路由
3637
Passport::routes();
38+
39+
// 启用隐式授权令牌
40+
Passport::enableImplicitGrant();
41+
42+
// 令牌作用域
43+
Passport::tokensCan([
44+
'basic-user-info' => '获取用户名、邮箱信息',
45+
'all-user-info' => '获取用户所有信息',
46+
'get-post-info' => '获取文章详细信息',
47+
]);
3748
}
3849

3950
}

app/User.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44

55
use App\Events\UserDeleted;
66
use App\Events\UserDeleting;
7-
use App\Scopes\EmailVerifiedAtScope;
8-
use Illuminate\Database\Eloquent\Builder;
9-
use Illuminate\Notifications\Notifiable;
107
use Illuminate\Contracts\Auth\MustVerifyEmail;
8+
use Illuminate\Notifications\Notifiable;
119
use Illuminate\Foundation\Auth\User as Authenticatable;
1210
use Laravel\Passport\HasApiTokens;
1311

14-
class User extends Authenticatable
12+
class User extends Authenticatable implements MustVerifyEmail
1513
{
16-
use HasApiTokens, Notifiable;
14+
use HasApiTokens, Notifiable, \Illuminate\Auth\MustVerifyEmail;
1715

1816
/**
1917
* The attributes that are mass assignable.
@@ -87,4 +85,9 @@ public function image()
8785
{
8886
return $this->morphOne(Image::class, 'imageable');
8987
}
88+
89+
public function pointLogs()
90+
{
91+
return $this->hasMany(PointLog::class);
92+
}
9093
}

bin/fswatch

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
WORK_DIR=$1
3+
if [ ! -n "${WORK_DIR}" ] ;then
4+
WORK_DIR="."
5+
fi
6+
7+
echo "Restarting LaravelS..."
8+
./bin/laravels restart -d -i
9+
10+
echo "Starting fswatch..."
11+
LOCKING=0
12+
fswatch -e ".*" -i "\\.php$" ${WORK_DIR} | while read file
13+
do
14+
if [ ${LOCKING} -eq 1 ] ;then
15+
echo "Reloading, skipped."
16+
continue
17+
fi
18+
echo "File ${file} has been modified."
19+
LOCKING=1
20+
./bin/laravels reload
21+
LOCKING=0
22+
done
23+
exit 0

bin/laravels

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env php
2+
<?php
3+
$basePath = realpath(__DIR__ . '/../');
4+
include $basePath . '/vendor/autoload.php';
5+
6+
$command = new Hhxsv5\LaravelS\Console\Portal($basePath);
7+
$input = new Symfony\Component\Console\Input\ArgvInput();
8+
$output = new Symfony\Component\Console\Output\ConsoleOutput();
9+
10+
$code = $command->run($input, $output);
11+
exit($code);

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"type": "project",
77
"require": {
88
"php": "^7.1.3",
9+
"barryvdh/laravel-ide-helper": "^2.5",
910
"doctrine/dbal": "^2.8",
1011
"fideloper/proxy": "^4.0",
1112
"garygreen/pretty-routes": "^1.0",
13+
"hhxsv5/laravel-s": "~3.3",
1214
"laravel/framework": "5.7.*",
1315
"laravel/passport": "^7.0",
1416
"laravel/tinker": "^1.0",

0 commit comments

Comments
 (0)