2022-04-30 14:52:07 +02:00
|
|
|
import { inject } from 'vue';
|
|
|
|
import { post } from '@/os';
|
|
|
|
import { $i, login } from '@/account';
|
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { getAccountFromId } from '@/scripts/get-account-from-id';
|
|
|
|
import { router } from '@/router';
|
|
|
|
|
|
|
|
export function swInject() {
|
|
|
|
const navHook = inject('navHook', null);
|
|
|
|
const sideViewHook = inject('sideViewHook', null);
|
|
|
|
|
|
|
|
navigator.serviceWorker.addEventListener('message', ev => {
|
|
|
|
if (_DEV_) {
|
|
|
|
console.log('sw msg', ev.data);
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
if (ev.data.type !== 'order') return;
|
2022-04-30 14:52:07 +02:00
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
if (ev.data.loginId !== $i?.id) {
|
|
|
|
return getAccountFromId(ev.data.loginId).then(account => {
|
2022-04-30 14:52:07 +02:00
|
|
|
if (!account) return;
|
2022-05-26 15:53:09 +02:00
|
|
|
return login(account.token, ev.data.url);
|
2022-04-30 14:52:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
switch (ev.data.order) {
|
2022-04-30 14:52:07 +02:00
|
|
|
case 'post':
|
2022-05-26 15:53:09 +02:00
|
|
|
return post(ev.data.options);
|
2022-04-30 14:52:07 +02:00
|
|
|
case 'push':
|
2022-05-26 15:53:09 +02:00
|
|
|
if (router.currentRoute.value.path === ev.data.url) {
|
2022-04-30 14:52:07 +02:00
|
|
|
return window.scroll({ top: 0, behavior: 'smooth' });
|
|
|
|
}
|
|
|
|
if (navHook) {
|
2022-05-26 15:53:09 +02:00
|
|
|
return navHook(ev.data.url);
|
2022-04-30 14:52:07 +02:00
|
|
|
}
|
2022-05-26 15:53:09 +02:00
|
|
|
if (sideViewHook && defaultStore.state.defaultSideView && ev.data.url !== '/') {
|
|
|
|
return sideViewHook(ev.data.url);
|
2022-04-30 14:52:07 +02:00
|
|
|
}
|
2022-05-26 15:53:09 +02:00
|
|
|
return router.push(ev.data.url);
|
2022-04-30 14:52:07 +02:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|