<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,
    };
  },
  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);
        }
      },
    });

    // debugger;

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

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

      uni.navigateTo({
        url: "/pages/login/roleSelection",
      });
      return;
    }
  },
  // onShow(){
  //  console.log('onShow')
  // },
  methods: {
    getLocation() {
      var that = this;
      if (this.isWechat()) {
        const isiOS = !!navigator.userAgent.match(
          /\(i[^;]+;( U;)? CPU.+Mac OS X/
        ); //ios终端
        // 进行签名的时候  Android 不用使用之前的链接， ios 需要
        const signLink = isiOS
          ? window.entryUrl
          : window.location.href.split("#")[0];
        //获取当前url然后传递给后台获取授权和签名信息，后台需要解码才能使用
        // const url =encodeURIComponent(signLink);
        const url = signLink;
        const data = {
          url: url,
        };
        this.$u.api.GetInfoMation(data).then((res) => {
          jweixin.config({
            debug: false,
            appId: res.appId,
            timestamp: res.timestamp,
            nonceStr: res.noncestr,
            signature: res.signature,
            jsApiList: [
              //这里是需要用到的接口名称
              "getLocation", //获取位置
              "openLocation", //打开位置
            ],
          });

          // jweixin.config 执行失败时调用
          jweixin.error((err) => {
            uni.hideLoading();
            console.log("授权失败，您可能无法使用部分功能", err);
          });
          jweixin.ready(function () {
            jweixin.getLocation({
              type: "gcj02", // 默认为wgs84的gps坐标，如果要返 回直接给openLocation用的火星坐标，可传入'gcj02'
              success: (res) => {
                uni.hideLoading();
                that.$u.vuex("vuex_userLocation", res);

                var data = {
                  userId: that.vuex_user.id,
                  longitude: res.longitude + "",
                  latitude: res.latitude + "",
                };
                that.$u.api.upPosition(data);
              },
              fail: function (res) {
                uni.hideLoading();
                console.log(res, "err");
              },
              complete: function (res) {
                uni.hideLoading();
                console.log(res, "is");
              },
            });
          });
        });
      } else {
        uni.getLocation({
          type: "gcj02 ",
          isHighAccuracy: true,
          highAccuracyExpireTime: 3000,
          success: (res) => {
            uni.hideLoading();

            this.$u.vuex("vuex_userLocation", res);
            var data = {
              userId: this.vuex_user.id,
              longitude: res.longitude + "",
              latitude: res.latitude + "",
            };
            this.$u.api.upPosition(data);
          },
          fail: (err) => {
            uni.hideLoading();
            console.log("地理位置获取失败", err);
          },
        });
      }
    },
    isWechat() {
      var ua = window.navigator.userAgent.toLowerCase();
      if (ua.match(/micromessenger/i) == "micromessenger") {
        // console.log(‘是微信客户端’)
        return true;
      } else {
        // console.log(‘不是微信客户端’)
        return false;
      }
    },
    charLink() {
      var that = this;
      (function start(ms) {
        setTimeout(() => {
          try {
            that.$connection
              .start()
              .then(() => {
                // console.log(that.$connection.connection.transport.webSocket._webSocket.url)
                // that.connectionId = that.$connection.connection.transport.url.split(
                // 	"=")[1]
                that.connectionId =
                  that.$connection.connection.transport.webSocket._webSocket.url.split(
                    "="
                  )[1];
                that.CharLogin();
              })
              .catch((e) => {
                //重试
                that.$connection.sto;
                console.log("websocket连接失败", e);
                return start(5000);
              });
          } catch (e) {
            //重试
            return start(5000);
          }
        }, ms);
      })(0);
      //接收消息
      this.$connection.on("ReceiveMessage", (user, message, type) => {
        if (this._route.fullPath.indexOf("dialogBox") < 0) {
          var tab = this.vuex_tabbar;
          tab[1].isDot = true;
          this.$u.vuex("vuex_tabbar", tab);
          var msgList = this.vuex_tabbar;
          var msgList = this.vuex_msgList;
          // 管理员对普通用户
          if (type == 4) {
            user = "admin";
          }
          if (type == 3) {
            user += "user";
          }
          if (msgList.indexOf(user) < 0) {
            msgList += user + ",";
          }
          this.$u.vuex("vuex_msgList", msgList);
        }
      });
      //接收系统消息
      this.$connection.on("SystemMessage", (title, content, time) => {
        if (this._route.fullPath.indexOf("sysList") < 0) {
          var tab = this.vuex_tabbar;
          tab[1].isDot = true;
          this.$u.vuex("vuex_tabbar", tab);
          var msgList = this.vuex_tabbar;
          var msgList = this.vuex_msgList;
          if (msgList.indexOf("SystemMessage") < 0) {
            msgList += "SystemMessage,";
          }
          this.$u.vuex("vuex_msgList", msgList);
        }
      });
      //接收互动消息
      this.$connection.on("InteractMessage", (data, type) => {
        if (this._route.fullPath.indexOf("interactionList") < 0) {
          var tab = this.vuex_tabbar;
          tab[1].isDot = true;
          this.$u.vuex("vuex_tabbar", tab);
          var msgList = this.vuex_tabbar;
          var msgList = this.vuex_msgList;
          if (msgList.indexOf("InteractMessage") < 0) {
            msgList += "InteractMessage,";
          }
          this.$u.vuex("vuex_msgList", msgList);
        }
      });
    },
    CharLogin() {
      //绑定用户账号和connectionId
      if (this.vuex_user.id) {
        this.$connection
          .invoke("login", this.connectionId, this.vuex_user.id)
          .then((res) => {})
          .catch((err) => {});
        //初始化获取定位
        // this.getLocation()
      } else {
        setTimeout(() => {
          this.CharLogin();
        }, 1000);
      }
    },
  },
  mounted() {
    this.charLink();
  },
};
</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>
