<template>
  <view class="content">
      <uni-frv-external-realname ref="frv"  @start="startVerify" @finish="verifyFinish"></uni-frv-external-realname>
  </view>
</template>

<script>
import { UploadFile } from './utils/uploads'
export default {
  data() {
    return {
      isCertified: false,
      realName: '',
      idCard: '',
	  ossData:null,
	  certifyId:'',
	  MetaInfo:null,
	  eventChannel: null,
    }
  },
  onLoad() {
	this.eventChannel = this.getOpenerEventChannel()
	this.MetaInfo=uni.getFacialRecognitionMetaInfo()
	const { idCard,realName}=uni.getStorageSync('userFData')
	this.realName=realName
	this.idCard=idCard
	this.initOss()
	this.startVerifys()
  },
  methods: {
	initOss(){
		const self=this
		const FilePath = "DDH/face";
		const params={
			fileName: 'jpg', //图片名
			fileType: FilePath,
		}
		self.$request('/app/v1/oss', 'GET',params).then(res => {
			console.log(res.result,'oss')
			self.ossData=res.result
			
		}).catch(err => {
			console.log(err, '请求出错')
		})  
	},
	startVerifys () {
		console.log('44325')
		// {realName,idCard,metaInfo}
	  const self = this
	  const params={
		appId:'__UNI__DE04B13',
		realName:self.realName,
		idCard:self.idCard,
		metaInfo:self.MetaInfo
	  }
	  console.log(params,'params')
	  self.$request('/app/v1/ocrTokenByUni', 'POST',params).then(res => {
		const {ocrToken}=res.result
		console.log(res.result)
		
		if(ocrToken.certifyId){
			self.certifyId=ocrToken.certifyId
			self.$refs.frv.startFacialRecognitionVerify(ocrToken.certifyId)
		}else{
			const {message}=ocrToken.error
			uni.showToast({
				title:message,
				icon: "none",
			})
		}
		
	  }).catch(err => {
	  	console.log(err, '请求出错')
	  })
	},
    async startVerify () {
		console.log('44325')
		// {realName,idCard,metaInfo}
	  const self = this
	  const params={
		appId:'__UNI__DE04B13',
		realName:self.realName,
		idCard:self.idCard,
		metaInfo:self.MetaInfo
	  }
	  console.log(params,'params')
	  self.$request('/app/v1/ocrTokenByUni', 'POST',params).then(res => {
	  	
		const {ocrToken}=res.result
		console.log(res.result)
		if(ocrToken.certifyId){
			self.certifyId=ocrToken.certifyId
			self.$refs.frv.startFacialRecognitionVerify(ocrToken.certifyId)
		}else{
			uni.showToast({
				title:ocrToken.errMsg,
				icon: "none",
			})
		}
		
	  }).catch(err => {
	  	console.log(err, '请求出错')
	  })
    },
    async verifyFinish () {
		const self = this
		const params={
			appId:'__UNI__DE04B13',
			certifyId:self.certifyId
		}
		self.$request('/app/v1/resultByUni', 'POST',params).then(res => {
			console.log(res,'resultByUni')
			const {ocrToken }=res.result
			UploadFile(self.ossData,`data:image/jpg;base64,${ocrToken.photo}`).then(file=>{
				console.log(file.fileUrl)
				uni.setStorageSync('fileUrlFace',file.fileUrl)
				this.eventChannel.emit('send', JSON.stringify(file.fileUrl))
				self.Back()
			})
		}).catch(err => {
			console.log(err, '请求出错')
		})
    },
  }
}
</script>

<style>

</style>

