|
@@ -0,0 +1,140 @@
|
|
|
+package com.gis.dingtalk.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.gis.common.util.Result;
|
|
|
+import com.gis.dingtalk.DingClient;
|
|
|
+import com.gis.dingtalk.conifg.DingTalkConstant;
|
|
|
+import com.gis.dingtalk.dto.DingMsgDto;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import springfox.documentation.annotations.ApiIgnore;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDate;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2023/6/14 0014 11:25
|
|
|
+ */
|
|
|
+@ApiIgnore
|
|
|
+@Slf4j
|
|
|
+@Api(tags = "钉钉相关接口")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/dingTalk")
|
|
|
+public class DingTalkController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ DingClient dingClient;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @ApiOperation(value = "获取AccessToken")
|
|
|
+ @GetMapping(value = "/accessToken")
|
|
|
+ public Result accessToken() {
|
|
|
+ return Result.success(dingClient.getToken(DingTalkConstant.app_key, DingTalkConstant.app_secret));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "获取UserId")
|
|
|
+ @GetMapping(value = "/getUserByMobile/{phone}")
|
|
|
+ public Result getUserId(@PathVariable String phone) {
|
|
|
+ JSONObject token = dingClient.getToken(DingTalkConstant.app_key, DingTalkConstant.app_secret);
|
|
|
+ String access_token = token.getString("access_token");
|
|
|
+ log.info("access_token:{}", access_token);
|
|
|
+
|
|
|
+ return Result.success(dingClient.getUserByMobile(access_token, phone));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @ApiOperation(value = "发送消息到用户")
|
|
|
+ @GetMapping(value = "/sendMsg/{phone}")
|
|
|
+ public Result sendMsg(@PathVariable String phone) {
|
|
|
+ JSONObject token = dingClient.getToken(DingTalkConstant.app_key, DingTalkConstant.app_secret);
|
|
|
+ String access_token = token.getString("access_token");
|
|
|
+ log.info("access_token:{}", access_token);
|
|
|
+
|
|
|
+ JSONObject userByMobile = dingClient.getUserByMobile(access_token, phone);
|
|
|
+ String userId = userByMobile.getString("userid");
|
|
|
+ log.info("userId:{}", userId);
|
|
|
+ DingMsgDto dto = new DingMsgDto();
|
|
|
+ dto.setAgent_id(DingTalkConstant.agent_id);
|
|
|
+
|
|
|
+ dto.setMsg(actionCard());
|
|
|
+ dto.setUserid_list(userId);
|
|
|
+ log.info("请求体: {}",dto.toString() );
|
|
|
+
|
|
|
+ return Result.success(dingClient.sendMsg(access_token, dto));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "发送消息到部门", notes = "web部门:65038809")
|
|
|
+ @GetMapping(value = "/sendDeptMsg/{deptId}")
|
|
|
+ public Result sendDeptMsg(@PathVariable String deptId) {
|
|
|
+ JSONObject token = dingClient.getToken(DingTalkConstant.app_key, DingTalkConstant.app_secret);
|
|
|
+ String access_token = token.getString("access_token");
|
|
|
+ log.info("access_token:{}", access_token);
|
|
|
+
|
|
|
+
|
|
|
+ DingMsgDto dto = new DingMsgDto();
|
|
|
+ dto.setAgent_id(DingTalkConstant.agent_id);
|
|
|
+ dto.setDept_id_list(deptId);
|
|
|
+ dto.setMsg(actionCard());
|
|
|
+ log.info("请求体: {}",dto.toString() );
|
|
|
+
|
|
|
+ return Result.success(dingClient.sendMsg(access_token, dto));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 消息模板参考
|
|
|
+ * https://open.dingtalk.com/document/isvapp/job-notification-type
|
|
|
+ * @return
|
|
|
+ *
|
|
|
+ * 卡片消息
|
|
|
+ */
|
|
|
+ private JSONObject actionCard(){
|
|
|
+ JSONObject bodyJson = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject subJson = new JSONObject();
|
|
|
+ subJson.put("title", "测试-收到请忽略-禅道工时填写提醒");
|
|
|
+ subJson.put("markdown", "## " + LocalDate.now());
|
|
|
+ subJson.put("single_title", "请填写禅道工时");
|
|
|
+ subJson.put("single_url", "http://face3d.4dage.com:8181/index.php");
|
|
|
+
|
|
|
+ bodyJson.put("msgtype", DingTalkConstant.MSG_TYPE_ACTION_CARD);
|
|
|
+ bodyJson.put(DingTalkConstant.MSG_TYPE_ACTION_CARD, subJson);
|
|
|
+ return bodyJson;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject markdown(){
|
|
|
+ JSONObject bodyJson = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject subJson = new JSONObject();
|
|
|
+ subJson.put("title", "请填写禅道title");
|
|
|
+ subJson.put("text", "## 支持markdown格式的正文内容");
|
|
|
+
|
|
|
+ bodyJson.put("msgtype", "markdown");
|
|
|
+ bodyJson.put("markdown", subJson);
|
|
|
+ return bodyJson;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject link(){
|
|
|
+ JSONObject bodyJson = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject subJson = new JSONObject();
|
|
|
+ subJson.put("title", "link-请填写禅道工时-" + LocalDate.now());
|
|
|
+ subJson.put("text", "测试");
|
|
|
+ subJson.put("messageUrl", "http://face3d.4dage.com:8181/index.php");
|
|
|
+ subJson.put("picUrl", "https://www.163.com");
|
|
|
+
|
|
|
+ bodyJson.put("msgtype", "link");
|
|
|
+ bodyJson.put("link", subJson);
|
|
|
+ return bodyJson;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|