<script>
	export default {
		globalData: {  
			serverUrl: 'http://hhq.jya-tech.com/api/h5/',
			uploadServerUrl: 'http://hhq.jya-tech.com/api/h5/',
			appId: 'wx2ee796cea8882441',
			redirectUri: 'http://hhqs.jya-tech.com/shop/#/pages/index/wechat',
			storagePre: 'shop_',
			host: 'http://hhqs.jya-tech.com/shop/',
			userinfo:{}
		},
		onLaunch: function() {
			console.log('onLaunch')
			var app = getApp();
			var orgId = this.getUrlCode('orgId');
			console.log('2222222',orgId);
			if(orgId){
				uni.setStorageSync(app.globalData.storagePre+'orgId',orgId);
				app.globalData.userinfo.orgId = orgId;
			}else{
				orgId = uni.getStorageSync(app.globalData.storagePre+'orgId');
				if(orgId){
					app.globalData.userinfo.orgId = orgId;
				}else{
					uni.showToast({
						title: '参数错误',
						icon: 'none',
						duration: 2000
					})
				}
			}
			console.log('33333333',app.globalData);
			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){
					uni.reLaunch({ //关闭所有页面，跳转到闪屏页
						url: '/pages/index/wechat'
					})
				}
			},
			checkCode:function(code){
			    if(code == -100){
					uni.showToast({
						title: '登录超时,请重新登录',
						icon: 'none',
						duration: 2000
					})
					uni.setStorageSync(app.globalData.storagePre+'token','');
					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.setStorageSync(app.globalData.storagePre+'token','');
					setTimeout(function () {
						uni.reLaunch({ //关闭所有页面，跳转到闪屏页
							url: '/pages/index/wechat'
						})
					}, 2000);
			      return false;
			    }
			    data['token'] = token;

				let orgId = uni.getStorageSync(app.globalData.storagePre+'orgId');
		
				data['orgId'] = orgId?orgId:0;
			    data['userId'] = userId?userId:0;
			    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>
