<script>
import router from "./static/common/js/router";
import config from "./static/common/js/config.js";

var jweixin = require("jweixin-module");
export default {
  data() {
    return {
      show: false,
      // WebSocket 实例与连接控制
      ws: null, // 当前 WebSocket 连接
      lockReconnect: false, // 是否处于稳定连接，防止重复重连
      // timeout: 30000, // 心跳间隔（毫秒）
      timeoutObj: null, // 心跳倒计时定时器
      serverTimeoutObj: null, // 心跳响应等待定时器
      timeoutnum: null, // 重连延时定时器
    };
  },
  globalData: {},
  created() {},
  onLaunch() {
    if (typeof window.entryUrl === "undefined" || window.entryUrl === "") {
      window.entryUrl = location.href.split("#")[0];
    }
    router.initApp(this);
    // 处理项目加载时白名单不生效异常问题
    let isUrl = this._route.fullPath.split("?")[0];
    let notNeed = config.whiteList.includes(isUrl);
    if (notNeed) {
      uni.navigateTo({
        url: this._route.fullPath,
      });
      return;
    }
    var that = this;
    uni.getSystemInfo({
      success: function (res) {
        // 根据 model 进行判断
        if (res.model.indexOf("iPhone") >= 0) {
          that.$u.vuex("vuex_iPhone", true);
        }
      },
    });

    if (!that.vuex_token) {
      const type = that.vuex_userType || 0; // 0:学生 1:教师
      this.$u.vuex("vuex_user", "");
      this.$u.vuex("vuex_token", "");
      uni.clearStorage();
      uni.reLaunch({
        url: `/pages/login/login/index?type=${type}`,
      });
      return;
    }

    // 根据用户类型跳转对应主页
    const userType = that.vuex_userType || 0;
    const currentPath = that._route.path;

    // 只有在根路径（"/"）或 首页（"/pages/home/index/index"）或教师端首页（"/pages/consultation/index"）时才进行跳转判断
    // 这样可以避免在其他有权限的页面刷新时被强制重定向
    const isRootPath =
      currentPath === "/" ||
      currentPath === "/pages/home/index/index" ||
      currentPath === "/pages/consultation/index";

    if (isRootPath) {
      if (userType === 1) {
        // 教师端主页
        if (currentPath !== "/pages/consultation/index") {
          uni.reLaunch({
            url: "/pages/consultation/index",
          });
        }
      } else {
        // 学生端主页
        if (currentPath !== "/pages/home/index/index") {
          uni.reLaunch({
            url: "/pages/home/index/index",
          });
        }
      }
    }

    return;
    if (!that.vuex_user.isFill) {
      this.$u.vuex("vuex_msgList", "");
      this.$u.vuex("vuex_user", "");
      this.$u.vuex("vuex_token", "");
      uni.clearStorage();
      this.$u.route({
        url: "/pages/login/login/login",
      });
      return;

      uni.navigateTo({
        url: "/pages/login/roleSelection",
      });
      return;
    }
  },
  // onShow(){
  //  console.log('onShow')
  // },
  methods: {
    // 构建 WebSocket 连接地址（与 oa-web-phone 保持一致的握手参数）
    buildWsUrl() {
      const protocol =
        window.location.protocol.indexOf("https") === 0 ? "wss" : "ws";
      const Id =
        (this.vuex_user && (this.vuex_user.id || this.vuex_user.Id)) || "";
      return `${protocol}://120.55.234.65:8073/api/Dialogue/HandleConnection?Id=${Id}`; // &equipmentType=0
    },
    // 初始化原生 WebSocket 连接
    initWebSocket() {
      try {
        this.ws = new WebSocket(this.buildWsUrl());
        this.ws.onopen = () => this.handleWsOpen();
        this.ws.onmessage = (e) => this.handleWsMessage(e);
        this.ws.onclose = (e) => this.handleWsClose(e);
        this.ws.onerror = (e) => this.handleWsError(e);
      } catch (err) {
        console.log("[WebSocket] 创建连接失败:", err);
        this.reconnect();
      }
    },
    // 重连（防抖，避免频繁重试）
    reconnect() {
      if (this.lockReconnect) return;
      this.timeoutnum && clearTimeout(this.timeoutnum);
      this.timeoutnum = setTimeout(() => {
        this.startLink();
      }, 5000);
    },
    // 心跳重置并重启
    reset() {
      this.timeoutObj && clearTimeout(this.timeoutObj);
      this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
      this.start();
    },
    // 启动心跳与超时处理
    start() {
      this.timeoutObj = setTimeout(() => {
        //这里发送一个心跳，后端收到后，返回一个心跳消息
        if (
          this.ws &&
          this.ws.readyState === 1 &&
          this.vuex_user &&
          (this.vuex_user.Id || this.vuex_user.id)
        ) {
          //如果连接正常
          this.ws.send("heartCheck");
        } else {
          //否则重连
          this.lockReconnect = false;
          this.reconnect();
        }

        // 心跳发送后等待后端响应，超时则断开重连
        this.serverTimeoutObj = setTimeout(() => {
          // console.log("[WebSocket] 心跳响应超时，断开重连");
          this.ws && this.ws.close();
          this.lockReconnect = false;
          this.reconnect();
        }, 30000);
      }, 30000);
    },
    // 连接成功
    handleWsOpen() {
      this.lockReconnect = true;
      // 可按需缓存连接实例到全局
      // this.$u.vuex('vuex_websocket', this.ws)
      this.reset();
    },
    // 收到消息
    handleWsMessage(e) {
      // 收到任何消息都重置心跳
      this.reset();

      // console.log("[WebSocket] 收到消息:", e);

      // 心跳消息不处理
      // if (typeof e.data === "string" && e.data.indexOf("heartCheck") >= 0) {
      //   return;
      // }

      // 尝试解析为 JSON（与 oa-web-phone 保持一致的结构：{ Type, Data }）
      let msgData = null;
      try {
        msgData = JSON.parse(e.data);
      } catch (err) {
        console.log("[WebSocket] 消息解析失败:", err, e.data);
        return;
      }

      const type = msgData.Type;

      console.log("收到消息:", type, msgData);
      // 收到服务端心跳响应不处理
      if (type === "pong") return;

      // 退出登录通知
      if (type === "Exit") {
        try {
          this.ws && this.ws.close();
        } catch (err) {}
        // 清理本地登录信息并返回登录页
        this.$u.vuex("vuex_user", "");
        this.$u.vuex("vuex_token", "");
        uni.clearStorage();
        const typeNum = this.vuex_userType || 0; // 0:学生 1:教师
        uni.reLaunch({ url: `/pages/login/login/index?type=${typeNum}` });
        return;
      }

      // 标记 TabBar 红点
      const tab = this.vuex_tabbar || [];
      if (Array.isArray(tab) && tab[1]) {
        tab[1].isDot = true;
        this.$u.vuex("vuex_tabbar", tab);
      }

      // 收到私聊消息
      if (type === "PrivateChatMessage") {
        const data = msgData.Data || {};
        const dialogueManagementId = data.DialogueManagementId || "";
        const processData = {
          dialogueManagementId,
          senderId: data.SenderId,
          receiverId: data.ReceiverId,
          sendDate: data.SendDate,
          message: data.Message,
          sendUserType: data.SendUserType, // 0 AI 1 学生 2 教师 3 提示
        };
        const id =
          data.Id || data.id || Math.random().toString(36).substring(2);
        const msg = {
          ...processData,
          messageType: 0,
          filePath: "",
          id,
        };
        const storeState = (this.$store && this.$store.state) || {};
        const isTransferChat = !!storeState.vuex_isTransferChat;
        const activeDialogueId =
          storeState.vuex_msgUser?.dialogueManagementId ||
          storeState.vuex_msgUser?.friendId ||
          storeState.vuex_msgUser?.id;
        const isActiveChat =
          activeDialogueId &&
          String(activeDialogueId) === String(processData.dialogueManagementId);

        if (isTransferChat) {
          if (isActiveChat) {
            this.$store.commit("push_AiLiveAgentMsg", msg);
          }
        } else {
          if (isActiveChat) {
            this.$store.commit("push_Msg", msg);
          }
        }

        // 更新会话列表的未读数 / 文案 / 时间，保持实时展示
        if (processData.dialogueManagementId) {
          this.$store.commit("apply_RealtimeMessageToList", {
            dialogueId: processData.dialogueManagementId,
            message: processData.message,
            sendDate: processData.sendDate,
            senderId: processData.senderId,
            receiverId: processData.receiverId,
          });
        }
      }
    },
    // 连接关闭
    handleWsClose(e) {
      // console.log(`[WebSocket] 连接关闭: code=${e.code}, reason=${e.reason}`);
      this.lockReconnect = false;
      this.reconnect();
    },
    // 连接错误
    handleWsError(e) {
      // console.log("[WebSocket] 连接错误:", e);
      this.lockReconnect = false;
      this.reconnect();
    },
    // 启动连接入口（需存在用户）
    startLink() {
      if (this.lockReconnect) return;
      const hasUser =
        this.vuex_user && (this.vuex_user.id || this.vuex_user.Id);
      const hasToken =
        !!this.vuex_token || !!window.localStorage.getItem("token");
      if (hasUser && hasToken) {
        this.initWebSocket();
      } else {
        this.lockReconnect = false;
        this.reconnect();
      }
    },
  },
  mounted() {
    // 使用与 oa-web-phone 相同的原生 WebSocket 通信方式
    this.startLink(); // 现在一直断线重连，先注释
  },
};
</script>

