package com.fjhx.dingding.service.impl; import cn.hutool.core.util.RandomUtil; import cn.hutool.extra.pinyin.PinyinUtil; import com.alibaba.fastjson.JSONObject; import com.fjhx.dingding.entity.suite.po.SuiteTicketInfo; import com.fjhx.dingding.service.DingService; import com.fjhx.dingding.service.suite.SuiteTicketInfoService; import com.fjhx.tenant.entity.tenant.po.TenantInfo; import com.fjhx.tenant.service.tenant.impl.TenantInfoServiceImpl; import com.ruoyi.common.constant.StatusConstant; import com.ruoyi.common.exception.ServiceException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus; import java.util.List; import java.util.stream.Collectors; @Service public class DingServiceImpl implements DingService { @Autowired private SuiteTicketInfoService suiteTicketInfoService; @Autowired private TenantInfoServiceImpl tenantInfoService; @Autowired private PlatformTransactionManager platformTransactionManager; @Autowired private TransactionDefinition transactionDefinition; /** * https://open.dingtalk.com/document/isvapp/data-formats */ @Override public void saveCallback(JSONObject callBackContent) { JSONObject bizData = callBackContent.getJSONObject("bizData"); switch (bizData.getInteger("biz_type")) { case 2: saveSuiteTicketInfo(bizData); break; case 4: corpAuthorization(bizData); break; } } /** * 企业授权 *
* https://open.dingtalk.com/document/isvapp/obtains-the-authorization-information-after-the-enterprise-activates-the-application
*/
private void corpAuthorization(JSONObject bizData) {
String syncAction = bizData.getString("syncAction");
switch (syncAction) {
// 企业授权
case "org_suite_auth":
orgSuiteAuth(bizData);
break;
// 企业变更授权范围
case "org_suite_change":
break;
// 表示企业解除授权
case "org_suite_relieve":
break;
}
}
/**
* 保存 suite_ticket
*/
private void saveSuiteTicketInfo(JSONObject bizData) {
String corpId = bizData.getString("corp_id");
String suiteTicket = bizData.getJSONObject("biz_data").getString("suiteTicket");
boolean update = suiteTicketInfoService.update(q -> q
.eq(SuiteTicketInfo::getCorpId, corpId)
.set(SuiteTicketInfo::getSuiteTicket, suiteTicket));
if (!update) {
SuiteTicketInfo suiteTicketInfo = new SuiteTicketInfo();
suiteTicketInfo.setCorpId(corpId);
suiteTicketInfo.setSuiteTicket(suiteTicket);
suiteTicketInfoService.save(suiteTicketInfo);
}
}
/**
* 企业授权
*/
private void orgSuiteAuth(JSONObject bizData) {
JSONObject authCorpInfo = bizData.getJSONObject("auth_corp_info");
// 企业id
String corpid = authCorpInfo.getString("corpid");
// 企业名称
String corpName = authCorpInfo.getString("corp_name");
// 企业id存在则跳过
if (tenantInfoService.getOne(q -> q.eq(TenantInfo::getCorpid, corpid)) != null) {
return;
}
// 获取tenantId
String firstLetter = PinyinUtil.getFirstLetter(corpName, "");
String tenantId = "dd-" + firstLetter;
List