|
@@ -0,0 +1,186 @@
|
|
|
+package com.qmth.sop.business.sync;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.qmth.sop.business.service.SysConfigService;
|
|
|
+import com.qmth.sop.business.sync.been.FxxkAppAuthInfo;
|
|
|
+import com.qmth.sop.business.sync.been.FxxkAppAuthResult;
|
|
|
+import com.qmth.sop.common.contant.SystemConstant;
|
|
|
+import com.qmth.sop.common.enums.ExceptionResultEnum;
|
|
|
+import com.qmth.sop.common.util.HttpUtil;
|
|
|
+import com.qmth.sop.common.util.JacksonUtil;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: 纷享销客开放接口工具类
|
|
|
+ * @Author: CaoZixuan
|
|
|
+ * @Date: 2023-10-10
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class FxxkApiUtils {
|
|
|
+ @Resource
|
|
|
+ private SysConfigService sysConfigService;
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(FxxkApiUtils.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询纷享销客应用级授权
|
|
|
+ *
|
|
|
+ * @return 应用级授权信息
|
|
|
+ */
|
|
|
+ public FxxkAppAuthInfo findFxxkAppAuthInfo() {
|
|
|
+ String appId = sysConfigService.findByConfigKey(SystemConstant.FXXK_APP_ID).getConfigValue();
|
|
|
+ String appSecret = sysConfigService.findByConfigKey(SystemConstant.FXXK_APP_SECRET).getConfigValue();
|
|
|
+ String permanentCode = sysConfigService.findByConfigKey(SystemConstant.FXXK_PERMANENT_CODE).getConfigValue();
|
|
|
+ String postUrl = sysConfigService.findByConfigKey(SystemConstant.FXXK_APP_AUTH_URL).getConfigValue();
|
|
|
+ try {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("appId", validParam(appId, null, true, "纷享销客AppID"));
|
|
|
+ map.put("appSecret", validParam(appSecret, null, true, "纷享销客APPSecret"));
|
|
|
+ map.put("permanentCode", validParam(permanentCode, null, true, "纷享销客永久授权码"));
|
|
|
+ String requestJson = JacksonUtil.parseJson(map);
|
|
|
+
|
|
|
+ String result = HttpUtil.postJson(postUrl, requestJson, null, null, false);
|
|
|
+ FxxkAppAuthResult fxxkAppAuthResult = JSONObject.parseObject(result, FxxkAppAuthResult.class);
|
|
|
+ if (Objects.isNull(fxxkAppAuthResult) || fxxkAppAuthResult.getErrorCode() != 0) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("获取纷享销客应用级授权信息失败 : " + fxxkAppAuthResult.getErrorMessage());
|
|
|
+ }
|
|
|
+ FxxkAppAuthInfo fxxkAppAuthInfo = new FxxkAppAuthInfo();
|
|
|
+ fxxkAppAuthInfo.setCorpId(fxxkAppAuthResult.getCorpId());
|
|
|
+ fxxkAppAuthInfo.setCorpAccessToken(fxxkAppAuthResult.getCorpAccessToken());
|
|
|
+ fxxkAppAuthInfo.setExpiresIn(fxxkAppAuthResult.getExpiresIn());
|
|
|
+ return fxxkAppAuthInfo;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前登录人openUserId
|
|
|
+ *
|
|
|
+ * @return currentOpenUserId
|
|
|
+ */
|
|
|
+ public String findCurrentOpenUserId() {
|
|
|
+ final String mobile = "18903719928";
|
|
|
+ String postUrl = sysConfigService.findByConfigKey(SystemConstant.FXXK_USER_BY_MOBILE_URL).getConfigValue();
|
|
|
+
|
|
|
+ try {
|
|
|
+ FxxkAppAuthInfo fxxkAppAuthInfo = this.findFxxkAppAuthInfo();
|
|
|
+ String corpAccessToken = fxxkAppAuthInfo.getCorpAccessToken();
|
|
|
+ String corpId = fxxkAppAuthInfo.getCorpId();
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("corpAccessToken", validParam(corpAccessToken, null, true, "企业应用访问公司合法性凭证"));
|
|
|
+ map.put("corpId", validParam(corpId, null, true, "开放平台公司帐号"));
|
|
|
+ map.put("mobile", validParam(mobile, null, true, "员工手机号"));
|
|
|
+ String requestJson = JacksonUtil.parseJson(map);
|
|
|
+
|
|
|
+ String result = HttpUtil.postJson(postUrl, requestJson, null, null, false);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ Long errorCode = jsonObject.getLong("errorCode");
|
|
|
+ String errorMessage = jsonObject.getString("errorMessage");
|
|
|
+ if (errorCode != 0) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("获取员工信息失败 : " + errorMessage);
|
|
|
+ }
|
|
|
+ JSONArray empList = jsonObject.getJSONArray("empList");
|
|
|
+ JSONObject emp = empList.getJSONObject(0);
|
|
|
+ return emp.getString("openUserId");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询自定义对象列表
|
|
|
+ */
|
|
|
+ public void findCustomList() {
|
|
|
+ String currentOpenUserId = sysConfigService.findByConfigKey(SystemConstant.FXXK_CURRENT_OPEN_USER_ID).getConfigValue();
|
|
|
+ String postUrl = sysConfigService.findByConfigKey(SystemConstant.FXXK_CUSTOM_QUERY_URL).getConfigValue();
|
|
|
+ try {
|
|
|
+ FxxkAppAuthInfo fxxkAppAuthInfo = this.findFxxkAppAuthInfo();
|
|
|
+ String corpAccessToken = fxxkAppAuthInfo.getCorpAccessToken();
|
|
|
+ String corpId = fxxkAppAuthInfo.getCorpId();
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("corpAccessToken", validParam(corpAccessToken, null, true, "企业应用访问公司合法性凭证"));
|
|
|
+ map.put("corpId", validParam(corpId, null, true, "开放平台公司帐号"));
|
|
|
+ map.put("currentOpenUserId", validParam(currentOpenUserId, null, true, "当前操作人OpenUserID"));
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
+ dataMap.put("dataObjectApiName", sysConfigService.findByConfigKey(SystemConstant.FXXK_API_NAME_CRM).getConfigValue());
|
|
|
+
|
|
|
+ Map<String, Object> queryMap = new HashMap<>();
|
|
|
+ queryMap.put("offset", 0.0);
|
|
|
+ queryMap.put("limit", 2.0);
|
|
|
+ queryMap.put("filters", new ArrayList<>());
|
|
|
+
|
|
|
+ dataMap.put("search_query_info", queryMap);
|
|
|
+ map.put("data", dataMap);
|
|
|
+
|
|
|
+ String requestJson = JacksonUtil.parseJson(map);
|
|
|
+
|
|
|
+ String result = HttpUtil.postJson(postUrl, requestJson, null, null, false);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ Long errorCode = jsonObject.getLong("errorCode");
|
|
|
+ String errorMessage = jsonObject.getString("errorMessage");
|
|
|
+ if (errorCode != 0) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("获取自定义对象列表失败 : " + errorMessage);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验参数值并返回(字符型)
|
|
|
+ *
|
|
|
+ * @param value 参数值
|
|
|
+ * @param defaultValue 默认值
|
|
|
+ * @param require 是否必填(true:是,false:否)
|
|
|
+ * @param name 参数名称
|
|
|
+ */
|
|
|
+ private String validParam(String value, String defaultValue, boolean require, String name) {
|
|
|
+ if (require && StringUtils.isAllBlank(value, defaultValue)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception((StringUtils.isBlank(name) ? "" : name) + "值必填");
|
|
|
+ }
|
|
|
+ return StringUtils.isBlank(value) ? defaultValue : value;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验参数值并返回(Long型)
|
|
|
+ *
|
|
|
+ * @param value 参数值
|
|
|
+ * @param defaultValue 默认值
|
|
|
+ * @param require 是否必填(true:是,false:否)
|
|
|
+ * @param name 参数名称
|
|
|
+ */
|
|
|
+ private Long validParam(Long value, Long defaultValue, boolean require, String name) {
|
|
|
+ if (require && Objects.isNull(value) && Objects.isNull(defaultValue)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception((StringUtils.isBlank(name) ? "" : name) + "值必填");
|
|
|
+ }
|
|
|
+ return Objects.isNull(value) ? defaultValue : value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验参数值并返回 (布尔型)
|
|
|
+ *
|
|
|
+ * @param value 参数值
|
|
|
+ * @param defaultValue 默认值
|
|
|
+ * @param require 是否必填
|
|
|
+ * @param name 描述
|
|
|
+ */
|
|
|
+ private Boolean validParam(Boolean value, Boolean defaultValue, Boolean require, String name) {
|
|
|
+ if (require && value == null && defaultValue == null) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception((StringUtils.isBlank(name) ? "" : name) + "值必填");
|
|
|
+ }
|
|
|
+ return value == null ? defaultValue : value;
|
|
|
+ }
|
|
|
+}
|