var app = getApp();
module.exports = Behavior({
  data: {},
  methods: {
    checktoInvite(data, flag) {
      let that = this;
      if (data.real_name_auth_is_open === 1) {
        // 判断实名插件是否开启
        if (data.is_auth !== 1) {
          // 是否已经实名过
          this.bind_real_name(data.real_name_auth_scene, app.globalData.pathData, flag);
        }
        return;
      }

      if (data.is_bind_mobile !== 0) {
        this.bindMobile(data.is_bind_mobile, app.globalData.pathData, flag);
      } else {
        if (data.is_login === 1) {
          // 登录
          if (data.is_invite !== 1) {
            // 是否填写过邀请人
            if (data.invite_page === 1) {
              //是否开启邀请页面
              wx.redirectTo({
                url: "/packageC/member/visit/Inviter/Inviter",
              });
            } else {
              that.gotourl(flag);
            }
          } else {
            if (typeof data.invite_mobile != "undefined" && data.invite_mobile !== 1 && data.invite_page === 1) {
              wx.redirectTo({
                url: "/packageC/member/visit/Inviter/Inviter",
              });
            } else {
              that.gotourl(flag);
            }
          }
        } else {
          that.gotourl(flag);
        }
      }
    },
    //查看用户是否需要绑定手机号   fromApp是路由跳转条件
    bindMobile(e, setpathData, flag) {
      if (e == 1) {
        //跳转绑定手机页面
        wx.reLaunch({
          url: "/packageA/member/editmobile/editmobile?logintype=1&fromApp=1",
        });
      } else if (e == 2 && setpathData.indexOf("index_v2/index_v2") > 0) {
        wx.reLaunch({
          url: "/packageA/member/editmobile/editmobile?logintype=1&fromApp=2",
        });
      } else if (e == 3 && setpathData.indexOf("detail_v2/detail_v2") > 0) {
        wx.reLaunch({
          url: "/packageA/member/editmobile/editmobile?logintype=1&fromApp=1",
        });
      } else if (e == 4 && setpathData.indexOf("extension/extension") > 0) {
        wx.reLaunch({
          url: "/packageA/member/editmobile/editmobile?logintype=1&fromApp=4",
        });
      } else {
        this.gotourl(flag);
      }
    },

    bind_real_name(scene, routeData, flag) {
      // 全局1 提现申请2  发布短视频3   发布微贴4  推广中心5
      if (routeData !== 'packageH/signPage/personBank/personBank') {
        wx.setStorageSync("real_name_redirect", routeData);
      }
      if (scene.indexOf('1') > -1) {
        wx.reLaunch({
          url: "/packageH/signPage/personBank/personBank?from=bindRealName",
        });
      } else if (scene.indexOf('2') > -1 && routeData == "packageA/member/withdrawal/withdrawal") {
        // 提现申请
        wx.reLaunch({
          url: "/packageH/signPage/personBank/personBank?from=bindRealName",
        });
      } else if (scene.indexOf('3') > -1 && (routeData == "packageC/video_goods/videoAgreement/videoAgreement" || routeData == "packageC/video_goods/releaseVideo/releaseVideo")) {
        // 发布短视频
        wx.reLaunch({
          url: "/packageH/signPage/personBank/personBank?from=bindRealName",
        });
      } else if (scene.indexOf('4') > -1 && routeData == "packageC/micro_communities/microRelease/microRelease") {
        // 发布微贴
        wx.reLaunch({
          url: "/packageH/signPage/personBank/personBank?from=bindRealName",
        });
      } else if (scene.indexOf('5') > -1 && routeData == "packageG/pages/member/extension/extension") {
        // 推广中心
        wx.reLaunch({
          url: "/packageH/signPage/personBank/personBank?from=bindRealName",
        });
      } else {
        this.gotourl(flag);
      }

    },
    gotourl(flag) {
      try {
        if (!this.data.yzredirect || flag === 'member') {
          this.gomember();
        } else {
          if (this.data.yzredirect.indexOf("login/login") > 0) {
            this.gomember();
          } else if (this.data.yzredirect.indexOf("pages/live-player-plugin") > 0) {
            //从直播进入商品详情授权登录
            let pages = getCurrentPages();
            let page = pages[pages.length - 2];
            page.onLoad(page.options);
            wx.navigateBack({
              delta: 1,
              fail: (res) => {
                // 防止跳转不了 尝试直接指向会员中心
                wx.reLaunch({
                  url: '/packageG/member_v2/member_v2',
                });
              }
            });
          } else {
            wx.reLaunch({
              url: "/" + this.data.yzredirect,
              fail: (res) => {
                // 防止跳转不了 尝试直接指向会员中心
                wx.reLaunch({
                  url: '/packageG/member_v2/member_v2',
                });
              }
            });
          }
        }
      } catch (e) {
        wx.reLaunch({
          url: "/packageG/index/index",
        });
      }
    },

    gomember() {
      wx.reLaunch({
        url: "/packageG/member_v2/member_v2",
      });
    },
  },
});