master
Andrey 2024-09-02 10:50:46 +03:00
parent 8dd012d79d
commit 77ed10609d
6 changed files with 178 additions and 77 deletions

View File

@ -9,6 +9,7 @@ use App\Services\Filters\FiltersService;
use App\Services\Forms\FormsService; use App\Services\Forms\FormsService;
use App\Transformers\Objects\ObjectTransformer; use App\Transformers\Objects\ObjectTransformer;
use App\Transformers\Objects\ObjectTypeTransformer; use App\Transformers\Objects\ObjectTypeTransformer;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -54,6 +55,13 @@ class FormsController extends Controller {
return FormsService::getService('feedback-form-support')->save($request->all()); return FormsService::getService('feedback-form-support')->save($request->all());
} }
public function getQrCodeForm(Request $request, $target, $type = null, $id = null){
if ($target === 'model') {
$service = FormsService::getService($type);
return $service->form($id, json_decode($request->get('extra_props'), true) ?? []);
}
}
public function saveQrCodeForm(Request $request): ?JsonResponse { public function saveQrCodeForm(Request $request): ?JsonResponse {
return FormsService::getService('QrCode')->save($request->all()); return FormsService::getService('QrCode')->save($request->all());
} }

View File

@ -4,11 +4,11 @@ namespace App\Models\QrCodes;
class ColorType class ColorType
{ {
public const EPS = 'solid'; public const SOLID = 'solid';
public const SVG = 'gradient'; public const GRADIENT = 'gradient';
public const TITLES = [ public const TITLES = [
self::EPS => 'Сплошной', self::SOLID => 'Сплошной',
self::SVG => 'Градиент' self::GRADIENT => 'Градиент'
]; ];
} }

View File

@ -0,0 +1,20 @@
<?php
namespace App\Models\QrCodes;
class GradientType
{
public const VERTICAL = 'vertical';
public const HORIZONTAL = 'horizontal';
public const DIAGONAL = 'diagonal';
public const INVERSE_DIAGONAL = 'inverse_diagonal';
public const RADIAL = 'radial';
public const TITLES = [
self::VERTICAL => 'Вертикальный',
self::HORIZONTAL => 'Горизонтальный',
self::DIAGONAL => 'Диагональ сверху вниз',
self::INVERSE_DIAGONAL => 'Диагональ снизу вверх',
self::RADIAL => 'Крулый'
];
}

View File

