企业微信获取token 存入redis 设置时长2小时 && 发送企业应用消息接口
1.常量类
package com.ruoyi.common.constant;
public class WeChatConstants {
public static final String BASE_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
public static final String DEPARTMENT_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
public static final String sendMessage_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
public static final String ACCESS_TOKEN_URL = BASE_URL + "?corpid=%s&corpsecret=%s";
public static final String ERRCODE = "errcode";
public static final String ACCESS_TOKEN = "access_token";
public static final String AGENT_SECRET = "agent_secret";
public static final String CORP_ID = "corp_id";
}
2.消息发送文本类
package com.ruoyi.system.domain.weChat;
import lombok.Data;
@Data
public class TextMessage extends BaseMessage{
private Text text;
private int safe;
}
3.企业微信发送基本参数类
package com.ruoyi.system.domain.weChat;
import lombok.Data;
@Data
public class BaseMessage {
private String touser;
private String toparty;
private String totag;
private String msgtype;
private int agentid;
}
4.文本类
package com.ruoyi.system.domain.weChat;
import lombok.Data;
@Data
public class Text {
private String content;
}
5.接受入参类
package com.ruoyi.system.domain.weChat.DTO;
import lombok.Data;
@Data
public class MessageDTO {
private String content;
private String userid;
private String toparty;
}
6.实现类
package com.ruoyi.system.service.impl;
import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.WeChatConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.weChat.DTO.MessageDTO;
import com.ruoyi.system.domain.weChat.Text;
import com.ruoyi.system.domain.weChat.TextMessage;
import com.ruoyi.system.service.IEnterpriseWeChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class EnterpriseWeChatServiceImpl implements IEnterpriseWeChatService {
@Autowired
private RedisCache redisCache;
@Override
public void weCharMessage(MessageDTO messageDTO) {
if (StringUtils.isNull(messageDTO.getUserid())&&StringUtils.isNull(messageDTO.getToparty())){
throw new ServiceException("发送人或发送部门最少选一个");
}
String url = WeChatConstants.sendMessage_url.replace("ACCESS_TOKEN", this.getAccessToken());
Gson gson = new Gson();
Text text = new Text();
TextMessage textMessage = new TextMessage();
textMessage.setTouser(messageDTO.getUserid());
textMessage.setToparty(messageDTO.getToparty());
textMessage.setAgentid(1000002);
textMessage.setMsgtype("text");
text.setContent(messageDTO.getContent());
textMessage.setText(text);
String jsonMessage = gson.toJson(textMessage);
Map response = (Map) JSON.parse(HttpUtils.sendPost(url,jsonMessage));
if (response.containsKey(WeChatConstants.ERRCODE) && (Integer) response.get(WeChatConstants.ERRCODE) != 0) {
throw new ServiceException("消息发送失败", (Integer) response.get(WeChatConstants.ERRCODE));
}
}
@Override
public String getAccessToken() {
String accessToken = redisCache.getCacheObject(getCacheKey(WeChatConstants.ACCESS_TOKEN));
if (StringUtils.isNotEmpty(accessToken) && this.isToken(accessToken)) {
return accessToken;
}
String corpId = redisCache.getCacheObject(getCacheKey(WeChatConstants.CORP_ID));
String agentSecret = redisCache.getCacheObject(getCacheKey(WeChatConstants.AGENT_SECRET));
if (StringUtils.isEmpty(corpId) || StringUtils.isEmpty(agentSecret)) {
throw new ServiceException("企业ID和应用secret不能为空");
}
String getAccessTokenUrl = String.format(WeChatConstants.ACCESS_TOKEN_URL, corpId, agentSecret);
Map response = (Map) JSON.parse(HttpUtils.sendGet(getAccessTokenUrl));
if (response.containsKey(WeChatConstants.ERRCODE) && (Integer) response.get(WeChatConstants.ERRCODE) != 0) {
throw new ServiceException("获取token失败", (Integer) response.get(WeChatConstants.ERRCODE));
}
accessToken = (String) response.get(WeChatConstants.ACCESS_TOKEN);
redisCache.setCacheObject(getCacheKey(WeChatConstants.ACCESS_TOKEN), accessToken, 2, TimeUnit.HOURS);
return accessToken;
}
private boolean isToken(String accessToken) {
Map department = (Map) JSON.parse(HttpUtils.sendGet(WeChatConstants.DEPARTMENT_URL, "access_token=" + accessToken));
if (department.containsKey(WeChatConstants.ERRCODE) && (Integer) department.get(WeChatConstants.ERRCODE) != 0) {
throw new ServiceException("token校验错误", (Integer) department.get(WeChatConstants.ERRCODE));
}
return true;
}
private String getCacheKey(String configKey) {
return CacheConstants.SYS_CONFIG_KEY + configKey;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)