<script>
	export default {
		globalData: {
			winHeight: "",
			is_auth: '',
			baseUrl: 'http://admin.l4j.cn/api', //线上地址
			product: '',
			code_no: '',
			code: '',
			latitude: '',
			longitude: '',
			subscribe: '',
		},

		onLaunch: function(e) {
			let that = this
			
			let code_no = e.query.no
			let code = e.query.code
			let code_num = e.query.state
			uni.setStorageSync('code_no', e.query.no)
			let location_url = window.location.href
			let skip_url = 'http://l4j.cn/pages/statistics'
			let skip_url1 = `http://l4j.cn/pages/statistics?code=${code}&state=state`
			//登陆方法线上需要谢在下方else位置
			if (location_url !== skip_url && location_url !== skip_url1) {
				let token = uni.getStorageSync('token')
				if (token) return
				if (!e.query.code) {
					that.globalData.code_no = e.query.no
					//线上解开此处
					window.location.replace(
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx22dc4d8cbc0a192d&redirect_uri=http://l4j.cn&response_type=code&scope=snsapi_base&state=${code_no}#wechat_redirect`
					)
					return
				} else {
					// 此处写登陆方法
					uni.showLoading({
						mask: true
					})
					uni.request({
						url: that.globalData.baseUrl + '/auth/wechat-login',
						method: 'POST',
						header: {
							'content-type': 'application/json',
							'Accept': 'application/json',
						},
						data: {
							code: e.query.code,
							//code: 'mead',
							appId: 'wx22dc4d8cbc0a192d',
						},
						success(res) {
							console.log(res, 'huoqutoken')
							if (res.data.message == "授权失败") {
								console.log(code_num, '红包码')
								uni.showModal({
									title: '提示',
									content: '授权失败是否重试',
									confirmText: '重试',
									showCancel: false,
									success: (res) => {
										if (res.confirm) {
											window.location.replace('http://l4j.cn/?no=' +
												code_num)
										}
									}
								})

							} else {
								uni.setStorageSync('token', res.data.data.token),
									uni.setStorageSync('is_auth', res.data.data.is_auth),
									uni.setStorageSync('subscribe', res.data.data.subscribe)
								that.globalData.subscribe = res.data.data.subscribe
								// uni.hideLoading()
							}
						},
						catch: (err => {
							uni.showToast({
								title: '授权失败',
								icon: 'none'
							})
						})
					})

				}

			} else {
				return;
			}
			uni.getSystemInfo({
				success(res) {
					console.log(res, '系统信息')
					that.globalData.winHeight = res.windowHeight
					console.log(that.globalData.winHeight, '系统信息')
				}
			})


		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			request(api, params, method) {
				var that = this;
				return new Promise((resolve, reject) => {
					uni.request({
						url: that.globalData.baseUrl + api,
						data: params,
						header: {
							'content-type': 'application/json',
							'Accept': 'application/json',
							'Authorization': uni.getStorageSync('token'),
						},
						method: method,
						success: (res) => {
							resolve(res.data)
							console.log(res, '请求结果----------------------')
							if (res.statusCode == 450) {
								return;
							}
							if (res.statusCode == 401) {
								uni.clearStorageSync('token')
								location.reload()
							} 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 == 429) {
								uni.showToast({
									title: "操作频繁请稍后再试",
									icon: 'none',
									duration: 3000,
									mask: true
								})
								uni.navigateBack({

								})
							}

						},
						fail: (err) => {
							// console.log('请求失败')
							resolve(err)
							// reject("请求失败")
						}
					})
				})
			},

		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
