// This file is auto-generated, don't edit it
|
import facebody20191230, * as $facebody20191230 from '@alicloud/facebody20191230';
|
import {checkUploadFile} from '@/api/user'
|
// 依赖的模块可通过下载工程中的模块依赖文件或右上角的获取 SDK 依赖信息查看
|
import * as $OpenApi from '@alicloud/openapi-client';
|
import Util, * as $Util from '@alicloud/tea-util';
|
import ViapiUtil from '@alicloud/viapi-utils';
|
import fs from 'fs';
|
import { confingInfoStore } from '@/stores/StoresConfing'
|
import {sendPationCodeApi} from './httpApi'
|
import { sockteStore } from '@/stores/sockteInfo';
|
import { type } from 'os';
|
|
|
// 要识别的人脸库
|
const faceDatabase= confingInfoStore().confingInfo.face_database
|
const accessKeyId='LTAI5tPBxZiqgd9h6gcL9Qzc'
|
const accessKeySecret='IE6nsjJMTul2ZHkeQ27bg4wmWIngTu'
|
const client=null
|
/**
|
* 使用AK&SK初始化账号Client
|
* @return Client
|
* @throws Exception
|
*/
|
const createClient=()=>{
|
let config = new $OpenApi.Config({
|
// 必填,您的 AccessKey ID
|
accessKeyId: 'LTAI5tPBxZiqgd9h6gcL9Qzc',
|
// 必填,您的 AccessKey Secret
|
accessKeySecret: 'IE6nsjJMTul2ZHkeQ27bg4wmWIngTu',
|
});
|
// 访问的域名
|
config.endpoint = `facebody.cn-shanghai.aliyuncs.com`;
|
return new facebody20191230(config);
|
}
|
// 将文件上传到oss
|
const getOssUrl=async(baseURL64:any)=>{
|
let file: string = baseURL64;
|
let ossUrl: string = await ViapiUtil.upload(accessKeyId, accessKeySecret, file);
|
return ossUrl
|
}
|
/**
|
*
|
* @param path 图片路径
|
*/
|
const faceShibie= async(path:any)=>{
|
console.log('文件路径',path)
|
const fileStream = fs.createReadStream(path);
|
let client=createClient()
|
// 通过本地文件
|
const facedata= confingInfoStore().confingInfo.face_database
|
const faceScore=confingInfoStore().confingInfo.face_score
|
let searchFaceAdvanceRequest = new $facebody20191230.SearchFaceAdvanceRequest();
|
searchFaceAdvanceRequest.imageUrlObject = fileStream;
|
searchFaceAdvanceRequest.dbName = facedata
|
searchFaceAdvanceRequest.limit = 2;
|
let runtime = new $Util.RuntimeOptions({ });
|
client.searchFaceAdvance(searchFaceAdvanceRequest, runtime).then(re=>{
|
if(re.statusCode===200){
|
const matchList=re.body.data?.matchList
|
// console.log('得到的人脸库')
|
console.log(matchList)
|
if(matchList?.length>0){
|
const faceItems =matchList[0].faceItems
|
if(faceItems?.length===0){
|
console.log('没有找到患者对应的人脸')
|
return false
|
}
|
if(faceItems[0].score>=faceScore){
|
const entityId=faceItems[0].entityId
|
console.log(entityId,'得到了人脸识别id,存患者code到vuex')
|
sockteStore().setfaceSockte({
|
deviceType: "人脸识别",
|
deviceName: "人脸识别",
|
result: entityId,
|
resultTime: ''
|
})
|
}else{
|
console.log('没有找到患者对应的人脸')
|
}
|
}
|
|
}
|
}).finally(()=>{
|
// console.log('---------------都要执行的')
|
// 删除图片
|
delImg(path)
|
})
|
}
|
// 验证---逻辑
|
const yanzhengimg=async(path:any)=>{
|
console.log('文件路径--逻辑',path)
|
const fileStream = fs.createReadStream(path);
|
let client=createClient()
|
// 通过本地文件
|
const facedata= confingInfoStore().confingInfo.face_database
|
const faceScore=confingInfoStore().confingInfo.face_score
|
let searchFaceAdvanceRequest = new $facebody20191230.SearchFaceAdvanceRequest();
|
searchFaceAdvanceRequest.imageUrlObject = fileStream;
|
searchFaceAdvanceRequest.dbName = facedata
|
searchFaceAdvanceRequest.limit = 2;
|
let runtime = new $Util.RuntimeOptions({ });
|
client.searchFaceAdvance(searchFaceAdvanceRequest, runtime).then(re=>{
|
if(re.statusCode===200){
|
const matchList=re.body.data?.matchList
|
// console.log('得到的人脸库')
|
console.log(matchList)
|
if(matchList?.length>0){
|
const faceItems =matchList[0].faceItems
|
if(faceItems?.length===0){
|
console.log('没有找到患者对应的人脸')
|
return false
|
}
|
if(faceItems[0].score>=faceScore){
|
const entityId=faceItems[0].entityId
|
console.log(entityId,'得到了人脸识别id,存患者code到vuex')
|
sockteStore().setfaceSockte({
|
deviceType: "人脸识别",
|
deviceName: "人脸识别",
|
result: entityId,
|
resultTime: ''
|
})
|
}else{
|
console.log('没有找到患者对应的人脸')
|
}
|
}
|
|
}
|
}).finally(()=>{
|
// console.log('---------------都要执行的')
|
// 删除图片
|
// delImg(path)
|
})
|
}
|
const faceApiCheck=async(path:any)=>{
|
fs.readFile(path, (err, data) => {
|
if (err) throw err;
|
let blobFile = new File([data], 'name.png', {type:"image/png"})
|
console.log(blobFile)
|
const formData = new FormData() // 下面有备注
|
formData.append('file', blobFile)
|
checkUploadFile(formData).then(re=>{
|
console.log('-----',re)
|
}).finally(()=>{
|
// 删除图片
|
delImg(path)
|
})
|
})
|
|
|
|
}
|
// base64z转文件后验证
|
const base64toFile = (dataurl:any,filename='file') => {
|
if (!fs.existsSync('./imgs')) {
|
fs.mkdir('./imgs', (err) => {
|
if (err) throw err
|
console.log('文件夹创建成功')
|
})
|
}
|
const path = './imgs/'+ Date.now() +'.png';
|
const base64 = dataurl.replace(/^data:image\/\w+;base64,/,""); //去掉图片base64码前面部分data:image/png;base64
|
// new Buffer 操作权限太大,v6.0后使用Buffer.from()创建构造函数
|
const dataBuffer = new Buffer(base64, 'base64'); //把base64码转成buffer对象,
|
// return dataBuffer
|
fs.writeFile(path, dataBuffer, function (err) {
|
if (err) {
|
console.log(err);
|
} else {
|
// console.log('写入成功!验证阿里', path);
|
// getOssUrl(path)
|
// 验证图片
|
// 是否本地识别
|
const isLanFace= confingInfoStore().confingInfo.isLanFace
|
if(isLanFace===true){
|
//本地识别
|
faceApiCheck(path)
|
}else{
|
//阿里验证外网
|
faceShibie(path)
|
}
|
|
|
}
|
})
|
}
|
//验证后删除文件图片
|
const delImg=(path:string)=>{
|
fs.unlink(path,(err:any)=>{
|
if(err){
|
console.log('删除失败')
|
}else{
|
console.log('删除成功')
|
}
|
})
|
}
|
|
export {
|
yanzhengimg,
|
createClient,
|
faceShibie,
|
base64toFile
|
}
|