新增表单接收通用接口
parent
c2e7c11986
commit
1169e5ef20
@ -0,0 +1,70 @@
|
||||
package com.yuyou.openapi.openapi.api;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.yuyou.openapi.openapi.common.CommonResponse;
|
||||
import com.yuyou.openapi.openapi.common.ResponseCode;
|
||||
import com.yuyou.openapi.openapi.model.dto.FormMessageDTO;
|
||||
import com.yuyou.openapi.openapi.model.vo.FormClientMessageVO;
|
||||
import com.yuyou.openapi.openapi.service.FormMessageService;
|
||||
import com.yuyou.openapi.openapi.utils.SecurityOperationUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
/**
|
||||
* 接收上游表单推送接口
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class FormClient {
|
||||
|
||||
@Autowired
|
||||
private FormMessageService formMessageService;
|
||||
|
||||
/**
|
||||
* 获取表单推送数据
|
||||
*
|
||||
* @return 返回调用信息
|
||||
*/
|
||||
@PostMapping(value = "/api/req/form/{actName}")
|
||||
@ResponseBody
|
||||
public CommonResponse getFormData(@PathVariable("actName") String actName, @RequestBody FormClientMessageVO formClientMessageVO){
|
||||
if (StringUtils.isEmpty(actName) || Objects.isNull(formClientMessageVO)) {
|
||||
return CommonResponse.createByErrorMessage(ResponseCode.EMPTY_ARGUMENT.getDesc());
|
||||
}
|
||||
log.info("====== [ Receive one request, content is {} ] ======", formClientMessageVO.toString());
|
||||
// 验证参数,并进行解密
|
||||
String callLog = formClientMessageVO.getCallLog();
|
||||
if (StringUtils.isEmpty(callLog)){
|
||||
return CommonResponse.createByErrorMessage("CallLog is empty.");
|
||||
}
|
||||
// 解密获取Json字串
|
||||
String jsonStr = SecurityOperationUtil.decCallLogSecurityInfo(callLog);
|
||||
if (StringUtils.isEmpty(jsonStr)){
|
||||
return CommonResponse.createByErrorMessage(ResponseCode.DECRYPT_ERROR.getDesc());
|
||||
}
|
||||
|
||||
FormMessageDTO formMessageDTO = convertFormMessageDTO(jsonStr, formClientMessageVO);
|
||||
|
||||
// 解析JSON并入库
|
||||
return formMessageService.recordFormMessage(actName, formMessageDTO) ?
|
||||
CommonResponse.createBySuccess() : CommonResponse.createByErrorMessage("调用失败请重试");
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换生成FormMessageDTO
|
||||
* @param jsonStr
|
||||
* @return
|
||||
*/
|
||||
private FormMessageDTO convertFormMessageDTO(String jsonStr, FormClientMessageVO formClientMessageVO) {
|
||||
FormMessageDTO formMessageDTO = JSONUtil.toBean(jsonStr, FormMessageDTO.class);
|
||||
BeanUtils.copyProperties(formClientMessageVO, formMessageDTO);
|
||||
|
||||
return formMessageDTO;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.yuyou.openapi.openapi.common.spi;
|
||||
|
||||
public interface ErrorCode {
|
||||
|
||||
// 错误码编号
|
||||
String getCode();
|
||||
|
||||
// 错误码描述
|
||||
String getDescription();
|
||||
|
||||
/** 必须提供toString的实现
|
||||
*
|
||||
* <pre>
|
||||
* @Override
|
||||
* public String toString() {
|
||||
* return String.format("Code:[%s], Description:[%s]. ", this.code, this.describe);
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
*/
|
||||
String toString();
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.yuyou.openapi.openapi.dao;
|
||||
|
||||
import com.yuyou.openapi.openapi.model.dataobject.FormActInfo;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface FormActInfoRepository extends JpaRepository<FormActInfo, Long> {
|
||||
|
||||
FormActInfo findFirstByActName(String actName);
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.yuyou.openapi.openapi.exception;
|
||||
|
||||
import com.yuyou.openapi.openapi.common.spi.ErrorCode;
|
||||
|
||||
/**
|
||||
* 业务异常
|
||||
*/
|
||||
public class BaiyeException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private ErrorCode errorCode;
|
||||
|
||||
public BaiyeException(ErrorCode errorCode) {
|
||||
super(errorCode.toString());
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public BaiyeException(ErrorCode errorCode, String errorMessage) {
|
||||
super(errorCode.toString() + " - " + errorMessage);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
private BaiyeException(ErrorCode errorCode, String errorMessage,
|
||||
Throwable cause) {
|
||||
super(errorCode.toString() + " - " + getMessage(errorMessage)
|
||||
+ " - " + getMessage(cause), cause);
|
||||
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public static BaiyeException asBaiyeException(ErrorCode errorCode, String message) {
|
||||
return new BaiyeException(errorCode, message);
|
||||
}
|
||||
|
||||
public static BaiyeException asBaiyeException(ErrorCode errorCode, String message,
|
||||
Throwable cause) {
|
||||
if (cause instanceof BaiyeException) {
|
||||
return (BaiyeException) cause;
|
||||
}
|
||||
return new BaiyeException(errorCode, message, cause);
|
||||
}
|
||||
|
||||
public static BaiyeException asBaiyeException(ErrorCode errorCode,
|
||||
Throwable cause) {
|
||||
if (cause instanceof BaiyeException) {
|
||||
return (BaiyeException) cause;
|
||||
}
|
||||
return new BaiyeException(errorCode, getMessage(cause), cause);
|
||||
}
|
||||
|
||||
public ErrorCode getErrorCode() {
|
||||
return this.errorCode;
|
||||
}
|
||||
|
||||
|
||||
private static String getMessage(Object obj) {
|
||||
if (obj == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (obj instanceof Throwable) {
|
||||
return ((Throwable) obj).getMessage();
|
||||
} else {
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.yuyou.openapi.openapi.exception;
|
||||
|
||||
import com.yuyou.openapi.openapi.common.spi.ErrorCode;
|
||||
|
||||
public enum CommonErrorCode implements ErrorCode {
|
||||
|
||||
CONFIG_ERROR(501, "您提供的配置文件存在错误信息,请检查您的作业配置"),
|
||||
RUNTIME_ERROR(500, "运行时内部调用错误"),
|
||||
DATA_NOT_EXIT(502, "数据不存在"),;
|
||||
private final int code;
|
||||
|
||||
private final String describe;
|
||||
|
||||
private CommonErrorCode(int code, String describe) {
|
||||
this.code = code;
|
||||
this.describe = describe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCode() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Code:[%s], Describe:[%s]", this.code,
|
||||
this.describe);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.yuyou.openapi.openapi.model.dataobject;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "form_act_info")
|
||||
public class FormActInfo {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long actId;
|
||||
|
||||
/**
|
||||
* 记录创建时间
|
||||
*/
|
||||
@Column(name = "gmt_create")
|
||||
@CreatedDate
|
||||
private Date gmtCreate;
|
||||
|
||||
/**
|
||||
* 记录修改时间
|
||||
*/
|
||||
@Column(name = "gmt_modified")
|
||||
@LastModifiedDate
|
||||
private Date gmtModified;
|
||||
|
||||
/**
|
||||
* 业务名
|
||||
*/
|
||||
@Column(name = "act_name")
|
||||
private String actName;
|
||||
|
||||
/**
|
||||
* 业务对应表名
|
||||
*/
|
||||
@Column(name = "act_table_name")
|
||||
private String actTableName;
|
||||
|
||||
/**
|
||||
* 所需字段及解析规则
|
||||
*/
|
||||
@Column(name = "columns_rule")
|
||||
private String columnsRule;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.yuyou.openapi.openapi.model.dto;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class FormMessageDTO {
|
||||
/**
|
||||
* 推送用户的ID
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 请求时间戳
|
||||
*/
|
||||
|
||||
private Long timestamp;
|
||||
|
||||
/**
|
||||
* 标记集合
|
||||
*/
|
||||
private JSONObject tag;
|
||||
|
||||
/**
|
||||
* 数据集合
|
||||
*/
|
||||
private JSONObject data;
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.yuyou.openapi.openapi.model.param;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 表单查询参数
|
||||
*/
|
||||
@Data
|
||||
public class FormQueryParam {
|
||||
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* uid
|
||||
*/
|
||||
private String uid;
|
||||
|
||||
/**
|
||||
* 记录ID
|
||||
*/
|
||||
private Long recId;
|
||||
|
||||
/**
|
||||
* 上游推送ID
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 学科
|
||||
*/
|
||||
private List<Integer> stuSubjects;
|
||||
|
||||
/**
|
||||
* 年级
|
||||
*/
|
||||
private List<Integer> stuGrades;
|
||||
|
||||
private Integer gradeStart;
|
||||
private Integer gradeEnd;
|
||||
|
||||
/**
|
||||
* 推送时间
|
||||
*/
|
||||
private String pushTimeStart;
|
||||
private String pushTimeEnd;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.yuyou.openapi.openapi.model.vo;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 表单请求接口表现层映射
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class FormClientMessageVO extends ABClientBaseVO implements Serializable {
|
||||
/**
|
||||
* 需要接收到的加密数据
|
||||
*/
|
||||
@JsonProperty(value = "calllog")
|
||||
private String callLog;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.yuyou.openapi.openapi.service;
|
||||
|
||||
import com.yuyou.openapi.openapi.model.dto.FormMessageDTO;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
* 表单数据处理服务层
|
||||
*/
|
||||
@Validated
|
||||
public interface FormMessageService {
|
||||
|
||||
/**
|
||||
* 记录表单数据
|
||||
* @param actName 业务名称
|
||||
* @param formMessageDTO
|
||||
*/
|
||||
boolean recordFormMessage(String actName, FormMessageDTO formMessageDTO);
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.yuyou.openapi.openapi.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yuyou.openapi.openapi.common.security.SecurityConstants;
|
||||
import com.yuyou.openapi.openapi.common.security.SecurityService;
|
||||
import com.yuyou.openapi.openapi.exception.SecretException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public final class ConvertHelp implements SecurityConstants {
|
||||
|
||||
/**
|
||||
* 表单通用接口的值类型转换
|
||||
* @param columnRules 列转换规则
|
||||
* @param filedValues
|
||||
* @return
|
||||
* @throws SecretException
|
||||
*/
|
||||
public static Map<String, Object> convertFields(JSONObject columnRules, JSONObject filedValues) throws SecretException {
|
||||
Map<String, Object> resultMap = new HashMap<>(columnRules.size());
|
||||
for (String column : columnRules.keySet()) {
|
||||
Object rule = columnRules.get(column);
|
||||
// 特殊列名在匹配时进行转换
|
||||
column = StringUtils.isNotEmpty(column) && "pnum" == column ? "mobile" : column;
|
||||
String value = filedValues.getString(column);
|
||||
|
||||
if (rule instanceof Integer) {
|
||||
Integer tag = (Integer) rule;
|
||||
switch (tag) {
|
||||
case PHONE_ENCRYPT_TAG:
|
||||
String pnum = SecurityService.encrypt(value, SecurityConstants.PHONE);
|
||||
resultMap.put("pnum", pnum);
|
||||
break;
|
||||
case SIMPLE_ENCRYPT_TAG:
|
||||
String encryptedValue = SecurityService.encrypt(value, SecurityConstants.SIMPLE);
|
||||
resultMap.put(column, encryptedValue);
|
||||
break;
|
||||
default:
|
||||
resultMap.put(column, value);
|
||||
break;
|
||||
}
|
||||
} else if (rule instanceof JSONObject) {
|
||||
// 基于规则进行映射, 规则统一有一个"未知"映射
|
||||
JSONObject map = (JSONObject) rule;
|
||||
Object mapValue = map.keySet().contains(value) ? map.get(value) : map.get("未知");
|
||||
resultMap.put(column, mapValue);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue