<template>
  <div class="login-wrap">
    <Nav />
    <button @click="login">微信授权登录</button>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import Nav from "@/components/NavBar.vue";
import { queryLogin } from "@/api/index";
import { getQueryString } from "@/utils/index";
import { useStore } from "vuex";
import { showToast } from "vant";

const store = useStore();
const route = useRoute();
const router = useRouter();

const login = async () => {
  window.location.href =
    "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxdefab7de9f113f08&redirect_uri=" +
    encodeURIComponent(location.href) +
    "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
};

const code = getQueryString("code");
if (code) {
  wxLogin({ accountInfoId: 1, code }).then((res) => {
    console.log("wxLoginres", res);
    if (res.code === 0) {
      store.commit("setToken", ret.data.upToken);
      store.commit("setUserInfo", {
        account: ret.data.openId,
      });
      router.replace(route.query.redirect ? route.query.redirect : "/");
    }
  });
}
</script>
<style scoped lang="scss"></style>
