index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { createRouter, createWebHashHistory, Router } from 'vue-router';
  2. import { useCookies } from '@vueuse/integrations/useCookies';
  3. const current = window.navigator.language || window.navigator.userLanguage || null;
  4. let mytitle = '用户反馈';
  5. if (current && !/^zh/.test(current)) {
  6. mytitle = 'Feedback';
  7. }
  8. import routes from './routes';
  9. const router: Router = createRouter({
  10. history: createWebHashHistory(),
  11. routes: routes,
  12. });
  13. router.beforeEach(async (_to, _from, next) => {
  14. console.log('_to', _to);
  15. const wxOpenId = useCookies().get('wxOpenId');
  16. if (_to.name == 'feedback' || _to.name == 'feedbacksuccess') {
  17. document.title = mytitle;
  18. } else {
  19. document.title = '四维时代售后';
  20. }
  21. if (wxOpenId || _to.name == 'feedback' || _to.name == 'feedbacksuccess' || getUrlKey('code')) {
  22. next();
  23. } else {
  24. getCodeApi(123);
  25. }
  26. });
  27. function getUrlKey(name) {
  28. //获取url 参数
  29. return (
  30. decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) ||
  31. null
  32. );
  33. }
  34. function getCodeApi(state) {
  35. //获取code
  36. console.log('getCodeApi');
  37. const urlNow = encodeURIComponent(window.location.href);
  38. const scope = 'snsapi_base'; //snsapi_userinfo //静默授权 用户无感知
  39. const appid = 'wxac3d59ea82d9b82a';
  40. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
  41. window.location.replace(url);
  42. }
  43. export default router;