<script>
  import store from '@/store'
  import socketStore from '@/uni_modules/vrapile-im/store'
  import { getToken } from '@/utils/token'
	import { setNodeId, getNodeId } from '@/utils/nodeId'
	import { getLongRandom } from '@/utils/nineTool';
	export default {
		globalData: {		
			// appId
			appId: "nine-demo-im", 
			// 系统平台
			os: "android", 
			// 消息页所在索引
			msgTabBarIndex: 0, 
			// 版本号
			version: "1.0.0", 
			// 手机信息
			systemInfo: {}, 
		},
		onLaunch(options) {
			// 初始化应用配置
			this.initConfig(options);
		},
		onShow() {
			// 连接websocket
			this.initWebSocket();
		},
		onHide() {
		},
		methods: {
			initConfig(options) {
				// 获取手机信息
				this.globalData.systemInfo = uni.getSystemInfoSync()
				this.globalData.os = this.globalData.systemInfo.platform
				// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					if(wgtinfo.version){
						this.globalData.version = wgtinfo.version;
					}else{
						// 获取当前版本号
						this.globalData.version = plus.runtime.version;
					}
				});
				// #endif
				// 终端
				this.globalData.terminal = "unine_other";
				// #ifdef MP-WEIXIN
				this.globalData.terminal = "unine_wx";
				// #endif
				// #ifdef H5
				this.globalData.terminal = "unine_h5";
				// #endif
				// #ifdef APP-PLUS
				this.globalData.terminal = "unine_app";
				// #endif
				
				// 节点
				let nodeId = getNodeId();
				if(!nodeId){
					nodeId = getLongRandom(24)
					setNodeId(nodeId)
				}
				
				// 扫码进来携带的数据
				if(options && options.query && options.query.invitee){
					this.globalData.invitee = options.query.invitee;
				}
			},
			initWebSocket(){
				if(getToken()){
					// #ifdef APP-PLUS
					// app无法获取本地存储的websocket状态，只能进行重连
					socketStore.dispatch('GetChatList', {userId: store.state.user.userInfo.userId}).then( res => {
						socketStore.dispatch('ConnSocket', { url: import.meta.env.VITE_APP_SOCKET_URL, token: getToken() });
					});
					// #endif
					// #ifndef APP-PLUS
					if(!socketStore.state.socket.websocket 
						|| !socketStore.state.socket.websocket.getReadyState 
						|| socketStore.state.socket.websocket.getReadyState() != 1){
						socketStore.dispatch('GetChatList', {userId: store.state.user.userInfo.userId}).then( res => {
							socketStore.dispatch('ConnSocket', { url: import.meta.env.VITE_APP_SOCKET_URL, token: getToken() });
						});
					}
					// #endif
				}
			}
		}
	}
</script>

<style lang="scss">
html, body, #app{
	height: 100%;
	background-color: #f1f1f1;
}
.nine-content-001{
	padding: 0;
	margin: 0;
	width: 750rpx;
	height: calc(100vh - 0px);
	background-color: #f1f1f1;
	display: flex;
	flex-direction: column;
}
.nine-content-002{
	padding: 0;
	margin: 0;
	width: 750rpx;
	height: calc(100vh - 50px);
	/* #ifndef H5 */
	height: calc(100vh - 0px);
	/* #endif */
	background-color: #f1f1f1;
	display: flex;
	flex-direction: column;
}
// uni.showModal 左对齐
.uni-modal__bd{
	text-align: left
}

@import './static/style/nine-image-001.scss';
@import './static/style/nine-btn-001.scss';
@import './static/style/nine-list-001.scss';
@import './static/style/nine-nav-001.scss';
@import './static/style/nine-base-002.scss';
@import './static/style/nine-base-001.scss';
</style>
