Compare commits

..

2 Commits

10 changed files with 361 additions and 150 deletions

View File

@ -0,0 +1,9 @@
baidu:
face:
app-id: 7034646
api-key: L3PiEzO9dMFJDExMTjOxuTtq
secret-key: 40LMey6WC1MYIqLU4m5Qe8K4foFUM1bc
similarity-threshold: 0.8
qps-limit-delay: 1000
max-retry-attempts: 3
retry-delay: 2000

View File

@ -0,0 +1,107 @@
package com.yanzhu.common.core.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author yanzhu
*/
@Configuration
@ConfigurationProperties(prefix = "baidu.face")
public class BaiduFaceProperties {
/**
* App ID
*/
private String appId = "7034646";
/**
* API Key
*/
private String apiKey = "L3PiEzO9dMFJDExMTjOxuTtq";
/**
* Secret Key
*/
private String secretKey = "40LMey6WC1MYIqLU4m5Qe8K4foFUM1bc";
/**
*
*/
private double similarityThreshold = 0.8;
/**
* QPS -
*/
private int qpsLimitDelay = 1000;
/**
*
*/
private int maxRetryAttempts = 3;
/**
*
*/
private int retryDelay = 2000;
// Getter和Setter方法
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public double getSimilarityThreshold() {
return similarityThreshold;
}
public void setSimilarityThreshold(double similarityThreshold) {
this.similarityThreshold = similarityThreshold;
}
public int getQpsLimitDelay() {
return qpsLimitDelay;
}
public void setQpsLimitDelay(int qpsLimitDelay) {
this.qpsLimitDelay = qpsLimitDelay;
}
public int getMaxRetryAttempts() {
return maxRetryAttempts;
}
public void setMaxRetryAttempts(int maxRetryAttempts) {
this.maxRetryAttempts = maxRetryAttempts;
}
public int getRetryDelay() {
return retryDelay;
}
public void setRetryDelay(int retryDelay) {
this.retryDelay = retryDelay;
}
}

View File

@ -0,0 +1,15 @@
package com.yanzhu.common.core.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author yanzhu
*/
@Configuration
@ComponentScan(basePackages = "com.yanzhu.common.core")
public class CoreConfig {
}

View File

