<script>
	import Vue from 'vue';

	export default {
		onLaunch: function(options) {
			uni.removeStorageSync("token")
			uni.removeStorageSync("websitData")
			uni.removeStorageSync("websitIdSj")
			let that = this;
			console.log(options);
			// 小程序更新
			const updateManager = uni.getUpdateManager();
			// 请求完新版本信息
			updateManager.onCheckForUpdate(function(res) {
				// console.log(res.hasUpdate);
			});
			// 新的版本已经下载好
			updateManager.onUpdateReady(function(res) {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					},
				});
			});
			// 新的版本下载失败
			updateManager.onUpdateFailed(function(res) {
				that.$toast('更新失败！');
			});

			uni.getSystemInfo({
				success: function(e) {
					console.log(e);
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar =
						custom.bottom + custom.top - e.statusBarHeight + 4;
					//用来判断是否iphoneX类型的全面屏设备
					if (e.model.indexOf('iPhone X') == 0) {
						Vue.prototype.isIphoneX = 68;
					} else {
						Vue.prototype.isIphoneX = 0;
					}

					if (e.environment == 'wxwork') {
						uni.setStorageSync('is_qywx', true);
					} else {
						uni.setStorageSync('is_qywx', false);
					}
				},
			});

			// 企业微信端
			if (uni.getStorageSync('is_qywx') == true) {
				wx.qy.login({
					success: function(loginRes) {
						if (loginRes.code) {
							//发起网络请求
							that
								.$axios({
									url: '/user/auth',
									params: {
										code: loginRes.code,
										work: true,
									},
								})
								.then((authRes) => {
									// console.log(authRes);
									that.$store.commit('changeUserId', authRes.data.userId);
									that.$store.commit('changeUserInfo', authRes.data);
									uni.setStorageSync('userInfo', authRes.data);
									uni.setStorageSync('userId', authRes.data.userId);
									uni.setStorageSync('token', authRes.data.token);
									that.$isResolve();
									this.userWebsit(authRes.data)
								});
						} else {
							console.log('登录失败！' + loginRes.errMsg);
						}
					},
				});
			}

			// 微信端
			else {
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log(loginRes);
						console.log('是否从企业微信端进入：' + uni.getStorageSync('is_qywx'));
						that
							.$axios({
								url: '/user/auth',
								params: {
									code: loginRes.code,
									work: uni.getStorageSync('is_qywx'),
								},
							})
							.then((res) => {
								try {
									console.log(res);
									that.$store.commit('changeUserId', res.data.userId);
									that.$store.commit('changeUserInfo', res.data);
									uni.setStorageSync('userInfo', res.data);
									uni.setStorageSync('userId', res.data.userId);
									uni.setStorageSync('token', res.data.token);
									that.$isResolve();
									this.userWebsit(res.data)
								} catch (e) {
									console.error(e);
								}
							});
					},
				});
			}
		},
		onShow: function() {
			console.log('App Show');
		},
		onHide: function() {
			console.log('App Hide');
		},
		methods: {
			userWebsit(userInfo) {
				this.$axios({
					url: '/user/user/detail',
					method: 'get',
					params: {
						userId: userInfo.userId
					}
				}).then(resData => {
					this.$store.commit('changeUserInfo', resData?.data);
					uni.setStorageSync('userInfo', resData?.data);
					if(resData?.data?.adminWebsit?.websitId){
						this.$store.commit('changeWebsitObj', resData?.data?.adminWebsit);
						uni.setStorageSync('websitData', resData?.data?.adminWebsit);
						uni.setStorageSync("websitIdSj", resData?.data?.adminWebsit?.websitId);
					}else{
						uni.getLocation({
							type: 'gcj02',
							success: res => {
								this.$axios({
									url: '/user/userWebsit',
									method: 'post',
									params: {
										lat: res.latitude,
										lng: res.longitude,
									},
								}).then((res) => {
									if(res?.data){
										this.$store.commit('changeWebsitObj', res?.data);
										uni.setStorageSync('websitData', res?.data);
										uni.setStorageSync("websitIdSj", res?.data.websitId)
									}else{
										uni.reLaunch({
											url: "/pages/mywebsit/index"
										})
									}
								});
							},
							fail: err => {
								uni.getSetting({
									success: (res) => {
										if (!res.authSetting['scope.userLocation']) {
											uni.showModal({
												title: '是否授权当前位置',
												content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
												success: (tip) => {
													if (tip.confirm) {
														uni.openSetting({
															success: (data) => {
																if (data
																	.authSetting[
																		"scope.userLocation"
																	] === true) {
																	this.$successToast(
																		'授权成功');
																	setTimeout(
																		this
																		.userWebsit,
																		1000
																	)
																}
															}
														})
													}
												}
											})
										}
									}
								})
							}
						})
					}
				})
			}
		}
	};
</script>

<style>
	@import './static/style/utils';