<style lang="scss">
@import "uview-ui/index.scss";
@import "common/demo.scss";

.u-tabbar__content__item__button {
  top: 0.07rem !important;
}

.u-navbar-placeholder,
.u-navbar-inner {
  height: 0.44rem !important;
}

.u-tabbar__content__item__text {
  font-size: 20rpx !important;
}

.u-tabbar__content__item__button .u-icon__img {
  width: 44rpx !important;
  height: 44rpx !important;
}

.u-size-mini {
  font-size: 0.12rem !important;
}

uni-modal .uni-modal__hd {
  padding: 0.1rem;
}

uni-modal .uni-modal__bd {
  padding-top: 20rpx;
  // font-weight: bold;
  font-size: 34rpx;
  color: rgba(0, 0, 0, 0.9);
}

uni-modal .uni-modal__btn {
  border-radius: 16rpx;
  font-size: 28rpx;
}

uni-modal .uni-modal__btn:after {
  border: none !important;
}

uni-modal .uni-modal__btn_default {
  background: #e6f6ff;
  color: #3cb5fb !important;
  margin-right: 20rpx;
}

uni-modal .uni-modal__ft {
  line-height: 80rpx;
}

uni-modal .uni-modal__ft:after {
  border: none !important;
}

uni-modal .uni-modal__btn_primary {
  background: #3cb5fb;
  color: #ffffff !important;
}

