e0f6fb774c88f2e6e4b2e739143b49c939e47dc1.svn-base 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package org.jeecg.modules.system.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jeecg.dingtalk.api.core.response.Response;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.jeecg.common.api.dto.message.MessageDTO;
  6. import org.jeecg.common.api.vo.Result;
  7. import org.jeecg.common.system.util.JwtUtil;
  8. import org.jeecg.config.thirdapp.ThirdAppConfig;
  9. import org.jeecg.modules.system.service.impl.ThirdAppDingtalkServiceImpl;
  10. import org.jeecg.modules.system.service.impl.ThirdAppWechatEnterpriseServiceImpl;
  11. import org.jeecg.modules.system.vo.thirdapp.SyncInfoVo;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.*;
  14. import javax.servlet.http.HttpServletRequest;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * 第三方App对接
  19. */
  20. @Slf4j
  21. @RestController("thirdAppController")
  22. @RequestMapping("/sys/thirdApp")
  23. public class ThirdAppController {
  24. @Autowired
  25. ThirdAppConfig thirdAppConfig;
  26. @Autowired
  27. ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService;
  28. @Autowired
  29. ThirdAppDingtalkServiceImpl dingtalkService;
  30. /**
  31. * 获取启用的系统
  32. */
  33. @GetMapping("/getEnabledType")
  34. public Result getEnabledType() {
  35. Map<String, Boolean> enabledMap = new HashMap<>();
  36. enabledMap.put("wechatEnterprise", thirdAppConfig.isWechatEnterpriseEnabled());
  37. enabledMap.put("dingtalk", thirdAppConfig.isDingtalkEnabled());
  38. return Result.OK(enabledMap);
  39. }
  40. /**
  41. * 同步本地[用户]到【企业微信】
  42. *
  43. * @param ids
  44. * @return
  45. */
  46. @GetMapping("/sync/wechatEnterprise/user/toApp")
  47. public Result syncWechatEnterpriseUserToApp(@RequestParam(value = "ids", required = false) String ids) {
  48. if (thirdAppConfig.isWechatEnterpriseEnabled()) {
  49. SyncInfoVo syncInfo = wechatEnterpriseService.syncLocalUserToThirdApp(ids);
  50. if (syncInfo.getFailInfo().size() == 0) {
  51. return Result.OK("同步成功", syncInfo);
  52. } else {
  53. return Result.error("同步失败", syncInfo);
  54. }
  55. }
  56. return Result.error("企业微信同步功能已禁用");
  57. }
  58. /**
  59. * 同步【企业微信】[用户]到本地
  60. *
  61. * @param ids 作废
  62. * @return
  63. */
  64. @GetMapping("/sync/wechatEnterprise/user/toLocal")
  65. public Result syncWechatEnterpriseUserToLocal(@RequestParam(value = "ids", required = false) String ids) {
  66. if (thirdAppConfig.isWechatEnterpriseEnabled()) {
  67. SyncInfoVo syncInfo = wechatEnterpriseService.syncThirdAppUserToLocal();
  68. if (syncInfo.getFailInfo().size() == 0) {
  69. return Result.OK("同步成功", syncInfo);
  70. } else {
  71. return Result.error("同步失败", syncInfo);
  72. }
  73. }
  74. return Result.error("企业微信同步功能已禁用");
  75. }
  76. /**
  77. * 同步本地[部门]到【企业微信】
  78. *
  79. * @param ids
  80. * @return
  81. */
  82. @GetMapping("/sync/wechatEnterprise/depart/toApp")
  83. public Result syncWechatEnterpriseDepartToApp(@RequestParam(value = "ids", required = false) String ids) {
  84. if (thirdAppConfig.isWechatEnterpriseEnabled()) {
  85. boolean flag = wechatEnterpriseService.syncLocalDepartmentToThirdApp(ids);
  86. return flag ? Result.OK("同步成功", null) : Result.error("同步失败");
  87. }
  88. return Result.error("企业微信同步功能已禁用");
  89. }
  90. /**
  91. * 同步【企业微信】[部门]到本地
  92. *
  93. * @param ids
  94. * @return
  95. */
  96. @GetMapping("/sync/wechatEnterprise/depart/toLocal")
  97. public Result syncWechatEnterpriseDepartToLocal(@RequestParam(value = "ids", required = false) String ids) {
  98. if (thirdAppConfig.isWechatEnterpriseEnabled()) {
  99. SyncInfoVo syncInfo = wechatEnterpriseService.syncThirdAppDepartmentToLocal(ids);
  100. if (syncInfo.getFailInfo().size() == 0) {
  101. return Result.OK("同步成功", syncInfo);
  102. } else {
  103. return Result.error("同步失败", syncInfo);
  104. }
  105. }
  106. return Result.error("企业微信同步功能已禁用");
  107. }
  108. /**
  109. * 同步本地[部门]到【钉钉】
  110. *
  111. * @param ids
  112. * @return
  113. */
  114. @GetMapping("/sync/dingtalk/depart/toApp")
  115. public Result syncDingtalkDepartToApp(@RequestParam(value = "ids", required = false) String ids) {
  116. if (thirdAppConfig.isDingtalkEnabled()) {
  117. boolean flag = dingtalkService.syncLocalDepartmentToThirdApp(ids);
  118. return flag ? Result.OK("同步成功", null) : Result.error("同步失败");
  119. }
  120. return Result.error("钉钉同步功能已禁用");
  121. }
  122. /**
  123. * 同步【钉钉】[部门]到本地
  124. *
  125. * @param ids
  126. * @return
  127. */
  128. @GetMapping("/sync/dingtalk/depart/toLocal")
  129. public Result syncDingtalkDepartToLocal(@RequestParam(value = "ids", required = false) String ids) {
  130. if (thirdAppConfig.isDingtalkEnabled()) {
  131. SyncInfoVo syncInfo = dingtalkService.syncThirdAppDepartmentToLocal(ids);
  132. if (syncInfo.getFailInfo().size() == 0) {
  133. return Result.OK("同步成功", syncInfo);
  134. } else {
  135. return Result.error("同步失败", syncInfo);
  136. }
  137. }
  138. return Result.error("钉钉同步功能已禁用");
  139. }
  140. /**
  141. * 同步本地[用户]到【钉钉】
  142. *
  143. * @param ids
  144. * @return
  145. */
  146. @GetMapping("/sync/dingtalk/user/toApp")
  147. public Result syncDingtalkUserToApp(@RequestParam(value = "ids", required = false) String ids) {
  148. if (thirdAppConfig.isDingtalkEnabled()) {
  149. SyncInfoVo syncInfo = dingtalkService.syncLocalUserToThirdApp(ids);
  150. if (syncInfo.getFailInfo().size() == 0) {
  151. return Result.OK("同步成功", syncInfo);
  152. } else {
  153. return Result.error("同步失败", syncInfo);
  154. }
  155. }
  156. return Result.error("钉钉同步功能已禁用");
  157. }
  158. /**
  159. * 同步【钉钉】[用户]到本地
  160. *
  161. * @param ids 作废
  162. * @return
  163. */
  164. @GetMapping("/sync/dingtalk/user/toLocal")
  165. public Result syncDingtalkUserToLocal(@RequestParam(value = "ids", required = false) String ids) {
  166. if (thirdAppConfig.isDingtalkEnabled()) {
  167. SyncInfoVo syncInfo = dingtalkService.syncThirdAppUserToLocal();
  168. if (syncInfo.getFailInfo().size() == 0) {
  169. return Result.OK("同步成功", syncInfo);
  170. } else {
  171. return Result.error("同步失败", syncInfo);
  172. }
  173. }
  174. return Result.error("钉钉同步功能已禁用");
  175. }
  176. /**
  177. * 发送消息测试
  178. *
  179. * @return
  180. */
  181. @PostMapping("/sendMessageTest")
  182. public Result sendMessageTest(@RequestBody JSONObject params, HttpServletRequest request) {
  183. /* 获取前台传递的参数 */
  184. // 第三方app的类型
  185. String app = params.getString("app");
  186. // 是否发送给全部人
  187. boolean sendAll = params.getBooleanValue("sendAll");
  188. // 消息接收者,传sys_user表的username字段,多个用逗号分割
  189. String receiver = params.getString("receiver");
  190. // 消息内容
  191. String content = params.getString("content");
  192. String fromUser = JwtUtil.getUserNameByToken(request);
  193. String title = "第三方APP消息测试";
  194. MessageDTO message = new MessageDTO(fromUser, receiver, title, content);
  195. message.setToAll(sendAll);
  196. if (ThirdAppConfig.WECHAT_ENTERPRISE.equals(app)) {
  197. if (thirdAppConfig.isWechatEnterpriseEnabled()) {
  198. JSONObject response = wechatEnterpriseService.sendMessageResponse(message, false);
  199. return Result.OK(response);
  200. }
  201. return Result.error("企业微信已被禁用");
  202. } else if (ThirdAppConfig.DINGTALK.equals(app)) {
  203. if (thirdAppConfig.isDingtalkEnabled()) {
  204. Response<String> response = dingtalkService.sendMessageResponse(message, false);
  205. return Result.OK(response);
  206. }
  207. return Result.error("钉钉已被禁用");
  208. }
  209. return Result.error("不识别的第三方APP");
  210. }
  211. /**
  212. * 撤回消息测试
  213. *
  214. * @return
  215. */
  216. @PostMapping("/recallMessageTest")
  217. public Result recallMessageTest(@RequestBody JSONObject params) {
  218. /* 获取前台传递的参数 */
  219. // 第三方app的类型
  220. String app = params.getString("app");
  221. // 消息id
  222. String msg_task_id = params.getString("msg_task_id");
  223. if (ThirdAppConfig.WECHAT_ENTERPRISE.equals(app)) {
  224. if (thirdAppConfig.isWechatEnterpriseEnabled()) {
  225. return Result.error("企业微信不支持撤回消息");
  226. }
  227. return Result.error("企业微信已被禁用");
  228. } else if (ThirdAppConfig.DINGTALK.equals(app)) {
  229. if (thirdAppConfig.isDingtalkEnabled()) {
  230. Response<JSONObject> response = dingtalkService.recallMessageResponse(msg_task_id);
  231. if (response.isSuccess()) {
  232. return Result.OK("撤回成功", response);
  233. } else {
  234. return Result.error("撤回失败:" + response.getErrcode() + "——" + response.getErrmsg(), response);
  235. }
  236. }
  237. return Result.error("钉钉已被禁用");
  238. }
  239. return Result.error("不识别的第三方APP");
  240. }
  241. }