TokensController.php
2.75 KB
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
85
<?php
namespace App\Http\Controllers;
use App\Models\Organization;
use App\Models\Tokens;
use App\Service\API;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Inertia\Inertia;
class TokensController extends Controller
{
function index(){
return Inertia::render('Tokens/Index', [
'filters' => Request::all('api', 'type', 'login'),
'types' => [Tokens::MAIN => 'Основной аккаунт', Tokens::GOAL=> 'Целевой аккаунт'],
'tokens' => Tokens::filter(Request::only('api', 'type', 'login'))
->paginate()
->withQueryString()
->through(function ($token) {
return [
'id' => $token->id,
'login' => $token->login,
'type' => $token->type,
'api' => $token->api,
];
}),
]);
}
function edit(Tokens $token){
$mainToken = Tokens::where('type', Tokens::MAIN)->get();
return Inertia::render('Tokens/Edit', [
'token' => [
'id' => $token->id,
'login' => $token->login,
'api' => $token->api,
'token' => $token->token,
'type' => $token->type,
'campaigns' => ($token->type==Tokens::MAIN?[]:null),
'cities' => ($token->type==Tokens::GOAL?[]:null),
],
'types' => $mainToken->count() && $token->type!=Tokens::MAIN
? [Tokens::GOAL=> 'Целевой аккаунт'] :
[Tokens::MAIN => 'Основной аккаунт', Tokens::GOAL=> 'Целевой аккаунт'],
]);
}
public function update(Tokens $token)
{
$token->update(
Request::validate([
'type' => ['required', 'in:' . Tokens::MAIN . "," . Tokens::GOAL],
])
);
return Redirect::back()->with('success', 'Token updated.');
}
public function destroy(Tokens $token)
{
$token->delete();
return Redirect::route('tokens')->with('success', 'Token deleted.');
}
function get($api){
return Inertia::location(API::getInstance($api)->getAuthLink());
}
function token($api){
$token = API::getInstance($api)->getToken(Request::get('code'));
$tokens = Tokens::firstOrNew(['token'=>$token['token']]);
$tokens->token = $token['token'];
$tokens->login = $token['login'];
$tokens->api = $api;
$tokens->created_by = Auth::user()->id;
$tokens->save();
return Redirect::route('tokens')->with('success', 'Token added.');
}
}