@ -5,6 +5,7 @@ namespace App\Services\Forms\QrCode;
use App\Models\Asset; use App\Models\Asset;
use App\Models\Objects\FieldType; use App\Models\Objects\FieldType;
use App\Models\QrCodes\ColorType; use App\Models\QrCodes\ColorType;
use App\Models\QrCodes\GradientType;
use App\Models\QrCodes\QrCode; use App\Models\QrCodes\QrCode;
use App\Models\QrCodes\QrCodeFormat; use App\Models\QrCodes\QrCodeFormat;
use App\Services\Forms\FormsService; use App\Services\Forms\FormsService;
@ -22,83 +23,72 @@ class QrCodeForms extends FormsService
public function form(?string $id = null, array $data = []): array { public function form(?string $id = null, array $data = []): array {
$model = QrCode::byUuid($id)->first(); $model = QrCode::byUuid($id)->first();
$groups = [ $groups = [
['name' => 'common', 'fields' => $this->commonGroupFields($model)] [
'name' => 'common', 'fields' => $this->commonGroupFields($model),
'dynamic' => [
['field' => 'color_type', 'show' => ['red', 'green', 'blue'], 'hide' => ['start_red', 'start_green', 'start_blue', 'end_red', 'end_green', 'end_blue', 'gradient_type']],
['field' => 'color_type', 'value' => ColorType::GRADIENT ,'show' => ['start_red', 'start_green', 'start_blue', 'end_red', 'end_green', 'end_blue', 'gradient_type'], 'hide' => ['red', 'green', 'blue']],
['field' => 'color_type', 'value' => ColorType::GRADIENT ,'show' => ['start_red', 'start_green', 'start_blue', 'end_red', 'end_green', 'end_blue', 'gradient_type'], 'hide' => ['red', 'green', 'blue']],
['field' => 'custom_eye_color', 'hide' => ['inner_red', 'inner_green', 'inner_blue', 'outside_red', 'outside_green', 'outside_blue']],
['field' => 'custom_eye_color', 'value' => true, 'show' => ['inner_red', 'inner_green', 'inner_blue', 'outside_red', 'outside_green', 'outside_blue']],
]
],
]; ];
return ['title' => $this->formTitle($model), 'data' => $groups]; return ['title' => $this->formTitle($model), 'data' => $groups];
} }
public function commonGroupFields(?QrCode $model): array { public function commonGroupFields(?QrCode $model): array {
$fields = [ $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', 'name' => 'text',
'title' => 'Текст', 'title' => 'Текст',
'type' => FieldType::TEXT, 'type' => FieldType::TEXT,
'required' => true, 'required' => true,
], ],
[
'name' => 'format',
'title' => 'Формат документа',
'type' => FieldType::RELATION,
'appearance' => 'radio',
'options' => $this->getRelationItems(QrCodeFormat::TITLES),
'value' => $this->getRelationValue(QrCodeFormat::TITLES, $model->format ?? QrCodeFormat::PNG)
],
[ [
'name' => 'size', 'name' => 'size',
'title' => 'Размер', 'title' => 'Размер',
'type' => FieldType::INTEGER, 'type' => FieldType::INTEGER,
], ],
[ // [
'name' => 'red', // 'name' => 'margin',
'title' => 'Красный', // 'title' => 'Отступы',
'type' => FieldType::INTEGER, // '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', 'name' => 'image',
'title' => 'Изображение', 'title' => 'Изображение',
'type' => FieldType::IMAGE, 'type' => FieldType::IMAGE,
'value' => ($model->image ?? null) ? fractal($model->image, new AssetTransformer()) : null 'value' => ($model->image ?? null) ? fractal($model->image, new AssetTransformer()) : null
], ],
[
'name' => 'color-type',
'title' => 'Тип заливки',
'type' => FieldType::RELATION,
'appearance' => 'radio',
'options' => $this->getRelationItems(ColorType::TITLES),
'value' => $this->getRelationValue(ColorType::TITLES, $model->color ?? ColorType::SOLID)
],
[
'name' => 'custom-eye-color',
'title' => 'Цвет меток',
'type' => FieldType::BOOLEAN,
],
[
'name' => 'gradient-type',
'title' => 'Тип градиента',
'type' => FieldType::RELATION,
'options' => $this->getRelationItems(GradientType::TITLES),
]
]; ];
return ['data' => $fields]; return ['data' => $fields];
} }
@ -106,35 +96,117 @@ class QrCodeForms extends FormsService
public function store(array $data): JsonResponse { public function store(array $data): JsonResponse {
$model = QrCode::create(); $model = QrCode::create();
$this->updateData($model, $data); $this->updateData($model, $data);
// $this->decode();
return fractal($model, new QrCodeTransformer())->respond(201); return fractal($model, new QrCodeTransformer())->respond(201);
} }
public function updateData(QrCode $model, $data) { public function updateData(QrCode $model, $data) {
$model->update(collect($data)->only('text', 'format', 'size', 'color', 'bg_color', 'margin')->all()); $model->update(collect($data)->only('text', 'format', 'size', 'color', 'bg_color', 'margin')->all());
$data['document'] = $this->generate($data);
$model->setImage($data['image'] ?? null); $model->setImage($data['image'] ?? null);
$data['document'] = $this->generate($model, $data);
$model->setDocument($data['document'] ?? null); $model->setDocument($data['document'] ?? null);
} }
public function generate($data){ function hexToRgb($hex, $return_string = false)
{
$hex = trim($hex, ' #');
$size = strlen($hex);
if ($size == 3 || $size == 4) {
$parts = str_split($hex, 1);
$hex = '';
foreach ($parts as $row) {
$hex .= $row . $row;
}
}
$dec = hexdec($hex);
$rgb = array();
if ($size == 3 || $size == 6) {
$rgb['red'] = 0xFF & ($dec >> 0x10);
$rgb['green'] = 0xFF & ($dec >> 0x8);
$rgb['blue'] = 0xFF & $dec;
if ($return_string) {
return 'rgb(' . implode(',', $rgb) . ')';
}
} elseif ($size == 4 || $size == 8) {
$rgb['red'] = 0xFF & ($dec >> 0x16);
$rgb['green'] = 0xFF & ($dec >> 0x10);
$rgb['blue'] = 0xFF & ($dec >> 0x8);
$rgb['alpha'] = 0xFF & $dec;
if ($return_string) {
$rgb['alpha'] = round(($rgb['alpha'] / (255 / 100)) / 100, 2);
return 'rgba(' . implode(',', $rgb) . ')';
} else {
$rgb['alpha'] = 127 - ($rgb['alpha'] >> 1);
}
} else {
return false;
}
return $rgb;
}
public function generate(QrCode $model, $data){
$path = Storage::path((new QrCodeService)->makeFilePath($data['format'])); $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); $QrCode = QR::errorCorrection('H')->format($data['format'] ?? 'png')->size($data['size'] ?? 200)->backgroundColor(
if ($data['color_type'] == 'gradient'){ $this->hexToRgb($data['background-color'])['red'],
$QrCode->gradient($data['red'], $data['green'], $data['blue'], 255,255,255, 'vertical'); $this->hexToRgb($data['background-color'])['green'],
$this->hexToRgb($data['background-color'])['blue']);
if ($model->image != null) {
$QrCode->merge(Storage::path(Asset::byUuid($data['image'])->first()->path), .27, true);
} }
if ($data['color_type'] == 'gradient'){ if ($data['color-type'] == 'gradient'){
$QrCode->gradient($data['red'], $data['green'], $data['blue'], 255,255,255, 'vertical'); $QrCode->gradient(
$this->hexToRgb($data['gradient-start'])['red'],
$this->hexToRgb($data['gradient-start'])['green'],
$this->hexToRgb($data['gradient-start'])['blue'],
$this->hexToRgb($data['gradient-end'])['red'],
$this->hexToRgb($data['gradient-end'])['green'],
$this->hexToRgb($data['gradient-end'])['blue'],
$data['gradient-type']);
} }
else $QrCode->color($data['red'],$data['green'],$data['blue']); else $QrCode->color(
if ($data['custom_eye_color']) { $this->hexToRgb($data['foreground-color'])['red'],
$QrCode->eyeColor(0, $data['red'], $data['green'], $data['blue'], $data['red'], $data['green'], $data['blue']); $this->hexToRgb($data['foreground-color'])['green'],
$QrCode->eyeColor(1, $data['red'], $data['green'], $data['blue'], $data['red'], $data['green'], $data['blue']); $this->hexToRgb($data['foreground-color'])['blue']);
$QrCode->eyeColor(2, $data['red'], $data['green'], $data['blue'], $data['red'], $data['green'], $data['blue']); if ($data['custom-eye-color']) {
$QrCode->eyeColor(0,
$this->hexToRgb($data['inner-eye-color'])['red'],
$this->hexToRgb($data['inner-eye-color'])['green'],
$this->hexToRgb($data['inner-eye-color'])['blue'],
$this->hexToRgb($data['outside-eye-color'])['red'],
$this->hexToRgb($data['outside-eye-color'])['green'],
$this->hexToRgb($data['outside-eye-color'])['blue']);
$QrCode->eyeColor(1,
$this->hexToRgb($data['inner-eye-color'])['red'],
$this->hexToRgb($data['inner-eye-color'])['green'],
$this->hexToRgb($data['inner-eye-color'])['blue'],
$this->hexToRgb($data['outside-eye-color'])['red'],
$this->hexToRgb($data['outside-eye-color'])['green'],
$this->hexToRgb($data['outside-eye-color'])['blue']);
$QrCode->eyeColor(2,
$this->hexToRgb($data['inner-eye-color'])['red'],
$this->hexToRgb($data['inner-eye-color'])['green'],
$this->hexToRgb($data['inner-eye-color'])['blue'],
$this->hexToRgb($data['outside-eye-color'])['red'],
$this->hexToRgb($data['outside-eye-color'])['green'],
$this->hexToRgb($data['outside-eye-color'])['blue']);
} }
$QrCode->style('square'); $QrCode->style('round');
$QrCode->eye('square'); $QrCode->eye('circle');
// $data['text'] = $this->decode();
$QrCode->generate($data['text'],$path); $QrCode->generate($data['text'],$path);
return (new QrCodeService)->makeAsset($path,null); return (new QrCodeService)->makeAsset($path,null)->id;
}
public function decode(){
$QRCodeReader = new \Libern\QRCodeReader\QRCodeReader();
$qrcode_text = $QRCodeReader->decode(Storage::path('documents\generated\8\8u4fwrybtgb06kf9.svg'));
var_dump($qrcode_text);
return $qrcode_text;
} }
} }

