<script>
// #ifdef APP-PLUS
import appUpdate from "@/components/appUpdate/appUpdate.js";
// #endif
import { jwtDecode } from "jwt-decode";
export default {
  onLaunch: function () {
    console.log("%c%s", "color:red", "onLaunch---");
    uni.showLoading({ title: "加载中" });
    // uni.setStorageSync("token", "");
    window.initFn = this.initFn;
    // this.initFn();
    //console.log('App Launch')
    // #ifdef APP-PLUS
    let systemInfo = uni.getSystemInfoSync();
    const data = {
      appid: systemInfo.appId,
      version: systemInfo.appVersion,
    };
    console.log(data);
    //把appid和版本发送请求服务器，看服务器上是否有更新
    this.$api.checkVersion(data).then((res) => {
      console.log(res);
      if (res.code == 0) {
        //如果返回0则表示有更新
        appUpdate(res);
      }
    });
    // #endif
  },
  onShow: function () {
    // console.log('%c%s', 'color:red', 'onShow---');
    // #ifdef MP-WEIXIN

    // 当向小程序后台请求完新版本信息，会进行回调。res: {hasUpdate: true, version: 1.0.0}
    updateManager.onCheckForUpdate(function (res) {
      if (res.hasUpdate) {
        // 有更新
        uni.showLoading({ title: "更新中..." }); // 开始下载前，显示Loading
      }
    });
    // 当新版本下载完成，会进行回调
    updateManager.onUpdateReady(function () {
      uni.hideLoading(); // 关闭 Loading
      uni.showModal({
        // 弹确认框（强制更新）
        title: "更新提示",
        content: "更新完毕，是否重启？",
        success: function (res) {
          if (res.confirm) {
            updateManager.applyUpdate(); // 强制小程序重启并使用新版本。
          }
        },
      });
    });
    // 当新版本下载失败，会进行回调
    updateManager.onUpdateFailed(function () {
      uni.hideLoading(); // 关闭 Loading
      uni.showToast({ title: "更新失败，稍后再试...", icon: "error" });
    });
    // #endif
  },
  onHide: function () {},
  methods: {
    initFn(data) {
      uni.hideLoading(); // 关闭 Loading
      if (!data) {
        return uni.showToast({ title: "未传入参数 initFn", icon: "none" });
      }
      // uni.showModal({
      //   title: "查看initFn",
      //   content: JSON.stringify(data) || "未传入参数 initFn",
      //   showCancel: true,
      //   success: function (res) {},
      // });
      //   "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9zaWQiOiIwOGRkNzFiZC01N2MxLTQ1YmMtODBiYS0xMjkzYjU4MTViMmEiLCJ1bmlxdWVfbmFtZSI6IuS_nea0gSIsInJvbGUiOiJVc2VyIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbW9iaWxlcGhvbmUiOiIxODE3NDAxMDU2MiIsImdyb3Vwc2lkIjoiNGIyZDk2NjItYTc2OC0wOGYyLWEwYzgtNzYyYjhjNzZkYTJhIiwibmJmIjoxNzQ0MjYzMTAyLCJleHAiOjE3NDQyNjY3MDIsImlhdCI6MTc0NDI2MzEwMn0.ZJTwC7rWCcywoM7ncysw-JRIiwXz4y_6hlmqctN-gMM"
      let token = data.token;
      uni.setStorageSync("token", "Bearer " + token);
      console.log(jwtDecode(token), "解析的token信息");
      let resData = jwtDecode(token);
      this.$u.vuex("vuex_userData", resData);
      // User  | Admin
      let role = resData.role;
      console.log(role, "role----");
      // return;
      if (role === "User") {
        const normalTabBar = [
          {
            pagePath: "/pages/index/planList",
            iconPath: "/static/tab/planList.png",
            selectedIconPath: "/static/tab/planList-active.png",
            text: "计划",
          },
          {
            pagePath: "/pages/index/index",
            iconPath: "/static/tab/admin-home.png",
            selectedIconPath: "/static/tab/admin-home-acitve.png",
            text: "首页",
          },
          {
            pagePath: "/pages/user/index",
            iconPath: "/static/tab/admin-my.png",
            selectedIconPath: "/static/tab/admin-my-active.png",
            text: "设置",
          },
        ];
        this.$u.vuex("vuex_tabbar", normalTabBar);
        uni.switchTab({
          url: "/pages/index/index",
          success() {
            console.log("跳转成功");
            window.initFn = null; // 清除全局函数
          },
          fail(err) {
            console.error("跳转失败", err);
          },
        });
      }
      if (role === "Admin") {
        const adminTabBar = [
          {
            pagePath: "/pages/adminHome/index",
            iconPath: "/static/tab/admin-home.png",
            selectedIconPath: "/static/tab/admin-home-acitve.png",
            text: "首页",
          },
          {
            pagePath: "/pages/adminNfc/index",
            iconPath: "/static/tab/nfc.png",
            selectedIconPath: "/static/tab/nfc-active.png",
            text: "nfc写入",
          },
          {
            pagePath: "/pages/adminMy/index",
            iconPath: "/static/tab/admin-my.png",
            selectedIconPath: "/static/tab/admin-my-active.png",
            text: "我的",
          },
        ];
        this.$u.vuex("vuex_tabbar", adminTabBar);
        uni.switchTab({
          url: "/pages/adminHome/index",
          success() {
            console.log("跳转成功");
            window.initFn = null; // 清除全局函数
          },
          fail(err) {
            console.error("跳转失败", err);
          },
        });
      }
    },
  },
};
</script>
<style lang="scss">
@import "@/uni_modules/uview-ui/index.scss";
</style>
