<template>

	<view style="display: none;">检测人脸...</view>

</template>

<script>
	import { simpleDetail } from '@/api/course.js'
	import { checkFace } from '@/api/course/face'

	export default {
		name: 'FaceCheck',
		props: {
			courseId: String,
			fileId: String
		},
		data() {
			return {
				faceTimer: null,
				file: {
					startFace: false,
					faceInterval: 0,
					randFace: false
				}
			}
		},
		watch: {
			fileId: {
				handler(){
					this.fileData()
				}
			}
		},

		mounted() {
			console.log('++++进行人脸校验...')
			// 进入即校验人脸
			this.fileData()
		},

		destroyed() {
			// 移除定时
			if (this.faceTimer) {
				clearInterval(this.faceTimer)
			}
		},

		methods: {
			
			// 获取课件描述信息
			fileData(){
				simpleDetail(this.courseId).then(res=>{
					// 初始化刷脸操作
					this.initFace(res)
				})
			},

			// 初始化人脸识别
			initFace(file) {

				// 校验是否需要人脸认证
				if (file.faceStart) {
					// 校验不通过，重新校验
					this.confirmFace(true)
				}
			},

			/**
			 * 确认人脸识别数据
			 */
			confirmFace(msg, tm) {
				// 校验不通过，重新校验
				checkFace({courseId: this.courseId, fileId: this.fileId}).then(res => {
					
					console.log(res)
					if (res) {
						// 可以继续阅读
						this.$emit('ok')
					} else {
					
						this.$emit('no')
						
						console.log('跳转人脸识别去....')
						
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/course/face?courseId=${this.courseId}&fileId=${this.fileId}`
							})
						}, 100)
					}
				})
			}
		}
	}
</script>

<style scoped>

</style>
