<template>
  <div id="initIndex" class="hello-box">
    <div class="loadingBox">等我～~我正在高速跑来的路上！</div>
  </div>
</template>

<script>
import store from "../store/store";
import apis1 from "../util/apis1.js";
import { getUrlQueryValueByKey } from "../util/tools.js";
// import {
//   joinVipService,
//   sureFollowShopPopComService,
// } from "../util/comCreate.js";
// import {
//   AmnjoinVipService,
//   AmnsureFollowShopPopComService
// } from "../util/hdzxtcomCreate.js";
import jdLogin from "../util/login";
import { getUuid } from "../util/getUuid";
let { getToken, myselfBargain, getDefenseUrls, initPinToken,initPage } = apis1;

export default {
  name: "InitIndex",
  data() {
    return {};
  },
  created() {
    this.initData();
  },

  mounted() {
    this.$destroy()
  },
  beforeDestroy() {
    // document.querySelector(".hello-box").remove();
  },
  destroyed() {
  },
  methods: {
    // initAppData(){
    //   this.initData();
    // },
    initData() {
      // let cur_url = 'https://shopmember.m.jd.com/shopcard/?venderId=1000002791&shopId=1000002791&venderType=1&channel=208&sceneval=2&jxsid=16117359411523567369&returnUrl=https://lzss1-isv.isvjcloud.com/#/index/?activityType=5&activityId=1354383403569696769&shareUserId=1354383506127630338&shareUuid=1354383506127630338';//location.href;
      let cur_url = location.href;
      sessionStorage.setItem('activityUrl', cur_url);
      console.log(cur_url, "cur_url链接地址");
      //缓存活动地址
      if (cur_url.indexOf('token=') != -1) {
        sessionStorage.setItem('oldUrl', cur_url.split('url=')[1])
      } else {
        sessionStorage.setItem('oldUrl', cur_url)
      }

      // console.log(getUrlQueryValueByKey('activityType', cur_url),'activityType123');
      this.activityType = getUrlQueryValueByKey("activityType", cur_url);
      sessionStorage.setItem("activityType", this.activityType);

      this.activityId = getUrlQueryValueByKey("activityId", cur_url);
      store.commit("setActivityId", this.activityId); //1344185866492006402
      sessionStorage.setItem("activityId", this.activityId);
      // console.log(getUrlQueryValueByKey('activityId', cur_url),'activityId1123');
      this.templateId = getUrlQueryValueByKey("templateId", cur_url);
      store.commit("setTemplateId", this.templateId); //1344185866492006402
      sessionStorage.setItem("templateId", this.templateId);

      this.shopId = getUrlQueryValueByKey("shopId", cur_url);
      sessionStorage.setItem("shopId", this.shopId);

      this.nodeId = getUrlQueryValueByKey("nodeId", cur_url);
      store.commit("setNodeId", this.nodeId); //1344185866492006402
      sessionStorage.setItem("nodeId", this.nodeId);
      this.previewCode = getUrlQueryValueByKey("previewCode", cur_url);

      var templatePage = getUrlQueryValueByKey("templatePage", cur_url);
      if (templatePage) {
        this.isTemplatePage = true;
      } else {
        this.isTemplatePage = false;
      }

      var tabValue = getUrlQueryValueByKey("tabValue", cur_url);
      this.tabValue = tabValue;

      //判断是否有分享人id shareUserId
      this.shareUserId1 = getUrlQueryValueByKey("shareUserId", cur_url);
      if (this.shareUserId1) {
        store.commit("setShareUserId", this.shareUserId1); //1344185866492006402
      } else {
        store.commit("setShareUserId", ""); //1344185866492006402
      }
      console.log('shareUserId1', this.shareUserId1, store.state.shareUserId, "this.shareUserId1");

      // 判断是否有队伍id
      this.teamId = getUrlQueryValueByKey("teamId", cur_url);
      if (this.teamId) {
        store.commit("setTeamId", this.teamId);
      } else {
        store.commit("setTeamId", "");
      }

      this.giftType = getUrlQueryValueByKey("giftType", cur_url);
      if (this.giftType) {
        sessionStorage.setItem("giftType", this.giftType);
      } else {
        sessionStorage.setItem("giftType", null);
      }

      this.imageMessage = getUrlQueryValueByKey("imageMessage", cur_url);
      if (this.imageMessage) {
        sessionStorage.setItem("imageMessage", this.imageMessage);
      } else {
        sessionStorage.setItem("imageMessage", null);
      }

      this.isGiftTrue = getUrlQueryValueByKey("isGiftTrue", cur_url);
      if (this.isGiftTrue) {
        sessionStorage.setItem("isGiftTrue", this.isGiftTrue);
      } else {
        sessionStorage.setItem("isGiftTrue", null);
      }

      this.gameDraw = getUrlQueryValueByKey("gameDraw", cur_url);
      if (this.gameDraw) {
        sessionStorage.setItem("gameDraw", this.gameDraw);
      } else {
        sessionStorage.setItem("gameDraw", null);
      }
      //判断是否有商品信息
      this.prizeInfoId = getUrlQueryValueByKey("prizeInfoId", cur_url);
      if (this.prizeInfoId) {
        sessionStorage.setItem("prizeInfoId", this.prizeInfoId);
      } else {
        sessionStorage.setItem("prizeInfoId", null);
      }

      // 判断是否有fordevpclogin参数
      this.fordevpclogin = getUrlQueryValueByKey("fordevpclogin", cur_url);
      if (this.fordevpclogin) {
        sessionStorage.setItem("fordevpclogin", this.fordevpclogin);
      } else {
        sessionStorage.setItem("fordevpclogin", null);
      }
      initPage((initdatas)=>{
        if(!this.isTemplatePage){
          this.templateId = initdatas.data.templateCode
          store.commit("setTemplateId", this.templateId); 
          sessionStorage.setItem("templateId", this.templateId);
        }
        if(initdatas.resp_code == 0){
          //判断是否有商品信息end
          getUuid().then(res => {
            console.log(res,'getUuid判断是否有商品信息end')
            let uuid = res.uuid
            sessionStorage.setItem("uuid", uuid);
            let params = { ...res }
            this.getToken1(params);
          })
        }
      })
    },
    getToken1(params) {
      // console.log('getToken');
      // console.log('asdasdasd', 'asdasdasd');

      if (this.isTemplatePage) {
        let cur_url = location.href;
        var activityData = getUrlQueryValueByKey("activityData", cur_url);
        // console.log(activityData, 'this.activityData');
        if (activityData) {
          if (this.shareUserId1) {
            let queryData = {
              activityType: this.activityType,
              templateId: this.templateId,
              shopId: this.shopId,
              activityData: activityData,
              tabValue: this.tabValue,
              giftType: this.giftType,
              shareUserId: this.shareUserId1,
              prd: sessionStorage.getItem("prd"),
            };
            for (let i in queryData) {
              if (!queryData[i] || queryData[i] == "null") {
                this.$delete(queryData, i);
              }
            }
            this.$router.replace({
              path: "/templatePage",
              query: queryData,
            });
          } else {
            if (this.imageMessage) {
              this.$router.replace({
                path: "/templatePage",
                query: {
                  activityType: this.activityType,
                  templateId: this.templateId,
                  shopId: this.shopId,
                  activityData: activityData,
                  tabValue: this.tabValue,
                  giftType: this.giftType,
                  imageMessage: this.imageMessage,
                  prd: sessionStorage.getItem("prd"),
                },
              });
            } else {
              let queryData = {
                activityType: this.activityType,
                templateId: this.templateId,
                shopId: this.shopId,
                activityData: activityData,
                tabValue: this.tabValue,
                giftType: this.giftType,
                prd: sessionStorage.getItem("prd"),
              };
              for (let i in queryData) {
                if (!queryData[i] || queryData[i] == "null") {
                  this.$delete(queryData, i);
                }
              }
              this.$router.replace({
                path: "/templatePage",
                query: queryData,
              });
            }
          }
        } else {
          if (this.imageMessage) {
            this.$router.replace({
              path: "/templatePage",
              query: {
                activityType: this.activityType,
                templateId: this.templateId,
                shopId: this.shopId,
                activityData: activityData,
                tabValue: this.tabValue,
                giftType: this.giftType,
                imageMessage: this.imageMessage,
                prd: sessionStorage.getItem("prd"),
              },
            });
          } else {
            this.$router.replace({
              path: "/templatePage",
              query: {
                activityType: this.activityType,
                templateId: this.templateId,
                shopId: this.shopId,
                previewCode: this.previewCode
              },
            });
          }
        }
      } else {
        if (process.env.NODE_ENV == "development") {
          getToken(params,(datas) => {
            console.log(datas, "datas");
            getDefenseUrls(res => {
              //if(res.data.length > 0){
                store.commit("setDefenseList", res.data);
                sessionStorage.setItem("defenseList", res.data);
                initPinToken(res => {
                  this.checkIsMySelf(datas);
                })
              //}
            })
            //判断joinCode
            // console.log( location.origin,'location.origin')
          });
        } else {
          let isAndroid = window.navigator.userAgent.startsWith('jdmini;android')
          if (isAndroid) {
            let openStr = 'openapp.jdmobile://virtual?params={"des":"upgrade","category":"jump","sourceType":"1","sourceValue":"1"}'
            location.href = openStr
            setTimeout(() => {
              location.href = 'xwebview://history.back/'
            }, 1000)
          } else {
            // console.log(store.state.clientType, '京东登录运行环境');
            jdLogin((data) => {
              // console.log(data, "京东登录1154545");
              // console.log(location.href, '京东登录回调');
              getToken(params,(datas) => {
                getDefenseUrls(res => {
                  //if(res.data.length > 0){
                    store.commit("setDefenseList", res.data);
                    sessionStorage.setItem("defenseList", res.data);
                    initPinToken(res => {
                      this.checkIsMySelf(datas);
                    })
                  //}
                })
                //判断joinCode
                // this.checkIsMySelf(datas);
              });
            });
          }

        }
      }
    },
    //查询是否是自己
    checkIsMySelf(loginDatas) {
      // console.log(loginDatas,"查询是否是自己");
      if (this.shareUserId1) {
        myselfBargain((data) => {
          if (data.data == true) {
            this.shareUserId1 = null;
            store.commit("setShareUserId", ""); //1344185866492006402
            this.inToPage(loginDatas);
          } else {
            this.inToPage(loginDatas);
          }
        });
      } else {
        this.inToPage(loginDatas);
      }
    },
    //进入页面
    inToPage(loginDatas) {
      if (loginDatas == "success") {
        // console.log("进入页面");
        let queryData = {
          activityType: this.activityType,
          templateId: this.templateId,
          activityId: store.state.activityId,
          nodeId: this.nodeId,
          teamId: this.teamId,
          giftType: sessionStorage.getItem("giftType"),
          isGiftTrue: sessionStorage.getItem("isGiftTrue"),
          shareUserId: this.shareUserId1,
          prd: sessionStorage.getItem("prd"),
          gameDraw: sessionStorage.getItem('gameDraw')
        };
        for (let i in queryData) {
          if (!queryData[i] || queryData[i] == "null") {
            this.$delete(queryData, i);
          }
        }
        this.$router.replace({
          path: "/index",
          query: queryData,
        });
      } else {
        // console.log('asssssssssssss')
        sessionStorage.setItem('joinInfo', JSON.stringify(loginDatas))

        let queryData = {
          activityType: this.activityType,
          templateId: this.templateId,
          activityId: store.state.activityId,
          nodeId: this.nodeId,
          teamId: this.teamId,
          giftType: sessionStorage.getItem("giftType"),
          isGiftTrue: sessionStorage.getItem("isGiftTrue"),
          shareUserId: this.shareUserId1,
          prd: sessionStorage.getItem("prd"),
          gameDraw: sessionStorage.getItem('gameDraw')
        };
        for (let i in queryData) {
          if (!queryData[i] || queryData[i] == "null") {
            this.$delete(queryData, i);
          }
        }
        this.$router.replace({
          path: "/noMeet",
          query: queryData,
        });

        // let code = loginDatas.joinInfo.joinCodeInfo.joinCode;
        // if (code == 1002 || code == 1003) {
        //   if(this.activityType == 10021 && this.templateId == "20220321151200dzpcj02"){
        //     AmnjoinVipService.create(loginDatas, (submit) => {});
        //   }else if(this.activityType == 10021 && this.templateId == "20220418140400dzpcj04"){
        //     AmnjoinVipService.create(loginDatas, (submit) => {});
        //   }else if(this.activityType == 10071){
        //     AmnjoinVipService.create(loginDatas, (submit) => {});
        //   }else{
        //     joinVipService.create(loginDatas, (submit) => {});
        //   }

        // }
        // if (code == 1004 || code == 1005 || code == 1006) {
        //   if(this.activityType == 10021 && this.templateId == "20220321151200dzpcj02"){
        //     AmnsureFollowShopPopComService.create(loginDatas, (submit) => {});
        //   }else if(this.activityType == 10021 && this.templateId == "20220418140400dzpcj04"){
        //     AmnsureFollowShopPopComService.create(loginDatas, (submit) => {});
        //   }else if(this.activityType == 10071){
        //     AmnsureFollowShopPopComService.create(loginDatas, (submit) => {});
        //   }else{
        //      sureFollowShopPopComService.create(loginDatas, (submit) => {});
        //   }

        // }
      }
    },
  },
};
</script>

<style lang="scss">
.hello-box{
  background-image: url(https://img10.360buyimg.com/imgzone/jfs/t1/419525/9/4832/622055/00504d01F2b6e28b3/00832ee536b481ee.gif);
  background-repeat: no-repeat;
  background-size: 100%;
  width: 100%;
  height: 100vh;
  .loadingBox{
    font-size: 0.24rem;
    color: #333333;
    text-align: center;
    width: 100%;
    position: absolute;
    top: 65%;
  }
}
</style>