uni-modal .uni-modal {
  padding: 40rpx;
  box-sizing: border-box;
  border-radius: 30rpx;
}

.u-tabbar__content {
  height: 0.5rem !important;
}

.phone .u-tabbar__content {
  padding-bottom: 0.14rem;
}

.u-tabs-scorll-flex .u-tabs-item {
  transition: all 0.1s;
  font-size: 0.15rem !important;
}

* {
  font-family: "pingfang";
}

html {
  font-size: 200rpx;
}

uni-page-body {
  height: 100%;
}

body {
  margin: 0;
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu",
    "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", "Microsoft Yahei",
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  // max-width: 1536rpx;
  margin: 0 auto;
  background-color: #f6f7fa;
}

view,
image,
text {
  box-sizing: border-box;
  flex-shrink: 0;
}

#app {
  width: 100vw;
  height: 100vh;
}

.flex-row {
  display: flex;
  flex-direction: row;
}

.flex-col {
  display: flex;
  flex-direction: column;
}

.justify-start {
  display: flex;
  justify-content: flex-start;
}

.justify-center {
  display: flex;
  justify-content: center;
}

.justify-end {
  display: flex;
  justify-content: flex-end;
}

.justify-evenly {
  display: flex;
  justify-content: space-evenly;
}

.justify-around {
  display: flex;
  justify-content: space-around;
}

.justify-between {
  display: flex;
  justify-content: space-between;
}

.items-start {
  display: flex;
  align-items: flex-start;
}

.items-center {
  display: flex;
  align-items: center;
}

.items-end {
  display: flex;
  align-items: flex-end;
}
</style>
