<script>
	export default {
		globalData: {  
			serverUrl: 'https://mdyg.sdmingde.cn/api/h5/',
			uploadServerUrl: 'https://mdyg.sdmingde.cn/api/h5/',
			appId: 'wx2f0195c58ad8c3ec',
			redirectUri: 'https://mdyg.sdmingde.cn/wap/visitor/#/pages/index/wechat',
			storagePre: 'visit_',
			host: 'https://mdyg.sdmingde.cn/wap/visitor/',
			userinfo:{}
		},
		onLaunch: function() {
			console.log('App Launch')
			var app = getApp();
			let token = uni.getStorageSync(app.globalData.storagePre+'token');
			if(token){
				app.globalData.userinfo.token = token;
				app.globalData.userinfo.userId = uni.getStorageSync(app.globalData.storagePre+'userId');
			}
			// app.globalData.userinfo.token = '1914162463525';
			// app.globalData.userinfo.userId = 1;

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			getUrlCode (name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
			},
			checkLogin: function(){
				
				var app = getApp();
				var token = app.globalData.userinfo.token;
				if(!token){
					let routes = getCurrentPages();
					let curRoute = routes[routes.length - 1].route //获取当前页面路由
					let curParam = routes[routes.length - 1].options; //获取路由参数
					let orgId = curParam.orgId?curParam.orgId:0;
					let type = curParam.type?curParam.type:0;
					if(curRoute == 'pages/index/add'&&orgId > 0){
						uni.reLaunch({ //关闭所有页面，跳转到闪屏页
							url: '/pages/index/wechat?state=' + orgId + '_' + type
						})
					}else{
						uni.reLaunch({ //关闭所有页面，跳转到闪屏页
							url: '/pages/index/wechat'
						})
					}
				}
			},
			checkCode:function(code){
			    if(code == -100){
					uni.showToast({
						title: '登录超时,请重新登录',
						icon: 'none',
						duration: 2000
					})
					var app = getApp();
					uni.removeStorageSync(app.globalData.storagePre+'token');
					uni.removeStorageSync(app.globalData.storagePre+'userId');
					setTimeout(function(){
						uni.reLaunch({ //关闭所有页面，打开到应用内的某个页面
							url: '/pages/index/wechat'
						})
					},2000);
					return false;
			    }
			    return true;
			},
			ajax: function(params){
			    if (!params.type) {
					var type = 'POST';
			    }else{
					var type = params.type;
			    }
			    if (!params.apiname) {
					var apiname = '1';
			    } else {
					var apiname = params.apiname;
			    }
			    if (!params.data) {
					var data = [];
			    }else{
					var data = params.data;
			    }
			    var app = getApp();
			    var token = app.globalData.userinfo.token;
				var userId = app.globalData.userinfo.userId;
		
			    if (!token || token == undefined) { //未登录
					uni.showToast({
						title: '未登录',
						icon: 'none',
						duration: 2000
					});
					uni.removeStorageSync(app.globalData.storagePre+'token');
					uni.removeStorageSync(app.globalData.storagePre+'userId');
					setTimeout(function () {
						uni.reLaunch({ //关闭所有页面，跳转到闪屏页
							url: '/pages/index/wechat'
						})
					}, 2000);
			      return false;
			    }
			    data['token'] = token;
				data['userId'] = userId;
		
			    uni.showLoading({
					title: '加载中',
					mask: true
			    });
			    
			    uni.request({
					url: params.url,
					data: data,
					method: type,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function (res) {
						uni.hideLoading();
						uni.stopPullDownRefresh();
						var ret = app.checkCode(res.data.code); //检查token是否失效
						if(ret == false){
							return false;
						}
			
						if (res.data.code == 0) {
							if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖
								res.data['apiname'] = apiname;
								app.ajaxReadyCallback(res)
							}
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none',
								duration: 2000
							})
						}
					},
					fail: function (err) {
						uni.hideLoading();
						uni.showToast({
							title: '请求失败',
							icon: 'none',
							duration: 2000
						})
					}
			    })
			}
		}
		
		
	}
</script>

<style>
	/*每个页面公共css */
	page{
		background-color: #F3F3F3;
	}
	.no-data{
		width: 100%;
		height: auto;
		margin: 0 auto;
		margin-top: 20%;
		text-align: center;
	}
	.no-data image{
		width: 430rpx;
		height: 316rpx;
	}
	.no-data .no-data-text{
		color: #C5C8D9;
		font-size: 25rpx;
	}
	
</style>
