<template>
	<view class="flex-col page">
		<!-- <image src="/static/common/img/16530269846087107196.png" class="image" /> -->
		<u-navbar title="确认密码"></u-navbar>
		<view class="flex-col group">
			<text class="text">确认密码后登录</text>
			<view class="flex-col group_7">
				<view class="flex-col">
					<view class="flex-row" style="padding: 0">
						<u-input type="password" v-model="user.passWord" placeholder="请输入密码"  class="view text-wrapper"/>
					</view>
				</view>
				<view class="flex-col group_6">
					<u-input type="password" v-model="isPassWord" placeholder="请确认密码" class="view text-wrapper" />
				</view>
        <view class="tips">
           <image src="/static/common/img/infoCircle.png"></image> 提示：密码长度应该大于5，必须包含字母、数字、特殊字符，且不能包含中文!
        </view>
			</view>
			<u-button class="flex-col items-center text-wrapper_1" throttle-time="1000" type="primary" shape="circle"
				@click="login"><text class="text_7">确定</text></u-button>
				<!-- 此次接入人脸验证  登录 ==> 确定 -->
		</view>
		<u-toast ref="uToast" />
		<u-top-tips ref="uTips" :navbar-height="0"></u-top-tips>
	</view>
</template>

<script>
import {aesEncrypt} from '@/utils/encrypt.js'
import md5 from "js-md5";
	export default {
		data() {
			return {
				show: false,
				userName: "",
				isPassWord: "",
				agreement: false,
				user: {
					phone: '',
					passWord: '',
					code:''
				}
			};
		},
		onLoad(e) {
			if (e.phone) {
				this.user.phone = e.phone
				this.user.code = e.code
			} else {
				uni.navigateTo({
					url: "../register/register",
				});
			}
		},
		methods: {
			login() {
				if (this.user.passWord === "") {
					return this.$refs.uToast.show({
						title: '请输入密码',
						type: 'error',
						})
				}
				if (this.user.passWord.length < 5) {
					// return this.$tips("您的密码长度应该大于5", "error");
					return this.$refs.uToast.show({
						title: '您的密码长度应该大于5',
						type: 'error',
						})
				}
				if (this.isPassWord === "") {
					return this.$refs.uToast.show({
						title: '请确认密码',
						type: 'error',
						})
					// return this.$tips("请确认密码", "error");
				}
				if (this.isPassWord !== this.user.passWord) {
					return this.$refs.uToast.show({
						title: '请确认两次密码是否一致',
						type: 'error',
						})
					// return this.$tips("请确认两次密码是否一致", "error");
				}
				uni.showLoading({
					title: "注册中",
				});
        const req = {...this.user}
        req.passWord = aesEncrypt(this.user.passWord)
		// 生成13位时间戳
		const timestamp = Date.now();
		// 生成签名
		const base64Key = btoa('xiaoyout!#@12345');
		const signStr = this.user.phone + timestamp + base64Key;
		const sign = md5(signStr);
		req.timeStamp = timestamp.toString();
		req.sign = sign;
        console.log(req,'req')
        // return 
				this.$u.api.RegisterUser(req).then((res) => {
          const newReq = {...this.user}
          newReq.passWord = aesEncrypt(this.user.passWord)
					this.$u.api.LoginApp(newReq).then((ress) => {
						uni.hideLoading();
						// 保存登录后得到的用户数据
						this.$u.vuex('vuex_user', ress.user)
						this.$u.vuex('vuex_token', ress.token)
						this.$refs.uToast.show({
							title: '注册成功',
							type: 'success',
						})
							// uni.navigateTo({
							// 	url: "../perfect/perfect",
							// });
						setTimeout(()=>{
							// 这里改为进行人脸识别
							this.toBaiduApi();
							return

							uni.navigateTo({
							url: "/pages/login/roleSelection",
							});
						},300)

					}).catch((e) => {
						uni.hideLoading();
					});

				}).catch((e) => {
					uni.hideLoading();
					// this.$tips( e.error, 'error')
					this.$refs.uToast.show({
						title: e.error,
						type: 'error',
					})
				});

			},

			// 跳转至人脸验证
			toBaiduApi(){
				this.$u.api.getAPIToken().then((res) => {
					const token = res.result.verify_token;

					// 跳转到百度人脸核验页面
					this.redirectToBaiduVerification(token);
				});
			},
			redirectToBaiduVerification(token) {
				// 获取当前域名
				const currentDomain = window.location.origin;

				// 编码回调URL - 使用相对路径，让uni-app处理路由
				// 百度人脸核验会在回调URL后附加verify_result和verify_info参数
				const successUrl = encodeURIComponent(
					`${currentDomain}/#/pages/login/recognitionResult/recognitionResult?token=${token}`
				);
				const failedUrl = encodeURIComponent(
					`${currentDomain}/#/pages/login/recognitionResult/recognitionFailed?token=${token}`
				);

				// 构建跳转URL
				const verifyUrl = `https://brain.baidu.com/face/print/verify/verify?token=${token}&successUrl=${successUrl}&failedUrl=${failedUrl}`;

				console.log("跳转到百度人脸核验页面:", verifyUrl);

				// 直接跳转到百度人脸核验页面
				window.location.href = verifyUrl;
				return;
			},
		},
	};
