8000 编写获取数据表信息 · aobics/laravel_template_with_vue@4ef01a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ef01a1

Browse files
committed
编写获取数据表信息
1 parent c9f2264 commit 4ef01a1

File tree

5 files changed

+88
-25
lines changed

5 files changed

+88
-25
lines changed

api/app/Http/Controllers/Admin/LoginController.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ public function login()
9696
public function bind(){
9797
$client_id = request('uuid');
9898
$uid = Auth::id();
99-
Gateway::$registerAddress = env('REGISTER_ADDRESS','127.0.0.1:1680');
100-
99+
$address = env('REGISTER_ADDRESS','127.0.0.1:1680');
100+
Gateway::$registerAddress = $address;
101101
Gateway::bindUid($client_id, $uid);
102102
// 获得所有的client_id,删除除了该次登录的内容以外,剔除其他的客户端,前端自动的退出
103103
$arr = Gateway::getClientIdByUid($uid);
104104
// 获得之前登录的所有client_id
105-
unset($arr[array_search($client_id, $arr)]);
105+
unset($arr[array_search($client_id, $arr)]); // 剔除当前登录的client_id后剩余的client_id内容,保证永远一对一,前端用于剔除之前登录的用户
106+
$arr = array_values($arr); // 此操作非常重要,这样才能保证经过json编码后为数组
106107
$result = [
107108
'type' => 'logout',
108109
'content' => null,
109-
'select' => 'all'
110+
'select' => 'all',
110111
];
111112
Gateway::sendToAll(json_encode($result), $arr);
112-
return $this->success();
113113
}
114114

115115
/**
@@ -133,6 +133,12 @@ public function me()
133133
public function logout()
134134
{
135135
if (Auth::check()){
136+
$id = Auth::id();
137+
$uuid = request('uuid', null);
138+
// 取消client_id与uid的绑定
139+
if ($uuid) {
140+
Gateway::unbindUid($uuid, $id);
141+
}
136142
Auth::user()->token()->delete();
137143
// $admin = Auth::user();
138144
// DB::table('oauth_access_tokens')->where('user_id', $admin->id)->update(['revoked' => 1]);

api/app/Http/Controllers/Admin/TableController.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
namespace App\Http\Controllers\Admin;
55

6+
use Illuminate\Pagination\LengthAwarePaginator;
67
use Illuminate\Support\Facades\DB;
78
use Rap2hpoutre\FastExcel\FastExcel;
89
use Illuminate\Support\Facades\Validator;
9-
use Illuminate\Validation\Rule;
1010
use Illuminate\Http\Request;
1111

1212
class TableController extends Controller
@@ -17,13 +17,57 @@ class TableController extends Controller
1717
protected $resource = 'App\Http\Resources\Table'; // 显示个体资源
1818
protected $resourceCollection = 'App\Http\Resources\TableCollection'; // 显示资源集合
1919
protected $map = []; // 导入导出时候 数据表字段与说明的映射表
20-
20+
protected $systemTable = [
21+
"admin_permissions",
22+
"admin_roles",
23+
"admins",
24+
"article_categories",
25+
"articles",
26+
"carousels",
27+
"failed_jobs",
28+
"logs",
29+
"migrations",
30+
"modules",
31+
"oauth_access_tokens",
32+
"oauth_auth_codes",
33+
"oauth_clients",
34+
"oauth_personal_access_clients",
35+
"oauth_refresh_tokens",
36+
"password_resets",
37+
"permissions",
38+
"role_permissions",
39+
"roles",
40+
"three_logins",
41+
"users",
42+
];
2143

2244
public function index(Request $request)
2345
{
2446
// 显示订单列表
25-
$pageSize = $request->input('pageSize', 10);
26-
return $this->getListData($pageSize);
47+
// $tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
48+
// $existsTable = array_diff($tables, $this->systemTable);
49+
// dd($existsTable);
50+
$dbName = env('DB_DATABASE');
51+
$sql = <<<SQL
52+
SELECT table_name,engine, table_collation, table_comment, create_time
53+
FROM INFORMATION_SCHEMA.TABLES
54+
WHERE TABLE_SCHEMA = '$dbName' and table_comment <> 'VIEW'
55+
SQL;
56+
$sql = str_replace('$dbName', $dbName, $sql);
57+
$tables = DB::connection("super")->select($sql);
58+
$myTable = array_filter($tables, function($table) {
59+
return in_array($table->table_name, $this->systemTable)?false:true;
60+
});
61+
$data = array_values($myTable);
62+
$page = $request->page ?: 1;
63+
//每页的条数
64+
$pageSize = request('pageSize', 10);
65+
//计算每页分页的初始位置
66+
$offset = ($page * $pageSize ) - $pageSize;
67+
$result = new LengthAwarePaginator(array_slice($data, $offset, $pageSize, true), count($data), $pageSize, $page);
68+
return new $this->resourceCollection($result);
69+
// $pageSize = $request->input('pageSize', 10);
70+
// return $this->getListData($pageSize);
2771
}
2872

2973
protected function getListData($pageSize){

api/app/Http/Resources/Table.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ class Table extends JsonResource
1414
*/
1515
public function toArray($request)
1616
{
17-
$data = parent::toArray($request);
18-
$data['created_at'] = $data['created_at'] * 1000;
19-
$data['updated_at'] = $data['updated_at'] * 1000;
20-
// 数据转换
21-
17+
$data = [
18+
'table_name' => $this->table_name,
19+
'engine' => $this->engine,
20+
'table_collation' => $this->table_collation,
21+
'table_comment' => $this->table_comment,
22+
'create_time' => $this->create_time
23+
];
2224
return $data;
2325
}
2426

