<script>
// #ifdef APP-PLUS
import APPUpdate, { getCurrentNo } from "@/config/appUpdate";
// #endif
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      version: "",
    };
  },
  onLaunch: function () {
    uni.setStorageSync("isImSound", true);

    this.globalData.isAppActive = true;
    // #ifdef APP-PLUS
    getCurrentNo((res) => {
      this.version = res.version;
    });

    const hasAgreed = uni.getStorageSync("isNeedAgreementDialog");
    if (hasAgreed) {
      this.onAPPUpdate();
    }

    this.initAppOrderPush();
    // #endif
  },
  computed: {
    ...mapState("user", ["uid", "userInfo", "socket"]),
  },
  onShow: function () {
    this.globalData.isAppActive = true;

    setTimeout(() => {
      if (this.userInfo && Object.keys(this.userInfo).length > 0) {
        this.connectSocket(this.userInfo);
      }
    });
  },
  onHide: function () {
    this.globalData.isAppActive = false;
  },
  globalData: {
    isAppActive: false,
  },
  onBackPress: function () {},
  methods: {
    ...mapActions("user", ["connectSocket"]),
    onAPPUpdate() {
      APPUpdate(this.version);
    },
    getCidAsync() {
      return new Promise(function (resolve, reject) {
        // 获取客户端推送信息
        plus.push.getClientInfoAsync(
          function (clientInfo) {
            // 获取CID
            var cid = clientInfo.clientid;
            // 调用resolve方法返回CID
            resolve(cid);
          },
          function (error) {
            // 获取CID失败，调用reject方法返回错误信息
            reject(error);
          }
        );
      });
    },
    initAppOrderPush() {
      uni.getPushClientId({
        success: (res) => {
          var push_clientid = res.cid;
          console.log("客户端推送标识cid:", push_clientid);
          uni.showToast({
            title: push_clientid,
            icon: "success",
            duration: 1000,
          });

          console.log(push_clientid);

          uni.setClipboardData({
            data: push_clientid, // 需要复制的内容
            success: () => {
              // 成功提示框显示 1 秒钟
              // uni.showToast({
              //   title: "复制成功",
              //   icon: "success",
              //   duration: 1000,
              // });
            },
            fail: () => {
              // 失败提示框显示 1 秒钟
              uni.showToast({
                title: "复制失败，请重试",
                icon: "none",
                duration: 1000,
              });
            },
          });
        },
        fail(err) {
          console.log(err);
        },
      });

      uni.onPushMessage((res) => {
        console.log("收到推送消息：", res); //监听推送消息

        if (res.data) {
          const app = getApp();
          if (!app.globalData.isAppActive) {
            // 读取历史数据
            const ordrPushHistoryList =
              uni.getStorageSync("PUSH_MSG_DATA") || [];
            // 追加新数据（确保 historyList 是数组）
            const newList = Array.isArray(ordrPushHistoryList)
              ? [...ordrPushHistoryList, res.data]
              : [res.data];
            // 存储更新后的数组
            uni.setStorageSync("PUSH_MSG_DATA", newList);
            // 创建通知
            uni.createPushMessage(res.data);
          } else {
            console.log("应用已打开，直接处理消息");
            // 可以在这里直接更新 UI
            this.$store.dispatch("order/orderPush", res.data);
          }
        }
      });

      const _self = this;
      //监听系统通知栏消息点击事件
    },
  },
};
</script>

<style lang="scss">
// @import "./uni_modules/uview-ui/index.scss";
// @import "./uni_modules/uview-ui/theme.scss";
/*每个页面公共css */
@import "@/static/reset.css";
@import "@/styles/myui.scss";

@import "@/static/font/iconfont.css";

::v-deep.uni-tabbar-bottom {
  display: none;
}

::v-deep.uni-navbar {
  min-height: 110rpx;
}
</style>
