2022-04-30 07:52:07 -05: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 08:53:09 -05:00
|
|
|
if (ev.data.type !== 'order') return;
|
2022-04-30 07:52:07 -05:00
|
|
|
|
2022-05-26 08:53:09 -05:00
|
|
|
if (ev.data.loginId !== $i?.id) {
|
|
|
|
return getAccountFromId(ev.data.loginId).then(account => {
|
2022-04-30 07:52:07 -05:00
|
|
|
if (!account) return;
|
2022-05-26 08:53:09 -05:00
|
|
|
return login(account.token, ev.data.url);
|
2022-04-30 07:52:07 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-26 08:53:09 -05:00
|
|
|
switch (ev.data.order) {
|
2022-04-30 07:52:07 -05:00
|
|
|
case 'post':
|
2022-05-26 08:53:09 -05:00
|
|
|
return post(ev.data.options);
|
2022-04-30 07:52:07 -05:00
|
|
|
case 'push':
|
2022-05-26 08:53:09 -05:00
|
|
|
if (router.currentRoute.value.path === ev.data.url) {
|
2022-04-30 07:52:07 -05:00
|
|
|
return window.scroll({ top: 0, behavior: 'smooth' });
|
|
|
|
}
|
|
|
|
if (navHook) {
|
2022-05-26 08:53:09 -05:00
|
|
|
return navHook(ev.data.url);
|
2022-04-30 07:52:07 -05:00
|
|
|
}
|
2022-05-26 08:53:09 -05:00
|
|
|
if (sideViewHook && defaultStore.state.defaultSideView && ev.data.url !== '/') {
|
|
|
|
return sideViewHook(ev.data.url);
|
2022-04-30 07:52:07 -05:00
|
|
|
}
|
2022-05-26 08:53:09 -05:00
|
|
|
return router.push(ev.data.url);
|
2022-04-30 07:52:07 -05:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|