ultimatepos/Modules/Portal/Http/Controllers/PwaController.php

91 lines
2.7 KiB
PHP

<?php
namespace Modules\Portal\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class PwaController extends Controller
{
public function manifest(Request $request, string $type)
{
$names = [
'customer' => __('portal::lang.customer_portal'),
'supplier' => __('portal::lang.supplier_portal'),
'marketer' => __('portal::lang.marketer_portal'),
];
$startUrls = [
'customer' => url('/customer/dashboard'),
'supplier' => url('/supplier/dashboard'),
'marketer' => url('/marketer/dashboard'),
];
$colors = [
'customer' => '#3c8dbc',
'supplier' => '#00a65a',
'marketer' => '#f39c12',
];
$businessName = session('business.name', config('app.name'));
$logo = session('business.logo');
$icons = [];
if (! empty($logo)) {
$icons[] = [
'src' => url('uploads/business_logos/'.$logo),
'sizes' => '192x192',
'type' => 'image/png',
'purpose' => 'any maskable',
];
}
$manifest = [
'name' => $businessName.' - '.($names[$type] ?? 'Portal'),
'short_name' => $names[$type] ?? 'Portal',
'description' => $names[$type] ?? 'Portal',
'start_url' => $startUrls[$type] ?? url('/login'),
'scope' => url('/'.$type),
'display' => 'standalone',
'orientation' => 'portrait-primary',
'background_color' => '#ffffff',
'theme_color' => $colors[$type] ?? '#3c8dbc',
'lang' => app()->getLocale(),
'dir' => in_array(app()->getLocale(), config('constants.langs_rtl', [])) ? 'rtl' : 'ltr',
'icons' => $icons ?: [[
'src' => asset('img/default.png'),
'sizes' => '192x192',
'type' => 'image/png',
]],
];
return response()->json($manifest)
->header('Content-Type', 'application/manifest+json');
}
public function serviceWorker()
{
$content = <<<'JS'
const CACHE = 'portal-pwa-v1';
const ASSETS = ['/'];
self.addEventListener('install', (e) => {
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', (e) => {
e.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', (e) => {
if (e.request.method !== 'GET') return;
e.respondWith(
fetch(e.request).catch(() => caches.match(e.request))
);
});
JS;
return response($content, 200, ['Content-Type' => 'application/javascript']);
}
}