View File

@ -22,7 +22,7 @@ class QrCodeTransformer extends TransformerAbstract{
'size' => $model->size, 'size' => $model->size,
'color' => $model->color, 'color' => $model->color,
'bg_color' => $model->bg_color, 'bg_color' => $model->bg_color,
'margin' => $model->phone 'margin' => $model->margin
]; ];
} }

View File

@ -32,15 +32,16 @@
"laravel/passport": "^10.0", "laravel/passport": "^10.0",
"laravel/socialite": "^5.1", "laravel/socialite": "^5.1",
"laravel/tinker": "^2.0", "laravel/tinker": "^2.0",
"libern/qr-code-reader": "^1.0",
"maatwebsite/excel": "^3.1", "maatwebsite/excel": "^3.1",
"movemoveapp/laravel-dadata": "^1.0", "movemoveapp/laravel-dadata": "^1.0",
"phpoffice/phpword": "^1.0", "phpoffice/phpword": "^1.0",
"rtippin/messenger": "^1.19", "rtippin/messenger": "^1.19",
"seyyedam7/laravel-html-parser": "^3.1", "seyyedam7/laravel-html-parser": "^3.1",
"simplesoftwareio/simple-qrcode": "~4",
"spatie/laravel-fractal": "^5.8", "spatie/laravel-fractal": "^5.8",
"spatie/laravel-permission": "^3.17", "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": { "require-dev": {
"brianium/paratest": "^6.2", "brianium/paratest": "^6.2",