<script>
import config from "./config";
import store from "@/store";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { getSqByaccesstoken, getUserinfo } from "@/api/jn/login.js";
import permision from "@/utils/permission.js";
export default {
  onLaunch: function () {
    console.log("初始化进入");
    this.initApp();
    //#ifdef APP-PLUS
    this.requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION");
    this.requestAndroidPermission("android.permission.CAMERA");
    this.requestAndroidPermission("android.permission.READ_EXTERNAL_STORAGE");
    this.requestAndroidPermission("android.permission.RECORD_AUDIO");
    //#endif
    removeToken();
  },
  onShow() {
    console.log("onShow");
  },
  methods: {
    // 初始化应用
    initApp() {
      // 初始化应用配置
      this.initConfig();
      // 检查用户登录状态
      // // #ifdef APP || H5
      this.checkLogin();
      // //#endif
    },
    initConfig() {
      this.globalData.config = config;
    },
    checkLogin() {
      const launchOptions = uni.getLaunchOptionsSync();
      const { query } = launchOptions;
      console.log(query, "query");
      // console.log(Object.keys(query).length,"query.lengh");
      console.log(getToken(), "getToken()");
      if (!getToken()) {
        if (Object.keys(query).length !== 0) {
          console.log("获取query");
          // let params = this.getRequestParams();
          // console.log(params,"params携带的参数");
          // if (location.href.includes("code=")) {
          const code = query.code;
          const token = query.token;
          uni.setStorageSync("token", token);
          // console.log(code,"code");
          // console.log(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);
            });
          // }
        } else {
          // // #ifdef APP
          console.log("进入APP判断");
          // this.$tab.reLaunch("/pages/myLogin");
          uni.reLaunch({
            url: "/pages/myLogin",
          });
          // 打开手机浏览器指定的url
          // plus.runtime.openURL( "https://t-jn-development-manager.jsszkd.com/login?clientId=641775453324", this.openErr );
          // // #endif
          // // #ifdef H5
          // location.href = `https://t-jn-development-manager.jsszkd.com/login?clientId=641775453324`
          // // #endif

          console.log("Before navigateTo or reLaunch");
          // this.$tab.reLaunch("/pages/myLogin");
          // uni.navigateTo({
          // 	url:'https://t-jn-development-manager.jsszkd.com/login?clientId=641775453324'
          // })
        }
      } else {
        this.$tab.reLaunch("/pages/index");
      }
    },
    openErr(err) {
      console.log(err, "报错信息");
    },
    // 获取参数
    getRequestParams() {
      let url = location.href;
      let requestParams = {};
      if (url.indexOf("?") !== -1) {
        let str = url.substr(url.indexOf("?") + 1); //截取?后面的内容作为字符串
        // console.log(str, '?后面的内容');
        let strs = str.split("&"); //将字符串内容以&分隔为一个数组
        // console.log(strs, '以&切割的数组');
        for (let i = 0; i < strs.length; i++) {
          requestParams[strs[i].split("=")[0]] = decodeURI(
            strs[i].split("=")[1]
          );
          // 将数组元素中'='左边的内容作为对象的属性名，'='右边的内容作为对象对应属性的属性值
        }
      }
      // console.log(requestParams, '处理后的对象');
      return requestParams;
    },
    async requestAndroidPermission(permisionID) {
      var result = await permision.requestAndroidPermission(permisionID);
      var strStatus;
      if (result == 1) {
        strStatus = "已获得授权";
      } else if (result == 0) {
        strStatus = "未获得授权";
        uni.showModal({
          content: "请在设置中打卡相机、麦克风、相册、定位权限",
          showCancel: false,
        });
        console.warn("权限：" + permisionID + strStatus);
        // 打开App权限设置界面
        this.gotoAppPermissionSetting();
      } else {
        strStatus = "被永久拒绝权限";
        uni.showModal({
          content: "请在设置中打卡相机、麦克风、相册、定位权限",
          showCancel: false,
        });
        console.warn("权限：" + permisionID + strStatus);
        // 打开App权限设置界面
        this.gotoAppPermissionSetting();
      }
    },
    gotoAppPermissionSetting: function () {
      permision.gotoAppPermissionSetting();
    },
  },
};
</script>

<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import "@/uni_modules/uview-ui/index.scss";
@import "@/static/scss/index.scss";
</style>