</style>
<style lang="scss">
	@import '@/uni_modules/uview-ui/index.scss';

	/*每个页面公共css */

	.clearfix:after {
		content: '';
		display: block;
		height: 0;
		clear: both;
		visibility: hidden;
	}

	.ellipsis {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.ellipsis-2 {
		display: -webkit-box;
		overflow: hidden;
		white-space: normal !important;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.ellipsis-3 {
		display: -webkit-box;
		overflow: hidden;
		white-space: normal !important;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
	}

	.app-container {
		min-height: 100vh;
		font-size: 28rpx;
		color: #333333;
		font-family: 'Microsoft YaHei';
	}

	.bt-container {
		height: 100rpx;

		.container {
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 99;
			width: 100%;
			padding: 10rpx 30rpx;
			background: #ffffff;

			button {
				width: 690rpx;
				height: 80rpx;
				margin: 0;
				background: #6da7ff;
				border-radius: 80rpx;
				color: #ffffff;
				line-height: 80rpx;
				font-size: 30rpx;
			}
		}
	}

	.global-mask {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 998;
		width: 100%;
		height: 100%;
		background: rgba($color: #000000, $alpha: 0.3);
	}

	.global-dialog {
		position: fixed;
		top: calc(50vh - 150rpx);
		left: 60rpx;
		z-index: 999;
		width: 630rpx;
		background: #ffffff;
		border-radius: 15rpx;
		overflow: hidden;

		.title {
			font-size: 36rpx;
			font-weight: 500;
			text-align: center;
			line-height: 100rpx;
			padding-bottom: 10rpx;
		}

		.content {
			.text {
				font-size: 32rpx;
				text-align: center;
				padding-bottom: 40rpx;
			}

			.form {
				padding: 0 40rpx;

				.item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 40rpx;

					input {
						width: 340rpx;
						height: 60rpx;
						border: 1px solid #eaeaea;
						border-radius: 10rpx;
						padding: 0 20rpx;
					}
				}
			}
		}

		.btn {
			border-top: 1px solid #eaeaea;
			display: flex;

			&>view {
				flex: 1;
				text-align: center;
				line-height: 100rpx;
				font-size: 32rpx;

				&.left {
					color: #666666;
				}

				&.right {
					color: #ffffff;
					background: #ff3f42;
				}
			}
		}
	}

	.goods-waterfall-list {
		padding: 20rpx 20rpx 0;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;

		.left,
		.right {
			display: flex;
			flex-direction: column;
		}

		.item {
			width: 348rpx;
			background: #ffffff;
			box-sizing: border-box;
			margin-bottom: 20rpx;
			border-radius: 20rpx;
			overflow: hidden;

			.image {
				width: 348rpx;
				height: 348rpx;
				flex-shrink: 0;
				position: relative;

				.img {
					width: 348rpx;
					height: 348rpx;
					display: block;
				}

				.water {
					width: 348rpx;
					height: 348rpx;
					display: block;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 1;
				}
			}

			.content {
				padding: 15rpx 20rpx;

				.title {
					font-size: 30rpx;
					color: #333333;
					line-height: 36rpx;
					font-weight: 600;
					max-height: 72rpx;
				}

				.tags {
					display: flex;
					flex-wrap: wrap;

					.it {
						height: 28rpx;
						padding: 0 10rpx;
						line-height: 28rpx;
						border-radius: 10rpx;
						background: #e8e8e8;
						font-size: 20rpx;
						color: #666666;
						margin-right: 10rpx;
						margin-top: 10rpx;
					}
				}

				.tags2 {
					display: flex;
					flex-wrap: wrap;

					.it {
						height: 36rpx;
						padding: 0 6rpx;
						font-size: 24rpx;
						margin-right: 10rpx;
						border: 1px solid #fe781f;
						color: #fe781f;
						box-sizing: border-box;
						margin-top: 10rpx;
						display: flex;
						align-items: center;
					}
				}

				.price {
					display: flex;
					align-items: flex-end;
					margin-top: 10rpx;

					.price-1 {
						font-size: 32rpx;
						color: #ff3f42;
						line-height: 32rpx;
					}

					.price-2 {
						font-size: 26rpx;
						color: #666666;
						line-height: 26rpx;
						text-decoration: line-through;
						margin-left: 12rpx;
					}
				}

				.text {
					font-size: 24rpx;
					color: #999999;
					margin-top: 10rpx;
				}
			}
		}
	}

	.goods-row-list {
		padding: 20rpx 20rpx 0;
		box-sizing: border-box;

		.item {
			padding: 20rpx;
			background: #ffffff;
			border-radius: 20rpx;
			display: flex;
			margin-bottom: 20rpx;

			.image {
				width: 240rpx;
				height: 240rpx;
				flex-shrink: 0;
				position: relative;

				.img {
					width: 240rpx;
					height: 240rpx;
					display: block;
				}

				.water {
					width: 240rpx;
					height: 240rpx;
					display: block;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 1;
				}
			}

			.right {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 20rpx;

				.title {
					font-size: 30rpx;
					color: #333333;
					line-height: 36rpx;
					font-weight: 600;
					max-height: 72rpx;
				}

				.tags {
					display: flex;
					flex-wrap: wrap;

					.it {
						height: 28rpx;
						padding: 0 10rpx;
						line-height: 28rpx;
						border-radius: 10rpx;
						background: #e8e8e8;
						font-size: 20rpx;
						color: #666666;
						margin-right: 10rpx;
						margin-top: 10rpx;
					}
				}

				.bottom {
					display: flex;
					align-items: flex-end;
					justify-content: space-between;

					.price {
						margin-top: 10rpx;

						.price-1 {
							font-size: 32rpx;
							color: #ff3f42;
							line-height: 32rpx;
						}

						.price-2 {
							font-size: 26rpx;
							color: #666666;
							line-height: 26rpx;
							text-decoration: line-through;
							margin-top: 6rpx;
						}
					}

					.text {
						font-size: 24rpx;
						color: #999999;
					}
				}
			}
		}
	}
</style>