<script>
	var token = ''
	if (uni.getStorageSync('token')) {
		token = uni.getStorageSync('token')
	}
	export default {
		globalData: {
			statusBarHeight: uni.getSystemInfoSync()['statusBarHeight'],
			windowHeight: uni.getSystemInfoSync()['windowHeight'],
		},
		systemInfo: {},
		onLaunch: function() {
			// console.log('App Launch');
			// uni.getStorageSync('token');
			console.log(token);
			var that = this;
			uni.getSystemInfo({
				success: res => {
					// console.log(res.safeArea.bottom)
					console.log('手机信息res' + res.model)
					res.lang = res.language.indexOf('zh') !== -1 ? 'zh' : 'en';
					that.systemInfo = res;
				}
			})
		},
		onShow: function() {
			console.log('App Show');

		},
		onHide: function() {
			console.log('App Hide');
		},
		methods: {
			request(api, params, method) {
				var that = this;
				var merchant_id = 0;
				return new Promise((resolve, reject) => {
					uni.request({
						url: 'http://bike.saas.yufengjiayun.com/app-api/' + api,
						// url:'http://dev.saas.yufengjiayun.com/app-api/'+api,
						data: params,
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'Authorization': 'bearer' + ' ' + uni.getStorageSync('token').token,
							'type': 'app',
							'merchant-id': merchant_id
						},
						method: method,
						success: (res) => {
							resolve(res)
							// console.log(res)  
							if (res.statusCode == 450) {
								return;
							}
							if (res.data.statusCode != 200) {
								if (res.message != undefined) {
									uni.showToast({
										title: res.message,
										icon: 'none',
										duration: 3000,
										mask: true
									})
								} else if (res.data.message != undefined) {
									uni.showToast({
										title: res.data.message,
										icon: 'none',
										duration: 3000,
										mask: true
									})
								}
							} else if (res.statusCode != 200) {
								if (res.message != undefined) {
									uni.showToast({
										title: res.message,
										icon: 'none',
										duration: 3000,
										mask: true
									})
								} else if (res.data.message != undefined) {
									uni.showToast({
										title: res.data.message,
										icon: 'none',
										duration: 3000,
										mask: true
									})
								}
							}
							if (res.statusCode == 401) {
								uni.removeStorageSync('token')
								uni.showToast({
									title: '登陆过期请重新登陆~',
									icon: 'none',
									mask: true,
									success: function() {
										uni.redirectTo({
											url: '/pages/login/login',
										})
									}
								})
							}
						},
						fail: (err) => {
							console.log(err)
							uni.showToast({
								title: '请求超时请重试',
								icon: 'none',
								mask: true
							});
							reject(err)
						}
					})
				})
			},
		}
	};
</script>

<style>
	/* 解决头条小程序组件内引入字体不生效的问题 */
	/* #ifdef MP-TOUTIAO */
	@font-face {
		font-family: uniicons;
		src: url('/static/uni.ttf');
	}

	/* #endif */
</style>
