QR_code generator
parent
78384fb7a3
commit
8dd012d79d
|
|
@ -54,6 +54,10 @@ class FormsController extends Controller {
|
|||
return FormsService::getService('feedback-form-support')->save($request->all());
|
||||
}
|
||||
|
||||
public function saveQrCodeForm(Request $request): ?JsonResponse {
|
||||
return FormsService::getService('QrCode')->save($request->all());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function filters(Request $request, $type) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\QrCodes;
|
||||
|
||||
class ColorType
|
||||
{
|
||||
public const EPS = 'solid';
|
||||
public const SVG = 'gradient';
|
||||
|
||||
public const TITLES = [
|
||||
self::EPS => 'Сплошной',
|
||||
self::SVG => 'Градиент'
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\QrCodes;
|
||||
|
||||
use App\Models\Asset;
|
||||
use App\Support\HasObjectsTrait;
|
||||
use App\Support\RelationValuesTrait;
|
||||
use App\Support\UuidScopeTrait;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class QrCode extends Model{
|
||||
use UuidScopeTrait, SoftDeletes, HasObjectsTrait, RelationValuesTrait;
|
||||
|
||||
protected $dates = [
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'uuid',
|
||||
'text',
|
||||
'format',
|
||||
'size',
|
||||
'color',
|
||||
'bg_color',
|
||||
'margin',
|
||||
'image_id',
|
||||
'document_id'
|
||||
];
|
||||
|
||||
|
||||
protected $hidden = [
|
||||
'id'
|
||||
];
|
||||
|
||||
public function image(): BelongsTo {
|
||||
return $this->belongsTo(Asset::class);
|
||||
}
|
||||
|
||||
public function document(): BelongsTo {
|
||||
return $this->belongsTo(Asset::class);
|
||||
}
|
||||
|
||||
public function getParsedFormatAttribute(): array {
|
||||
return ['name' => $this->format, 'title' => QrCodeFormat::TITLES[$this->format] ?? null];
|
||||
}
|
||||
|
||||
public function setImage($val) {
|
||||
$asset = Asset::byUuid($val)->first();
|
||||
$this->update(['image_id' => $asset->id ?? null]);
|
||||
}
|
||||
public function setDocument($val) {
|
||||
$asset = Asset::byUuid($val)->first();
|
||||
$this->update(['document_id' => $asset->id ?? null]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\QrCodes;
|
||||
|
||||
class QrCodeFormat
|
||||
{
|
||||
public const PNG = 'png';
|
||||
public const EPS = 'eps';
|
||||
public const SVG = 'svg';
|
||||
|
||||
public const TITLES = [
|
||||
self::PNG => '*.png',
|
||||
self::EPS => '*.eps',
|
||||
self::SVG => '*.svg'
|
||||
];
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ use App\Services\Forms\Companies\CompanyFormsServices;
|
|||
use App\Services\Forms\Feedback\FeedbackFormsServices;
|
||||
use App\Services\Forms\Pages\PageFormsServices;
|
||||
use App\Services\Forms\Publications\PublicationFormsServices;
|
||||
use App\Services\Forms\QrCode\QrCodeFormsServices;
|
||||
use App\Services\Forms\Registries\RegistryFormsServices;
|
||||
use App\Services\Forms\Users\UserFormsServices;
|
||||
|
||||
|
|
@ -23,7 +24,8 @@ class FormsService {
|
|||
FeedbackFormsServices::class,
|
||||
AdvisoryFormsServices::class,
|
||||
CompanyFormsServices::class,
|
||||
ApplicationFormsServices::class
|
||||
ApplicationFormsServices::class,
|
||||
QrCodeFormsServices::class
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Forms\QrCode;
|
||||
|
||||
use App\Models\Asset;
|
||||
use App\Models\Objects\FieldType;
|
||||
use App\Models\QrCodes\ColorType;
|
||||
use App\Models\QrCodes\QrCode;
|
||||
use App\Models\QrCodes\QrCodeFormat;
|
||||
use App\Services\Forms\FormsService;
|
||||
use App\Services\QrCodeService;
|
||||
use App\Transformers\Assets\AssetTransformer;
|
||||
use App\Transformers\QrCodes\QrCodeTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use SimpleSoftwareIO\QrCode\Facades\QrCode as QR;
|
||||
|
||||
class QrCodeForms extends FormsService
|
||||
{
|
||||
public array $formTitles = ['create' => '', 'update' => ''];
|
||||
|
||||
public function form(?string $id = null, array $data = []): array {
|
||||
$model = QrCode::byUuid($id)->first();
|
||||
$groups = [
|
||||
['name' => 'common', 'fields' => $this->commonGroupFields($model)]
|
||||
];
|
||||
return ['title' => $this->formTitle($model), 'data' => $groups];
|
||||
}
|
||||
|
||||
public function commonGroupFields(?QrCode $model): array {
|
||||
$fields = [
|
||||
[
|
||||
'name' => 'format',
|
||||
'title' => 'Формат документа',
|
||||
'type' => FieldType::RELATION,
|
||||
'appearance' => 'radio',
|
||||
'options' => $this->getRelationItems(QrCodeFormat::TITLES),
|
||||
],
|
||||
[
|
||||
'name' => 'color_type',
|
||||
'title' => 'Тип заливки',
|
||||
'type' => FieldType::RELATION,
|
||||
'appearance' => 'radio',
|
||||
'options' => $this->getRelationItems(ColorType::TITLES),
|
||||
],
|
||||
[
|
||||
'name' => 'custom_eye_color',
|
||||
'title' => 'Цвет меток',
|
||||
'type' => FieldType::BOOLEAN,
|
||||
],
|
||||
[
|
||||
'name' => 'text',
|
||||
'title' => 'Текст',
|
||||
'type' => FieldType::TEXT,
|
||||
'required' => true,
|
||||
],
|
||||
[
|
||||
'name' => 'size',
|
||||
'title' => 'Размер',
|
||||
'type' => FieldType::INTEGER,
|
||||
],
|
||||
[
|
||||
'name' => 'red',
|
||||
'title' => 'Красный',
|
||||
'type' => FieldType::INTEGER,
|
||||
], [
|
||||
'name' => 'green',
|
||||
'title' => 'Зеленый',
|
||||
'type' => FieldType::INTEGER,
|
||||
], [
|
||||
'name' => 'blue',
|
||||
'title' => 'Синий',
|
||||
'type' => FieldType::INTEGER,
|
||||
],
|
||||
[
|
||||
'name' => 'bg_red',
|
||||
'title' => 'Фон красный',
|
||||
'type' => FieldType::INTEGER,
|
||||
],
|
||||
[
|
||||
'name' => 'bg_green',
|
||||
'title' => 'Фон зеленый',
|
||||
'type' => FieldType::INTEGER,
|
||||
], [
|
||||
'name' => 'bg_blue',
|
||||
'title' => 'Фон синий',
|
||||
'type' => FieldType::INTEGER,
|
||||
],
|
||||
[
|
||||
'name' => 'margin',
|
||||
'title' => 'Отступы',
|
||||
'type' => FieldType::INTEGER,
|
||||
'value' => $model->margin ?? null
|
||||
],
|
||||
[
|
||||
'name' => 'image',
|
||||
'title' => 'Изображение',
|
||||
'type' => FieldType::IMAGE,
|
||||
'value' => ($model->image ?? null) ? fractal($model->image, new AssetTransformer()) : null
|
||||
],
|
||||
|
||||
];
|
||||
return ['data' => $fields];
|
||||
}
|
||||
|
||||
public function store(array $data): JsonResponse {
|
||||
$model = QrCode::create();
|
||||
$this->updateData($model, $data);
|
||||
return fractal($model, new QrCodeTransformer())->respond(201);
|
||||
}
|
||||
|
||||
public function updateData(QrCode $model, $data) {
|
||||
$model->update(collect($data)->only('text', 'format', 'size', 'color', 'bg_color', 'margin')->all());
|
||||
$data['document'] = $this->generate($data);
|
||||
$model->setImage($data['image'] ?? null);
|
||||
$model->setDocument($data['document'] ?? null);
|
||||
}
|
||||
|
||||
public function generate($data){
|
||||
$path = Storage::path((new QrCodeService)->makeFilePath($data['format']));
|
||||
$QrCode = QR::errorCorrection('H')->format($data['format'])->size($data['size'])->backgroundColor($data['bg_red'], $data['bg_green'], $data['bg_blue']);
|
||||
if ($data['image'] != null) $QrCode->merge(Storage::path($data['image']->path), .27, true);
|
||||
if ($data['color_type'] == 'gradient'){
|
||||
$QrCode->gradient($data['red'], $data['green'], $data['blue'], 255,255,255, 'vertical');
|
||||
}
|
||||
if ($data['color_type'] == 'gradient'){
|
||||
$QrCode->gradient($data['red'], $data['green'], $data['blue'], 255,255,255, 'vertical');
|
||||
}
|
||||
else $QrCode->color($data['red'],$data['green'],$data['blue']);
|
||||
if ($data['custom_eye_color']) {
|
||||
$QrCode->eyeColor(0, $data['red'], $data['green'], $data['blue'], $data['red'], $data['green'], $data['blue']);
|
||||
$QrCode->eyeColor(1, $data['red'], $data['green'], $data['blue'], $data['red'], $data['green'], $data['blue']);
|
||||
$QrCode->eyeColor(2, $data['red'], $data['green'], $data['blue'], $data['red'], $data['green'], $data['blue']);
|
||||
}
|
||||
$QrCode->style('square');
|
||||
$QrCode->eye('square');
|
||||
$QrCode->generate($data['text'],$path);
|
||||
return (new QrCodeService)->makeAsset($path,null);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Forms\QrCode;
|
||||
|
||||
class QrCodeFormsServices
|
||||
{
|
||||
public static array $services = [
|
||||
'QrCode' => QrCodeForms::class
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class QrCodeService
|
||||
{
|
||||
|
||||
|
||||
public function makeFilePath($ext): string {
|
||||
$fileName = Str::lower(Str::random());
|
||||
$dir = "documents/generated/{$fileName[0]}";
|
||||
Storage::makeDirectory($dir);
|
||||
return "{$dir}/{$fileName}.{$ext}";
|
||||
}
|
||||
|
||||
public function makeAsset($path, $name) {
|
||||
return (new FileDownloadService())->makeAsset($path, $name);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace App\Transformers\QrCodes;
|
||||
|
||||
use App\Models\QrCodes\QrCode;
|
||||
use App\Transformers\Assets\AssetTransformer;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class QrCodeTransformer extends TransformerAbstract{
|
||||
protected array $defaultIncludes = [];
|
||||
|
||||
protected array $availableIncludes = [
|
||||
'image', 'document'
|
||||
];
|
||||
|
||||
public function transform(QrCode $model): array {
|
||||
return [
|
||||
'id' => $model->uuid,
|
||||
'format' => $model->parsedFormat,
|
||||
'text' => $model->text,
|
||||
'size' => $model->size,
|
||||
'color' => $model->color,
|
||||
'bg_color' => $model->bg_color,
|
||||
'margin' => $model->phone
|
||||
];
|
||||
}
|
||||
|
||||
public function includeImage(QrCode $model): ?Item {
|
||||
return $model->image ? $this->item($model->image, new AssetTransformer()) : null;
|
||||
}
|
||||
|
||||
public function includeDocument(QrCode $model): ?Item {
|
||||
return $model->document ? $this->item($model->document, new AssetTransformer()) : null;
|
||||
}
|
||||
}
|
||||
|
|
@ -39,7 +39,8 @@
|
|||
"seyyedam7/laravel-html-parser": "^3.1",
|
||||
"spatie/laravel-fractal": "^5.8",
|
||||
"spatie/laravel-permission": "^3.17",
|
||||
"tomlerendu/laravel-convert-case-middleware": "^1.1"
|
||||
"tomlerendu/laravel-convert-case-middleware": "^1.1",
|
||||
"simplesoftwareio/simple-qrcode": "~4"
|
||||
},
|
||||
"require-dev": {
|
||||
"brianium/paratest": "^6.2",
|
||||
|
|
|
|||
|
|
@ -151,6 +151,7 @@ return [
|
|||
App\Providers\MessengerServiceProvider::class,
|
||||
App\Providers\RouteServiceProvider::class,
|
||||
Barryvdh\DomPDF\ServiceProvider::class,
|
||||
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class
|
||||
],
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
@ -199,5 +200,6 @@ return [
|
|||
'Validator' => Illuminate\Support\Facades\Validator::class,
|
||||
'View' => Illuminate\Support\Facades\View::class,
|
||||
'PDF' => Barryvdh\DomPDF\Facade::class,
|
||||
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
|
||||
],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateQrCodesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('qr_codes', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->char('uuid', 36)->index()->unique();
|
||||
$table->string('text')->nullable()->index();
|
||||
$table->string('format')->nullable()->index();
|
||||
$table->integer('size')->nullable()->index();
|
||||
$table->string('color')->nullable()->index();
|
||||
$table->string('bg_color')->nullable()->index();
|
||||
$table->integer('margin')->nullable()->index();
|
||||
$table->integer('image_id')->nullable();
|
||||
$table->integer('document_id')->nullable();
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('qr_codes');
|
||||
}
|
||||
}
|
||||
|
|
@ -78,6 +78,7 @@ Route::group(['prefix' => 'registries'], function() {
|
|||
Route::group(['prefix' => 'forms'], function() {
|
||||
Route::get('/object/feedback-form-support', 'Api\Forms\FormsController@getFeedbackFormSupport');
|
||||
Route::post('/model/feedback-form-support', 'Api\Forms\FormsController@saveFeedbackFormSupport');
|
||||
Route::post('/model/QrCode', 'Api\Forms\FormsController@saveQrCodeForm');
|
||||
Route::group(['middleware' => ['auth:api']], function() {
|
||||
Route::get('/{target}/{type?}/{id?}', 'Api\Forms\FormsController@get');
|
||||
Route::post('/{target}/{type?}/{id?}', 'Api\Forms\FormsController@save');
|
||||
|
|
|
|||
Loading…
Reference in New Issue