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

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

    // plus.push.addEventListener("click", function (msg) {
    //   if (permision.isIOS) {
    //     console.log("ios:click收到" + JSON.parse(JSON.stringify(msg)).payload);
    //     var obj = JSON.parse(JSON.stringify(msg)).payload;
    //     that.toPushMessagePage(obj.data ? obj.data : obj);
    //   } else {
    //     console.log("安卓:click收到" + JSON.parse(JSON.stringify(msg)));
    //     var obj = JSON.parse(JSON.stringify(msg)).payload;
    //     that.toPushMessagePage(obj.data ? obj.data : obj);
    //   }
    // });
    // plus.push.addEventListener("receive", (message) => {
    //   console.log("receive收到");
    //   //收到透传消息，执行该事件
    //   let payload = message.payload; //自定义内容获取
    //   let text = message.payload.text;

    //   if (permision.isIOS) {
    //     console.log("ios:receive收到");
    //     let payload = message.payload; //自定义内容获取
    //     let text = message.content;
    //     that.createLocalMessage(text, payload);
    //   } else {
    //     console.log("安卓收到");
    //     that.toPushMessagePage(payload);
    //   }
    // });

    this.initAppOrderPush();
    // #endif

    // #ifdef H5
    this.initH5OrderPush();
    // #endif
  },
  computed: {
    ...mapState("user", ["uid", "userInfo", "socket"]),
  },
  onShow: function () {
    setTimeout(() => {
      if (this.userInfo && Object.keys(this.userInfo).length > 0) {
        this.connectSocket(this.userInfo);
      }
    });
  },
  onHide: function () {},
  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);
          }
        );
      });
    },

    initH5OrderPush() {
      let _this = this;
      GtPush.setDebugMode(true);
      GtPush.init({
        appid: "KXgzOaKSzd5HG3p9IPaVa8",
        onClientId: (res) => {
          console.log("onClientId = " + res.cid);
          _this.cid = res.cid;
        },
        onlineState: (res) => {
          console.log("onlineState = " + res.online);
          _this.online = res.online;
        },
        onPushMsg: (res) => {
          console.log("onPushMsg = " + res.message);
          debugger;
          _this.messages += "onPushMsg: " + res.message + "\n";
        },
        onError: (res) => {
          _this.messages += "onError: " + res.error + "\n";
        },
      });
    },
    async initAppOrderPush() {
      var pinf = plus.push.getClientInfo();
      var cid = pinf.clientid; //客户端标识

      console.log("当前机子CID：", cid);

      plus.push.getClientInfoAsync((info) => {
        let cid = info["clientid"];
        console.log("当前机子CID：", cid);
      });

      let res = await this.getCidAsync();
      console.log("getCidAsync", res);

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

      uni.showToast({
        title: cid,
        icon: "success",
        duration: 20000,
      });

      const _self = this;
      //监听系统通知栏消息点击事件
      plus.push.addEventListener(
        "click",
        function (message) {
          //处理点击消息的业务逻辑代码
          console.log(message);
          plus.nativeUI.toast("click:" + JSON.stringify(message));
          //plus.nativeUI.toast('push click');
          _self._handlePush(message);
          uni.showToast({
            title: message,
            icon: "none",
            duration: 1000,
          });
        },
        false
      );

      //监听接收透传消息事件
      plus.push.addEventListener(
        "receive",
        function (message) {
          console.log(message);
          plus.nativeUI.toast("receive:" + JSON.stringify(message));
          //处理透传消息的业务逻辑代码
          //plus.nativeUI.toast('push receive');
          _self._handlePush(message);
          uni.showToast({
            title: message,
            icon: "none",
            duration: 1000,
          });
        },
        false
      );
    },
  },
};
</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>
