1.全电发票业务场景处理;

2.企微单点登录;
This commit is contained in:
刘鹏 2024-08-23 20:13:41 +08:00
parent 003fb6c7f6
commit b7f3a037fc
7 changed files with 487 additions and 22 deletions

View File

@ -146,5 +146,9 @@ public class AppflgConstant {
map.get(AppflgConstant.DZPZK_PASSWORD); map.get(AppflgConstant.DZPZK_PASSWORD);
} }
/**
* 系统主机端口
*/
public static final String KINGDEE_HOST = "kingdee_host";
} }

View File

@ -0,0 +1,31 @@
package shkd.fi.fi.common.orc;
public class FullElecInvoiceBean {
private String fileName;
private String invoiceNo;
private String fileType;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getInvoiceNo() {
return invoiceNo;
}
public void setInvoiceNo(String invoiceNo) {
this.invoiceNo = invoiceNo;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
}

View File

@ -0,0 +1,26 @@
package shkd.fi.fi.common.orc;
import kd.bos.dataentity.entity.DynamicObject;
import java.util.Map;
public class InvoiceDyobjDataBean {
private DynamicObject shkd_invoicereceipt;
private Map<String, Object> attachmentDatum;
public DynamicObject getShkd_invoicereceipt() {
return shkd_invoicereceipt;
}
public void setShkd_invoicereceipt(DynamicObject shkd_invoicereceipt) {
this.shkd_invoicereceipt = shkd_invoicereceipt;
}
public Map<String, Object> getAttachmentDatum() {
return attachmentDatum;
}
public void setAttachmentDatum(Map<String, Object> attachmentDatum) {
this.attachmentDatum = attachmentDatum;
}
}

View File

@ -33,10 +33,7 @@ import kd.bos.util.FileNameUtils;
import kd.bos.util.HttpClientUtils; import kd.bos.util.HttpClientUtils;
import kd.bos.util.StringUtils; import kd.bos.util.StringUtils;
import shkd.fi.fi.common.AppflgConstant; import shkd.fi.fi.common.AppflgConstant;
import shkd.fi.fi.common.orc.DetailList; import shkd.fi.fi.common.orc.*;
import shkd.fi.fi.common.orc.ErrorResponse;
import shkd.fi.fi.common.orc.OCRRetureData;
import shkd.fi.fi.common.orc.Response;
import shkd.fi.fi.util.LogBillUtils; import shkd.fi.fi.util.LogBillUtils;
import shkd.fi.fi.util.ParamsUtils; import shkd.fi.fi.util.ParamsUtils;
@ -77,14 +74,13 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
public void click(EventObject evt) { public void click(EventObject evt) {
super.click(evt); super.click(evt);
Button source = (Button) evt.getSource(); Button source = (Button) evt.getSource();
if (Objects.equals("btnok", source.getKey())) {
this.getView().close();
}
//获取上传附件信息 //获取上传附件信息
AttachmentPanel shkd_attachmentpanelap = this.getView().getControl("shkd_attachmentpanelap"); AttachmentPanel shkd_attachmentpanelap = this.getView().getControl("shkd_attachmentpanelap");
List<Map<String, Object>> attachmentData = shkd_attachmentpanelap.getAttachmentData(); List<Map<String, Object>> attachmentData = shkd_attachmentpanelap.getAttachmentData();
if (attachmentData.isEmpty()) { if (attachmentData.isEmpty()) {
this.getView().showMessage("请上传附件"); this.getView().showMessage("请上传附件");
return;
} }
FormShowParameter formShowParameter = this.getView().getFormShowParameter(); FormShowParameter formShowParameter = this.getView().getFormShowParameter();
//财务应付单 id //财务应付单 id
@ -146,8 +142,12 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
log.info(e.getMessage()); log.info(e.getMessage());
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Map<String, FullElecInvoiceBean> fullElecFileContisXmlMap = new HashMap<String,FullElecInvoiceBean>();
List<DynamicObject> returnDataList = new ArrayList<>();
List<DynamicObject> returnDataList = new ArrayList<DynamicObject>();
List<InvoiceDyobjDataBean> invoiceDyobjDataBeanList = new ArrayList<InvoiceDyobjDataBean>();
//循环遍历附件信息 //循环遍历附件信息
for (Map<String, Object> attachmentDatum : attachmentData) { for (Map<String, Object> attachmentDatum : attachmentData) {
//1.1.OCR发票识别接口请求头 //1.1.OCR发票识别接口请求头
@ -158,8 +158,10 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
JSONObject ocrScannerBody = new JSONObject(); JSONObject ocrScannerBody = new JSONObject();
//临时附件url //临时附件url
String attachmentUrl = (String) attachmentDatum.get("url"); String attachmentUrl = (String) attachmentDatum.get("url");
String fileName = (String)attachmentDatum.get("name");//附件名称
//文件服务器 附件url //文件服务器 附件url
String url = uploadTempfile(attachmentUrl, (String) attachmentDatum.get("name")); String url = uploadTempfile(attachmentUrl, fileName);
attachmentDatum.put("url", url); attachmentDatum.put("url", url);
attachmentDatum.put("modifytime", new Date().getTime()); attachmentDatum.put("modifytime", new Date().getTime());
@ -200,6 +202,7 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
LogBillUtils.SaveLogBill(ocr_invoicescanner_url, postjson, "OCR识别接口", ocrScannerBody.toString()); LogBillUtils.SaveLogBill(ocr_invoicescanner_url, postjson, "OCR识别接口", ocrScannerBody.toString());
if (StringUtils.isEmpty(postjson)) { if (StringUtils.isEmpty(postjson)) {
this.getView().showMessage("OCR接口识别失败"); this.getView().showMessage("OCR接口识别失败");
return;
} }
OCRRetureData ocrRetureData = JSONObject.parseObject(postjson, OCRRetureData.class); OCRRetureData ocrRetureData = JSONObject.parseObject(postjson, OCRRetureData.class);
@ -222,6 +225,23 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
this.getView().showMessage("发票收票公司和当前应付单结算组织不匹配"); this.getView().showMessage("发票收票公司和当前应付单结算组织不匹配");
return; return;
} }
//add 处理接口 start 0822 1.识别2.处理3.验真
String invoiceNo = response.getInvoiceNo();
String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
if(invoiceNo.length()==20){//全电票发票号码为20位长度
FullElecInvoiceBean fullElecInvoiceBean = fullElecFileContisXmlMap.get(invoiceNo);
if(fullElecInvoiceBean==null){
fullElecInvoiceBean = new FullElecInvoiceBean();
}
fullElecInvoiceBean.setInvoiceNo(invoiceNo);
fullElecInvoiceBean.setFileName(fileName);
if(fileType.toUpperCase().equals("XML")){
fullElecInvoiceBean.setFileType(fileType);
}
fullElecFileContisXmlMap.put(invoiceNo,fullElecInvoiceBean);
}
//add 处理接口 end 0822 1.识别2.处理3.验真
List<DetailList> detailList = response.getDetailList(); List<DetailList> detailList = response.getDetailList();
BigDecimal shkd_tax = BigDecimal.ZERO;//税额 BigDecimal shkd_tax = BigDecimal.ZERO;//税额
BigDecimal shkd_amount = BigDecimal.ZERO;//金额 BigDecimal shkd_amount = BigDecimal.ZERO;//金额
@ -271,7 +291,8 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
this.getView().showMessage(message); this.getView().showMessage(message);
return; return;
} }
this.getView().showMessage("发票已识别验真通过,生成发票明细成功");
//2.5.发票合规查验接口调用成功 生成发票识别单据 //2.5.发票合规查验接口调用成功 生成发票识别单据
DynamicObject shkd_invoicereceipt = BusinessDataServiceHelper.newDynamicObject("shkd_invoicereceipt"); DynamicObject shkd_invoicereceipt = BusinessDataServiceHelper.newDynamicObject("shkd_invoicereceipt");
shkd_invoicereceipt.set("billno", String.valueOf(System.currentTimeMillis())); shkd_invoicereceipt.set("billno", String.valueOf(System.currentTimeMillis()));
@ -315,20 +336,56 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
shkd_invoicereceipt.set("billstatus", "C"); shkd_invoicereceipt.set("billstatus", "C");
shkd_invoicereceipt.set("creator", RequestContext.get().getCurrUserId()); shkd_invoicereceipt.set("creator", RequestContext.get().getCurrUserId());
// option.setVariableValue(OperateOptionConst.ORM_SAVEOP_REMOVECACHE, "true"); // option.setVariableValue(OperateOptionConst.ORM_SAVEOP_REMOVECACHE, "true");
//保存发票识别单据并上传发票附件到发票识别单据 //保存发票识别单据并上传发票附件到发票识别单据
// SaveServiceHelper.save(new DynamicObject[]{shkd_invoicereceipt}, option);
// List<Map<String, Object>> attachmentDataList = new ArrayList<>();
// attachmentDataList.add(attachmentDatum);
// AttachmentServiceHelper.upload("shkd_invoicereceipt", shkd_invoicereceipt.getPkValue(),
// "attachmentpanel", attachmentDataList);
//先把附件放这里
InvoiceDyobjDataBean invoiceDyobjDataBean = new InvoiceDyobjDataBean();
invoiceDyobjDataBean.setShkd_invoicereceipt(shkd_invoicereceipt);
invoiceDyobjDataBean.setAttachmentDatum(attachmentDatum);
invoiceDyobjDataBeanList.add(invoiceDyobjDataBean);
} catch (IOException e) {
log.info(e.getMessage());
throw new RuntimeException(e);
}
}
StringBuffer error = new StringBuffer();
if(null!=fullElecFileContisXmlMap&&fullElecFileContisXmlMap.size()>0){
Set<Map.Entry<String, FullElecInvoiceBean>> entries = fullElecFileContisXmlMap.entrySet();
for(Map.Entry<String, FullElecInvoiceBean> fullElecInvoiceBean : entries) {
FullElecInvoiceBean value = fullElecInvoiceBean.getValue();
if(StringUtils.isEmpty(value.getFileType())){
error.append("全电票发票号码:").append(value.getInvoiceNo()).append("附件名称:").append(value.getFileName()).append(" 未上传XML格式附件").append("\r\n");
}
}
}
if(!"".equals(error.toString())){
this.getView().showMessage(error.append("请上传对应XML格式附件").toString());
return;
}
//生成发票识别单
for (int i = 0; i < invoiceDyobjDataBeanList.size(); i++) {
InvoiceDyobjDataBean invoiceDyobjDataBean = invoiceDyobjDataBeanList.get(i);
DynamicObject shkd_invoicereceipt = invoiceDyobjDataBean.getShkd_invoicereceipt();
Map<String, Object> attachmentDatum = invoiceDyobjDataBean.getAttachmentDatum();
//保存发票识别单据并上传发票附件到发票识别单据
SaveServiceHelper.save(new DynamicObject[]{shkd_invoicereceipt}, option); SaveServiceHelper.save(new DynamicObject[]{shkd_invoicereceipt}, option);
List<Map<String, Object>> attachmentDataList = new ArrayList<>(); List<Map<String, Object>> attachmentDataList = new ArrayList<>();
attachmentDataList.add(attachmentDatum); attachmentDataList.add(attachmentDatum);
AttachmentServiceHelper.upload("shkd_invoicereceipt", shkd_invoicereceipt.getPkValue(), AttachmentServiceHelper.upload("shkd_invoicereceipt", shkd_invoicereceipt.getPkValue(),
"attachmentpanel", attachmentDataList); "attachmentpanel", attachmentDataList);
returnDataList.add(shkd_invoicereceipt); returnDataList.add(shkd_invoicereceipt);
} catch (IOException e) {
log.info(e.getMessage());
throw new RuntimeException(e);
}
} }
//回写发票信息到财务应付单发票明细 //回写发票信息到财务应付单发票明细
@ -382,8 +439,12 @@ public class UploadAttachmentsFormPlugin extends AbstractFormPlugin {
} }
} }
//刷新财务应付单 //刷新财务应付单
this.getView().showMessage("发票已识别验真通过,生成发票明细成功");
parentView.invokeOperation("refresh"); parentView.invokeOperation("refresh");
this.getView().sendFormAction(parentView); this.getView().sendFormAction(parentView);
// if (Objects.equals("btnok", source.getKey())) {
this.getView().close();
// }
} }
} }

