<script>
	export default {
		// 此处globalData为了演示其作用，不是uView框架的一部分
		globalData: {
			appid: 'wxf7d6140f507466be'
		},
		onLaunch() {
		
			// #ifdef APP-PLUS
			let client = plus.push.getClientInfo();
			let qyjyClientId = client.clientid;
			uni.setStorageSync('qyjyClientId', qyjyClientId)
	
			console.log("设备唯一标识：",qyjyClientId)
			// #endif
			var that = this
			// 1.1.0版本之前关于http拦截器代码，已平滑移动到/common/http.interceptor.js中
			// 注意，需要在/main.js中实例化Vue之后引入如下(详见文档说明)：
			// import httpInterceptor from '@/common/http.interceptor.js'
			// Vue.use(httpInterceptor, app)
			// process.env.VUE_APP_PLATFORM 为通过js判断平台名称的方法，结果分别如下：
			/**
			 * h5，app-plus(nvue下也为app-plus)，mp-weixin，mp-alipay......
			 */
			//var that=this
			// 小程序自动登录
			// #ifdef MP-WEIXIN
			uni.showLoading({
				title: '微信自动登录中...',
				mask: true
			})
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					that.$u.api.wxmplogin({
						code: loginRes.code
					}).then(res => {
						that.$u.vuex('vuex_token', res.access)
						that.$u.api.getUserInfo().then(res => {
							if(res.avatar){
								res.avatar = that.vuex_host + res.avatar
							}
							uni.hideLoading()
							uni.showToast({
								title: "登录成功",
								icon: "none"
							})
							uni.reLaunch({
								url: "/pages/home/home"
							})
							that.$u.vuex('vuex_user', res)
							// 修改资源请求地址
							// this.$u.vuex('vuex_user.avatar', this.vuex_user.avatar +
							// 	'?token=' + this.vuex_token)
						})
					}).catch(e => {
						console.log(e)
						uni.setStorageSync('wxmp_openid', e.data.wxmp_openid)
						uni.hideLoading()
						// uni.showToast({
						// 	title: "暂未绑定微信!",
						// 	icon: "none"
						// })
						uni.reLaunch({
							url: '/pages/login/login'
						})
					})
				}
			});
			// #endif
			uni.getSystemInfo({
				success(res) {
					that.$u.vuex('vuex_version', res.appVersion)
					if (res.platform == 'android') {
						that.$u.api.apkCheck().then(res1 => {
							if (res.appVersion < res1.version) {
								uni.showModal({
									title: "版本更新",
									content: '有新的版本发布, 请下载新版本',
									confirmText: '立即更新',
									showCancel: false,
									success: function(res2) {
										if (res2.confirm) {
											if(res1.file.indexOf("http") == 0){
												plus.runtime.openURL(res1.file)
											}else{
												plus.runtime.openURL(that.vuex_host + res1.file)
											}
											
										}
									}
								});
							}
						})
					}
				}
			})
			// #ifdef APP-PLUS
			uni.showLoading({
				title: '自动登录中...',
				mask: true
			})
			uni.getStorage({
				key: 'mySecret',
				success: function(res) {
					let secret = res.data
					if (secret) {
						uni.showToast({
							title: JSON.parse(secret).username,
							icon: "none"
						})
						that.$u.api.loginSecret(JSON.parse(secret)).then(res => {
							that.$u.api.getUserInfo().then(res => {
								if(res.avatar){
									res.avatar = this.vuex_host + res.avatar
								}
								that.$u.vuex('vuex_user', res)
								let perms = [];
								for (let key in user.perms) {
								        perms.push(key);
								    }
								    console.log(perms);
								that.$u.vuex('vuex_perm', perms)
								// 修改资源请求地址
							})
							uni.reLaunch({
								url: '/pages/home/home_'
							})
						})
					}

				},
				complete() {
					uni.hideLoading()
				}
			});
			
			// 根据设备唯一标识进行自动登录
			uni.showToast({
				title: qyjyClientId,
				icon: "none"
			})
			/* that.$u.api.loginCOde(qyjyClientId).then(res => {
				that.$u.api.getUserInfo().then(res => {
					that.$u.vuex('vuex_user', res)
					// 修改资源请求地址
				})
				uni.reLaunch({
					url: '/pages/home/home_'
				})
			}).catch(res=>{
				uni.reLaunch({
					url: '/pages/login/login_'
				})
			}) */
			// #endif
			
			
			
			
			uni.showLoading({
				title: '自动登录中...',
				mask: true
			})
			uni.getStorage({
				key: 'mySecret',
				success: function(res) {
					let secret = res.data
					if (secret) {
					
						uni.showToast({
							title: JSON.parse(secret).username,
							icon: "none"
						})
						let a=JSON.parse(secret).username;
						console.log(a)
						that.$u.api.getUserInfo().then(res => {
							that.$u.vuex('vuex_user', res)
							let perms = [];
							for (let key in user.perms) {
							        perms.push(key);
							    }
							    console.log(perms);
							that.$u.vuex('vuex_perm', perms)
							if(res.type==='employee'||res.type==='remployee'){
								uni.reLaunch({
									url: '/pages/home/home_'
								})
							}else{
								uni.reLaunch({
									url: '/pages/workSpace/workSpace'
								})
							}
							// 修改资源请求地址
						})
					}
				},
				complete() {
					uni.hideLoading()
				}
			});
		},
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	@import "common/demo.scss";
	.star {
		color: red;
	}
	uni-page-body{
		min-height: 100%;
	}
	.my-top-search-nav {
		position: fixed;
		left: 0;
		right: 0;
		z-index: 999;
		background-color: #f3fbff;
		border-bottom: 1upx solid #eeeeee;
	}
	
	.search-wrap {
		background-blend-mode: normal, normal;
		padding-bottom: 20rpx;
		padding-top: 20rpx;
		display: flex;
		justify-content: space-around;
	}
	.tabbar-title{
	    background-color: #FFFFFF;
	    height: 55px;
	    display: flex;
	    padding: 0 17px;
	    box-sizing: border-box;
	    font-size: 17px;
	    line-height: 55px;
	    color: #505050;
	}
	.tabbar-title {
		background-color: #FFFFFF;
		height: 100upx;
		display: flex;
		/* justify-content: space-between; */
		padding: 0 32upx;
		box-sizing: border-box;
		font-size: 32rpx;
		line-height: 100rpx;
		color: #505050;
	}
	
	.text-title {
		position: relative;
		margin-right: 40upx;
	}
	
	.active.text-title::after {
		content: "";
		width: 46rpx;
		height: 4rpx;
		background-image: linear-gradient(90deg,
				#164cc3 0%,
				#2c6fd9 100%),
			linear-gradient(#085f56,
				#085f56);
		background-blend-mode: normal,
			normal;
		border-radius: 2rpx;
		position: absolute;
		bottom: 20rpx;
		left: 50%;
		transform: translateX(-50%);
	}
	
	
	.search-body {
		width: 600upx;
		height: 78upx;
		background-color: #ffffff;
		border-radius: 39upx;
		padding: 13upx;
		padding-left: 25upx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}
	
	.search-body .search-input {
		height: 100%;
		flex: 1;
		padding-left: 12rpx;
		box-sizing: border-box;
	}
	
	.left-img {
		width: 26rpx;
		height: 26rpx;
	
	}
	
	.right-img {
		width: 30rpx;
		height: 30rpx;
		margin-right: 10rpx;
	}
	
	.right-btn {
		width: 110rpx;
		height: 58rpx;
		line-height: 58rpx;
		background-image: linear-gradient(0deg,
				#2da0f3 0%,
				#54bcff 100%);
		border-radius: 29rpx;
		font-size: 28rpx;
		color: #ffffff;
		text-align: center;
	}
	
	.right-btn.allBtn {
		margin: 13rpx 0;
	}
	
</style>
