<script>
/**
 * vuex管理登陆状态，具体可以参考官方登陆模板示例
 */
import { mapMutations, mapActions } from "vuex";
import request from "./utils/request";
// #ifdef MP-WEIXIN
import SMSdk from "./utils/shumei";
// #endif

import { onLaunchSubmit } from "@/utils/subscribe";
import cfg from "@/config/index";
import appIdMap from "@/config/appId.config";
import config from "@/config/config.json";
import { saTrackEvent } from "@/utils/sa.js";
import { param2Obj } from "@/utils/index";
import { uploadSMDeviceID } from "@/api/home.api";
export default {
  globalData: {
    ...config
  },
  methods: {
    ...mapMutations(["login"]),
    ...mapActions({
      setGDTVID: "setGDTVID"
    })
  },
  // #ifdef MP-WEIXIN
  SMSdk,
// #endif
  onLaunch: async function() {
    let appId;
    let smDeviceId=""
    // #ifdef MP-WEIXIN
     smDeviceId = await SMSdk.SMSdk.getDeviceId();
   // #endif

    console.log("smDeviceId = ", smDeviceId);
    uni.setStorageSync("smDeviceId", smDeviceId || "");
    if (smDeviceId) {
      const [shumeiKey] = await uploadSMDeviceID({
        shumeiDeviceId: smDeviceId
      });
      console.log("shumeiKey after kdsp redis = ", shumeiKey);
      if (shumeiKey) {
        uni.setStorageSync("shumeiKey", shumeiKey);
      }
    }
    // #ifdef MP-ALIPAY
    appId = uni.getAppIdSync().appId;
    // #endif

    // #ifndef MP-ALIPAY || H5
    const accountInfo = uni.getAccountInfoSync();
    appId = accountInfo.miniProgram.appId;
    // #endif
    const tenantBasicInfo = appIdMap[appId] || {};
    uni.setStorageSync("appId", appId);
    uni.setStorageSync("tenantId", tenantBasicInfo.tenantId || 560761);
    uni.setStorageSync("tenantBasicInfo", tenantBasicInfo);
    tenantBasicInfo.vccChannel && uni.setStorageSync("vccChannel", tenantBasicInfo.vccChannel);
    console.log("jhb cfg",cfg)
    this.$ry?.init(cfg.REYUN_APP_KEY);
    // todo
    if (appId === "wxe16bf9293671506c" && process.env.NODE_ENV === "production") {
      //wxe16bf9293671506c 测试环境appid
      let [data] = await request.get("https://talos.xyqb.com/api/kdsp/miniapp/getData");
      uni.setStorageSync("url", data);
    } else {
      uni.removeStorageSync("url");
    }
    let userInfo = uni.getStorageSync("userInfo") || "";
    if (userInfo?.id) {
      //更新登陆状态
      uni.getStorage({
        key: "userInfo",
        success: res => {
          this.login(res.data);
        }
      });
    }
    // #ifdef MP-WEIXIN
    onLaunchSubmit();
    // #endif
  },
  onShow: function(option) {
    // #ifdef MP-WEIXIN
    // 存储腾讯广告id
    const { query } = option;
    if (query && query.gdt_vid) {
      // store存储gdt_vid以便后续登录拿到openid后使用
      this.setGDTVID(query.gdt_vid);
    }
    //新版本更新
    if (uni.canIUse("getUpdateManager")) {
      //判断当前微信版本是否支持版本更新
      const updateManager = uni.getUpdateManager();
      updateManager.onCheckForUpdate(function(res) {
        if (res.hasUpdate) {
          // 请求完新版本信息的回调
          updateManager.onUpdateReady(function() {
            uni.showModal({
              title: "更新提示",
              content: "新版本已经准备好，是否重启应用？",
              success: function(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                }
              }
            });
          });
          updateManager.onUpdateFailed(function() {
            uni.showModal({
              // 新的版本下载失败
              title: "已经有新版本了哟~",
              content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~"
            });
          });
        }
      });
    } else {
      uni.showModal({
        // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
        title: "提示",
        content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
      });
    }
    // #endif
    // #ifndef MP-ALIPAY  || H5
    uni.onAppShow(obj => {
      const TPL_TITLE = "tplTitle";
      const tplTitle = param2Obj(obj.path)[TPL_TITLE] || obj.query[TPL_TITLE];
      if ((obj.scene === 1014 || obj.scene === 1107) && tplTitle) {
        saTrackEvent("MINI_SubscribeMessageCardBtnClick", {
          cards_name: tplTitle
        });
      }
    });
    // #endif


  },
  onHide: function() {
    console.log("App Hide");
  }
};
</script>

<style lang="scss">
@import "@/style/icon.scss";
@import "@/style/uni-ui.scss";
page {
  background: $page-color-base;
  height: 100%;

  /* #ifdef MP-ALIPAY */
  /* width: 100%;
  position: fixed;
  top: 0;
  bottom: 0; */
  /* #endif */
}

view,
scroll-view,
swiper,
swiper-item,
cover-view,
cover-image,
icon,
text,
rich-text,
progress,
button,
checkbox,
form,
input,
label,
radio,
slider,
switch,
textarea,
navigator,
audio,
camera,
image,
video {
  box-sizing: border-box;
}
/* 骨架屏替代方案 */
.Skeleton {
  background: #f3f3f3;
  padding: 20rpx 0;
  border-radius: 8rpx;
}

/* 图片载入替代方案 */
.image-wrapper {
  font-size: 0;
  background: #f3f3f3;
  border-radius: 4px;

  image {
    width: 100%;
    height: 100%;
    transition: 0.6s;
    opacity: 0;

    &.loaded {
      opacity: 1;
    }
  }
}

.clamp {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
}

.common-hover {
  background: #f5f5f5;
}

/*边框*/
.b-b:after,
.b-t:after {
  position: absolute;
  z-index: 3;
  left: 0;
  right: 0;
  height: 0;
  content: "";
  transform: scaleY(0.5);
  border-bottom: 1px solid $border-color-base;
}

.b-b:after {
  bottom: 0;
}

.b-t:after {
  top: 0;
}

/* button样式改写 */
uni-button,
button {
  height: 80rpx;
  line-height: 80rpx;
  font-size: $font-lg + 2rpx;
  font-weight: normal;

  &.no-border:before,
  &.no-border:after {
    border: 0;
  }
}

uni-button[type="default"],
button[type="default"] {
  color: $font-color-dark;
}

/* input 样式 */
.placeholder {
  font-size: 28rpx;
  line-height: 40rpx;
  color: #999999;
}
button::after {
  display: none;
}
/* requestRevokeTest test */
</style>
