|
@@ -1,12 +1,16 @@
|
|
|
package com.xjrsoft.common.utils;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.xjrsoft.common.enums.WeChatType;
|
|
|
import com.xjrsoft.common.exception.MyException;
|
|
|
import com.xjrsoft.module.organization.dto.WeChatDepartDto;
|
|
|
import com.xjrsoft.module.organization.dto.WeChatUserDto;
|
|
|
+import com.xjrsoft.module.system.vo.WeChatUserInfo;
|
|
|
import lombok.Data;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -42,7 +46,7 @@ public class WeChatUtil {
|
|
|
public String appletAppSecret;
|
|
|
|
|
|
//公众号获取Openid
|
|
|
- public String[] getMpOpenid(String code) {
|
|
|
+ public WeChatUserInfo getMpOpenid(String code) {
|
|
|
HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
paramMap.put("appid", mpAppKey);
|
|
|
paramMap.put("secret", mpAppSecret);
|
|
@@ -54,17 +58,11 @@ public class WeChatUtil {
|
|
|
//throw new MyException(result);
|
|
|
return null;
|
|
|
}
|
|
|
- String openId = jsonObject.get("openid").toString();
|
|
|
- String unionid = "";
|
|
|
- if (jsonObject.containsKey("unionid")) {
|
|
|
- unionid = jsonObject.get("unionid").toString();
|
|
|
- }
|
|
|
- String[] arrayRefVar = {openId, unionid};
|
|
|
- return arrayRefVar;
|
|
|
+ return jsonObject.toJavaObject(WeChatUserInfo.class);
|
|
|
}
|
|
|
|
|
|
// 小程序
|
|
|
- public String[] getOpenid(String code) {
|
|
|
+ public WeChatUserInfo getOpenid(String code) {
|
|
|
HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
paramMap.put("appid", appletAppKey);
|
|
|
paramMap.put("secret", appletAppSecret);
|
|
@@ -76,37 +74,44 @@ public class WeChatUtil {
|
|
|
//throw new MyException(result);
|
|
|
return null;
|
|
|
}
|
|
|
- String openId = jsonObject.get("openid").toString();
|
|
|
- String unionid = "";
|
|
|
- if (jsonObject.containsKey("unionid")) {
|
|
|
- unionid = jsonObject.get("unionid").toString();
|
|
|
- }
|
|
|
- String[] arrayRefVar = {openId, unionid};
|
|
|
- return arrayRefVar;
|
|
|
+ return jsonObject.toJavaObject(WeChatUserInfo.class);
|
|
|
}
|
|
|
|
|
|
//获取通讯录或发消息token
|
|
|
- public String getToken(Integer type) {
|
|
|
+ public String getToken(WeChatType type) {
|
|
|
HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
- if (type == 1) {
|
|
|
+ String key = "";
|
|
|
+ if (WeChatType.WEMINI == type) {
|
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
|
|
|
paramMap.put("corpsecret", appSecret);
|
|
|
paramMap.put("corpid", appKey);
|
|
|
- } else if (type == 2) {
|
|
|
+ key = String.format("access_token_%s",appKey);
|
|
|
+ } else if (WeChatType.WEWEB == type) {
|
|
|
paramMap.put("grant_type", "client_credential");
|
|
|
paramMap.put("appid", mpAppKey);
|
|
|
paramMap.put("secret", mpAppSecret);
|
|
|
+ key = String.format("access_token_%s",mpAppKey);
|
|
|
+ }
|
|
|
+ // 判断redis中token是否过期
|
|
|
+ RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
|
|
|
+ String accessToken = redisUtil.get(key);
|
|
|
+ if (StrUtil.isNotBlank(accessToken) && !accessToken.equals("null")) {
|
|
|
+ return accessToken;
|
|
|
}
|
|
|
+
|
|
|
String result = HttpUtil.get(url, paramMap);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
String token = jsonObject.get("access_token").toString();
|
|
|
+ // 缓存1小时
|
|
|
+ redisUtil.set(key,token,3600);
|
|
|
+
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
//根据所有部门信息
|
|
|
public List<WeChatDepartDto> getDepartmentList(Long dept_id) {
|
|
|
- String token = this.getToken(1);
|
|
|
+ String token = this.getToken(WeChatType.WEMINI);
|
|
|
HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
paramMap.put("access_token", token);
|
|
|
if (dept_id != null) {
|
|
@@ -121,7 +126,7 @@ public class WeChatUtil {
|
|
|
|
|
|
//获取部门的所有用户信息
|
|
|
public List<WeChatUserDto> getDepartmentUser(Long dept_id) {
|
|
|
- String token = this.getToken(1);
|
|
|
+ String token = this.getToken(WeChatType.WEMINI);
|
|
|
HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
paramMap.put("access_token", token);
|
|
|
paramMap.put("department_id", dept_id);
|
|
@@ -135,7 +140,7 @@ public class WeChatUtil {
|
|
|
|
|
|
//公众号发送文本消息给用户
|
|
|
public Boolean sendMessage(String content, String userId) {
|
|
|
- String token = this.getToken(2);
|
|
|
+ String token = this.getToken(WeChatType.WEWEB);
|
|
|
JSONObject object = new JSONObject();
|
|
|
JSONArray touser=new JSONArray();
|
|
|
touser.add(userId);
|
|
@@ -171,7 +176,7 @@ public class WeChatUtil {
|
|
|
// }
|
|
|
// }
|
|
|
public Boolean sendTemplateMessage(JSONObject object){
|
|
|
- String token = this.getToken(2);
|
|
|
+ String token = this.getToken(WeChatType.WEWEB);
|
|
|
String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token, JSONObject.toJSONString(object));
|
|
|
System.out.printf(result);
|
|
|
return true;
|