|
@@ -0,0 +1,314 @@
|
|
|
+package com.malk.pro.zyjn.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyun.dingtalkyida_1_0.models.SearchFormDatasResponse;
|
|
|
+import com.aliyun.dingtalkyida_1_0.models.UpdateFormDataResponse;
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
+import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
+import com.dingtalk.api.DingTalkClient;
|
|
|
+import com.dingtalk.api.request.OapiGettokenRequest;
|
|
|
+import com.dingtalk.api.request.OapiV2UserGetRequest;
|
|
|
+import com.dingtalk.api.response.OapiGettokenResponse;
|
|
|
+import com.dingtalk.api.response.OapiV2UserGetResponse;
|
|
|
+import com.malk.server.common.McR;
|
|
|
+import com.malk.service.dingtalk.DDClient;
|
|
|
+import com.malk.pro.zyjn.service.TongbuService;
|
|
|
+import com.taobao.api.ApiException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class TongbuServiceImpl implements TongbuService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DDClient ddClient;
|
|
|
+
|
|
|
+ public static com.aliyun.dingtalkcalendar_1_0.Client createClient() throws Exception {
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
|
|
+ config.protocol = "https";
|
|
|
+ config.regionId = "central";
|
|
|
+ return new com.aliyun.dingtalkcalendar_1_0.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ //宜搭接口调用
|
|
|
+ public static com.aliyun.dingtalkyida_1_0.Client createClient2() throws Exception {
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
|
|
+ config.protocol = "https";
|
|
|
+ config.regionId = "central";
|
|
|
+ return new com.aliyun.dingtalkyida_1_0.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ //APPKEY
|
|
|
+ private static String APPKEY = "dingn2nkwrrosmyjk9o2";
|
|
|
+ //APPSECRET
|
|
|
+ private static String APPSECRET = "t26NJ2TIHeQZAG17QPJAPoVgrVwPUZCLH0JLO955_c0BoAhdaAS5eNf9Fc6KAepo";
|
|
|
+ //AgenthId:3191553633
|
|
|
+
|
|
|
+ //获取钉钉access_token的方法
|
|
|
+ public String getAccessToken() {
|
|
|
+
|
|
|
+ String accessToken = null;
|
|
|
+ DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
+ OapiGettokenRequest request = new OapiGettokenRequest();
|
|
|
+ request.setAppkey(APPKEY);
|
|
|
+ request.setAppsecret(APPSECRET);
|
|
|
+ request.setHttpMethod("GET");
|
|
|
+ try {
|
|
|
+ OapiGettokenResponse response = client.execute(request);
|
|
|
+ accessToken = response.getAccessToken();
|
|
|
+ System.out.println("申通-设备数字化管理中心-AccessToken:" + accessToken);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**根据userId获取unionId*/
|
|
|
+ public String getUnionId(String userId){
|
|
|
+
|
|
|
+ String unionid = null;
|
|
|
+ try {
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
|
|
|
+ OapiV2UserGetRequest req = new OapiV2UserGetRequest();
|
|
|
+ req.setUserid(userId);
|
|
|
+ OapiV2UserGetResponse rsp = client.execute(req, getAccessToken());
|
|
|
+// System.out.println(rsp.getBody());
|
|
|
+ String body = rsp.getBody();
|
|
|
+ //通过fastjson工具转换成json对象,逐层解析
|
|
|
+ JSONObject jsonObject = JSON.parseObject(body);
|
|
|
+ JSONObject result = jsonObject.getJSONObject("result");
|
|
|
+ unionid = result.getString("unionid");
|
|
|
+ System.out.println("根据钉钉userId获取的unionid:" + unionid);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return unionid;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //宜搭创建会议申请后同步创建钉钉日程
|
|
|
+ /**
|
|
|
+ * @param data 宜搭会议申请表单数据
|
|
|
+ * @param formInstId 宜搭会议申请表单实例ID
|
|
|
+ * */
|
|
|
+ @Override
|
|
|
+ public McR createRicheng(String data, String formInstId) {
|
|
|
+
|
|
|
+ log.info("宜搭传参【会议申请】全部表单数据:" + data);
|
|
|
+ log.info("数据实例id:" + formInstId);
|
|
|
+
|
|
|
+ log.info("传参解析中......");
|
|
|
+ JSONObject jsonObject = JSON.parseObject(data);
|
|
|
+ System.out.println("表单数据解析:" + jsonObject);
|
|
|
+
|
|
|
+ //时间格式一:定义时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ //时间格式二:使用ISO_8601格式化器来格式化Instant对象
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
|
|
|
+
|
|
|
+ //宜搭数据详情链接
|
|
|
+ String yidaURL = "https://so09k2.aliwork.com/APP_E0BKS4I6U44824QM1KE5/processDetail?formUuid=FORM-8039B0A8CD7C48E8B7B1D5FF56AD3EF0VVHP&procInsId="+ formInstId +"&navConfig.layout=1180";
|
|
|
+ System.out.println("宜搭数据详情链接:" + yidaURL);
|
|
|
+
|
|
|
+ /**宜搭表单字段*/
|
|
|
+ //创建人userId
|
|
|
+ String userId = jsonObject.getString("textField_m54zg5b0");
|
|
|
+ System.out.println("创建人userId:" + userId);
|
|
|
+ /**根据创建人userId获取unionId*/
|
|
|
+ String chuangjianrenUnionId = getUnionId(userId);
|
|
|
+
|
|
|
+ //会议主题
|
|
|
+ String huiyizhuti = jsonObject.getString("textField_m523ok9w");
|
|
|
+ System.out.println("会议主题:" + huiyizhuti);
|
|
|
+
|
|
|
+ JSONArray timeArray = jsonObject.getJSONArray("cascadeDateField_m523oka0");
|
|
|
+ //会议开始时间
|
|
|
+ long huiyikaishishijian = timeArray.getLong(0);
|
|
|
+ Date startDate = new Date(huiyikaishishijian);
|
|
|
+ String startTime = sdf.format(startDate);
|
|
|
+ System.out.println("格式化后的开始时间(正常格式):" + startTime);
|
|
|
+ Instant instantStart = Instant.ofEpochMilli(huiyikaishishijian);
|
|
|
+ String isoStartDateTime = formatter.format(instantStart);
|
|
|
+ System.out.println("会议开始时间(ISO-8601 格式的时间):" + isoStartDateTime);
|
|
|
+ //会议结束时间
|
|
|
+ long huiyijieshushijian = timeArray.getLong(1);
|
|
|
+ Date endDate = new Date(huiyijieshushijian);
|
|
|
+ String endTime = sdf.format(endDate);
|
|
|
+ System.out.println("格式化后的开始时间(正常格式):" + endTime);
|
|
|
+ Instant instantEnd = Instant.ofEpochMilli(huiyijieshushijian);
|
|
|
+ String isoEndDateTime = formatter.format(instantEnd);
|
|
|
+ System.out.println("会议结束时间(ISO-8601 格式的时间):" + isoEndDateTime);
|
|
|
+
|
|
|
+ //会议地点类型
|
|
|
+ String huiyididianType = jsonObject.getString("radioField_m98fljlv");
|
|
|
+ System.out.println("会议地点类型:" + huiyididianType);
|
|
|
+
|
|
|
+ //开会地址
|
|
|
+ String kaihuidizhi = "";
|
|
|
+ if (huiyididianType.equals("中心会议室")){
|
|
|
+ kaihuidizhi = jsonObject.getString("selectField_m98fljlp");
|
|
|
+ }else if (huiyididianType.equals("中心外部")){
|
|
|
+ kaihuidizhi = jsonObject.getString("textField_m98fljlw");
|
|
|
+ }
|
|
|
+ System.out.println("开会地址:" + kaihuidizhi);
|
|
|
+
|
|
|
+ //参会人员(多选)
|
|
|
+ JSONArray canhuirenyuanArray = jsonObject.getJSONArray("employeeField_m9atzdf9");
|
|
|
+ System.out.println("参会人员(多选):" + canhuirenyuanArray);
|
|
|
+
|
|
|
+ List canhuirenyuanList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < canhuirenyuanArray.size(); i++) {
|
|
|
+ String canhuirenyuanUserId = canhuirenyuanArray.getString(i);
|
|
|
+ /**根据参会人员userId获取unionId*/
|
|
|
+ String canhuirenyuanUnionId = getUnionId(canhuirenyuanUserId);
|
|
|
+
|
|
|
+ Map canhuirenyuanMap = new HashMap<>();
|
|
|
+ canhuirenyuanMap.put("id", canhuirenyuanUnionId);
|
|
|
+ canhuirenyuanMap.put("isOptional", true);
|
|
|
+ canhuirenyuanList.add(canhuirenyuanMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**创建钉钉日程*/
|
|
|
+ Map startMap = new HashMap<>();
|
|
|
+ startMap.put("dateTime", isoStartDateTime);//格式为ISO-8601的date-time格式
|
|
|
+ startMap.put("timeZone", "Asia/Shanghai");//所属时区,TZ database name格式
|
|
|
+
|
|
|
+ Map endMap = new HashMap<>();
|
|
|
+ endMap.put("dateTime", isoEndDateTime);//格式为ISO-8601的date-time格式
|
|
|
+ endMap.put("timeZone", "Asia/Shanghai");//所属时区,TZ database name格式
|
|
|
+
|
|
|
+ Map locationMap = new HashMap<>();
|
|
|
+ locationMap.put("displayName", kaihuidizhi);
|
|
|
+
|
|
|
+ Map mapAll = new HashMap<>();
|
|
|
+ mapAll.put("summary", huiyizhuti);//会议主题
|
|
|
+ mapAll.put("description", yidaURL);//会议描述,传入宜搭数据详情的URL
|
|
|
+ mapAll.put("isAllDay", false);//是否为全天日程
|
|
|
+ mapAll.put("start", startMap);//开始时间
|
|
|
+ mapAll.put("end", endMap);//结束时间
|
|
|
+ mapAll.put("attendees", canhuirenyuanList);//参会人员
|
|
|
+ mapAll.put("location", locationMap);//日程地点
|
|
|
+
|
|
|
+ String jsonString = JSON.toJSONString(mapAll);
|
|
|
+ System.out.println("封装密钥参数:" + jsonString);
|
|
|
+
|
|
|
+ String richengID = null;
|
|
|
+ try {
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
+ .build();
|
|
|
+ MediaType mediaType = MediaType.parse("application/json");
|
|
|
+ RequestBody body = RequestBody.create(mediaType, jsonString);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url("https://api.dingtalk.com/v1.0/calendar/users/"+ chuangjianrenUnionId +"/calendars/primary/events")
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("x-acs-dingtalk-access-token", getAccessToken())
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ String bodyString = response.body().string();
|
|
|
+ JSONObject JsonBody = JSON.parseObject(bodyString);
|
|
|
+ System.out.println("返回体body:" + JsonBody);
|
|
|
+
|
|
|
+ richengID = JsonBody.getString("id");
|
|
|
+ System.out.println("创建钉钉日程接口返回的日程ID:" + richengID);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (richengID != null){
|
|
|
+ /**将日程ID回写至宜搭表单*/
|
|
|
+ //根据实例id修改宜搭表单数据
|
|
|
+ Map mapData = new HashMap<>();
|
|
|
+ mapData.put("textField_m54zg5b1", richengID);
|
|
|
+
|
|
|
+ String mapAllString = JSON.toJSONString(mapData);
|
|
|
+ System.out.println("封装修改字段:" + mapAllString);
|
|
|
+
|
|
|
+ //更新子表控件数据【全量更新】
|
|
|
+ try {
|
|
|
+ com.aliyun.dingtalkyida_1_0.Client client = createClient2();
|
|
|
+ com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders updateFormDataHeaders = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders();
|
|
|
+ updateFormDataHeaders.xAcsDingtalkAccessToken = getAccessToken();
|
|
|
+ com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest updateFormDataRequest = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest()
|
|
|
+ .setSystemToken("U0E66VB1J86UR5RKDPXQTA7WM66K3896KHW8MS5")
|
|
|
+ .setFormInstanceId(formInstId)
|
|
|
+ .setUserId("yida_pub_account")
|
|
|
+ .setAppType("APP_E0BKS4I6U44824QM1KE5")
|
|
|
+ .setUpdateFormDataJson(mapAllString)
|
|
|
+ //使用最新的表单版本进行更新
|
|
|
+ .setUseLatestVersion(true);
|
|
|
+
|
|
|
+ UpdateFormDataResponse updateFormDataResponse = client.updateFormDataWithOptions(updateFormDataRequest, updateFormDataHeaders, new RuntimeOptions());
|
|
|
+ System.out.println(JSON.toJSONString(updateFormDataResponse));
|
|
|
+ } catch (TeaException err) {
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ } catch (Exception _err) {
|
|
|
+ TeaException err = new TeaException(_err.getMessage(), _err);
|
|
|
+ if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
+ // err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return McR.success("创建钉钉日程成功,并将日程ID反写记录至宜搭表单!");
|
|
|
+ }else {
|
|
|
+ return McR.errorParam("创建钉钉日程失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //宜搭删除会议申请后同步删除钉钉日程
|
|
|
+ /**
|
|
|
+ * @param formInstId 宜搭会议申请表单实例ID
|
|
|
+ * @param createUserId 创建人userId
|
|
|
+ * @param dingdingRichengID 钉钉日程ID
|
|
|
+ * */
|
|
|
+ @Override
|
|
|
+ public McR deleteRicheng(String formInstId, String createUserId, String dingdingRichengID) {
|
|
|
+
|
|
|
+ log.info("数据实例id:" + formInstId);
|
|
|
+ log.info("创建人userId:" + createUserId);
|
|
|
+ log.info("钉钉日程ID:" + dingdingRichengID);
|
|
|
+
|
|
|
+ /**根据创建人userId获取unionId*/
|
|
|
+ String chuangjianrenUnionId = getUnionId(createUserId);
|
|
|
+
|
|
|
+ /**删除钉钉日程*/
|
|
|
+ try {
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
+ .build();
|
|
|
+ MediaType mediaType = MediaType.parse("text/plain");
|
|
|
+ RequestBody body = RequestBody.create(mediaType, "");
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url("https://api.dingtalk.com/v1.0/calendar/users/"+ chuangjianrenUnionId +"/calendars/primary/events/"+ dingdingRichengID +"?pushNotification=true")
|
|
|
+ .method("DELETE", body)
|
|
|
+ .addHeader("x-acs-dingtalk-access-token", getAccessToken())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ String bodyString = response.body().string();
|
|
|
+ JSONObject JsonBody = JSON.parseObject(bodyString);
|
|
|
+ System.out.println("返回体body:" + JsonBody);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return McR.success("删除钉钉日程成功!");
|
|
|
+ }
|
|
|
+}
|