<template>
  <div>
    <!-- 微信页面授权回调:用户首次进入时判断当前用户所在环境：微信公众号和微信小程序 -->
    <!-- 微信页面授权回调:用户首次进入时判断当前用户所在环境：微信公众号和微信小程序 -->
  </div>
</template>
<script>
import { KIP_API_CODE } from '@/common/js/config';
import { getUrlParams, getAppIdByGroupIdAndMallId } from '@/utils/index';
import KipCacheTool from '@/utils/kip-cache-tool';
import log from '@/utils/log';
import { WxJsOpenId } from '@/utils/login';
import MemberCacheTool from '@/utils/member-cache-tool';
import uni from '@/utils/uniHooks';
import {
  kipCheckToken,
  kipGetNewAccessTokenByRefreshToken,
} from '@/utils/api-kip';
import { Toast } from 'vant';
import loginMinix from '@/mixins/login';

export default {
  created() {
    console.log(12121212);
    this.getCode();
  },
  mixins: [loginMinix],
  mounted() {
    console.log(17171717171717);
    const kipAccessToken = uni.getStorageSync('kipAccessToken');
    const openid = uni.getStorageSync('openid');
    if (openid && !kipAccessToken) {
      /* if (!openid) {
            // 当进入其他页面时，需要先打开路由展示
            this.isInit = true;
            // 前往授权页面
            this.$router.replace({
              path: '/openWx',
              query: query,
            });
            return;
          } */
      // 初始化微信
      // initWxJsSdkConfig(['chooseWXPay', 'scanQRCode', 'checkJsApi']);
      // 如果用户不选择登录，则不走登录逻辑
      const query = getUrlParams();
      this.$router.replace({
        path: 'login',
        query: query,
      });
      return;
    }
  },
  methods: {
    getCode() {
      // console.log(56, this.$route);
      // debugger;
      const { code = '' } = getUrlParams(); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
      const local = `${window.location.href}`.replace(/openWx/g, '');
      // return;
      if (code == null || code === '') {
        // const componentAppid = 'wxd830fe4d1e04988e'
        const appid = getAppIdByGroupIdAndMallId({
          groupId: this.$route.params.groupId,
          mallId: this.$route.params.mallId,
          type: 'appid',
        });
        window.location.href =
          'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
          //   window.APPID +
          // `wx907c27f16841a919` +
          appid +
          '&redirect_uri=' +
          encodeURIComponent(local) +
          // `&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
          `&response_type=code&scope=snsapi_base&state=1#wechat_redirect`;
      } else {
        console.log(78, code);
        this.getOpenId(code); //把code传给后台获取用户信息
      }
    },
    async getOpenId(code) {
      try {
        /*
        // this.$store.commit('SET_OPENID', 'odJnI0-Y6ZJap-Ow9nmJH-4WyxhU');
        this.$store.commit('SET_OPENID', 'oEDin6CwQMev3iRu74IAsE4mYYwo'); // 参数公众号
        // 判断用户是否需要登录,
        await this.checkIsLogin(() => {
          this.$router.replace({
            path: 'home',
          });
        });*/
        // 通过code获取 openId等用户信息，/api/user/wechat/login 为后台接口
        // const { appid, secret } = getAppIdByGroupIdAndMallId({
        //   groupId: this.$route.params.groupId,
        //   mallId: this.$route.params.mallId,
        //   type: 'all',
        // });
        // console.log(99, appid, secret, code);
        // return;
        // ?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        const res = await WxJsOpenId({
          // appid,
          // secret,
          code,
          // grant_type: 'authorization_code',
        });
        if (res?.code === '000000' && res?.data) {
          this.$store.commit('SET_OPENID', res.data);
          // this.$store.commit('SET_WX_ACCESS_TOKEN', res.access_token);
          // this.$store.commit('SET_WX_REFRESH_TOKEN', res.refresh_token);
          // 判断用户是否需要登录,
          await this.checkIsLogin(() => {
            this.$router.replace({
              path: 'home',
            });
          });
        }
      } catch (err) {
        console.log(106, err);
      }
    },
  },
};
</script>