@ -2,7 +2,12 @@ package com.yanzhu.common.core.utils;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.yanzhu.common.core.config.BaiduFaceProperties;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
@ -16,28 +21,42 @@ import java.io.ByteArrayOutputStream;
*
* @author yanzhu
*/
@Component
public class BaiduFaceSimilarityUtils {
// 百度人脸识别应用的API Key和Secret Key
private static final String APP_ID = "您的AppID"; // 需要设置有效的AppID
private static final String API_KEY = "L3PiEzO9dMFJDExMTjOxuTtq";
private static final String SECRET_KEY = "40LMey6WC1MYIqLU4m5Qe8K4foFUM1bc";
@Autowired
private BaiduFaceProperties baiduFaceProperties;
// 初始化AipFace客户端
private static final AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
private static AipFace client;
// 相似度阈值,大于此值认为是同一个人
private static final double SIMILARITY_THRESHOLD = 0.8;
public static double SIMILARITY_THRESHOLD = 0.8;
// QPS限制相关常量
private static final int QPS_LIMIT_DELAY = 1000; // 1秒延迟避免QPS限制
private static final int MAX_RETRY_ATTEMPTS = 3; // 最大重试次数
private static final int RETRY_DELAY = 2000; // 重试延迟(毫秒)
private static int QPS_LIMIT_DELAY = 1000; // 1秒延迟避免QPS限制
private static int MAX_RETRY_ATTEMPTS = 3; // 最大重试次数
private static int RETRY_DELAY = 2000; // 重试延迟(毫秒)
@PostConstruct
public void init() {
// 从配置中获取参数
String appId = baiduFaceProperties.getAppId();
String apiKey = baiduFaceProperties.getApiKey();
String secretKey = baiduFaceProperties.getSecretKey();
// 初始化AipFace客户端
client = new AipFace(appId, apiKey, secretKey);
static {
// 设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 更新静态变量
SIMILARITY_THRESHOLD = baiduFaceProperties.getSimilarityThreshold();
QPS_LIMIT_DELAY = baiduFaceProperties.getQpsLimitDelay();
MAX_RETRY_ATTEMPTS = baiduFaceProperties.getMaxRetryAttempts();
RETRY_DELAY = baiduFaceProperties.getRetryDelay();
}
/**
@ -224,18 +243,6 @@ public class BaiduFaceSimilarityUtils {
}
}
/**
* API KeySecret Key
*
* @param appId App ID
* @param apiKey API Key
* @param secretKey Secret Key
*/
public static void setCredentials(String appId, String apiKey, String secretKey) {
// 重新初始化AipFace客户端
// 注意:在实际应用中,应该通过配置文件或环境变量来设置这些密钥
}
public static void main(String[] args) {
System.out.println("开始测试人脸相似度算法...");
@ -272,5 +279,4 @@ public class BaiduFaceSimilarityUtils {
System.out.println("\n测试完成。");
}
}

View File

@ -3,12 +3,16 @@ package com.yanzhu.common.security.config;
import java.util.TimeZone;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author ruoyi
*/
@Configuration
@ComponentScan(basePackages = "com.yanzhu.common.core")
public class ApplicationConfig
{
/**

View File

@ -136,7 +136,7 @@ public class ProMobileAttendanceConfigController extends BaseController
double similarity = BaiduFaceSimilarityUtils.calculateFaceSimilarity(userPicture, attImg);
System.out.println("相似度:"+similarity);
double threshold = 0.8;
double threshold = BaiduFaceSimilarityUtils.SIMILARITY_THRESHOLD;
if (similarity >= threshold) {
// 相似度达标,允许考勤

View File

@ -4,29 +4,31 @@ Component({
* 组件的属性列表
*/
properties: {
title:{
type:String
title: {
type: String,
},
index:{
type:String
index: {
type: String,
},
choose:{
type:String
choose: {
type: String,
},
multiple:{
type:Boolean,
value:true
multiple: {
type: Boolean,
value: true,
},
rectifierData:{
type:Array,
value:[]
}
rectifierData: {
type: Array,
value: [],
observer: function (newVal, oldVal) {
// 当rectifierData变化时重新初始化选中状态
this.initSelectedData(newVal);
},
observers: {
},
},
observers: {},
lifetimes: {
created: function(){
created: function () {
//在组件实例刚刚被创建时执行,注意此时不能调用 setData
},
attached: function () {
@ -34,6 +36,8 @@ Component({
},
ready: function () {
// 在组件在视图层布局完成后执行
// 初始化选中数据
this.initSelectedData(this.data.rectifierData);
},
detached: function () {
// 在组件实例被从页面节点树移除时执行
@ -44,87 +48,138 @@ Component({
* 组件的初始数据
*/
data: {
show:false,
gridData:[],
selectedIndex:[]
show: false,
gridData: [],
selectedIndex: [],
},
/**
* 组件的方法列表
*/
methods: {
onAddResponsible(){
this.setData({
show:true
})
},
onClose(){
this.setData({
show:false
})
},
onSelected(e){
var data = this.data.rectifierData;
let ei = e.currentTarget.dataset.index;
let index = ei.split('_');
let userdata = data[index[0]].userList[index[1]];
let of = this.data.selectedIndex.indexOf(ei);
if(of>-1){
this.data.selectedIndex.splice(of, 1);
userdata.state = false;
}else{
this.data.selectedIndex.push(ei);
userdata.state = true;
// 初始化选中数据
initSelectedData(rectifierData) {
if (!rectifierData || rectifierData.length === 0) return;
let selectedIndex = [];
let chooses = "";
// 遍历数据,查找已选中的用户
rectifierData.forEach((group, groupIndex) => {
if (group.userList && group.userList.length > 0) {
group.userList.forEach((user, userIndex) => {
if (user.state === true) {
// 记录选中项的索引
selectedIndex.push(groupIndex + "_" + userIndex);
// 构建显示文本
let post = user.userPostName
? " [" + user.userPostName + "]"
: "";
chooses += "," + user.userName + post;
}
if(!this.data.multiple && this.data.selectedIndex.length>0){
if(this.data.selectedIndex.length>1){
this.data.selectedIndex.forEach((item) =>{
let _indexs = item.split('_');
});
}
});
// 更新数据
this.setData({
selectedIndex: selectedIndex,
choose: chooses ? chooses.substring(1) : "",
});
},
onAddResponsible() {
this.setData({
show: true,
});
},
onClose() {
this.setData({
show: false,
});
},
onSelected(e) {
// 使用深拷贝避免直接修改原始数据
const data = JSON.parse(JSON.stringify(this.data.rectifierData));
let ei = e.currentTarget.dataset.index;
let index = ei.split("_");
let userdata = data[index[0]].userList[index[1]];
let selectedIndex = [...this.data.selectedIndex];
let of = selectedIndex.indexOf(ei);
if (of > -1) {
// 取消选中
selectedIndex.splice(of, 1);
userdata.state = false;
} else {
// 选中
if (!this.data.multiple) {
// 单选模式,清空之前选中的项
selectedIndex.forEach((item) => {
let _indexs = item.split("_");
data[_indexs[0]].userList[_indexs[1]].state = false;
});
selectedIndex = [ei];
} else {
// 多选模式
selectedIndex.push(ei);
}
userdata.state = true;
this.data.selectedIndex=[];
this.data.selectedIndex.push(ei);
}
let _post="";
if(userdata.userPostName){
_post = " ["+userdata.userPostName+"]";
}
let _gridData=[{'userId':userdata.userId,'userName':userdata.userName+_post,'userPhone':userdata.userPhone}];
this.triggerEvent('selected',_gridData)
this.setData({
choose:_gridData[0].userName,
rectifierData:data,
show:false
})
}else{
this.setData({
rectifierData : data
})
}
},
onConfirm(){
var data = this.data.rectifierData;
let _gridData=[];
let chooses="";
if(this.data.selectedIndex.length>0){
this.data.selectedIndex.forEach((item) =>{
let _indexs = item.split('_');
let _post="";
if(data[_indexs[0]].userList[_indexs[1]].userPostName){
_post = " ["+data[_indexs[0]].userList[_indexs[1]].userPostName+"]";
}
let name = data[_indexs[0]].userList[_indexs[1]].userName+_post;
_gridData.push({'userId':data[_indexs[0]].userList[_indexs[1]].userId,'userName':name,'userPhone':data[_indexs[0]].userList[_indexs[1]].userPhone});
chooses+=","+name;
});
}
this.triggerEvent('selected',_gridData)
this.setData({
choose:chooses.substring(1),
show:false
})
}
// 如果是单选模式且有选中项,直接触发事件并关闭弹窗
if (!this.data.multiple && selectedIndex.length > 0) {
let _post = userdata.userPostName
? " [" + userdata.userPostName + "]"
: "";
let _gridData = [
{
userId: userdata.userId,
userName: userdata.userName + _post,
userPhone: userdata.userPhone,
},
];
this.triggerEvent("selected", _gridData);
this.setData({
choose: _gridData[0].userName,
rectifierData: data,
selectedIndex: selectedIndex,
show: false,
});
} else {
this.setData({
rectifierData: data,
selectedIndex: selectedIndex,
});
}
})
},
onConfirm() {
const data = this.data.rectifierData;
let _gridData = [];
let chooses = "";
if (this.data.selectedIndex.length > 0) {
this.data.selectedIndex.forEach((item) => {
let _indexs = item.split("_");
let _post = "";
if (data[_indexs[0]].userList[_indexs[1]].userPostName) {
_post =
" [" + data[_indexs[0]].userList[_indexs[1]].userPostName + "]";
}
let name = data[_indexs[0]].userList[_indexs[1]].userName + _post;
_gridData.push({
userId: data[_indexs[0]].userList[_indexs[1]].userId,
userName: name,
userPhone: data[_indexs[0]].userList[_indexs[1]].userPhone,
});
chooses += "," + name;
});
}
this.triggerEvent("selected", _gridData);
this.setData({
choose: chooses.substring(1),
show: false,
});
},
},
});

View File

@ -381,7 +381,18 @@ Page({
app.toast("考勤成功");
this.returnToPage();
} else {
app.toast("考勤失败: " + res.msg);
// 显示确认框,提供更友好的错误提示
wx.showModal({
title: "考勤失败",
content: res.msg || "考勤提交失败,请稍后重试",
showCancel: false, // 只显示确定按钮
confirmColor: "#ff0000", // 深色确认按钮
confirmText: "确定",
success: function (res) {
// 用户点击确定按钮后返回考勤列表页面
this.returnToPage();
}.bind(this),
});
}
});
},

View File

@ -66,8 +66,8 @@
<text class="txt-blue"> 考勤人像刷脸</text>
</view>
<view class="content_box_content att-img">
<image src="{{faceImage}}" mode="aspectFit" wx:if="{{faceImage}}" />
<view wx:else class="placeholder-text">拍照后显示图片</view>
<image src="{{faceImage}}" mode="aspectFit" bindtap="openCamera" wx:if="{{faceImage}}" />
<view wx:else class="placeholder-text" bindtap="openCamera">拍照后显示图片</view>
<button bindtap="openCamera">点击打开摄像头刷脸</button>
</view>
</view>

View File

@ -95,7 +95,7 @@ Page({
form.address = cfgData.address;
form.range = cfgData.range;
let groupList = (cfgData.groupList || []).map((it) => {
it.status = true;
it.state = true;
it.userId = it.id;
it.userName = it.groupName;
it.subDeptName = it.deptName;
@ -129,6 +129,10 @@ Page({
}).then((res) => {
if (res.code == 200) {
let tmps = res.rows || [];
let findIds = [];
if (this.data.cfgData) {
findIds = this.data.form.subGroup.map((it) => it.id);
}
this.setData({
groupList: tmps,
});
@ -153,7 +157,7 @@ Page({
subDeptType: val.subDeptType,
subDeptTypeName: deptType?.dictLabel || "",
userList: obj[key].map((it) => {
it.status = false;
it.state = findIds.includes(it.id);
it.userId = it.id;
it.userName = it.groupName;
return it;