<template>
	<!-- <web-view src="../../static/index.html"></web-view> -->
</template>

<script>
	import {
		getUser
	} from '@/common/auth.js';
	import config from '@/config.js';
	import {
		translate,
		base64ToUrl
	} from '@/common/image.js'
	export default {
		data() {
			return {
				face: '',
				groupId: '',
				userInfo: {},
				// 百度云access_token
				token: '',
				client_id: config.face_client_id,
				client_secret: config.face_client_secret,
			}
		},
		onLoad() {
			this.getImage()
			this.userInfo = getUser()
			this.groupId = this.userInfo.dept.locationCode.substring(0, 6)
		},
		methods: {
			getImage() {
				uni.chooseImage({
					count: 1,
					sourceType: ['camera'],
					sizeType: ['compressed'],
					success: (res) => {
						uni.showLoading({
							title: '正在上传中...'
						})

						let size = res.tempFiles[0].size
						let scale = 1
						if (size / 1024 / 1024 > 0.9) scale = 0.6

						translate(res.tempFilePaths[0], scale, 'blob', this.setSrc)
					}
				})
			},
			setSrc(e, blobUrl) {
				this.src = blobUrl
				uni.getFileInfo({
					filePath: blobUrl,
					success: (res) => {
						console.log(res);
						let size = res.size
						let scale = 1
						if (size / 1024 / 1024 > 0.9) {
							scale = 0.6
							translate(this.src, scale, 'blob', this.setSrc)
						} else {
							e = e.replace('data:image/jpeg;base64,', "");
							this.face = e
							this.getAccessToken()
						}
					},
					fail: (err) => {
						console.log("err:", err);
					}
				})
			},
			// 人脸搜索
			faceSearch() {
				let face = this.face
				let data = {
					image: face,
					image_type: 'BASE64',
					group_id_list: this.groupId,
					match_threshold: config.score
				}
				console.log("人脸搜索:", data)
				uni.request({
					url: '/baiduApi/rest/2.0/face/v3/search?access_token=' + this.token,
					data: data,
					method: 'POST',
					header: {
						'Content-Type': 'application/json'
					},
					success: (res) => {
						uni.hideLoading()
						console.log("人脸搜索:", res)
						if (res.data.error_msg == 'SUCCESS') {
							// this.isSearch = true
							if (res.data.result.user_list.length < 1) {
								console.log("人脸不存在");
							} else {


							}
						} else {
							console.log("人脸不存在1111");

						}

					},
					error: (err) => {
						uni.hideLoading()
					}
				})
			},
			getAccessToken() {
				uni.request({
					url: '/baiduApi/oauth/2.0/token',
					data: {
						grant_type: 'client_credentials',
						client_id: this.client_id,
						client_secret: this.client_secret
					},
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						if (res.statusCode == 200) {
							this.token = res.data.access_token
							this.faceSearch()
						}
					},
					error: (err) => {
						uni.hideLoading()
					}
				})
			},
		}
	}
</script>

<style>

</style>