Commit 2cc8fc1a by Евгений

Улучшение #19447

Добавление токенов
1 parent e2e0568e
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Models\Organization;
use App\Models\Tokens;
use App\Service\API;
use Illuminate\Support\Facades\Auth;
......@@ -11,6 +12,60 @@ 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());
}
......@@ -25,6 +80,6 @@ class TokensController extends Controller
$tokens->created_by = Auth::user()->id;
$tokens->save();
return Redirect::route('dashboard')->with('success', 'Token added.');
return Redirect::route('tokens')->with('success', 'Token added.');
}
}
......@@ -7,5 +7,19 @@ use Illuminate\Database\Eloquent\Model;
class Tokens extends Model
{
protected $fillable = ['token', 'login'];
CONST MAIN = 'main';
CONST GOAL = 'goal';
protected $fillable = ['token', 'login', 'type'];
public function scopeFilter($query, array $filters)
{
$query->when($filters['login'] ?? null, function ($query, $search) {
$query->where('login', 'like', '%'.$search.'%');
})->when($filters['type'] ?? null, function ($query, $type) {
$query->where('type', $type);
})->when($filters['api'] ?? null, function ($query, $api) {
$query->where('api', $api);
});
}
}
<template>
<div>
<h1 class="mb-8 font-bold text-3xl">
<inertia-link class="text-indigo-400 hover:text-indigo-600" :href="route('tokens')">Tokens</inertia-link>
<span class="text-indigo-400 font-medium">/</span>
{{ form.login }}
</h1>
<div class="bg-white rounded-md shadow overflow-hidden max-w-3xl">
<form @submit.prevent="update">
<div class="p-8 -mr-6 -mb-8 flex flex-wrap">
<text-input v-model="form.login" :error="form.errors.login" :readonly="true" class="pr-6 pb-8 w-full lg:w-1/2"
label="Login"/>
<text-input v-model="form.token" :error="form.errors.token" :readonly="true" class="pr-6 pb-8 w-full lg:w-1/2"
label="Token"/>
<text-input v-model="form.api" :error="form.errors.api" :readonly="true" class="pr-6 pb-8 w-full lg:w-1/2"
label="API"/>
<select-input v-model="form.type" :error="form.errors.type"
:readonly="form.type"
class="pr-6 pb-8 w-full lg:w-1/2"
label="Type">
<option :value="null"/>
<option v-for="type,key in types" :value="key">{{ type }}</option>
</select-input>
</div>
<div class="px-8 py-4 bg-gray-50 border-t border-gray-100 flex items-center">
<button class="text-red-600 hover:underline" tabindex="-1"
type="button" @click="destroy">Delete Token
</button>
<loading-button :loading="form.processing" v-if="!this.token.type" class="btn-indigo ml-auto" type="submit">Update
Token
</loading-button>
</div>
</form>
</div>
<div v-if="token.campaigns">
<h2 class="mt-12 font-bold text-2xl">Campaigns</h2>
<div class="mt-6 bg-white rounded shadow overflow-x-auto">
<table class="w-full whitespace-nowrap">
<tr class="text-left font-bold">
<th class="px-6 pt-6 pb-4">Name</th>
</tr>
<tr v-for="campaign in token.campaigns" :key="campaign.id"
class="hover:bg-gray-100 focus-within:bg-gray-100">
<td class="border-t">
{{ campaign.name }}
</td>
</tr>
<tr v-if="token.campaigns.length === 0">
<td class="border-t px-6 py-4" colspan="4">No campaigns found.</td>
</tr>
</table>
</div>
</div>
<div v-if="token.cities">
<h2 class="mt-12 font-bold text-2xl">Cities</h2>
<div class="mt-6 bg-white rounded shadow overflow-x-auto">
<table class="w-full whitespace-nowrap">
<tr class="text-left font-bold">
<th class="px-6 pt-6 pb-4">Name</th>
</tr>
<tr v-for="city in token.cities" :key="city.id"
class="hover:bg-gray-100 focus-within:bg-gray-100">
<td class="border-t">
{{ city.name }}
</td>
</tr>
<tr v-if="token.cities.length === 0">
<td class="border-t px-6 py-4" colspan="4">No City found.</td>
</tr>
</table>
</div>
</div>
</div>
</template>
<script>
import Icon from '@/Shared/Icon'
import Layout from '@/Shared/Layout'
import TextInput from '@/Shared/TextInput'
import SelectInput from '@/Shared/SelectInput'
import LoadingButton from '@/Shared/LoadingButton'
import TrashedMessage from '@/Shared/TrashedMessage'
export default {
metaInfo() {
return {title: this.form.login}
},
components: {
Icon,
LoadingButton,
SelectInput,
TextInput,
TrashedMessage,
},
layout: Layout,
props: {
token: Object,
types: Object
},
remember: 'form',
data() {
return {
form: this.$inertia.form({
id: this.token.id,
login: this.token.login,
token: this.token.token,
api: this.token.api,
type: this.token.type,
}),
}
},
methods: {
update() {
this.form.post(this.route('token.update', this.token.id))
},
destroy() {
if (confirm('Are you sure you want to delete this token?')) {
this.$inertia.delete(this.route('token.destroy', this.token.id))
}
},
},
}
</script>
<template>
<div>
<h1 class="mb-8 font-bold text-3xl">Tokens</h1>
<div class="mb-6 flex justify-between items-center">
<search-filter v-model="form.login" class="w-full max-w-md mr-4" @reset="reset">
<label class="block text-gray-700">Тип:</label>
<select v-model="form.type" class="mt-1 w-full form-select">
<option :value="null" />
<option v-for="type,key in types" :value="key">{{ type }}</option>
</select>
</search-filter>
<inertia-link class="btn-indigo" :href="route('token.get', {'api': 'yd'})">
<span>Добавить </span>
<span class="hidden md:inline">токен</span>
</inertia-link>
</div>
<div class="bg-white rounded-md shadow overflow-x-auto">
<table class="w-full whitespace-nowrap">
<tr class="text-left font-bold">
<th class="px-6 pt-6 pb-4">Login</th>
<th class="px-6 pt-6 pb-4">АПИ</th>
<th class="px-6 pt-6 pb-4" colspan="2">Тип</th>
</tr>
<tr v-for="token in tokens.data" :key="token.id" class="hover:bg-gray-100 focus-within:bg-gray-100">
<td class="border-t">
<inertia-link class="px-6 py-4 flex items-center focus:text-indigo-500" :href="route('token.edit', token.id)">
{{ token.login }}
</inertia-link>
</td>
<td class="border-t">
<inertia-link class="px-6 py-4 flex items-center" :href="route('token.edit', token.id)" tabindex="-1">
{{ token.api }}
</inertia-link>
</td>
<td class="border-t">
<inertia-link class="px-6 py-4 flex items-center" :href="route('token.edit', token.id)" tabindex="-1">
{{ types[token.type] }}
</inertia-link>
</td>
<td class="border-t w-px">
<inertia-link class="px-4 flex items-center" :href="route('token.edit', token.id)" tabindex="-1">
<icon name="cheveron-right" class="block w-6 h-6 fill-gray-400" />
</inertia-link>
</td>
</tr>
<tr v-if="tokens.data.length === 0">
<td class="border-t px-6 py-4" colspan="4">No tokens found.</td>
</tr>
</table>
</div>
<pagination class="mt-6" :links="tokens.links" />
</div>
</template>
<script>
import Icon from '@/Shared/Icon'
import pickBy from 'lodash/pickBy'
import Layout from '@/Shared/Layout'
import throttle from 'lodash/throttle'
import mapValues from 'lodash/mapValues'
import Pagination from '@/Shared/Pagination'
import SearchFilter from '@/Shared/SearchFilter'
export default {
metaInfo: { title: 'Tokens' },
components: {
Icon,
Pagination,
SearchFilter,
},
layout: Layout,
props: {
tokens: Object,
filters: Object,
types: Object,
},
data() {
return {
form: {
login: this.filters.login,
type: this.filters.type,
},
}
},
watch: {
form: {
handler: throttle(function() {
let query = pickBy(this.form)
this.$inertia.replace(this.route('tokens', Object.keys(query).length ? query : { remember: 'forget' }))
}, 150),
deep: true,
},
},
methods: {
reset() {
this.form = mapValues(this.form, () => null)
},
},
}
</script>
......@@ -7,6 +7,12 @@
</inertia-link>
</div>
<div class="mb-4">
<inertia-link class="flex items-center group py-3" :href="route('tokens')">
<icon name="office" class="w-4 h-4 mr-2" :class="isUrl('tokens') ? 'fill-white' : 'fill-indigo-400 group-hover:fill-white'" />
<div :class="isUrl('tokens') ? 'text-white' : 'text-indigo-300 group-hover:text-white'">Tokens</div>
</inertia-link>
</div>
<div class="mb-4">
<inertia-link class="flex items-center group py-3" :href="route('organizations')">
<icon name="office" class="w-4 h-4 mr-2" :class="isUrl('organizations') ? 'fill-white' : 'fill-indigo-400 group-hover:fill-white'" />
<div :class="isUrl('organizations') ? 'text-white' : 'text-indigo-300 group-hover:text-white'">Organizations</div>
......
<template>
<div>
<label v-if="label" class="form-label" :for="id">{{ label }}:</label>
<input :id="id" ref="input" v-bind="$attrs" class="form-input" :class="{ error: error }" :type="type" :value="value" @input="$emit('input', $event.target.value)" />
<input :id="id" ref="input" :readonly="readonly" v-bind="$attrs" class="form-input" :class="{ error: error }" :type="type" :value="value" @input="$emit('input', $event.target.value)" />
<div v-if="error" class="form-error">{{ error }}</div>
</div>
</template>
......@@ -23,6 +23,7 @@ export default {
value: String,
label: String,
error: String,
readonly: Boolean,
},
methods: {
focus() {
......
......@@ -158,3 +158,17 @@ Route::get('/token/{api}', [TokensController::class, 'token'])
Route::get('/token/{api}/get', [TokensController::class, 'get'])
->name('token.get')
->middleware('auth');
Route::get('tokens', [TokensController::class, 'index'])
->name('tokens')
->middleware('auth');
Route::get('token/edit/{token}', [TokensController::class, 'edit'])
->name('token.edit')
->middleware('auth');
Route::post('token/edit/{token}', [TokensController::class, 'update'])
->name('token.update')
->middleware('auth');
Route::delete('token/delete/{token}', [TokensController::class, 'destroy'])
->name('token.destroy')
->middleware('auth');
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!