获取企业微信Token由存放到数据库,改为存放到EHCache缓存。

This commit is contained in:
bo.yang 2021-08-01 00:29:14 +08:00
parent 3f46f3d6d2
commit 934b2854aa
3 changed files with 73 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import com.ruoyi.system.service.IWechatApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletException;
@ -24,13 +25,14 @@ public class WechatApiController extends BaseController {
@Autowired
IWechatApiService wechatApiService;
/* @RequestMapping("anon/getAccessToken")
@RequestMapping("anon/getAccessToken")
@ResponseBody
public String getAccessToken() {
return wechatApiService.GetAccessToken();
}
*/
@GetMapping("anon/userInfo")
public Map<String, Object> getJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
public Map<String, Object> getJSON(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
@ -38,8 +40,8 @@ public class WechatApiController extends BaseController {
while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
Map<String, Object> params = JSON.parseObject(responseStrBuilder.toString(), Map.class);
return params;
return JSON.parseObject(responseStrBuilder.toString(), Map.class);
}
@ -51,7 +53,7 @@ public class WechatApiController extends BaseController {
userIdList.add("erqrqwe");//错误userId示例
userIdList.add(""); //空UserId示例
userIdList.add("359");
if(!ShiroUtils.getUserId().equals("359")){
if(! String.valueOf(ShiroUtils.getUserId()).equals("359")){
userIdList.add(String.valueOf(ShiroUtils.getUserId()));
}
Map<String, String> resultMap = wechatApiService.SendTextMessageToWechatUser(userIdList,"<a href=\"www.baidu.com\">哈哈哈!</a>");
@ -68,7 +70,7 @@ public class WechatApiController extends BaseController {
userIdList.add("359");
//userIdList.add("454");
//userIdList.add("408");
if(!ShiroUtils.getUserId().equals("359")){
if(!String.valueOf(ShiroUtils.getUserId()).equals("359")){
userIdList.add(String.valueOf(ShiroUtils.getUserId()));
}
String title="号外:特大优惠!限时抢购";

View File

@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--360浏览器优先以webkit内核解析-->
<title>BPS后台管理系统介绍</title>
<title>BPS后台管理系统</title>
<link rel="shortcut icon" href="favicon.ico">
<link href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
<link href="../static/css/font-awesome.min.css" th:href="@{/css/font-awesome.min.css}" rel="stylesheet"/>
@ -33,7 +33,7 @@
</head>
<body class="gray-bg">
<div id="parent" class="col-sm-4">
<div id="parent" class="col-sm-6">
<div id="child">
<h3 class="text-primary"> <strong>[[${wordsContent}]]</strong></h3>
<h5 class="pull-right text-warning">--&nbsp;&nbsp;《[[${wordsOrigin}]]》 · [[${wordsAuthor}]]</h5>

View File

@ -2,23 +2,25 @@ package com.ruoyi.system.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.WechatAccessToken;
import com.ruoyi.system.domain.WechatSendMessage;
import com.ruoyi.system.domain.WechatUserInfo;
import com.ruoyi.system.mapper.WechatAccessTokenMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IWechatApiService;
import org.apache.shiro.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class WechatApiServiceImpl implements IWechatApiService {
@ -45,6 +47,31 @@ public class WechatApiServiceImpl implements IWechatApiService {
*/
@Override
public String GetAccessToken() {
String cacheName = "wechatToke:" + corpId + "," + agentId;
//获取缓存中accessToken与缓存的时间信息
Cache<String, Object> cache = CacheUtils.getCache(cacheName);
String accessToken = (String) cache.get("accessToken");
Date getTokenTime = ObjectToDate(cache.get("getTokenTime"));
Integer expires_in = ObjectToInteger(cache.get("expires_in"));
//如果没有获取到cache或者accessToken为空或者cache即将过期则重新获取并返回新的accessToken;
if (StringUtils.isEmpty(accessToken) || null == getTokenTime ? true : (differenceSecond(DateUtils.getNowDate(), getTokenTime) + 1000 > expires_in ? true : false)) {
//清空wechatAccessTokenCache
CacheUtils.removeAll(cacheName);
//从企业微信获取新的accessToken
WechatAccessToken wechatAccessToken = getAccessTokenFromWechat(corpId, secret, agentId);
//将Token写入缓存
CacheUtils.put(cacheName, "accessToken", wechatAccessToken.getAccess_token());
CacheUtils.put(cacheName, "expires_in", wechatAccessToken.getExpires_in());
CacheUtils.put(cacheName, "getTokenTime", DateUtils.getNowDate());
//返回token
return wechatAccessToken.getAccess_token();
}
return accessToken;
}
/*
public String GetAccessToken() {
//获取本地数据库中的Token
WechatAccessToken wat = new WechatAccessToken();
wat.setCorpId(corpId);
@ -55,6 +82,7 @@ public class WechatApiServiceImpl implements IWechatApiService {
if(list.isEmpty() || list.size() <=0)
{
returnWat= getAccessTokenFromWechat(corpId,secret,agentId);
//将accessToken写入数据库
wechatAccessTokenMapper.insertWechatAccessToken(returnWat);
return returnWat.getAccess_token();
}
@ -83,6 +111,34 @@ public class WechatApiServiceImpl implements IWechatApiService {
//如果以上情况皆不是则返回本地数据库的token
return list.get(0).getAccess_token();
}*/
//将对象转化为日期如果无法转换则返回空值避免抛出异常
private Date ObjectToDate(Object obj){
try {
return (Date) obj;
}catch (Exception e){
return null;
}
}
//将对象转化为Integer,如果对象为空或无法转换返回0
private Integer ObjectToInteger(Object obj){
//如果为空返回零
if(null==obj){
return 0;
}
try{
return Integer.parseInt(obj.toString());
}catch (Exception e){
//发生异常返回0
return 0;
}
}
//获取相两个日期相差秒数
private int differenceSecond(Date minuendDate, Date subtractionDate ) {
return (int)((minuendDate.getTime() - subtractionDate.getTime()) / 1000);
}
//根据corpId与corpSecret获取Token
@ -97,12 +153,6 @@ public class WechatApiServiceImpl implements IWechatApiService {
return wechatAccessToken;
}
//获取相两个日期相差秒数
private int differenceSecond(Date minuendDate, Date subtractionDate ) {
return (int)((minuendDate.getTime() - subtractionDate.getTime()) / 1000);
}
/**
* 根据企业微信登录身份获取本地LoginName
*
@ -201,6 +251,8 @@ public class WechatApiServiceImpl implements IWechatApiService {
/**
* 推送text消息到企业微信用户
* 其中text参数的content字段可以支持换行以及A标签即可打开自定义的网页
* 示例"content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
* @param toUserList 发送的用户列表
* @param message 发送的消息内容
* @return 消息发送结果