<template>
	<view>
		<web-view :src="href"></web-view>
	</view>
</template>

<script>
	import "@/common/time.js"

	// 返回指定日期距今多少天
	const daysComputed = time => {
		let oldTimeFormat = new Date(time)
		let nowDate = new Date()
		if (nowDate.getTime() - oldTimeFormat.getTime() > 0) {
			let times = nowDate.getTime() - oldTimeFormat.getTime()
			let days = parseInt(times / (60 * 60 * 24 * 1000))
			return days
		} else {
			throw ('传入时间不能大于当前日期')
		}
	}

	// console.log(daysComputed('2022-02-26'))

	export default {
		data() {
			return {
				href: "",
				
				staff_num:"",
				mine_code:""
			};
		},
		onLoad() {
			this.staff_num = uni.getStorageSync('user').staff_num
			this.mine_code = uni.getStorageSync('mine_code')
			
			console.log(this.staff_num,this.mine_code)
			
			let shangtang_login_time = uni.getStorageSync('shangtang_login_time')

			console.log(shangtang_login_time)

			if (!uni.getStorageSync('shangtang_login_time')) {
				console.log('第一次登录')
				// 第一次登录
				this.get_token()
			} else {
				// 有登录记录
				console.log('有登录记录')
				// 判断距离登录日期过去多少天
				console.log(daysComputed(shangtang_login_time))
				// 如果大于5天 重新登录
				if (daysComputed(shangtang_login_time) > 2) {
					console.log("大于2天")

					this.get_token()
				} else {
					console.log("小于2天")
					
					// 小于5天，直接进入对应页面
					this.href = "http://shangtang.nxmy.com:8011/?token=" + uni.getStorageSync('shangtang_token')
				}
			}

		},
		methods:{
			get_token(){
				uni.request({
					// 登录 获取token
					method:"POST",
					url: "http://shangtang.nxmy.com:8011/v1/app/token/get",
					header:{
						"Content-Type": "application/json"
					},
					data: {
						staff_num: this.staff_num,
						area_code: this.mine_code
					},
					success: (res) => {
						console.log(res.data.data)
						
						
						// uni.setStorageSync('shangtang_token',res.data.data.access_token)
						// uni.setStorageSync('shangtang_login_time',new Date().format("yyyy-MM-dd"))
						
						// 登录成功后进入对应页面
						// 商汤链接+token
						this.href = "http://shangtang.nxmy.com:8011/?token=" + uni.getStorageSync('shangtang_token')
						// this.href = "http://shangtang.nxmy.com:8011/?token=" + res.data.data.access_token
					},
					fail: (err) => {
						uni.showToast({
							icon:"none",
							title:err
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
