<template>
  <view class="content">
    <view class="param">{{ param }}</view>
  </view>
</template>

<script>
import { getToken, setToken, removeToken } from "@/utils/auth";
import { getSqByaccesstoken, getUserinfo } from "@/api/jn/login.js";
export default {
  data() {
    return {
      style: {
        "padding-top": 0,
      },
      statusBarHeight: 0,
      isApp: true,
      webview: null,
      param: "",
    };
  },

  created() {},
  mounted() {},
  onLoad(options) {
    let _this = this;
    // #ifdef APP-PLUS
    let curwebview = this.$mp.page.$getAppWebview();
    this.webview = plus.webview.open(
      "https://t-jn-development-manager.jsszkd.com/login?clientId=641775453324",
      ""
    );
    curwebview.append(this.webview);
    this.webview.show();
    this.webview.overrideUrlLoading({ mode: "reject" }, (e) => {
      //根据参数的不同,做不同的操作!url中,把双引号用别的字符串替换.就OK了.再替换回来可以转json
      console.warn("参数为:" + e.url);
      

      // 获取当前页面的 URL
      var url = e.url;
      console.warn("1:" + url);
      // 获取特定参数的值
      var code = _this.getQueryString("code", url);
      var clientId = _this.getQueryString("clientId", url);
      var token = _this.getQueryString("token", url);
      console.warn("code:" + _this.getQueryString("code", url));
      console.warn("clientId:" + _this.getQueryString("clientId", url));
      console.warn("token:" + _this.getQueryString("token", url));
      if (token) {
		uni.setStorageSync("token", token);
		getSqByaccesstoken({ code: code,}).then((res) => {
		    // console.log("获取accessToken");
		    uni.setStorageSync("accessToken", res.data.accessToken);
		    return getUserinfo({accessToken: res.data.accessToken});
		}).then((el) => {
		    this.$tab.switchTab("/pages/index");
		    uni.setStorageSync("userInfo", el.data);
		    setToken(el.data.token);
		    // location.reload();
		    console.log("用户信息", el);
		});
      }
    });
    // #endif
  },
  methods: {
    getQueryString(name, url) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      const lastIndex = url.lastIndexOf("/");
      const result = url.substring(lastIndex + 1);
      const newUrl = result.replace(/\?/g, "&");
      let r = newUrl.match(reg);
      //   console.log(r);
      if (r != null) {
        return decodeURIComponent(r[2]);
      }
      return null;
    },
  },
};
</script>

<style lang="less">
.content {
  text-align: center;
}
</style>