View File

@ -2,7 +2,6 @@ package shkd.fi.fi.opplugin;
import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory; import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.sap.db.jdbc.packet.ErrorLevel; import com.sap.db.jdbc.packet.ErrorLevel;
import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.entity.DynamicObjectCollection;
@ -16,9 +15,7 @@ import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.util.StringUtils; import kd.bos.util.StringUtils;
import shkd.fi.fi.common.AppflgConstant; import shkd.fi.fi.common.AppflgConstant;
import shkd.fi.fi.sap.*;
import shkd.fi.fi.sap.requst.docuentif.*; import shkd.fi.fi.sap.requst.docuentif.*;
import shkd.fi.fi.sap.result.docuentif.Data;
import shkd.fi.fi.sap.result.docuentif.Item; import shkd.fi.fi.sap.result.docuentif.Item;
import shkd.fi.fi.sap.result.docuentif.MTDocumentIFRet; import shkd.fi.fi.sap.result.docuentif.MTDocumentIFRet;
import shkd.fi.fi.util.HttpUtils; import shkd.fi.fi.util.HttpUtils;
@ -27,7 +24,6 @@ import shkd.fi.fi.util.ParamsUtils;
import shkd.fi.fi.util.XmlUtils; import shkd.fi.fi.util.XmlUtils;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -253,7 +249,7 @@ public class SynchronizaSapOPPlugin extends AbstractOperationServicePlugIn {
} }
//4.记录条数(ZRTOALL)--所传数据的凭证分录总条数 //4.记录条数(ZRTOALL)--所传数据的凭证分录总条数
DynamicObjectCollection entries = gl_voucher.getDynamicObjectCollection("entries"); DynamicObjectCollection entries = gl_voucher.getDynamicObjectCollection("entries");
integer += entries.size(); integer = entries.size();
//5.数据包唯一标识号(PACKGID)--格式JDN+时间戳 //5.数据包唯一标识号(PACKGID)--格式JDN+时间戳
String packgid = AppflgConstant.SYS_NUMBER(paramsMap) + date.getTime(); String packgid = AppflgConstant.SYS_NUMBER(paramsMap) + date.getTime();
//6.数据状态T测试运行P正式运行(ZFLAG)--固定传T //6.数据状态T测试运行P正式运行(ZFLAG)--固定传T
@ -284,7 +280,7 @@ public class SynchronizaSapOPPlugin extends AbstractOperationServicePlugIn {
//19.按本位币计的金额(DMBTR)----原币金额(oriamount) //19.按本位币计的金额(DMBTR)----原币金额(oriamount)
//20.项目文本(SGTXT)----凭证行摘要(edescription) //20.项目文本(SGTXT)----凭证行摘要(edescription)
//21.成本中心(KOSTL)----金蝶核算维度成本中心SAP编码() //21.成本中心(KOSTL)----金蝶核算维度成本中心SAP编码()
List<DT_DOCUMENT_IF_SRCDATAIT_ITEM> itemsList = new ArrayList<>(); List<DT_DOCUMENT_IF_SRCDATAIT_ITEM> itemsList = new ArrayList<DT_DOCUMENT_IF_SRCDATAIT_ITEM>();
for (DynamicObject entry : entries) { for (DynamicObject entry : entries) {
DT_DOCUMENT_IF_SRCDATAIT_ITEM items1 = new DT_DOCUMENT_IF_SRCDATAIT_ITEM(); DT_DOCUMENT_IF_SRCDATAIT_ITEM items1 = new DT_DOCUMENT_IF_SRCDATAIT_ITEM();

View File

@ -0,0 +1,193 @@
package shkd.fi.fi.sso;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.dc.api.model.Account;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.login.thirdauth.ThirdSSOAuthHandler;
import kd.bos.login.thirdauth.UserAuthResult;
import kd.bos.login.thirdauth.UserProperType;
import kd.bos.login.thirdauth.app.UserType;
import kd.bos.login.thirdauth.app.tencent.WxqyhUtils;
import kd.bos.login.utils.StringUtils;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import static kd.bos.login.thirdauth.UserProperType.Email;
import static kd.bos.login.thirdauth.UserProperType.WorkerNumber;
/**
* @ClassName OtherSysSSOAuth
* @Author zb
* @Description TODO
* @Date 2022/2/21 15:09
**/
public class OtherSysSSOAuth implements ThirdSSOAuthHandler {
private static Log logger = LogFactory.getLog(OtherSysSSOAuth.class);
private static String ERROR_USER = "errorUser";
/**
* @Author zb
* @Description 该方法是用户没有登录的时候插件需要转移到正确的登录地址
* @Date 15:10 2022/2/21
* @Param [request, response, s]
* @return void
**/
@Override
public void callTrdSSOLogin(HttpServletRequest request, HttpServletResponse response, String s) {
//用户需要登录的地址
String requestURL = URLEncoder.encode(request.getRequestURL().toString()) ;
String loginUrl = "http://10.13.11.210/ierp/login.html?redirect="+requestURL;
try {
response.sendRedirect(loginUrl);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Author zb
* @Description 该方法实现第三发插件认证及认证结果的返回
* @Date 15:10 2022/2/21
* @Param [httpServletRequest, httpServletResponse]
* @return kd.bos.login.thirdauth.UserAuthResult
**/
@Override
public UserAuthResult getTrdSSOAuth(HttpServletRequest request, HttpServletResponse response) {
UserAuthResult result = new UserAuthResult();
result.setSucess(false);
logger.info("getTrdSSOAuth apptype:"+request.getRequestURL());
String app_type = request.getParameter("apptype");
logger.info("getTrdSSOAuth apptype:"+app_type);
if (StringUtils.isEmpty(app_type)) {
logger.error("##1:wxqyh login: url not have params apptype !");
return result;
} else {
String code = request.getParameter("code");
logger.info("getTrdSSOAuth code:"+code);
if (StringUtils.isEmpty(code)) {
logger.error("##2:wxqyh login: url not have params code !");
return result;
} else {
String corpId = request.getParameter("corpid");
logger.info("getTrdSSOAuth corpId:"+corpId);
if (StringUtils.isEmpty(corpId)) {
corpId = request.getParameter("eid");
logger.info("getTrdSSOAuth eid:"+corpId);
if (StringUtils.isEmpty(corpId)) {
logger.error("##3:wxqyh login: url not have params corpId !");
return result;
}
}
String agentId = request.getParameter("agentid");
logger.info("getTrdSSOAuth agentId:"+agentId);
if (StringUtils.isEmpty(agentId)) {
logger.error("##4:wxqyh login: url not have params agentId !");
return result;
} else {
logger.info("wxqyhcorpId is " + corpId);
logger.info("wxqyhagentId is " + agentId);
String corpSecret = StringUtils.getTrimedBlank(this.getCorpSecret(corpId, agentId));
if (StringUtils.isEmpty(corpSecret)) {
logger.error("##5:wxqyh login: corpSecret is null !");
return result;
} else {
corpSecret = corpSecret.trim();
String accessToken = WxqyhUtils.getAccess_token(corpId, corpSecret, null);
if (StringUtils.isEmpty(accessToken)) {
logger.error("##6:wxqyh login: access_token is null !");
return result;
} else {
logger.info("wxqyh: code is : " + code);
String userId = null;
Map<String, Object> wxUserByCode = null;
if (app_type.equalsIgnoreCase("wxqyh")) {
wxUserByCode = WxqyhUtils.getUserByCode(accessToken, code, null);
if(null==wxUserByCode){
logger.error("##7:wxqyh login: WxqyhUtils.getUserByCode return null ");
}
userId = WxqyhUtils.getUserId(wxUserByCode);
} else if (app_type.equalsIgnoreCase("wxqyhxcx")) {
userId = WxqyhUtils.getUserIdByMicroWx(accessToken, code, null);
}
if (StringUtils.isEmpty(userId)) {
logger.info("##8:wxqyh login: userId is null !");
return result;
} else {
String userTicket = null;
HashMap<String, Object> userMap = null;
if (wxUserByCode != null && wxUserByCode.size() > 0) {
userTicket = WxqyhUtils.getUserTicket(wxUserByCode);
logger.info("##9:wxqyh login: userTicket is null !");
}
if (StringUtils.isNotEmpty(userTicket)) {
logger.info("##10:wxqyh login: userTicket is not null ,to WxqyhUtils.getUserInfoByTicket !");
userMap = WxqyhUtils.getUserInfoByTicket(accessToken, userTicket);
} else {
logger.info("##11:wxqyh login: userTicket is null ,to WxqyhUtils.getUserInfo !");
userMap = WxqyhUtils.getUserInfo(accessToken, userId, null);
}
// if(StringUtils.isEmpty(userTicket)){
// userMap = WxqyhUtils.getUserInfoByTicket(accessToken, userTicket);
// }
if (null == userMap) {
logger.info("##11:wxqyh loginget user info is error");
return result;
} else if (userMap.get("errcode") != null && !userMap.get("errcode").toString().equals("0")) {
logger.info("##12:wxqyh loginget user info is error " + userMap.get("errmsg"));
return result;
} else {
// Object mobile = userMap.get("mobile");
Object email = userMap.get("email");
logger.info("##13:wxqyh loginfrom userMap to get email:"+email);
// Object qywxhUserid = userMap.get("userid");
// logger.info("##14:wxqyh loginfrom userMap to get qywxhUserid:"+qywxhUserid);
// String wxqyhMode = request.getParameter("wxqyhMode");
if (ObjectUtils.isEmpty(email)) {
logger.info("##16:wxqyh loginemail is null !");
return result;
}
result.setUserType(Email);
result.setUser(email.toString());
result.setSucess(true);
return result;
}
}
}
}
}
}
}
}
private String getCorpSecret(String corpId, String agentId) {
DynamicObject[] load = BusinessDataServiceHelper.load("cts_wxqyh", "corpsecret", new QFilter[]{new QFilter("corpid", "=", corpId), new QFilter("agentid", "=", agentId)});
if (null != load && load.length > 0) {
DynamicObject dynamicObject = load[0];
return dynamicObject.getString("corpsecret");
} else {
return null;
}
}
}

View File

@ -0,0 +1,154 @@
package shkd.fi.fi.sso;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.dc.api.model.Account;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.login.thirdauth.app.AppAuthResult;
import kd.bos.login.thirdauth.app.ThirdAppAuthtication;
import kd.bos.login.thirdauth.app.UserType;
import kd.bos.login.thirdauth.app.tencent.WxqyhUtils;
import kd.bos.login.utils.StringUtils;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
public class WxqyhAuthtication extends ThirdAppAuthtication {
private static final String ERRMSG = "errmsg";
private static Log logger = LogFactory.getLog(WxqyhAuthtication.class);
private static final Integer QYWX_TIME_OUT = 120;
public WxqyhAuthtication() {
}
public boolean isNeedHandle(HttpServletRequest request, Account currentCenter) {
String app_type = request.getParameter("apptype");
if (StringUtils.isEmpty(app_type)) {
return false;
} else {
return app_type.equalsIgnoreCase("wxqyh") || app_type.equalsIgnoreCase("wxqyhxcx") ;
}
}
public AppAuthResult appAuthtication(HttpServletRequest request, Account currentCenter) {
AppAuthResult result = new AppAuthResult();
result.setSucceed(false);
String app_type = request.getParameter("apptype");
if (StringUtils.isEmpty(app_type)) {
return result;
} else {
String code = request.getParameter("code");
if (StringUtils.isEmpty(code)) {
return result;
} else {
String corpId = request.getParameter("corpid");
if (StringUtils.isEmpty(corpId)) {
corpId = request.getParameter("eid");
if (StringUtils.isEmpty(corpId)) {
logger.error("wxqyh login: not corpId !");
return result;
}
}
String agentId = request.getParameter("agentid");
if (StringUtils.isEmpty(agentId)) {
logger.error("wxqyh login: not agentId !");
return result;
} else {
logger.info("wxqyhcorpId is " + corpId);
logger.info("wxqyhagentId is " + agentId);
String corpSecret = StringUtils.getTrimedBlank(this.getCorpSecret(corpId, agentId, currentCenter));
if (StringUtils.isEmpty(corpSecret)) {
logger.error("wxqyh login: corpSecret is null !");
return result;
} else {
corpSecret = corpSecret.trim();
String accessToken = WxqyhUtils.getAccess_token(corpId, corpSecret, currentCenter);
if (StringUtils.isEmpty(accessToken)) {
logger.info("wxqyhaccess_token is null !");
result.setErrorMessage("wxqyhaccess_token is null !");
return result;
} else {
logger.info("wxqyh: code is : " + code);
String userId = null;
Map<String, Object> wxUserByCode = null;
if (app_type.equalsIgnoreCase("wxqyh")) {
wxUserByCode = WxqyhUtils.getUserByCode(accessToken, code, currentCenter);
userId = WxqyhUtils.getUserId(wxUserByCode);
} else if (app_type.equalsIgnoreCase("wxqyhxcx")) {
userId = WxqyhUtils.getUserIdByMicroWx(accessToken, code, currentCenter);
}
if (StringUtils.isEmpty(userId)) {
logger.info("wxqyhuserId is null !");
result.setErrorMessage("userId is null !");
return result;
} else {
String userTicket = null;
HashMap<String, Object> userMap = null;
if (wxUserByCode != null && wxUserByCode.size() > 0) {
userTicket = WxqyhUtils.getUserTicket(wxUserByCode);
}
if (StringUtils.isNotEmpty(userTicket)) {
userMap = WxqyhUtils.getUserInfoByTicket(accessToken, userTicket);
} else {
userMap = WxqyhUtils.getUserInfo(accessToken, userId, currentCenter);
}
if (null == userMap) {
logger.info("wxqyhget user info is error");
result.setErrorMessage("wxqyhget user info is error");
return result;
} else if (userMap.get("errcode") != null && !userMap.get("errcode").toString().equals("0")) {
logger.info("wxqyhget user info is error " + userMap.get("errmsg"));
result.setErrorMessage("wxqyhget user info is error, " + userMap.get("errmsg"));
return result;
} else {
String userFlag = "";
Object mobile = userMap.get("mobile");
Object qywxhUserid = userMap.get("userid");
String wxqyhMode = request.getParameter("wxqyhMode");
if (ObjectUtils.isEmpty(mobile) && !ObjectUtils.isEmpty(qywxhUserid)) {
result.setErrorMessage("wxqyh用户手机号不正确 !");
return result;
} else {
userFlag = mobile.toString();
result.setUserType(UserType.MOBILE_PHONE);
}
if (StringUtils.isEmpty(userFlag)) {
logger.info("wxqyhuser userFlag is null ! ");
result.setErrorMessage("wxqyhuser userFlag is null !");
return result;
} else {
result.setSucceed(true);
result.setAppType(request.getParameter("apptype"));
result.setUserFlag(userFlag);
return result;
}
}
}
}
}
}
}
}
}
private String getCorpSecret(String corpId, String agentId, Account currentCenter) {
DynamicObject[] load = BusinessDataServiceHelper.load("cts_wxqyh", "corpsecret", new QFilter[]{new QFilter("corpid", "=", corpId), new QFilter("agentid", "=", agentId)});
if (null != load && load.length > 0) {
DynamicObject dynamicObject = load[0];
return dynamicObject.getString("corpsecret");
} else {
return null;
}
}
}