 <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,setAdminToken,setAdminInfo
 	} from '@/utils/auth'
 	import {
 		login,
 		verifyScanCode,
    getIsNoLogin
 	} from '@/api/login'
 	import {
 		getScanData
 	} from '@/api/scan'
 	export default {
 		data() {
 			return {
 				scanCode: '',
 				path: '',
 				status: false
 			}
 		},
 		onLoad(options) {
 			uni.removeStorageSync("scanCode");
 			uni.removeStorageSync("Authorization-status");
 			// 获取并解码 q 参数
 			let qlink = decodeURIComponent(options.q || '');
       console.log(qlink);
 			if (qlink) {
 				let decodedUrl = decodeURIComponent(qlink); // 先解码
 				// 1. 去掉域名部分，提取路径
 				const scanCode = decodedUrl.replace('https://e.dnzc.vip/', ''); // 得到 "a7e4a3f2947004f6"
 				this.scanCode = scanCode;

        uni.redirectTo({
          url: '/pages/skip?url='+skipUrl
        })

 			}
      let decodedUrl = "https://d.dnzc.vip/a00f8b7dc36984c1";
      if (decodedUrl.indexOf('https://d.dnzc.vip') !== -1){
        uni.navigateTo({
          url: '/pages/common?url=' + encodeURIComponent(decodedUrl)
        })
        return;
      }
 			this.login(); // 调用登录方法

 		},
 		methods: {
 			login() {
 				let that = this;
 				if (this.scanCode == '' || this.scanCode == undefined) {
 					this.status = true;
 				}
 				wx.login({
 					success(resLogin) {
 						if (resLogin.code) {
 							// 例如发送到服务器进行登录态验证等操作
 							login(that.scanCode, resLogin.code).then(res => {
 								console.log('登录成功', res);
 								if (res.code === 0) {
 									if (res.data.isWxAuth == 1) {
 										uni.setStorageSync('Authorization-status', true);
 									}
                  // if ((that.scanCode === '' || that.scanCode === undefined) && res.data.roles.length > 1){
                  //    that.getIsNoLogin(res.data.uid);
                  //    return;
                  // }

 									that.scanCode = res.data.qrCode;
 									setToken(res.data.sessionId);
 									that.path = res.data.page_path;
 									that.verify();
 								} else {
 									if (res.code === 98) {
 										that.getIsNoLogin();
 										return;
 									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none'
										});
 										console.log('登录失败！' + res.msg);
 									}
 								}
 							})
 						} else {
 							console.log('登录失败！' + res.errMsg);
 							uni.showToast({
 								title: res.msg || '登录失败',
 								icon: 'none'
 							});
 						}
 					},
 					fail(err) {
 						uni.showToast({
 							title: err.msg || '登录失败',
 							icon: 'none'
 						});
 						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 !== '') {
              let skipUrl = '/pages/' + this.path + '/index/authCode';
              uni.redirectTo({
                url: '/pages/skip?url='+skipUrl
              })
 							return;
 						}

 						getScanData(this.scanCode).then(res => {
 							let url;
 							if (res.code === 0) {
                uni.setStorageSync('scanDetail', res.data);
                let skipUrl =  '/pages/' + this.path + '/index/' + res.data.url;
                uni.redirectTo({
                  url: '/pages/skip?url='+skipUrl
                })
                return;

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

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