SynQyWebChatUtil.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package jnpf.base.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import jnpf.exception.WxErrorException;
  4. import jnpf.message.model.message.OrganizeListVO;
  5. import jnpf.util.JsonUtil;
  6. import jnpf.util.wxutil.HttpUtil;
  7. import java.util.*;
  8. /**
  9. * 同步到企业微信的接口
  10. *
  11. * @版本: V3.1.0
  12. * @版权: 引迈信息技术有限公司(https://www.jnpfsoft.com)
  13. * @作者: JNPF开发平台组
  14. * @日期: 2021/4/21 8:20
  15. */
  16. public class SynQyWebChatUtil {
  17. /**
  18. * token 接口
  19. */
  20. public static final String TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
  21. //--------------------------------------------部门--------------------------------------
  22. /**
  23. * 创建部门
  24. */
  25. public static final String CREATE_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=%s";
  26. /**
  27. * 更新部门
  28. */
  29. public static final String UPDATE_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=%s";
  30. /**
  31. * 删除部门
  32. */
  33. public static final String DELETE_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=%s&id=%s";
  34. /**
  35. * 获取部门列表
  36. */
  37. public static final String GET_DEPARTMENT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s&id=%s";
  38. public static final String GET_DEPARTMENT_SIMPLELIST = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%s";
  39. //-------------------------------------------用户-----------------------------------------------------
  40. /**
  41. * 创建用户
  42. */
  43. public static final String CREATE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=%s";
  44. /**
  45. * 更新用户
  46. */
  47. public static final String UPDATE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=%s";
  48. /**
  49. * 删除用户
  50. */
  51. public static final String DELETE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=%s&userid=%s";
  52. /**
  53. * 获取用户列表(返回精简的员工信息列表)
  54. */
  55. public static final String GET_USER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=%s&department_id=%s&fetch_child=%s";
  56. /**
  57. * 获取用户列表(返回详细的员工信息列表)
  58. */
  59. public static final String GET_USER_DETAIL_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=%s&department_id=%s&fetch_child=%s";
  60. /**
  61. * 获取单个成员信息
  62. */
  63. public static final String GET_SINGLE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s";
  64. /**
  65. * 根据手机号获取用户id
  66. */
  67. public static final String POST_MOBILE_USERID = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=%s";
  68. /**
  69. * 自建获取单个成员信息
  70. */
  71. public static final String GET_LINKEDCORP_USER = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/get?access_token=%s";
  72. //-------------------------------------上传---------------------------------------------------
  73. /**
  74. * 上传素材
  75. */
  76. public static final String MEDIA_UPLOAD = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";
  77. //-------------------------------------消息--------------------------------------------------
  78. /**
  79. * 发送消息
  80. */
  81. public static final String SEND_MESSAGE = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";
  82. /**
  83. * 获取接口访问凭证
  84. */
  85. public static JSONObject getAccessToken(String corpId, String corpSecret) {
  86. JSONObject rstObj = HttpUtil.httpRequest(String.format(TOKEN,corpId, corpSecret), "GET", null);
  87. rstObj.put("code",true);
  88. if (HttpUtil.isWxError(rstObj)) {
  89. rstObj.put("code",false);
  90. rstObj.put("access_token","");
  91. }
  92. return rstObj;
  93. }
  94. /**
  95. * 发送消息 20210416 Add By GongXishan
  96. * 不抛出异常,返回Json
  97. */
  98. public static JSONObject sendMessage(String message, String accessToken){
  99. JSONObject retMsg = new JSONObject();
  100. boolean codeFlag = true;
  101. String errorMsg = "";
  102. JSONObject rstObj = HttpUtil.httpRequest(String.format(SEND_MESSAGE, accessToken), "POST", message);
  103. if (HttpUtil.isWxError(rstObj)) {
  104. codeFlag = false;
  105. errorMsg = rstObj.toString();
  106. }
  107. retMsg.put("code",codeFlag);
  108. retMsg.put("error",errorMsg);
  109. return retMsg;
  110. }
  111. /**
  112. * 向企业微信发送信息
  113. * @param corpId
  114. * @param corpSecret
  115. * @param agentId
  116. * @param toUserId
  117. * @param contents
  118. * @return
  119. */
  120. public static JSONObject sendWxMessage(String corpId, String corpSecret, String agentId, String toUserId, String contents) {
  121. JSONObject retMsg = null;
  122. JSONObject message = null;
  123. JSONObject tokenObject = null;
  124. JSONObject content = null;
  125. message = new JSONObject();
  126. message.put("touser", toUserId);
  127. message.put("agentid", agentId);
  128. content = new JSONObject();
  129. content.put("content", contents);
  130. message.put("text", content);
  131. message.put("msgtype", "text");
  132. tokenObject = getAccessToken(corpId, corpSecret);
  133. if(tokenObject.getString("access_token")!=null && !"".equals(tokenObject.getString("access_token"))){
  134. retMsg = sendMessage(message.toJSONString(), tokenObject.getString("access_token"));
  135. }else
  136. {
  137. retMsg.put("code",false);
  138. retMsg.put("error","access_token值为空,不能发送信息!");
  139. }
  140. return retMsg;
  141. }
  142. //------------------------------------企业微信接口:部门管理的增删改查-------------------------------------
  143. /**
  144. * 创建部门
  145. * @param department json数据格式
  146. * @param accessToken
  147. * @return
  148. * @throws WxErrorException
  149. */
  150. public static JSONObject createDepartment(String department, String accessToken) throws WxErrorException {
  151. JSONObject retMsg = new JSONObject();
  152. boolean codeFlag = true;
  153. String errorMsg = "";
  154. String qyDeptId = "0";
  155. JSONObject rstObj = HttpUtil.httpRequest(String.format(CREATE_DEPARTMENT, accessToken), "POST", department);
  156. if (HttpUtil.isWxError(rstObj)) {
  157. codeFlag = false;
  158. errorMsg = rstObj.toString();
  159. }else{
  160. codeFlag = true;
  161. qyDeptId = rstObj.getInteger("id").toString();
  162. }
  163. retMsg.put("code",codeFlag);
  164. retMsg.put("error",errorMsg);
  165. retMsg.put("retDeptId",qyDeptId);
  166. return retMsg;
  167. }
  168. /**
  169. * 更新部门
  170. * @param department json数据格式
  171. * @param accessToken
  172. * @return
  173. * @throws WxErrorException
  174. */
  175. public static JSONObject updateDepartment(String department, String accessToken) throws WxErrorException {
  176. JSONObject retMsg = new JSONObject();
  177. boolean codeFlag = true;
  178. String errorMsg = "";
  179. JSONObject rstObj = HttpUtil.httpRequest(String.format(UPDATE_DEPARTMENT, accessToken), "POST", department);
  180. if (HttpUtil.isWxError(rstObj)) {
  181. codeFlag = false;
  182. errorMsg = rstObj.toString();
  183. }
  184. retMsg.put("code",codeFlag);
  185. retMsg.put("error",errorMsg);
  186. return retMsg;
  187. }
  188. /**
  189. * 删除部门
  190. * @param id 部门ID
  191. * @param accessToken
  192. * @return
  193. * @throws WxErrorException
  194. */
  195. public static JSONObject deleteDepartment(String id, String accessToken) throws WxErrorException {
  196. JSONObject retMsg = new JSONObject();
  197. boolean codeFlag = true;
  198. String errorMsg = "";
  199. JSONObject rstObj = HttpUtil.httpRequest(String.format(DELETE_DEPARTMENT, accessToken, id), "GET", null);
  200. if (HttpUtil.isWxError(rstObj)) {
  201. codeFlag = false;
  202. errorMsg = rstObj.toString();
  203. }
  204. retMsg.put("code",codeFlag);
  205. retMsg.put("error",errorMsg);
  206. return retMsg;
  207. }
  208. /**
  209. * 部门列表
  210. * @param id 根部门ID
  211. * @param accessToken
  212. * @return
  213. * @throws WxErrorException
  214. */
  215. public static JSONObject getDepartmentList(String id, String accessToken) throws WxErrorException {
  216. JSONObject retMsg = new JSONObject();
  217. boolean codeFlag = true;
  218. String errorMsg = "";
  219. String departmentStr = "";
  220. JSONObject rstObj = HttpUtil.httpRequest(String.format(GET_DEPARTMENT_LIST, accessToken, id), "GET", null);
  221. if (HttpUtil.isWxError(rstObj)) {
  222. codeFlag = false;
  223. errorMsg = rstObj.toString();
  224. }else{
  225. departmentStr = rstObj.getString("department");
  226. }
  227. retMsg.put("code",codeFlag);
  228. retMsg.put("error",errorMsg);
  229. retMsg.put("department",departmentStr);
  230. return retMsg;
  231. }
  232. //------------------------------------------企业微信接口:用户管理的增删改查--------------------------------------------
  233. /**
  234. * 创建用户
  235. * @param user json数据格式
  236. * @param accessToken
  237. * @return
  238. * @throws WxErrorException
  239. */
  240. public static JSONObject createUser(String user, String accessToken) throws WxErrorException {
  241. JSONObject retMsg = new JSONObject();
  242. boolean codeFlag = true;
  243. String errorMsg = "";
  244. JSONObject rstObj = HttpUtil.httpRequest(String.format(CREATE_USER, accessToken), "POST", user);
  245. if (HttpUtil.isWxError(rstObj)) {
  246. codeFlag = false;
  247. errorMsg = rstObj.toString();
  248. }
  249. retMsg.put("code",codeFlag);
  250. retMsg.put("error",errorMsg);
  251. return retMsg;
  252. }
  253. /**
  254. * 更新用户
  255. * @param user json数据格式
  256. * @param accessToken
  257. * @return
  258. * @throws WxErrorException
  259. */
  260. public static JSONObject updateUser(String user, String accessToken) throws WxErrorException {
  261. JSONObject retMsg = new JSONObject();
  262. boolean codeFlag = true;
  263. String errorMsg = "";
  264. JSONObject rstObj = HttpUtil.httpRequest(String.format(UPDATE_USER, accessToken), "POST", user);
  265. if (HttpUtil.isWxError(rstObj)) {
  266. codeFlag = false;
  267. errorMsg = rstObj.toString();
  268. }
  269. retMsg.put("code",codeFlag);
  270. retMsg.put("error",errorMsg);
  271. return retMsg;
  272. }
  273. /**
  274. * 删除用户
  275. * @param id 用户ID
  276. * @param accessToken
  277. * @return
  278. * @throws WxErrorException
  279. */
  280. public static JSONObject deleteUser(String id, String accessToken) throws WxErrorException {
  281. JSONObject retMsg = new JSONObject();
  282. boolean codeFlag = true;
  283. String errorMsg = "";
  284. JSONObject rstObj = HttpUtil.httpRequest(String.format(DELETE_USER, accessToken, id), "GET", null);
  285. if (HttpUtil.isWxError(rstObj)) {
  286. codeFlag = false;
  287. errorMsg = rstObj.toString();
  288. }
  289. retMsg.put("code",codeFlag);
  290. retMsg.put("error",errorMsg);
  291. return retMsg;
  292. }
  293. /**
  294. * 获取单个成员信息
  295. * @param id 企业微信成员ID
  296. * @param accessToken
  297. * @return
  298. * @throws WxErrorException
  299. */
  300. public static JSONObject getUserById(String id, String accessToken) throws WxErrorException {
  301. JSONObject retMsg = new JSONObject();
  302. boolean codeFlag = true;
  303. String errorMsg = "";
  304. String userInfo = "";
  305. JSONObject rstObj = HttpUtil.httpRequest(String.format(GET_SINGLE_USER, accessToken, id), "GET", null);
  306. if (HttpUtil.isWxError(rstObj)) {
  307. codeFlag = false;
  308. errorMsg = rstObj.toString();
  309. }else{
  310. userInfo = rstObj.toJSONString();
  311. }
  312. retMsg.put("code",codeFlag);
  313. retMsg.put("error",errorMsg);
  314. retMsg.put("userinfo",userInfo);
  315. return retMsg;
  316. }
  317. /**
  318. * 自建应用获取单个成员信息
  319. * @param id 企业微信成员ID
  320. * @param accessToken
  321. * @return
  322. * @throws WxErrorException
  323. */
  324. public static JSONObject getLinkedcorpUserById(String id, String accessToken) throws WxErrorException {
  325. JSONObject retMsg = new JSONObject();
  326. boolean codeFlag = true;
  327. String errorMsg = "";
  328. String userInfo = "";
  329. JSONObject userObject = new JSONObject();
  330. userObject.put("userid",id);
  331. JSONObject rstObj = HttpUtil.httpRequest(String.format(GET_LINKEDCORP_USER, accessToken), "POST", JsonUtil.getObjectToString(userObject));
  332. if (HttpUtil.isWxError(rstObj)) {
  333. codeFlag = false;
  334. errorMsg = rstObj.toString();
  335. }else{
  336. userInfo = rstObj.getJSONObject("user_info").toJSONString();
  337. }
  338. retMsg.put("code",codeFlag);
  339. retMsg.put("error",errorMsg);
  340. retMsg.put("userinfo",userInfo);
  341. return retMsg;
  342. }
  343. /**
  344. * 获取用户列表(返回精简的成员信息)
  345. * @param id
  346. * @param isGetChild 1-递归获取,0-只获取本部门
  347. * @param accessToken
  348. * GET_USER_LIST
  349. * @return
  350. * @throws WxErrorException
  351. */
  352. public static JSONObject getUserList(String id, String isGetChild, String accessToken) throws WxErrorException {
  353. JSONObject retMsg = new JSONObject();
  354. boolean codeFlag = true;
  355. String errorMsg = "";
  356. List<Map<String,Object>> userList = new ArrayList<>();
  357. JSONObject rstObj = HttpUtil.httpRequest(String.format(GET_USER_DETAIL_LIST, accessToken, id,isGetChild), "GET", null);
  358. if (HttpUtil.isWxError(rstObj)) {
  359. codeFlag = false;
  360. errorMsg = rstObj.toString();
  361. }else{
  362. userList = JsonUtil.getJsonToList(rstObj.getJSONArray("userlist"));
  363. }
  364. retMsg.put("code",codeFlag);
  365. retMsg.put("error",errorMsg);
  366. retMsg.put("userlist",userList);
  367. return retMsg;
  368. }
  369. /**
  370. * 获取用户列表(返回详细的成员信息)
  371. * @param id
  372. * @param isGetChild 1-递归获取,0-只获取本部门
  373. * @param accessToken
  374. * @return
  375. * @throws WxErrorException
  376. */
  377. public static JSONObject getUserDetailList(String id, String isGetChild, String accessToken) throws WxErrorException {
  378. JSONObject retMsg = new JSONObject();
  379. boolean codeFlag = true;
  380. String errorMsg = "";
  381. String userList = "";
  382. JSONObject rstObj = HttpUtil.httpRequest(String.format(GET_SINGLE_USER, accessToken, id,isGetChild), "GET", null);
  383. if (HttpUtil.isWxError(rstObj)) {
  384. codeFlag = false;
  385. errorMsg = rstObj.toString();
  386. }else{
  387. userList = rstObj.getString("userlist");
  388. }
  389. retMsg.put("code",codeFlag);
  390. retMsg.put("error",errorMsg);
  391. retMsg.put("userlist",userList);
  392. return retMsg;
  393. }
  394. /**
  395. * 按目录树结构数据转化为列表
  396. * @param selectorVO
  397. * @param organizeList
  398. * @param listByOrder
  399. */
  400. public static <T> void getOrganizeTreeToList(OrganizeListVO selectorVO, Map<String, T> organizeList, List<T> listByOrder){
  401. if(selectorVO.isHasChildren()) {
  402. List<OrganizeListVO> voChildren = selectorVO.getChildren();
  403. Iterator<OrganizeListVO> iterator = voChildren.iterator();
  404. while (iterator.hasNext()) {
  405. OrganizeListVO organizeSelectorVO = iterator.next();
  406. T entity = organizeList.get(organizeSelectorVO.getId());
  407. listByOrder.add(entity);
  408. if (organizeSelectorVO.isHasChildren()) {
  409. getOrganizeTreeToList(organizeSelectorVO, organizeList, listByOrder);
  410. }
  411. }
  412. }
  413. }
  414. public static JSONObject getUserIdByMobile(String mobile, String token) {
  415. JSONObject retMsg = new JSONObject();
  416. boolean codeFlag = true;
  417. String errorMsg = "";
  418. String userId = "";
  419. JSONObject userObject = new JSONObject();
  420. userObject.put("mobile",mobile);
  421. JSONObject rstObj = HttpUtil.httpRequest(String.format(POST_MOBILE_USERID, token), "POST", JsonUtil.getObjectToString(userObject));
  422. if (HttpUtil.isWxError(rstObj)) {
  423. codeFlag = false;
  424. errorMsg = rstObj.toString();
  425. }else{
  426. userId = rstObj.getString("userid");
  427. }
  428. retMsg.put("code",codeFlag);
  429. retMsg.put("error",errorMsg);
  430. retMsg.put("userid",userId);
  431. return retMsg;
  432. }
  433. }