<script>
import { getTechnicianToken } from "@/api/newLogin";

export default {
	globalData: {
		couponSelected: {}
	},
	data() {
		return {
		}
	},
	onLaunch() {
		this.getTechnicianTokenFun()
		console.log('App Launch')


	},
	onShow() {
		console.log('App Show')
	},
	methods: {
		//存入openid
		getTechnicianTokenFun() {
			let isWechat = navigator.userAgent.toLowerCase().includes('micromessenger');
			// 非微信环境直接跳过
			//if (!isWechat) return;//guo
			let openid = uni.getStorageSync('wx_copenid')
			const code = this.$utils.getQueryStr('code');
			console.log(code,'6666')
			if (openid) {
				this.checkMerchantStatus()
			} else {
				// // 1. 没有code → 跳转微信静默授权
				// if (!code && isWechat) {
				// 	let appid = 'wx9dc677bd41e82569'
				// 	//https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9dc677bd41e82569&redirect_uri=https://city.baoxianzhanggui.com/fragrance/wxcode/&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
				// 	//let ruri = encodeURIComponent('https://city.baoxianzhanggui.com/fragrance/');
				// 	let ruri = encodeURIComponent('https://city.baoxianzhanggui.com/fragrance/wxcode');
				// 	const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${ruri}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`
				// 	window.location.href = authUrl
				// }

				let params = {
					//code: this.$utils.('code') //guo
					code: '041Gaa100q6IDW1BH6100Wgpvv4Gaa1K'
				}
				getTechnicianToken(params).then(res => {
					console.log(res,'77777')
					if (res.data.code == 200) {
						uni.setStorageSync('wx_copenid', res.data.data.copenid);
						this.checkMerchantStatus()
					}
				})
			}



		},
		//查询商户注册和入驻信息   auditStatus 审核状态：-1-申请入住,0-待入驻，1-待审核，2-审核通过，3-审核驳回"

		async checkMerchantStatus() {
			// let token = uni.getStorageSync('access-token'); // 假设token保存在本地缓存中
			// if (!token) {
			// 	uni.navigateTo({
			// 		url: '/setting/myNew/phone'
			// 	})
			// 	return
			// };
			const openid = uni.getStorageSync('wx_copenid')
			const merchantInfo = await this.$utils.checkMerchantStatus(openid)
			if (!merchantInfo) return
			//merchantInfo.auditStatus = 3  //guo
			//merchantInfo.fileGroups = []
			uni.setStorageSync('auditStatus', merchantInfo.auditStatus)
			uni.setStorageSync('serviceTag', merchantInfo.serviceTag)

			if (merchantInfo.auditStatus == -1) {//未注册
				uni.navigateTo({
					url: '/pages/join/staff'
				})
				return

			} else if (merchantInfo.auditStatus == 0 || merchantInfo.auditStatus == 1 || merchantInfo.auditStatus == 2 || merchantInfo.auditStatus == 3) {
				if (merchantInfo.auditStatus == 0) { //待入驻
					if (!this.$utils.checkLogin({ type: 'navigate' })) return;
					uni.switchTab({
						url: '/pages/index/index'
					})
					return
				}
				if (merchantInfo.fileGroups.length) {//已有入驻信息
					if (!this.$utils.checkLogin({ type: 'navigate' })) return;
					uni.switchTab({
						url: '/pages/index/index?merchantInfo=' + encodeURIComponent(JSON.stringify(merchantInfo))
					})

				} else {//无入驻信息
					if (merchantInfo.auditStatus == 1 || merchantInfo.auditStatus == 3) {
						uni.navigateTo({
							url: '/pages/join/staff?merchantInfo=' + encodeURIComponent(JSON.stringify(merchantInfo))
						})
					} if (merchantInfo.auditStatus == 2) {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				}
			}

		},


	},
	onHide() {
		console.log('App Hide')
	}
}
</script>

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

html,
body {
	height: 100vh;
	overflow-x: hidden;

	&::-webkit-scrollbar {
		display: none;
	}
}

page {
	--theme-color: #03C8BE;
	--theme-color-gradient: linear-gradient(131deg, #00DAAB 0%, #07CDC4 100%);
	;
}

// 弹窗
.modal-wrap {
	width: 590rpx;
	//height: 314rpx;
	background: #fff;
	border-radius: 16rpx;

	.title {
		font-weight: 600;
		font-size: 32rpx;
		color: #1D2129;
		text-align: center;
		margin-top: 32rpx;
	}

	.content {
		padding: 32rpx 48rpx;
		font-weight: 400;
		font-size: 30rpx;
		color: #4E5969;
	}

	.btn-box {
		display: flex;
		border-top: 1rpx solid #E7E7E7;
	}

	.btn {
		flex: 1;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
		color: #1D2129;
		font-weight: 400;
	}

	.left {
		font-weight: 400;
		font-size: 30rpx;
		color: #86909C;
		text-align: center;
		border-right: 1rpx solid #E7E7E7;
	}

	.right {
		font-weight: 400;
		font-size: 30rpx;
		color: #1D2129;
		text-align: center;
	}

}
</style>
