App.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <router-view />
  3. </template>
  4. <script setup lang="ts">
  5. // import 'vant/lib/index.css';
  6. import { onMounted, computed } from 'vue';
  7. import { useUserStore } from '/@/store/modules/user';
  8. import { useCookies } from '@vueuse/integrations/useCookies';
  9. const userStore = useUserStore();
  10. const wxOpenId = computed(() => {
  11. return userStore.getWxOpenId;
  12. });
  13. onMounted(async () => {
  14. let code = getUrlKey('code'); //获取url参数code
  15. if (wxOpenId.value) {
  16. //
  17. console.log('已登录', wxOpenId.value);
  18. } else if (code) {
  19. //存在code
  20. userStore.setWxOpenId(code);
  21. } else {
  22. //进行微信登录
  23. getCodeApi(123);
  24. }
  25. let clear = getUrlKey('clear'); //获取url参数code
  26. if(clear){
  27. useCookies().set('wxOpenId', '')
  28. }
  29. });
  30. function getUrlKey(name) {
  31. //获取url 参数
  32. return (
  33. decodeURIComponent(
  34. (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20'),
  35. ) || null
  36. );
  37. }
  38. function getCodeApi(state) {
  39. //获取code
  40. let urlNow = encodeURIComponent(window.location.href);
  41. let scope = 'snsapi_base'; //snsapi_userinfo //静默授权 用户无感知
  42. let appid = 'wxac3d59ea82d9b82a';
  43. let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
  44. window.location.replace(url);
  45. }
  46. </script>
  47. <style>
  48. #app {
  49. font-family: PingFang SC-Regular, PingFang SC;
  50. -webkit-font-smoothing: antialiased;
  51. -moz-osx-font-smoothing: grayscale;
  52. color: #2c3e50;
  53. background-color: var(--color-bg-1);
  54. }
  55. .van-toast {
  56. background: rgba(0, 0, 0, 0.7) !important;
  57. }
  58. </style>