1.登录金蝶云星空的客户端,金蝶测试环境的账号:demo 密码:888888,登录界面如下:
金蝶系统比较复杂,对接接口之前,最好请金蝶的售前人员对业务流程培训一下,确认好要对接的接口
2.查看web api接口文档,接口地址
3.找到要对接的接口,这里我们以物料接口为例:
4.物料接口的字段非常多,而且没有java对接的事例的,java开发人员看到这里一般都想爆粗口,这特么什么接口?当初我也是这样的。接下来,我讲一下接口对接具体流程
1.调用金蝶的登录接口
2.登录成功获取金蝶的kdservice-sessionid
3.调用金蝶保存接口,并带上kdservice-sessionid,否则调用不成功
4.调用金蝶的提交接口,并带上kdservice-sessionid
5.调用金蝶的审核接口,并带上kdservice-sessionid
一般来说,通过这几步整个接口的流程也就完成,如果有还有其他需求的,比如要查询,或者不要提交、审核的,在加上查询或去掉第四部第五步就行了,其他接口的调用方式都一下。
5.上面讲了,接口调用的具体流程,接下来讲一下物料基础数据的构造,上面看到了物料的字段非常多,我们根本就不知道要传那些数据,这里有个简单的方法,步骤如下:
这里你就拿到物料接口需要的数据了,接下来可以把测试数据,拷贝出来,转换成json格式,保存到一个文件里面,当做物料接口的基础数据。当然如果你不怕麻烦,你也可以建对应的java对象,然后构造成物料接口需要的数据格式。
6.基础数据有了,接下来就简单了,在来看物料接口的api
上面这三个字段是接口的必须字段,刚刚我们获取的基础数据就是data里面的数据
formid就是接口对应的表单id,物料接口对应的就是BD_MATERIAL,不同的接口对应的表单id不同,不知道表单id的可以找到对应的api,然后选择保存,在事例里面就可以找到对应的表单id
7.接下来开始编码
金蝶接口配置,配置为yml格式:
kingdee:
url: http://192.168.0.215/K3Cloud/
login: Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc #登录接口
view: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc #查看表单数据接口
viewList: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc #表单数据查询接口
save: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc #保存接口
batchSave: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc #批量保存接口
submit: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc #提交接口
audit: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc #审核接口
statusConver: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.StatusConvert.common.kdsvc #状态转换接口
K3CloundConfig.java
package com.kn.common.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties(prefix = "kingdee")
@Component
public class K3CloundConfig {
/**
* 金蝶url前缀
*/
private String url;
/**
* 登录接口
*/
private String login;
/**
* 查看接口
*/
private String view;
/**
* 保存接口
*/
private String save;
/**
* 提交接口
*/
private String submit;
/**
* 审核接口
*/
private String audit;
/**
* 状态改变接口
*/
private String statusConver;
}
KDFormIdEnum.java
package com.kn.common.enums;
import lombok.Getter;
@Getter
public enum KDFormIdEnum {
MATERIAL("BD_MATERIAL"),//物料
OUT_STOCK("SAL_OUTSTOCK"),//出库单
STOCK("BD_STOCK"),//仓库
CUSTOMER("BD_Customer");//客户
private String formid;
KDFormIdEnum(String formid) {
this.formid = formid;
}
}
HttpUtil.java
package com.kn.common.utils.http;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HttpUtil {
private HttpUtil(){
}
/**
* 连接超时时间
*/
private static final int CONN_TIMEOUT = 30000;
/**
* 请求超时时间
*/
private static final int READ_TIMEOUT = 30000;
private static RestTemplate restTemplate = null;
static{
//设置超时时间
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(CONN_TIMEOUT);
requestFactory.setReadTimeout(READ_TIMEOUT);
restTemplate = new RestTemplate(requestFactory);
}
/**
* 设置header公共参数
*/
private static HttpHeaders initHeader(){
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Accpet-Encoding", "gzip");
headers.add("Content-Encoding", "UTF-8");
headers.add("Content-Type", "application/json; charset=UTF-8");
return headers;
}
/**
* 设置header参数
* @param httpHeaders
* @param headers
*/
private static void setHeaderParam(HttpHeaders httpHeaders,Map headers){
if(!CollectionUtils.isEmpty(headers)){
Set keys = headers.keySet();
for(String key:keys){
httpHeaders.add(key,headers.get(key).toString());
}
}
}
/**
* 设置参数
* @param param
* @return
*/
private static MultiValueMap setParam(Map param){
MultiValueMap valueMap = new LinkedMultiValueMap<>();
if(CollectionUtils.isEmpty(param)){
return valueMap;
}
Set paramKeys = param.keySet();
for(String key:paramKeys){
valueMap.add(key,param.get(key).toString());
}
return valueMap;
}
/**
* 发送Get请求
* @param url 请求地址
* @param headers 请求头参数
* @return
*/
public static String httpGet(String url,Map headers){
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//设置header参数
setHeaderParam(httpHeaders,headers);
//发送请求
HttpEntity httpEntity = new HttpEntity(null,httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET,httpEntity,String.class);
return responseEntity.getBody();
}
/**
* 发送Get请求
* @param url 请求地址
* @param headers 请求头参数
* @param param 查询参数
* @return
*/
public static String httpGet(String url, Map headers,Map param){
//如果查询参数为空,则调用不带参数的Get请求
if(CollectionUtils.isEmpty(param)){
return httpGet(url,headers);
}
//组装查询参数
Set keys = param.keySet();
StringBuilder builder = new StringBuilder();
builder.append("?");
for(String key:keys){
builder.append(key).append("=").append(param.get(key)).append("&");
}
builder.deleteCharAt(builder.lastIndexOf("&"));
url = url+builder.toString();
//发送请求
return httpGet(url,headers);
}
/**
* 发送Post请求
* @param url 请求地址
* @param param 请求参数
* @return
*/
public static String httpPost(String url,Map param){
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//设置参数
MultiValueMap valueMap = setParam(param);
//发送请求
return toPost(url,httpHeaders,valueMap);
}
/**
* 发送Post请求
* @param url 请求地址
* @param json json格式参数参数
* @return
*/
public static ResponseEntity httpPost(String url,String json){
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//发送请求
return toPost(url,httpHeaders,json);
}
/**
* 发送Post请求
* @param url 请求地址
* @param json json格式参数参数
* @return
*/
public static String httpPost(String url,Map header,String json){
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
setHeaderParam(httpHeaders,header);
//发送请求
return toPost(url,httpHeaders,json).getBody();
}
/**
* 发送POST请求
* @param url 请求地址
* @param headers 请求头参数
* @param param 查询参数
* @return
*/
public static String httpPost(String url, Map headers,Map param){
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//设置header参数
setHeaderParam(httpHeaders,headers);
//设置参数
MultiValueMap valueMap = setParam(param);
return toPost(url,httpHeaders,valueMap);
}
/**
* 发送请求
* @param valueMap
* @param httpHeaders
* @param url
* @return
*/
private static String toPost(String url,HttpHeaders httpHeaders,MultiValueMap valueMap){
HttpEntity> httpEntity = new HttpEntity<>(valueMap,httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST,httpEntity,String.class);
return responseEntity.getBody();
}
/**
* 发送请求
* @param json
* @param httpHeaders
* @param url
* @return
*/
private static ResponseEntity toPost(String url,HttpHeaders httpHeaders,String json){
HttpEntity httpEntity = new HttpEntity<>(json,httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST,httpEntity,String.class);
return responseEntity;
}
}
BaseUtil.java
package com.kn.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* @date 2019/1/7 15:58
*/
public class BaseUtil {
private BaseUtil(){};
/**
* 构造登录参数
* @param dbid 账套id
* @param userName 用户名
* @param password 密码
* @param lang 语言
* @return
*/
public static String buildLogin(String dbid, String userName, String password, int lang){
Map param = new HashMap<>(4);
param.put("acctID",dbid);
param.put("username",userName);
param.put("password",password);
param.put("lcid",lang);
return JSON.toJSONString(param);
}
/**
* 初始化物料信息
* @param template 物料基础数据模板
* @param formid 表单id
* @param code 物料编码
* @param name 物料名称
* @param attr 物料规格属性
* @return
*/
public static String buildMaterial(String template,String formid,String code,String name,String attr){
JSONObject basic = JSON.parseObject(template);
Map model = (Map) basic.get("Model");
model.put("FNumber",code);
model.put("FName",name);
model.put("FSpecification",attr);
basic.put("Model",model);
JSONObject jsonObject = new JSONObject();
jsonObject.put("formid",formid);
jsonObject.put("data",JSON.toJSONString(basic));
return JSON.toJSONString(jsonObject);
}
/**
* 构造提交、审核参数
* @param formid 表单id
* @param numbers 编码 多个编码以,分隔
* @param flags 审核标示 多个以,分隔 和编码一一对应
* @return
*/
public static String buildParam(String formid,String numbers,String flags){
JSONObject jsonObject = new JSONObject();
JSONObject param = new JSONObject();
if(flags!=null){
String[] arr_flag = flags.split(",");
param.put("InterationFlags",arr_flag);
}
String[] arr_number = numbers.split(",");
param.put("Numbers",arr_number);
jsonObject.put("formid",formid);
jsonObject.put("data",JSON.toJSONString(param));
return JSON.toJSONString(jsonObject);
}
}
KingdeeService.java
package com.kn.common.kingdee.service;
import com.kn.common.vo.ResultVo;
/**
* @Description: 金蝶接口服务
* @date 2019/1/4 14:09
*/
public interface KingdeeService {
/**
* @Description: 金蝶登录接口
* @param url 接口地址
* @param content 查询参数
* @date 2019/1/4 14:12
*/
ResultVo login(String url,String content);
/**
* @Description: 金蝶查询接口
* @param cookie 登录cookie
* @param content 查询参数
* @date 2019/1/7 17:21
*/
ResultVo view(String cookie,String content);
/**
* @Description: 保存接口
* @param url 接口地址
* @param cookie 登录cookie
* @param content json格式参数
* @date 2019/1/4 14:18
*/
ResultVo save(String url,String cookie,String content);
}
KingdeeServiceImpl.java
package com.kn.common.kingdee.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kn.common.config.K3CloundConfig;
import com.kn.common.kingdee.service.KingdeeService;
import com.kn.common.utils.http.HttpUtil;
import com.kn.common.utils.result.ResultUtil;
import com.kn.common.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @Description: 金蝶物料接口实现类
* @date 2019/1/4 14:24
*/
@Service
@Slf4j
public class KingdeeServiceImpl implements KingdeeService {
@Autowired
private K3CloundConfig k3CloundConfig;
@Override
public ResultVo login(String url,String content) {
ResponseEntity responseEntity = HttpUtil.httpPost(url, content);
//获取登录cookie
if(responseEntity.getStatusCode()==HttpStatus.OK){
String login_cookie = "";
Set keys = responseEntity.getHeaders().keySet();
for(String key:keys){
if (key.equalsIgnoreCase("Set-Cookie")) {
List cookies = responseEntity.getHeaders().get(key);
for(String cookie:cookies){
if(cookie.startsWith("kdservice-sessionid")){
login_cookie=cookie;
break;
}
}
}
}
Map map = new HashMap<>();
map.put("cookie",login_cookie);
return ResultUtil.success(map);
}
Map result = JSON.parseObject(responseEntity.getBody());
return ResultUtil.error(result.get("Message").toString());
}
@Override
public ResultVo view(String cookie, String content) {
return null;
}
@Override
public ResultVo save(String url,String cookie, String content) {
//保存
Map header = new HashMap<>();
header.put("Cookie",cookie);
String result = HttpUtil.httpPost(url,header,content);
JSONObject jsonObject = JSON.parseObject(result);
Map map = (Map) jsonObject.get("Result");
Map responseStatus = (Map) map.get("ResponseStatus");
Boolean isSuccess = (Boolean) responseStatus.get("IsSuccess");
if(isSuccess){
return ResultUtil.success();
}else{
List
TimerTask.java
package com.kn.common.task;
import com.kn.common.config.K3CloundConfig;
import com.kn.common.enums.KDFormIdEnum;
import com.kn.common.enums.ResultEnum;
import com.kn.common.kingdee.service.KingdeeService;
import com.kn.common.utils.BaseUtil;
import com.kn.common.utils.file.FileUtil;
import com.kn.common.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
/**
* @date 2019/1/4 15:07
*/
@Component
@Slf4j
public class TimerTask {
@Autowired
private KingdeeService kingdeeService;
@Autowired
private K3CloundConfig k3CloundConfig;
private static final String POST_K3CloudUR = "";
@Scheduled(fixedRate = 200000)
public void login() throws IOException {
//登录金蝶系统
String loginParam = BaseUtil.buildLogin("5c1b710501f5c7","demo","888888",2052);
ResultVo login = kingdeeService.login(k3CloundConfig.getUrl()+k3CloundConfig.getLogin(),loginParam);
if(login.getCode() != ResultEnum.SUCCESS.getCode()){
log.error("【登录金蝶系统失败】:{}",login.getMsg());
return;
}
Map map = (Map) login.getData();
String cookie = map.get("cookie").toString();
//获取物料数据模板
String template = FileUtil.loadFile("classpath:json/material.json");
//构造物料接口数据
String material = BaseUtil.buildMaterial(template,KDFormIdEnum.MATERIAL.getFormid(),"10007","麻辣小龙虾","小份微辣");
//调用保存接口
ResultVo save = kingdeeService.save(k3CloundConfig.getUrl()+k3CloundConfig.getSave(),cookie,material);
if(save.getCode()!=ResultEnum.SUCCESS.getCode()){
log.error("【保存出错】:{}",save.getMsg());
return;
}
//提交物料
String submitParam = BaseUtil.buildParam(KDFormIdEnum.MATERIAL.getFormid(),"10007",null);
ResultVo submit = kingdeeService.save(k3CloundConfig.getUrl()+k3CloundConfig.getSubmit(),cookie,submitParam);
if(submit.getCode()!=ResultEnum.SUCCESS.getCode()){
log.error("【提交出错】:{}",submit.getMsg());
return;
}
//审核物料
String auditParam = BaseUtil.buildParam(KDFormIdEnum.MATERIAL.getFormid(),"10007","true");
ResultVo audit = kingdeeService.save(k3CloundConfig.getUrl()+k3CloundConfig.getAudit(),cookie,auditParam);
if(audit.getCode()!=ResultEnum.SUCCESS.getCode()){
log.error("【审核出错】:{}",audit.getMsg());
}
}
}
物料基础数据模板material.json
{
"Creator": "",
"NeedUpDateFields": [],
"NeedReturnFields": [],
"IsDeleteEntry": "True",
"SubSystemId": "",
"IsVerifyBaseDataField": "false",
"IsEntryBatchFill": "True",
"ValidateFlag": "True",
"NumberSearch": "True",
"InterationFlags": "",
"Model": {
"FMATERIALID": 0,
"FCreateOrgId": {
"FNumber": "100"
},
"FUseOrgId": {
"FNumber": "100"
},
"FNumber":"",
"FName": "测试104",
"FSpecification": "大份,微辣",
"FImgStorageType": "B",
"FIsSalseByNet": false,
"SubHeadEntity": {
"FErpClsID": "1",
"FCategoryID": {
"FNumber": "CHLB01_SYS"
},
"FTaxType": {
"FNumber": "WLDSFL01_SYS"
},
"FTaxRateId": {
"FNUMBER": "SL31_SYS"
},
"FBaseUnitId": {
"FNumber": "Pcs"
},
"FIsPurchase": true,
"FIsInventory": true,
"FIsSubContract": false,
"FIsSale": true,
"FIsProduce": false,
"FIsAsset": false,
"FWEIGHTUNITID": {
"FNUMBER": "kg"
},
"FVOLUMEUNITID": {
"FNUMBER": "m"
}
},
"SubHeadEntity1": {
"FStoreUnitID": {
"FNumber": "Pcs"
},
"FUnitConvertDir": "1",
"FIsLockStock": true,
"FIsCycleCounting": false,
"FCountCycle": "1",
"FCountDay": 1,
"FIsMustCounting": false,
"FIsBatchManage": false,
"FIsKFPeriod": false,
"FIsExpParToFlot": false,
"FCurrencyId": {
"FNumber": "PRE001"
},
"FIsEnableMinStock": false,
"FIsEnableMaxStock": false,
"FIsEnableSafeStock": false,
"FIsEnableReOrder": false,
"FIsSNManage": false,
"FIsSNPRDTracy": false,
"FSNManageType": "1",
"FSNGenerateTime": "1"
},
"SubHeadEntity2": {
"FSaleUnitId": {
"FNumber": "Pcs"
},
"FSalePriceUnitId": {
"FNumber": "Pcs"
},
"FMaxQty": 100000.0,
"FIsATPCheck": false,
"FIsReturnPart": false,
"FIsInvoice": false,
"FIsReturn": true,
"FAllowPublish": false,
"FISAFTERSALE": true,
"FISPRODUCTFILES": true,
"FISWARRANTED": false,
"FWARRANTYUNITID": "D",
"FOutLmtUnit": "SAL",
"FIsTaxEnjoy": false
},
"SubHeadEntity3": {
"FPurchaseUnitId": {
"FNumber": "Pcs"
},
"FPurchasePriceUnitId": {
"FNumber": "Pcs"
},
"FIsQuota": false,
"FQuotaType": "1",
"FIsVmiBusiness": false,
"FEnableSL": false,
"FIsPR": false,
"FIsReturnMaterial": true,
"FIsSourceControl": false,
"FPOBillTypeId": {
"FNUMBER": "CGSQD01_SYS"
},
"FPrintCount": 1,
"FMinPackCount": 1
},
"SubHeadEntity4": {
"FPlanningStrategy": "1",
"FMfgPolicyId": {
"FNumber": "ZZCL001_SYS"
},
"FFixLeadTimeType": "1",
"FVarLeadTimeType": "1",
"FCheckLeadTimeType": "1",
"FOrderIntervalTimeType": "3",
"FMaxPOQty": 100000.0,
"FEOQ": 1.0,
"FVarLeadTimeLotSize": 1.0,
"FIsMrpComReq": false,
"FReserveType": "1",
"FAllowPartAhead": false,
"FCanDelayDays": 999,
"FAllowPartDelay": true,
"FPlanOffsetTimeType": "1"
},
"SubHeadEntity5": {
"FProduceUnitId": {
"FNumber": "Pcs"
},
"FProduceBillType": {
"FNUMBER": "SCDD03_SYS"
},
"FOrgTrustBillType": {
"FNUMBER": "SCDD06_SYS"
},
"FIsSNCarryToParent": false,
"FIsProductLine": false,
"FBOMUnitId": {
"FNumber": "Pcs"
},
"FIsMainPrd": false,
"FIsCoby": false,
"FIsECN": false,
"FIssueType": "1",
"FOverControlMode": "1",
"FMinIssueQty": 1.0,
"FISMinIssueQty": false,
"FIsKitting": false,
"FIsCompleteSet": false,
"FMinIssueUnitId": {
"FNUMBER": "Pcs"
},
"FStandHourUnitId": "3600",
"FBackFlushType": "1"
},
"SubHeadEntity7": {
"FSubconUnitId": {
"FNumber": "Pcs"
},
"FSubconPriceUnitId": {
"FNumber": "Pcs"
},
"FSubBillType": {
"FNUMBER": "WWDD01_SYS"
}
},
"SubHeadEntity6": {
"FCheckIncoming": false,
"FCheckProduct": false,
"FCheckStock": false,
"FCheckReturn": false,
"FCheckDelivery": false,
"FEnableCyclistQCSTK": false,
"FEnableCyclistQCSTKEW": false,
"FCheckEntrusted": false,
"FCheckOther": false
},
"FEntityInvPty": [
{
"FInvPtyId": {
"FNumber": "01"
},
"FIsEnable": true,
"FIsAffectPrice": false,
"FIsAffectPlan": false,
"FIsAffectCost": false
},
{
"FInvPtyId": {
"FNumber": "02"
},
"FIsEnable": true,
"FIsAffectPrice": false,
"FIsAffectPlan": false,
"FIsAffectCost": false
},
{
"FInvPtyId": {
"FNumber": "03"
},
"FIsEnable": false,
"FIsAffectPrice": false,
"FIsAffectPlan": false,
"FIsAffectCost": false
},
{
"FInvPtyId": {
"FNumber": "04"
},
"FIsEnable": false,
"FIsAffectPrice": false,
"FIsAffectPlan": false,
"FIsAffectCost": false
},
{
"FInvPtyId": {
"FNumber": "06"
},
"FIsEnable": false,
"FIsAffectPrice": false,
"FIsAffectPlan": false,
"FIsAffectCost": false
}
]
}
}
ResultUtil类型
package com.kn.common.utils.result;
import com.kn.common.enums.*;
import com.kn.common.utils.Utils;
import com.kn.common.utils.page.PageGrid;
import com.kn.common.vo.ResultVo;
import java.util.ArrayList;
public class ResultUtil {
/**
* 成功结果集,不带返回数据
* @return
*/
public static ResultVo success(){
ResultVo resultVo = new ResultVo(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),null);
return resultVo;
}
/**
* 成功结果集,自定义消息类型
* @return
*/
public static ResultVo success(String msg){
ResultVo resultVo = new ResultVo(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),msg);
return resultVo;
}
/**
* 成功结果集,带返回数据
* @return
*/
public static ResultVo success(Object data){
ResultVo resultVo = buildResultVo(data);
if(resultVo!=null){
return resultVo;
}else{
return new ResultVo(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),data);
}
}
/**
* 成功结果集,带返回数据,自定义消息类型
* @return
*/
public static ResultVo success(String msg,Object data){
ResultVo resultVo = new ResultVo(ResultEnum.SUCCESS.getCode(),msg,data);
return resultVo;
}
/**
* 失败结果集,不带返回数据
* @return
*/
public static ResultVo error(){
ResultVo resultVo = new ResultVo(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg(),null);
return resultVo;
}
/**
* 失败结果集,自定义消息类型
* @return
*/
public static ResultVo error(String msg){
ResultVo resultVo = new ResultVo(1,msg,null);
return resultVo;
}
/**
* 失败结果集,自定义错误码和消息类型
* @return
*/
public static ResultVo error(Integer code ,String msg){
ResultVo resultVo = new ResultVo(code,msg,null);
return resultVo;
}
/**
* 失败结果集,带返回数据
* @return
*/
public static ResultVo error(Object data){
ResultVo resultVo = buildResultVo(data);
if(resultVo!=null){
return resultVo;
}else{
return new ResultVo(1,"error",data);
}
}
/**
* 失败结果集,带返回数据,自定义消息类型
* @return
*/
public static ResultVo error(String msg,Object data){
ResultVo resultVo = new ResultVo(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg(),data);
return resultVo;
}
/**
* 根据枚举类型返回结果,不包含数据
* @param resultEnum
* @return
*/
public static ResultVo result(ResultEnum resultEnum){
ResultVo resultVo = new ResultVo(resultEnum.getCode(),resultEnum.getMsg(),null);
return resultVo;
}
/**
* 根据枚举类型返回结果,包含数据
* @param resultEnum
* @return
*/
public static ResultVo result(ResultEnum resultEnum,Object data){
ResultVo resultVo = new ResultVo(resultEnum.getCode(),resultEnum.getMsg(),data);
return resultVo;
}
public static ResultVo buildResultVo(Object data){
if(data instanceof ProductResultEnum){
ProductResultEnum productResultEnum = (ProductResultEnum) data;
return new ResultVo(productResultEnum.getCode(),productResultEnum.getMsg(),null);
}
if(data instanceof OrderResultEnum){
OrderResultEnum orderResultEnum = (OrderResultEnum) data;
return new ResultVo(orderResultEnum.getCode(),orderResultEnum.getMeg(),null);
}
if(data instanceof BusinessResultEnum){
BusinessResultEnum busiResultEnum = (BusinessResultEnum) data;
return new ResultVo(busiResultEnum.getCode(),busiResultEnum.getMeg(),null);
}
if(data instanceof NoticeEnum){
NoticeEnum noticeEnum = (NoticeEnum) data;
return new ResultVo(noticeEnum.getCode(),noticeEnum.getMsg(),null);
}
return null;
}
public static PageGrid emptyGrid(){
return new PageGrid(new ArrayList(),0);
}
/**
* 判空校验,为空抛出运行时异常
* @param resultVo 判断对象
* @param message 异常消息
* @param 类型
* @return 不为空,返回原结果
*/
public static ResultVo requireNonNull(ResultVo resultVo, String message){
if (resultVo == null || resultVo.getCode() != ResultEnum.SUCCESS.getCode() || resultVo.getData() == null) {
throw new RuntimeException(message);
}
return resultVo;
}
/**
* 判空校验,为空抛出运行时异常
* @param pageGrid 判断对象
* @param message 异常消息
* @param 类型
* @return 不为空,返回原结果
*/
public static PageGrid requireNonNull(PageGrid pageGrid,String message){
if (Utils.gridIsEmpty(pageGrid)) {
throw new RuntimeException(message);
}
return pageGrid;
}
}
ResultVo类型
package com.kn.common.vo;
import lombok.Data;
import java.io.Serializable;
@Data
public class ResultVo implements Serializable {
private static final long serialVersionUID = 4721351384345845191L;
/**
* 状态码
*/
private int code;
/**
* 消息
*/
private String msg;
/**
* 数据结果集
*/
private T data;
public ResultVo(){}
public ResultVo(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
}
ResultEnum枚举类
package com.kn.common.enums;
import lombok.Getter;
@Getter
public enum ResultEnum {
SUCCESS(0,"成功"),
ERROR(1,"失败"),
ERROR_PAGE(2,"error/error"),
PARAMS_NOT_FOUNT(3,"缺少必要参数"),
PUBLISH_REDIS_SUCCESS(100,"发送消息成功"),
PUBLISH_REDIS_ERROR(101,"发送消息失败");
private Integer code;
private String msg;
ResultEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
附金蝶webAPI说明文档地址:https://download.csdn.net/download/u013890624/11868901