<template>
	<view class="container">
	</view>
</template>

<script>
	var config = require('../../common/config.js');
	var http = require('../../common/http.js');
	var common = require('../../common/common.js');
	export default {
		data() {
			return {
				code:'',      //返回code
				message:'',   //返回message
				backImg:'',   //返回图片id
				backVideo:'', //返回视频id  
				backNo:'',    //接口调用批次号
				idcard: '',   //返回身份证号
				name: '',     //返回用户姓名
				serialNumber: '', //序列号
				intervalId: null,
				face_token: '',
				face_key: '',
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			var me = this;
			// // 获取参数
			me.setData({
				code:options.code,
				message:decodeURIComponent(options.message),
				backImg:options.backImg,
				backVideo:options.backVideo,
				backNo:options.backNo,
				idcard: options.idcard,
				name: decodeURIComponent(options.name),
				serialNumber: options.serialNumber
			})
			this.loadFaceStatus();
			this.startInterval();
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			
		},
		methods: {
			loadFaceStatus() {
				common.loading()
				const me = this
				http.postApi(config.API_USER_FACE_STATUS,{
					card_no: me.idcard,
					name: me.name,
					serial_no: me.serialNumber
				},(resp) => {
					if (resp.data.code === 200) {
						if (resp.data.data.status == 1) {
							// 1 认证中
						} else if (resp.data.data.status == 2) {
							// 2 认证失败
							uni.hideLoading()
							me.stopInterval();
							// me.navToAuthIdentity()
							const remark = resp.data.data.remark!=''?'原因：'+resp.data.data.remark:''
							uni.setStorageSync('faceKey', me.face_key);
							uni.showModal({
								title: '提示',
								content: `认证失败，${remark},请重试`,
								showCancel: false,
								confirmText: '确定',
								success: function (res) {
									if (res.confirm) {
										setTimeout(() => {
											uni.switchTab({
												url: '/pages/index/index'
											});
										},500)
									}
								},
								fail: function (res) { },
								complete: function (res) { },
							})
						} else if (resp.data.data.status == 3) {
							// 3 认证成功
							uni.hideLoading()
							me.stopInterval();
							uni.showToast({
								title: '认证成功,即将跳转',
								icon: 'success',
								duration: 2000
							});
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								});
							},2000)
						}
					} else{
						uni.hideLoading()
						me.stopInterval();
						common.simpleToast(resp.data.msg)
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							});
						},1500)
					}
				})
			},
			
			navToAuthIdentity() {
				http.postApi(config.API_USER_FACE_TOKEN,{},(resp) => {
					if (resp.data.code === 200) {
						this.face_token = resp.data.data.token
						this.face_key = resp.data.data.key
					} else{
						common.simpleToast(resp.data.msg)
					}
				})
			},
			
			startInterval() {
				this.intervalId = setInterval(() => {
					this.loadFaceStatus();
				}, 2000);
			}, 
			
			stopInterval() {
				clearInterval(this.intervalId);
				this.intervalId = null;
			},
		},
		
		onUnload() {
			// 清除定时器，防止内存泄漏
			if (this.intervalId) {
				this.stopInterval();
			}
		}
	};
</script>

<style>
	@import './callPage.css';
</style>