<script>
export default {
  globalData: {
    plAd: true,
    dotNumber: {},
    apiHost: "https://dev-app.igandan.com/app",
    //BASE_URL='https://dev-app.igandan.com/app'
  },
  onLaunch(options) {
    //uni.setStorageSync('apiHost', 'value')
    console.log("打印options");
    console.log(options);
    if (
      options &&
      options.referrerInfo.extraData &&
      options.referrerInfo.extraData.token
    ) {
      uni.setStorageSync(
        "AUTH_TOKEN_App",
        options.referrerInfo.extraData.token
      );
      uni.setStorageSync("userInfo", options.referrerInfo.extraData.userInfo);
      //getApp({allowDefault: true}).globalData.apiHost = options.referrerInfo.extraData.apiHost;
    }
    if (
      options &&
      options.referrerInfo.extraData &&
      options.referrerInfo.extraData.targetPath
    ) {
      uni.navigateTo({
        url: options.referrerInfo.extraData.targetPath,
      });
    }

    console.log("app传递登录信息onLaunch-------------------");
    try {
      // #ifdef APP
      uni.onNativeEventReceive((event, data) => {
        console.log("event", event);
        console.log("data", data);
        if (event.indexOf("clearToken") > -1) {
          uni.setStorageSync("AUTH_TOKEN_App", "");
          plus.runtime.quit();
        }
        if(event.indexOf("goPage") > -1){
          uni.navigateTo({
            url: data,
          });
        }
      });
      // #endif
    } catch (error) {
      console.log("app传递登录信息error-------------------");
      console.log(error);
    }
  },
  onShow() {
    try {
      // #ifdef APP
      let checkTokenCallBackFlag = false;
      console.log("执行onshow");

      uni.sendNativeEvent(
        "checkToken",
        {
          msg: "checkToken",
        },
        (ret) => {
          checkTokenCallBackFlag = true;
          console.log("check回调");
          console.log(ret);
          if (ret.code == 0) {
            uni.showModal({
              title: "提示",
              showCancel: false,
              content: "您已退出登录！",
              success: function (res) {
                if (res.confirm) {
                  uni.sendNativeEvent(
                    "goTabbarPage",
                    {
                      msg: "home",
                    },
                    (ret) => {
                      console.log(ret);
                    }
                  );
                  plus.runtime.quit();
                } else if (res.cancel) {
                  console.log("用户点击取消");
                }
              },
            });
          }
        }
      );

      // setTimeout(function(){
      // 	console.log("执行 setTimeout")
      // 	if(!checkTokenCallBackFlag){
      // 		uni.showModal({
      // 			title: '提示',
      //           showCancel: false,
      // 			content: '您已退出登录',
      // 			success: function (res) {
      // 				if (res.confirm) {
      // 					uni.sendNativeEvent('goTabbarPage', {
      // 						msg: 'home'
      // 					}, ret => {
      // 						console.log(ret);
      // 					})
      // 				} else if (res.cancel) {
      // 					console.log('用户点击取消');
      // 				}
      // 			}
      // 		});
      // 	}
      // }, 1000)

     
      let main = plus.android.runtimeMainActivity();
      //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
      plus.runtime.quit = function () {
        main.moveTaskToBack(false);
      };
      //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用，其他正常toast
      plus.nativeUI.toast = function (str) {
        if (str == "再按一次退出应用") {
          main.moveTaskToBack(false);
        } else {
          uni.showToast({
            title: str,
            icon: "none",
          });
        }
      };

      // uni.onNativeEventReceive((event, data) => {
      //   console.log("app传递登录信息onshow-------------------");
      //   console.log("event", event);
      //   console.log("data", data);
      //   if (event == "loginInfo") {
      //     uni.setStorageSync("DEV_AUTH_YX_TOKEN_App", data.token);
      //     uni.setStorageSync("userInfo", data.userInfo);
      //   }else if(event == "showRedDot"){
      //     console.log("showRedDot");
      //     console.log(data);
      //     getApp().globalData.dotNumber=data;
      //   }
      // });
      // #endif
    } catch (error) {
      console.log("error");
      console.log(error);
    }
  },
  onHide() {},
  methods: {},
};
</script>

<template>
  <GlobalDialog />
</template>

<style lang="scss">
/*每个页面公共css */
@import "@/uni_modules/uni-scss/index.scss";
/* #ifndef APP-NVUE */
@import "@/static/customicons.css";

// 设置整个项目的背景色
page {
  background-color: #f5f5f5;
}

.uni-navbar__header-btns-right {
  min-width: 120rpx;
  width: auto !important;
}

/* #endif */
.uni-nav-bar-text {
  font-weight: bold;
  font-size: 36rpx !important;
}

.example-info {
  font-size: 14px;
  color: #333;
  padding: 10px;
}

.twoline {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.oneline {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.uni-navbar .uniui-left {
  font-weight: bold;
  font-size: 50rpx !important;
}

::-webkit-scrollbar {
  display: none;
}

.status_bar {
  height: var(--status-bar-height);
  background-color: #eeeeee;
}

.navbox {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: calc(var(--status-bar-height) + 44px);
  z-index: 9999;
}
</style>