refactor: move spa to frontend folder
This commit is contained in:
47
frontend/spa/src/utils/AppMetaInitializer.ts
Normal file
47
frontend/spa/src/utils/AppMetaInitializer.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
class AppMetaInitializer {
|
||||
private readonly settings: object;
|
||||
|
||||
constructor(settings: object) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public init() {
|
||||
console.log('Init app meta');
|
||||
document.title = this.settings.app_name;
|
||||
this.setMeta('application-name', this.settings.app_name);
|
||||
this.setMeta('apple-mobile-web-app-title', this.settings.app_name);
|
||||
this.setMeta('mobile-web-app-capable', 'yes');
|
||||
this.setMeta('apple-mobile-web-app-capable', 'yes');
|
||||
this.setMeta('apple-mobile-web-app-status-bar-style', 'default');
|
||||
this.setMeta('theme-color', '#000000');
|
||||
this.setMeta('msapplication-navbutton-color', '#000000');
|
||||
this.setMeta('apple-mobile-web-app-status-bar-style', 'black-translucent');
|
||||
this.addLink('manifest', this.settings.manifest_url);
|
||||
|
||||
this.addLink('icon', this.settings.app_icon192, '192x192');
|
||||
this.addLink('apple-touch-icon', this.settings.app_icon192);
|
||||
this.addLink('apple-touch-icon', this.settings.app_icon180, '180x180');
|
||||
this.addLink('apple-touch-icon', this.settings.app_icon152, '152x152');
|
||||
this.addLink('apple-touch-icon', this.settings.app_icon120, '120x120');
|
||||
}
|
||||
|
||||
private setMeta(name: string, content: string) {
|
||||
let meta = document.querySelector(`meta[name="${name}"]`);
|
||||
if (!meta) {
|
||||
meta = document.createElement('meta');
|
||||
meta.setAttribute('name', name);
|
||||
document.head.appendChild(meta);
|
||||
}
|
||||
meta.setAttribute('content', content);
|
||||
}
|
||||
|
||||
private addLink(rel: string, href: string, sizes?: string) {
|
||||
const link = document.createElement('link');
|
||||
link.rel = rel;
|
||||
link.href = href;
|
||||
if (sizes) link.sizes = sizes;
|
||||
document.head.appendChild(link);
|
||||
}
|
||||
}
|
||||
|
||||
export default AppMetaInitializer;
|
||||
99
frontend/spa/src/utils/ftch.js
Normal file
99
frontend/spa/src/utils/ftch.js
Normal file
@@ -0,0 +1,99 @@
|
||||
import {ofetch} from "ofetch";
|
||||
|
||||
const BASE_URL = '/';
|
||||
|
||||
function encodeBase64Unicode(str) {
|
||||
return btoa(new TextEncoder().encode(str).reduce((data, byte) => data + String.fromCharCode(byte), ''));
|
||||
}
|
||||
|
||||
export const apiFetch = ofetch.create({
|
||||
throwHttpErrors: true,
|
||||
onRequest({request, options}) {
|
||||
const data = window.Telegram?.WebApp?.initData;
|
||||
|
||||
if (data) {
|
||||
const encoded = encodeBase64Unicode(data);
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
'X-Telegram-InitData': encoded,
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
async function ftch(action, query = null, json = null) {
|
||||
const options = {
|
||||
method: json ? 'POST' : 'GET',
|
||||
}
|
||||
if (query) options.query = query;
|
||||
if (json) options.body = json;
|
||||
|
||||
return await apiFetch(`${BASE_URL}index.php?route=extension/tgshop/handle&api_action=${action}`, options);
|
||||
}
|
||||
|
||||
export async function storeOrder(data) {
|
||||
return await apiFetch(`${BASE_URL}index.php?route=extension/tgshop/handle&api_action=storeOrder`, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getCart() {
|
||||
return await ftch('getCart');
|
||||
}
|
||||
|
||||
export async function addToCart(data) {
|
||||
return await apiFetch(`${BASE_URL}index.php?route=checkout/cart/add`, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function cartRemoveItem(data) {
|
||||
return await apiFetch(`${BASE_URL}index.php?route=checkout/cart/remove`, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function cartEditItem(data) {
|
||||
return await apiFetch(`${BASE_URL}index.php?route=checkout/cart/edit`, {
|
||||
redirect: 'manual',
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function fetchSettings() {
|
||||
return await ftch('settings');
|
||||
}
|
||||
|
||||
export async function getFiltersForMainPage() {
|
||||
return await ftch('filtersForMainPage');
|
||||
}
|
||||
|
||||
export async function setCoupon(coupon) {
|
||||
const formData = new FormData();
|
||||
formData.append('coupon', coupon);
|
||||
|
||||
return await apiFetch(`${BASE_URL}index.php?route=extension/total/coupon/coupon`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
}
|
||||
|
||||
export async function setVoucher(voucher) {
|
||||
const formData = new FormData();
|
||||
formData.append('voucher', voucher);
|
||||
|
||||
return await apiFetch(`${BASE_URL}index.php?route=extension/total/voucher/voucher`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
}
|
||||
|
||||
export async function fetchBanner() {
|
||||
return await ftch('banner');
|
||||
}
|
||||
|
||||
export default ftch;
|
||||
30
frontend/spa/src/utils/yaMetrika.js
Normal file
30
frontend/spa/src/utils/yaMetrika.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import {useYaMetrikaStore} from "@/stores/yaMetrikaStore.js";
|
||||
|
||||
function getMetrikaId() {
|
||||
// Пробуем найти все элементы <script> с mc.yandex.ru
|
||||
const scripts = Array.from(document.scripts);
|
||||
for (const s of scripts) {
|
||||
if (s.src.includes('mc.yandex.ru/metrika/tag.js')) {
|
||||
const match = s.src.match(/id=(\d+)/);
|
||||
if (match) return match[1];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function injectYaMetrika() {
|
||||
const script = document.createElement('script');
|
||||
script.src = '/index.php?route=extension/tgshop/handle/ya_metrika';
|
||||
// script.async = true;
|
||||
document.head.appendChild(script);
|
||||
console.debug('[Init] Yandex Metrika injected to the page.');
|
||||
|
||||
script.onload = () => {
|
||||
window.YA_METRIKA_ID = getMetrikaId();
|
||||
console.debug('[Init] Detected Yandex.Metrika ID:', window.YA_METRIKA_ID);
|
||||
const yaMetrika = useYaMetrikaStore();
|
||||
yaMetrika.initUserParams();
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
yaMetrika.processQueue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user