api/config/database.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@
6262
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
6363
]) : [],
6464
],
65+
'super' => [
66+
'driver' => 'mysql',
67+
'url' => env('DATABASE_URL'),
68+
'host' => env('DB_HOST', '127.0.0.1'),
69+
'port' => env('DB_PORT', '3306'),
70+
'database' => 'INFORMATION_SCHEMA',
71+
'username' => 'root',
72+
'password' => 'Test@123456',
73+
'unix_socket' => env('DB_SOCKET', ''),
74+
'charset' => 'utf8mb4',
75+
'collation' => 'utf8mb4_unicode_ci',
76+
'prefix' => '',
77+
'prefix_indexes' => true,
78+
'strict' => true,
79+
'engine' => null,
80+
'options' => extension_loaded('pdo_mysql') ? array_filter([
81+
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
82+
]) : [],
83+
],
6584

6685
'pgsql' => [
6786
'driver' => 'pgsql',

api/routes/api.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Http\Request;
4+
use Illuminate\Support\Facades\DB;
45

56
/*
67
|--------------------------------------------------------------------------
@@ -32,6 +33,7 @@
3233
Route::get('oauth/test1', 'OauthController@test1')->name('login.test1');
3334
Route::get('oauth/test2', 'OauthController@test2')->name('login.test2');
3435
Route::post('user/bind', "LoginController@bind")->name('login.bind');
36+
Route::apiResource('tables', 'TableController');
3537
});
3638

3739

@@ -59,19 +61,9 @@
5961
// 轮播图
6062
Route::apiResource('carousels', 'CarouselController');
6163

62-
// 聊天室等功能
63-
Route::post('/chat', function(){
64-
$msg = request()->only(['name', 'time', 'timezone', 'content']);
65-
broadcast(new \App\Events\Chat($msg))->toOthers();
66-
})->name('chat.menu');
67-
68-
Route::post('/kefu', function(){
69-
$msg = request()->only(['sendName', 'receiveName', 'time', 'timezone', 'content']);
70-
broadcast(new \App\Events\CustomerService($msg))->toOthers();
71-
})->name('kefu.menu');
7264

7365
// 系统工具 代码生成
74-
Route::apiResource('tables', 'TableController');
66+
7567
Route::apiResource('codes', 'CodeController');
7668
Route::apiResource('code_configs', 'CodeConfigController');
7769
Route::apiResource('code_snippets', 'CodeSnippetController');

0 commit comments

Comments
 (0)
0