 <template>

 	<view class="">
 		<u-loading-page iconSize="160" :loading="true" loading-text="loading..."
 			image="/static/images/common/loading.png"></u-loading-page>
 	</view>
 </template>

 <script>
 	import {
 		setToken
 	} from '@/utils/auth'
 	import {
 		login,
 		verifyScanCode
 	} from '@/api/login'
 	import {
 		getScanData
 	} from '@/api/scan'
 	export default {
 		data() {
 			return {
 				scanCode: 'a77af70c72ae74e0',
 				path: '',
 			}
 		},
 		onLoad(options) {
 			uni.removeStorageSync("scanCode");
 			uni.removeStorageSync("Authorization-status");
 			// 获取并解码 q 参数
 			let qlink = decodeURIComponent(options.q || '');
 			if (qlink) {
 				const decodedUrl = decodeURIComponent(qlink); // 先解码
 				// 1. 去掉域名部分，提取路径
 				const scanCode = decodedUrl.replace('https://scantest.dnzc.vip/', ''); // 得到 "a7e4a3f2947004f6"
 				this.scanCode = scanCode;
 			}
 			this.login(); // 调用登录方法
 		},
 		methods: {
 			login() {
 				let that = this;
 				wx.login({
 					success(res) {
 						if (res.code) {
 							// 例如发送到服务器进行登录态验证等操作
 							login(that.scanCode, res.code).then(res => {
 								console.log('登录成功', res);
 								if (res.code === 0) {
 									if (res.data.isWxAuth == 1) {
 										uni.setStorageSync('Authorization-status', true);
 									}
 									setToken(res.data.sessionId)
 									that.path = res.data.page_path;
 									that.verify();
 								} else {
 									console.log('登录失败！' + res.msg);
 								}
 							})
 						} else {
 							console.log('登录失败！' + res.errMsg);
 						}
 					},
 					fail(err) {
 						console.log('登录失败', err);
 					}
 				});
 			},
 			verify() {
 				verifyScanCode(this.scanCode).then(res => {
 					console.log(res);
 					if (res.code === 0) {
						uni.setStorageSync("scanCode", this.scanCode);
 						if (res.data.url !== null && res.data.url !== undefined && res.data.url !== '') {
 							uni.redirectTo({
 								url: '/pages/' + this.path + '/index/authCode'
 							})
 							return;
 						}
 						getScanData(this.scanCode).then(res => {
 							if (res.code === 0) {
 								let url = '/pages/' + this.path + '/index/' + res.data.url;
 							
 								uni.setStorageSync('scanDetail', res.data);
 								uni.redirectTo({
 									url: url
 								})
 							} else {

 							}
 						})

 					} else {
 						let url = '/pages/' + this.path + '/error?msg=' + res.msg;
 						uni.redirectTo({
 							url: url // 目标页面路径
 						})
 					}
 				})
 			}
 		}
 	}
 </script>