<script>
import { mapState, mapMutations } from "vuex";
const systemInfo = uni.getSystemInfoSync();
const statusBarHeight = Math.round(
  (750 / systemInfo.screenWidth) * systemInfo.statusBarHeight
);
import request from "./common/httpRequest.js";
import cache from "./common/cache.js";
import plusHelper from "@/common/helper.js";
import { initOwnedRoutes } from "./router";
import { getFlattenMenu, initMenu } from "./utils/menu";
import { initToken } from "./utils/token";
import { initCheckLoginTime } from "./utils/checkLoginTime";



export default {
  computed: {
    ...mapState(["hasLogin", "openid", "userinfo"]),
  },
  globalData: {
    systemInfo: systemInfo,
    statusBarHeight: statusBarHeight + "rpx",
    statusBarHeightNum: statusBarHeight,
    request: request,
    cache: cache,
  },
  onLaunch: async function () {
    // await initCheckLoginTime();
    initToken();
    initMenu();
    initOwnedRoutes(getFlattenMenu());
    if (process.env.NODE_ENV === "development") {
      uni.setKeepScreenOn({
        keepScreenOn: true,
      });
    }
    // #ifdef APP-PLUS
    this.getDeviceInfo();
    // #endif

    //#ifdef APP-PLUS
    plus.screen.lockOrientation("portrait-primary"); //锁定屏幕方向
    //#endif
	let systemInfo = uni.getSystemInfoSync()
	
	if(!!systemInfo.platform && systemInfo.platform === 'com.alibaba.dingtalk' && !this.hasLogin)
	{
		dd.ready(function() {
			dd.runtime.permission.requestAuthCode({
				corpId: "2988194002", // 企业id
				onSuccess: function (info) {
					console.log('钉钉code'+info.code)
				}});
		});
	}


    //APP推送代码，功能正常，原封不动复制https://www.icode9.com/content-4-999837.html
    // //#ifdef APP-PLUS
    // var info = plus.push.getClientInfo();
    // info && uni.setStorageSync('clientInfo', info);
    // plus.push.addEventListener(
    // 	'click',
    // 	function(msg) {
    // 		console.log('click', JSON.stringify(msg));
    // 		// IOS
    // 		if (uni.getSystemInfoSync().platform == 'ios') {
    // 			var payload;
    // 			if (msg.type == 'click') {
    // 				payload = msg.payload;
    // 			} else {
    // 				payload = JSON.parse(msg.payload);
    // 			}
    // 			if (payload != null || payload != undefined) {
    // 				setTimeout(() => {
    // 					uni.navigateTo({
    // 						url: payload
    // 					});
    // 				}, 1000);
    // 			}
    // 		} else {
    // 			// Android
    // 			var payload = msg.payload;
    // 			if (payload != null || payload != undefined) {
    // 				setTimeout(() => {
    // 					uni.navigateTo({
    // 						url: payload
    // 					});
    // 				}, 1000);
    // 			}
    // 		}
    // 	},
    // 	false
    // );
    // plus.push.addEventListener(
    // 	'receive',
    // 	function(msg) {
    // 		if (uni.getSystemInfoSync().platform == 'ios') {
    // 			if (msg.type == 'receive') {
    // 				plus.push.createMessage(msg.content, JSON.stringify(msg.payload), {
    // 					title: msg.title
    // 				});
    // 			}
    // 		} else {
    // 			var payload = JSON.parse(msg.content);
    // 			var messageTitle = payload.title;
    // 			var messageContent = payload.content;
    // 			plus.push.createMessage(messageContent, payload, {
    // 				title: messageTitle
    // 			});
    // 		}
    // 	},
    // 	false
    // );
    // //#endif
  },
  onShow: function () {
    console.log("App Show");
    // this.goLogin();
  },
  onHide: function () {
    console.log("App Hide");
    // #ifdef APP-PLUS
    // if (plus.push.getAllMessage()) {
    // 	plus.push.clear();
    // }
    // #endif
  },
  onError: function (err) {
    //全局错误监听
    // #ifdef APP-PLUS
    plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
      const res = uni.getSystemInfoSync();
      let errMsg = `手机品牌：${res.brand}；手机型号：${res.model}；操作系统版本：${res.system}；客户端平台：${res.platform}；错误描述：${err}`;
      console.log("发生错误：" + errMsg);
    });
    // #endif
  },
  methods: {
    ...mapMutations(["getUseriInfo", "login"]),
    isIos() {
      return systemInfo.platform === "android" ? false : true;
    },
    isAndroid() {
      return systemInfo.platform === "ios" ? false : true;
    },
    isApp() {
      // #ifdef APP-PLUS
      return true;
      // #endif
      return false;
    },
    config(name) {
      return request.config(name);
    },
    goLogin() {
      console.log("app.vue");
      // if (this.hasLogin == false) {
      //   uni.redirectTo({
      //     url: "/pages/common/login/login?href2=user/user/index",
      //   });
      // }
    },
    //#ifdef APP-PLUS
    async getDeviceInfo() {
      let uuid = "";
      if (this.isIos()) {
        uuid = plusHelper.getIdfa();
      } else {
        var device = await plusHelper.getDeviceInfo();
        try {
          uuid = device.uuid[0];
        } catch (e) {
          uuid = e.imei.split(",");
        }
      }
      this.$SysCache.put("plus_device_uuid", uuid);
      console.log("获取到的--->uuid：", uuid);
    },
    //#endif
  },
};
</script>

<style>
/*每个页面公共css uParse为优化版本*/
@import "./common/app.css";
/* #ifndef APP-NVUE */
@import "./components/uni/uParse/src/wxParse.css";
/* #endif */
uni-toast,
uni-modal {
  z-index: 99999999;
}
</style>
