<script>
	import { get, post } from './api/request.js'
	import { compareVersion } from './utils/index.js'
	import { getShopInfo } from './components/common.js'
	import api from "./api/index.js"
	
	export default {
		onLaunch: function(options) {
			// this.authLogin(options.scene, options.query.source || '')
			getShopInfo()
			// this.isLogin()
			// this.judgeLogin()
			// plus.runtime.quit()
		},
		methods: {
			judgeLogin() {
				let saveTime = uni.getStorageSync('saveTime')
				let expires_in = uni.getStorageSync('expires_in')
				let token = uni.getStorageSync('token')
				if((saveTime && expires_in && Date.now() > saveTime + expires_in) || !expires_in || !saveTime || !token) {
					uni.navigateTo({
						url: '/pages/user/login'
					})
				} else {
					getShopInfo().then((ress) => {
						uni.switchTab({
						    url: '/pages/index/index'
						})
					})
					this.getMsgNum()
				}
			},
			isLogin() {
				let val = {
					data: {
						access_token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9zaG9wLmNodXRhbmc2Ni5jb21cL2FwaVwvdjFcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNzQzOTkwODEyLCJleHAiOjE3NDQwNzcyMTIsIm5iZiI6MTc0Mzk5MDgxMiwianRpIjoiQmY1ZWk4VUF2YXV5VlRibSIsInN1YiI6MTUsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.E-be6v6i7_zFKijrzDhkZ7K5-IWQKsqTIKZ8ntK7oF4',
						avatar: 'https://ct-upimg.yx090.com/ju8hn6/shop/image/2024/07/13/19DnMNnjGlwKrBlPafc6gtLyrS9aqw3hb5xHYc7q.jpg',
						crop_user: true,
						expires_in: 86400,
						has_sale: true,
						is_authorized: 1,
						is_default_avatar: false,
						is_new: false,
						is_subscribe: 0,
						is_vip: false,
						nickname: "Van",
						role: 0,
						token_type: "Bearer"
					}
				}
				let token = val.data.token_type + ' ' + val.data.access_token
				uni.setStorageSync('token', token)
				uni.setStorageSync('subscribe', val.data.is_subscribe)
				uni.setStorageSync('avatar', val.data.avatar)
				uni.setStorageSync('nickname', val.data.nickname)
				uni.setStorageSync('role', val.data.role)
				uni.setStorageSync('is_vip', val.data.is_vip)
				uni.setStorageSync('is_new', val.data.is_new)
				uni.setStorageSync('is_authorized', val.data.is_authorized)
				uni.setStorageSync('is_default_avatar', val.data.is_default_avatar)
				uni.setStorageSync('sessionKey', val.data.session_key)
				uni.setStorageSync('has_sale', val.data.has_sale)
				uni.setStorageSync('saveTime', Date.now()) // 存储时间
				uni.setStorageSync('expires_in', val.data.expires_in * 1000) // 失效时间
				getShopInfo()
				this.getMsgNum()
				
				// let saveTime = uni.getStorageSync('saveTime')
				// let expires_in = uni.getStorageSync('expires_in')
				// if((saveTime && expires_in && Date.now() > saveTime + expires_in) || !expires_in || !saveTime) {
				// 	uni.navigateTo({
				// 		url: '/pages/user/login'
				// 	})
				// }
			},
			authLogin(scene, source) {
				console.log('authLogin')
				const _this = this
				uni.login({
					provider: 'weixin',
					success: res => {
						let { code } = res
						uni.request({
							method: 'POST',
							url: api.url + '/api/v1/auth/login',
							data: {code, from: 0, source: source},
							header: {
								Authorization: uni.getStorageSync('token') || '',
								accept: "application/json",
								appid: api.appId
							},
							success: (val) => {
								console.log('wx-login')
								if(!Array.isArray(val.data)) {
									let token = val.data.token_type + ' ' + val.data.access_token
									uni.setStorageSync('token', token)
									uni.setStorageSync('subscribe', val.data.is_subscribe)
									uni.setStorageSync('avatar', val.data.avatar)
									uni.setStorageSync('nickname', val.data.nickname)
									uni.setStorageSync('role', 0)
									uni.setStorageSync('login_type', 'mini-app')
									uni.setStorageSync('is_vip', val.data.is_vip)
									uni.setStorageSync('is_new', val.data.is_new)
									uni.setStorageSync('is_authorized', val.data.is_authorized)
									uni.setStorageSync('is_default_avatar', val.data.is_default_avatar)
									uni.setStorageSync('sessionKey', val.data.session_key)
									uni.setStorageSync('has_sale', val.data.has_sale)
									uni.setStorageSync('saveTime', Date.now()) // 存储时间
									uni.setStorageSync('expires_in', val.data.expires_in * 1000) // 失效时间
									uni.setStorageSync('is_crop_user', val.data.crop_user)
									getShopInfo()
									// 如果是普通用户-则在onLaunch向后台发送opengid
									if(val.data.role === 0) {
										if(!val.data.crop_user) {
											setTimeout(() => {
												_this.getUserHome()
											}, 30000)
										}
									}
									_this.getMsgNum()
								}
								_this.$isResolve()
							}
						})
					}
				})
			},
			getMsgNum() {
				get('/api/v1/messages/count').then((res) => {
					uni.setStorageSync('msgNum', res.data.total)
				})
			},
			getUserHome() {
				get('/api/v1/user/home').then((res) => {
					uni.setStorageSync('is_crop_user', res.data.is_crop_user)
				})
			}
		},
		onShow: function(options) {
			// #ifdef APP-PLUS
			uni.hideTabBar()
			// #endif
		},
		onHide: function() {
			
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-plus/index.scss";
	@import url("/static/icon/iconfont.css");
	page {
		width: 100%;
		height: 100%;
		background: $uni-bg-color;
		overflow: auto;
		-webkit-text-size-adjust: 100%;
		font-family: 'Microsoft YaHei';
		position: static !important;
	}
	.tabbar .u-badge{
		position: absolute;
		left: 50%;
		margin-left: 4px;
		top: 4px;
	}
	uni-view uni-button, button {
		padding: 0;
		margin: 0;
		background-color: #fff;
	}
	button:after, uni-view uni-button:after{
		border:none;
	}
	.u-icon{
		display: inline-flex !important;
	}
	.u-transition{
		display: inline-block;
	}
	.u-tag-wrapper{
		display: inline-block;
	}
	.u-tag{
		display: inline-flex;
	}
	.u-tag+.u-tag {
	    margin-left: 4px;
	}
	.u-tag--mini{
		height: 18px !important;
		line-height: 18px !important;
		margin-right: 4px !important;
	}
	.scrollbar::-webkit-scrollbar {
		width: 12px;
		height: 4px;
		color:#ffffff;
		display: block;
	}
	
	/*定义滚动条轨道 内阴影+圆角*/
	.scrollbar::-webkit-scrollbar-track {
		-webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.3);
		box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
		border-radius: 6px;
		background-color:#FFFFFF;
		display: block;
	}
	
	/*定义滑块 内阴影+圆角*/
	.scrollbar::-webkit-scrollbar-thumb {
		border-radius: 6px;
		-webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.3);
		box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
		background-color: #F14939;
		display: block;
	}
	.kuajing{
		background: linear-gradient(to bottom , #A500FF, #63009B);
		color: #fff;
		font-size: 22rpx;
		margin-right: 10rpx;
		display: inline-block;
		height: 32rpx;
		vertical-align: middle;
		width: 100rpx;
		border-radius: 6rpx;
		text-align: center;
		line-height: 32rpx;
	}
	.u-button{
		width: auto !important;
	}
	.u-number-box__plus, .u-number-box__minus{
		width: 25px !important;
		height: 25px !important;
	}
	.u-number-box__plus .u-icon__icon, .u-number-box__minus .u-icon__icon{
		font-size: 12px !important;
	}
	.u-number-box__input{
		background-color: #fff !important;
		height: 25px !important;
	}
</style>