</script>

<style scoped lang="scss">
	.custom-style {
		color: #606266;
	}

	.custom-style::after {
		border: none;
	}

	.page {
		padding-bottom: 0.015rem;
		background-color: rgb(255, 255, 255);
		overflow-y: auto;
		width: 100%;
		overflow-x: hidden;
		height: 100%;

		.image {
			flex-shrink: 0;
			align-self: flex-start;
			width: 102%;
		}

		.group {
			padding: 0.15rem 0.29rem;

			.text {
				font-family: PingFang;
				font-weight: bold;
				font-size: 48rpx;
				color: #000000;
				margin-top: 200rpx;
			}

			.group_7 {
				margin-top: 0.29rem;

				.text-wrapper {
					border: solid 2rpx #dcdfe6;
					border-radius: 100rpx;
					background: #f6f8fa;
				}

				.group_6 {
					margin-top: 0.2rem;
				}

				.text_1 {
					margin-left: 0.01rem;
					align-self: flex-start;
					color: rgb(51, 51, 51);
					font-size: 0.15rem;
					font-family: PingFang;
					line-height: 0.14rem;
				}

				.view {
					margin-top: 20rpx;
					padding: 20rpx 50rpx !important;
				}
			}

			.text_6 {
				margin-left: 0.01rem;
				margin-top: 0.13rem;
				align-self: flex-start;
				color: rgb(25, 140, 255);
				font-size: 0.13rem;
				font-family: PingFang;
				line-height: 0.13rem;
			}

			.text-wrapper_1 {
				margin: 0.35rem 0rem 0;
				padding: 20rpx 50rpx;
				height: 108rpx;
				/* background-image: linear-gradient(90deg,
						rgb(135, 230, 254) 0%,
						rgb(91, 192, 254) 52%,
						rgb(46, 155, 255) 100%);
				box-shadow: 0px 0.03rem 0.09rem rgba(38, 122, 199, 0.34); */
				background: #3cb5fb;
				border-radius: 0.23rem;

				.text_7 {
					color: rgb(255, 255, 255);
					font-size: 0.16rem;
					font-family: PingFang;
					line-height: 0.15rem;
				}
			}

			.group_1 {
				margin-top: 0.12rem;

				.text_8 {
					color: rgb(102, 102, 102);
					font-size: 0.14rem;
					font-family: PingFang;
					line-height: 0.13rem;
				}

				.text_9 {
					margin-left: 0.035rem;
					color: rgb(25, 140, 255);
					font-size: 0.14rem;
					font-family: PingFang;
					line-height: 0.13rem;
				}
			}

			.group_2 {
				margin-top: 0.36rem;
				justify-content: center;

				.section_1 {
					flex-shrink: 0;
					width: 0.17rem;
				}

				.group_3 {
					margin-left: 0.11rem;
					height: 0.18rem;
					line-height: 0.18rem;
					font-size: 0;

					.text_10 {
						color: rgb(153, 153, 153);
						font-size: 0.11rem;
						font-family: PingFang;
					}

					.text_11 {
						color: rgb(25, 140, 255);
						font-size: 0.11rem;
						font-family: PingFang;
					}

					.text_12 {
						color: rgb(102, 102, 102);
						font-size: 0.11rem;
						font-family: PingFang;
					}

					.text_13 {
						color: rgb(25, 140, 255);
						font-size: 0.11rem;
						font-family: PingFang;
					}
				}
			}
		}
		.tips{
			font-size: 24rpx;
			color: #EF3920;
			display: flex;
			margin-top: 20rpx;
			image{
				width: 32rpx;
				height: 32rpx;
			}
		}
	}
</style>
