WeChatUtil.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.xjrsoft.common.utils;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.extra.spring.SpringUtil;
  4. import cn.hutool.http.HttpUtil;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.xjrsoft.common.enums.WeChatType;
  8. import com.xjrsoft.common.exception.MyException;
  9. import com.xjrsoft.module.organization.dto.WeChatDepartDto;
  10. import com.xjrsoft.module.organization.dto.WeChatUserDto;
  11. import com.xjrsoft.module.system.vo.WeChatUserInfo;
  12. import lombok.Data;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Component;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. @Component
  18. @Data
  19. public class WeChatUtil {
  20. @Value("${xjrsoft.enterpriseWeChat.corpid}")
  21. public String appKey;
  22. @Value("${xjrsoft.enterpriseWeChat.secret}")
  23. public String appSecret;
  24. @Value("${xjrsoft.enterpriseWeChat.secret1}")
  25. public String appSecret1;
  26. @Value("${xjrsoft.enterpriseWeChat.agentid}")
  27. public Integer agentid;
  28. @Value("${xjrsoft.mpWeChat.appKey}")
  29. public String mpAppKey;
  30. @Value("${xjrsoft.mpWeChat.appSecret}")
  31. public String mpAppSecret;
  32. @Value("${xjrsoft.appletWeChat.appKey}")
  33. public String appletAppKey;
  34. @Value("${xjrsoft.appletWeChat.appSecret}")
  35. public String appletAppSecret;
  36. //公众号获取Openid
  37. public WeChatUserInfo getMpOpenid(String code) {
  38. HashMap<String, Object> paramMap = new HashMap<>();
  39. paramMap.put("appid", mpAppKey);
  40. paramMap.put("secret", mpAppSecret);
  41. paramMap.put("code", code);
  42. paramMap.put("grant_type", "authorization_code");
  43. String result = HttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token", paramMap);
  44. JSONObject jsonObject = JSONObject.parseObject(result);
  45. if (jsonObject.containsKey("errcode")) {
  46. //throw new MyException(result);
  47. return null;
  48. }
  49. return jsonObject.toJavaObject(WeChatUserInfo.class);
  50. }
  51. // 小程序
  52. public WeChatUserInfo getOpenid(String code) {
  53. HashMap<String, Object> paramMap = new HashMap<>();
  54. paramMap.put("appid", appletAppKey);
  55. paramMap.put("secret", appletAppSecret);
  56. paramMap.put("js_code", code);
  57. paramMap.put("grant_type", "authorization_code");
  58. String result = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", paramMap);
  59. JSONObject jsonObject = JSONObject.parseObject(result);
  60. if (jsonObject.containsKey("errcode")) {
  61. //throw new MyException(result);
  62. return null;
  63. }
  64. return jsonObject.toJavaObject(WeChatUserInfo.class);
  65. }
  66. //获取通讯录或发消息token
  67. public String getToken(WeChatType type) {
  68. HashMap<String, Object> paramMap = new HashMap<>();
  69. String url = "https://api.weixin.qq.com/cgi-bin/token";
  70. String key = "";
  71. if (WeChatType.WEMINI == type) {
  72. url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
  73. paramMap.put("corpsecret", appSecret);
  74. paramMap.put("corpid", appKey);
  75. key = String.format("access_token_%s",appKey);
  76. } else if (WeChatType.WEWEB == type) {
  77. paramMap.put("grant_type", "client_credential");
  78. paramMap.put("appid", mpAppKey);
  79. paramMap.put("secret", mpAppSecret);
  80. key = String.format("access_token_%s",mpAppKey);
  81. }
  82. // 判断redis中token是否过期
  83. RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
  84. String accessToken = redisUtil.get(key);
  85. if (StrUtil.isNotBlank(accessToken) && !accessToken.equals("null")) {
  86. return accessToken;
  87. }
  88. String result = HttpUtil.get(url, paramMap);
  89. JSONObject jsonObject = JSONObject.parseObject(result);
  90. String token = jsonObject.get("access_token").toString();
  91. // 缓存1小时
  92. redisUtil.set(key,token,3600);
  93. return token;
  94. }
  95. //根据所有部门信息
  96. public List<WeChatDepartDto> getDepartmentList(Long dept_id) {
  97. String token = this.getToken(WeChatType.WEMINI);
  98. HashMap<String, Object> paramMap = new HashMap<>();
  99. paramMap.put("access_token", token);
  100. if (dept_id != null) {
  101. paramMap.put("id", dept_id);
  102. }
  103. String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/department/list", paramMap);
  104. JSONObject jsonObject = JSONObject.parseObject(result);
  105. Object department = jsonObject.get("department");
  106. List<WeChatDepartDto> weChatDepartDtos = JSONObject.parseArray(department.toString(), WeChatDepartDto.class);
  107. return weChatDepartDtos;
  108. }
  109. //获取部门的所有用户信息
  110. public List<WeChatUserDto> getDepartmentUser(Long dept_id) {
  111. String token = this.getToken(WeChatType.WEMINI);
  112. HashMap<String, Object> paramMap = new HashMap<>();
  113. paramMap.put("access_token", token);
  114. paramMap.put("department_id", dept_id);
  115. paramMap.put("fetch_child", 1);
  116. String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/user/list", paramMap);
  117. JSONObject jsonObject = JSONObject.parseObject(result);
  118. Object department = jsonObject.get("userlist");
  119. List<WeChatUserDto> weChatUserDtos = JSONObject.parseArray(department.toString(), WeChatUserDto.class);
  120. return weChatUserDtos;
  121. }
  122. //公众号发送文本消息给用户
  123. public Boolean sendMessage(String content, String userId) {
  124. String token = this.getToken(WeChatType.WEWEB);
  125. JSONObject object = new JSONObject();
  126. JSONArray touser=new JSONArray();
  127. touser.add(userId);
  128. object.put("touser", touser);
  129. object.put("msgtype", "text");
  130. JSONObject text = new JSONObject();
  131. text.put("content", content);
  132. object.put("text",text);
  133. String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token="+token, JSONObject.toJSONString(object));
  134. System.out.printf(result);
  135. return true;
  136. }
  137. //{
  138. // "touser":"OPENID",
  139. // "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
  140. // "url":"http://weixin.qq.com/download",
  141. // "miniprogram":{
  142. // "appid":"xiaochengxuappid12345",
  143. // "pagepath":"index?foo=bar"
  144. // },
  145. // "client_msg_id":"MSG_000001",
  146. // "data":{
  147. // "keyword1":{
  148. // "value":"巧克力"
  149. // },
  150. // "keyword2": {
  151. // "value":"39.8元"
  152. // },
  153. // "keyword3": {
  154. // "value":"2014年9月22日"
  155. // }
  156. // }
  157. // }
  158. public Boolean sendTemplateMessage(JSONObject object){
  159. String token = this.getToken(WeChatType.WEWEB);
  160. String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token, JSONObject.toJSONString(object));
  161. System.out.printf(result);
  162. return true;
  163. }
  164. }