|
|
@@ -1,18 +1,31 @@
|
|
|
package biz.xy;
|
|
|
|
|
|
+import base.WechatC;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import okhttp3.Response;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
+import ss.AppB;
|
|
|
import ss.cmn.idcard.IdcardU;
|
|
|
+import ss.cmn.str.StrDate;
|
|
|
+import ss.cmn.str.StrUuid;
|
|
|
import ss.db.Db;
|
|
|
import ss.db.FaceCollectDb;
|
|
|
import ss.db.SqlCmd;
|
|
|
+import ss.http.HttpU;
|
|
|
import ss.serv.Serv;
|
|
|
import ss.serv.ServC;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.sql.SQLException;
|
|
|
import java.sql.Timestamp;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
@@ -31,9 +44,16 @@ public class FaceCollectServ extends Serv {
|
|
|
private static final String TAB_FACE_COLLECT_TOKEN = "XY_FACE_COLLECT_TOKEN";
|
|
|
private static final String SEQ_FACE_COLLECT = "SEQ_XY_FACE_COLLECT";
|
|
|
private static final String SEQ_FACE_COLLECT_LOG = "SEQ_XY_FACE_COLLECT_LOG";
|
|
|
+ private static final String SEQ_FACE_COLLECT_TOKEN = "SEQ_XY_FACE_COLLECT_TOKEN";
|
|
|
private static final String DEF_SCENE = "faceCollect";
|
|
|
private static final String DEF_CHANNEL = "miniProgram";
|
|
|
private static final String DEF_PLATFORM = "mp-weixin";
|
|
|
+ private static final String DEF_QRCODE_PAGE = "pages/public/faceCollect/index";
|
|
|
+ private static final String DEF_QRCODE_DIR = "faceCollect/qrcode";
|
|
|
+ private static final String DEF_QRCODE_ENV = "release";
|
|
|
+ private static final int DEF_QRCODE_EXPIRE_DAYS = 30;
|
|
|
+ private static final String URL_WECHAT_QRCODE =
|
|
|
+ "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";
|
|
|
private static final int HK_SYNC_STATUS_UNSYNC = 0;
|
|
|
private static final int JLZT_EDITING = 1;
|
|
|
private static final long DUP_REQ_GAP_MS = 1200L;
|
|
|
@@ -66,6 +86,94 @@ public class FaceCollectServ extends Serv {
|
|
|
return ServC.SERV_notByA;
|
|
|
}
|
|
|
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ public String genQrcode() {
|
|
|
+ super.wrJsonByResp(genQrcodeResult());
|
|
|
+ return ServC.SERV_notByA;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> genQrcodeResult() {
|
|
|
+ String batchId = StringUtils.defaultIfBlank(rdReqTrim("batchId"), buildBatchId());
|
|
|
+ String remark = StringUtils.left(StringUtils.defaultIfBlank(rdReqTrim("remark"), "匿名人脸采集二维码"), 500);
|
|
|
+ String envVersion = normalizeEnvVersion(rdReqTrim("envVersion"));
|
|
|
+ boolean checkPath = rdReqBool("checkPath", false);
|
|
|
+ Integer maxSubmitNum = rdReqNullableInt("maxSubmitNum");
|
|
|
+ Timestamp expireTime;
|
|
|
+ try {
|
|
|
+ expireTime = resolveReqExpireTime();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return mkFail("FACE_COLLECT_PARAM_INVALID", rdErrMsg(e, "二维码有效期参数不正确。"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(batchId))
|
|
|
+ return mkFail("FACE_COLLECT_PARAM_INVALID", "二维码批次不能为空。");
|
|
|
+ if (!"release".equals(envVersion) && !"trial".equals(envVersion) && !"develop".equals(envVersion))
|
|
|
+ return mkFail("FACE_COLLECT_PARAM_INVALID", "envVersion 仅支持 release、trial、develop。");
|
|
|
+ if (expireTime == null || !expireTime.after(new Timestamp(System.currentTimeMillis())))
|
|
|
+ return mkFail("FACE_COLLECT_PARAM_INVALID", "二维码过期时间必须大于当前时间。");
|
|
|
+ if (maxSubmitNum != null && maxSubmitNum <= 0)
|
|
|
+ return mkFail("FACE_COLLECT_PARAM_INVALID", "maxSubmitNum 必须大于 0。");
|
|
|
+ if (StringUtils.isBlank(AppB.imgBaseDir))
|
|
|
+ return mkFail("FACE_COLLECT_CFG_ERROR", "系统未配置图片根目录,无法保存二维码。");
|
|
|
+
|
|
|
+ String token = StrUuid.makeUuid();
|
|
|
+ String sceneValue = token;
|
|
|
+ String qrPath = buildQrcodeRelPath(token);
|
|
|
+ File qrFile = null;
|
|
|
+
|
|
|
+ try (Db gdba = FaceCollectDb.makeConn()) {
|
|
|
+ if (gdba == null)
|
|
|
+ return mkFail("FACE_COLLECT_DB_ERROR", "人脸采集数据源不可用。");
|
|
|
+
|
|
|
+ byte[] qrBytes = rdWechatMiniProgramQrcode(sceneValue, envVersion, checkPath);
|
|
|
+ qrFile = new File(AppB.imgBaseDir, qrPath.replace("/", File.separator));
|
|
|
+ File parent = qrFile.getParentFile();
|
|
|
+ if (parent != null && !parent.exists() && !parent.mkdirs())
|
|
|
+ throw new RuntimeException("二维码目录创建失败:" + parent.getAbsolutePath());
|
|
|
+ FileUtils.writeByteArrayToFile(qrFile, qrBytes);
|
|
|
+
|
|
|
+ long tokenId = gdba.rdNextSeqVal(SEQ_FACE_COLLECT_TOKEN);
|
|
|
+ Timestamp now = new Timestamp(System.currentTimeMillis());
|
|
|
+ SqlCmd insSql = new SqlCmd("insert into " + TAB_FACE_COLLECT_TOKEN +
|
|
|
+ " (TOKEN_ID,TOKEN,SCENE,BATCH_ID,STATUS,EXPIRE_TIME,MAX_SUBMIT_NUM,CUR_SUBMIT_NUM,REMARK,CJSJ)" +
|
|
|
+ " values (:tokenId,:token,:scene,:batchId,1,:expireTime,:maxSubmitNum,0,:remark,:cjsj)");
|
|
|
+ insSql.wrLongParm("tokenId", tokenId);
|
|
|
+ insSql.wrStrParm("token", token);
|
|
|
+ insSql.wrStrParm("scene", DEF_SCENE);
|
|
|
+ insSql.wrStrParm("batchId", StringUtils.left(batchId, 100));
|
|
|
+ insSql.wrTimeParm("expireTime", expireTime);
|
|
|
+ insSql.wrIntParm("maxSubmitNum", maxSubmitNum);
|
|
|
+ insSql.wrStrParm("remark", remark);
|
|
|
+ insSql.wrTimeParm("cjsj", now);
|
|
|
+ gdba.doModByC(insSql);
|
|
|
+
|
|
|
+ logOper(gdba, 0L, "", token, "genQrcode", true,
|
|
|
+ "二维码生成成功,envVersion=" + envVersion + ",batchId=" + batchId + "。");
|
|
|
+ gdba.commit();
|
|
|
+
|
|
|
+ Map<String, Object> result = mkOk("二维码生成成功");
|
|
|
+ Map<String, Object> data = castData(result);
|
|
|
+ data.put("token", token);
|
|
|
+ data.put("scene", DEF_SCENE);
|
|
|
+ data.put("sceneValue", sceneValue);
|
|
|
+ data.put("page", DEF_QRCODE_PAGE);
|
|
|
+ data.put("batchId", batchId);
|
|
|
+ data.put("expireTime", fmtTime(expireTime));
|
|
|
+ data.put("maxSubmitNum", maxSubmitNum);
|
|
|
+ data.put("envVersion", envVersion);
|
|
|
+ data.put("checkPath", checkPath);
|
|
|
+ data.put("qrPath", qrPath);
|
|
|
+ data.put("qrPreviewUrl", buildQrcodePreviewUrl(qrPath));
|
|
|
+ data.put("miniProgramAppId", WechatC.WECHATP_appId);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (qrFile != null && qrFile.exists())
|
|
|
+ FileUtils.deleteQuietly(qrFile);
|
|
|
+ log4j.error("FaceCollectServ.genQrcodeResult() 异常", e);
|
|
|
+ return mkFail("FACE_COLLECT_QRCODE_FAIL", rdErrMsg(e, "生成小程序二维码失败。"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private Map<String, Object> chkSceneTokenResult() {
|
|
|
String token = rdReqTrim("token");
|
|
|
String scene = rdReqTrim("scene");
|
|
|
@@ -428,6 +536,24 @@ public class FaceCollectServ extends Serv {
|
|
|
return StringUtils.trimToEmpty(super.req == null ? null : super.req.getParameter(parmName));
|
|
|
}
|
|
|
|
|
|
+ private boolean rdReqBool(String parmName, boolean defVal) {
|
|
|
+ String valStr = rdReqTrim(parmName);
|
|
|
+ if (StringUtils.isBlank(valStr))
|
|
|
+ return defVal;
|
|
|
+ return rdBoolStr(valStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer rdReqNullableInt(String parmName) {
|
|
|
+ String valStr = rdReqTrim(parmName);
|
|
|
+ if (StringUtils.isBlank(valStr))
|
|
|
+ return null;
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(valStr);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private long rdReqLong(String parmName) {
|
|
|
String valStr = rdReqTrim(parmName);
|
|
|
if (StringUtils.isBlank(valStr))
|
|
|
@@ -559,6 +685,95 @@ public class FaceCollectServ extends Serv {
|
|
|
return StringUtils.isBlank(msg) ? defMsg : msg;
|
|
|
}
|
|
|
|
|
|
+ private Timestamp resolveReqExpireTime() throws Exception {
|
|
|
+ String expireTimeStr = rdReqTrim("expireTime");
|
|
|
+ if (StringUtils.isNotBlank(expireTimeStr)) {
|
|
|
+ try {
|
|
|
+ return StrDate.toTimestamp(expireTimeStr, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ } catch (Exception ignore) {
|
|
|
+ return StrDate.toTimestamp(expireTimeStr, "yyyy-MM-dd HH:mm");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer expireDays = rdReqNullableInt("expireDays");
|
|
|
+ if (expireDays == null)
|
|
|
+ expireDays = DEF_QRCODE_EXPIRE_DAYS;
|
|
|
+ if (expireDays <= 0)
|
|
|
+ throw new RuntimeException("expireDays 必须大于 0。");
|
|
|
+ return new Timestamp(System.currentTimeMillis() + expireDays.longValue() * 24 * 60 * 60 * 1000L);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String normalizeEnvVersion(String envVersion) {
|
|
|
+ return StringUtils.defaultIfBlank(StringUtils.trimToNull(envVersion), DEF_QRCODE_ENV).toLowerCase();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildBatchId() {
|
|
|
+ return "FC" + new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildQrcodeRelPath(String token) {
|
|
|
+ return DEF_QRCODE_DIR + "/" + new SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date()) + "/" + token + ".jpg";
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildQrcodePreviewUrl(String qrPath) {
|
|
|
+ return ServC.SPATH_root + "?" + ServC.URL_ssServ + "=dlByHttp&type=img&path=" +
|
|
|
+ URLEncoder.encode(qrPath, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] rdWechatMiniProgramQrcode(String sceneValue, String envVersion, boolean checkPath) throws Exception {
|
|
|
+ String accessToken = rdWechatMiniProgramAccessToken();
|
|
|
+ if (StringUtils.isBlank(accessToken))
|
|
|
+ throw new RuntimeException("未获取到微信 access_token。");
|
|
|
+
|
|
|
+ JsonObject reqBody = new JsonObject();
|
|
|
+ reqBody.addProperty("scene", sceneValue);
|
|
|
+ reqBody.addProperty("page", DEF_QRCODE_PAGE);
|
|
|
+ reqBody.addProperty("env_version", envVersion);
|
|
|
+ reqBody.addProperty("check_path", checkPath);
|
|
|
+
|
|
|
+ try (Response response = HttpU.doPostUrl(String.format(URL_WECHAT_QRCODE, accessToken), reqBody)) {
|
|
|
+ if (response == null || response.body() == null)
|
|
|
+ throw new RuntimeException("微信小程序码接口无响应。");
|
|
|
+
|
|
|
+ byte[] bodyBytes = response.body().bytes();
|
|
|
+ if (bodyBytes.length == 0)
|
|
|
+ throw new RuntimeException("微信小程序码接口返回空内容。");
|
|
|
+ if (ifJsonBody(bodyBytes))
|
|
|
+ throw new RuntimeException(parseWechatErrMsg(bodyBytes));
|
|
|
+ return bodyBytes;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String rdWechatMiniProgramAccessToken() {
|
|
|
+ JsonObject tokenResp = HttpU.getUrl(WechatC.URL_rdWechatpToken);
|
|
|
+ if (tokenResp == null)
|
|
|
+ return null;
|
|
|
+ JsonElement accessToken = tokenResp.get("access_token");
|
|
|
+ return accessToken == null ? null : accessToken.getAsString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean ifJsonBody(byte[] bodyBytes) {
|
|
|
+ for (byte bodyByte : bodyBytes) {
|
|
|
+ char ch = (char) (bodyByte & 0xFF);
|
|
|
+ if (Character.isWhitespace(ch))
|
|
|
+ continue;
|
|
|
+ return ch == '{' || ch == '[';
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String parseWechatErrMsg(byte[] bodyBytes) {
|
|
|
+ try {
|
|
|
+ String bodyStr = new String(bodyBytes, StandardCharsets.UTF_8);
|
|
|
+ JsonObject errObj = JsonParser.parseString(bodyStr).getAsJsonObject();
|
|
|
+ String errCode = errObj.has("errcode") ? errObj.get("errcode").getAsString() : "";
|
|
|
+ String errMsg = errObj.has("errmsg") ? errObj.get("errmsg").getAsString() : bodyStr;
|
|
|
+ return "微信小程序码生成失败:errcode=" + errCode + ",errmsg=" + errMsg;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "微信小程序码生成失败,且错误返回无法解析。";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static class TokenChkResult {
|
|
|
private final Map<String, Object> result;
|
|
|
private final Map<String, Object> tokenRow;
|