完成BPS 企业微信获取Access Token
This commit is contained in:
parent
5280a7200c
commit
90093a1327
|
|
@ -0,0 +1,17 @@
|
|||
package com.ruoyi.bps.controller;
|
||||
|
||||
import com.ruoyi.bps.service.IWechatApiService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class WechatApiController {
|
||||
@Autowired
|
||||
IWechatApiService wechatApiService;
|
||||
|
||||
@RequestMapping("anon/getAccessToken")
|
||||
public String getAccessToken() {
|
||||
return wechatApiService.GetAccessToken();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
package com.ruoyi.bps.domain;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class WechatAccessToken {
|
||||
private Long sid;
|
||||
private String corpId;
|
||||
private String agentId;
|
||||
private String secret;
|
||||
private String errcode;
|
||||
private String errmsg;
|
||||
private String access_token; //access_token
|
||||
private String expires_in;
|
||||
private Date getTokenTime;
|
||||
private String aesKey;
|
||||
|
||||
public Long getSid() {
|
||||
return sid;
|
||||
}
|
||||
|
||||
public void setSid(Long sid) {
|
||||
this.sid = sid;
|
||||
}
|
||||
|
||||
public String getCorpId() {
|
||||
return corpId;
|
||||
}
|
||||
|
||||
public void setCorpId(String corpId) {
|
||||
this.corpId = corpId;
|
||||
}
|
||||
|
||||
public String getAgentId() {
|
||||
return agentId;
|
||||
}
|
||||
|
||||
public void setAgentId(String agentId) {
|
||||
this.agentId = agentId;
|
||||
}
|
||||
|
||||
public String getSecret() {
|
||||
return secret;
|
||||
}
|
||||
|
||||
public void setSecret(String secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
public String getErrcode() {
|
||||
return errcode;
|
||||
}
|
||||
|
||||
public void setErrcode(String errcode) {
|
||||
this.errcode = errcode;
|
||||
}
|
||||
|
||||
public String getErrmsg() {
|
||||
return errmsg;
|
||||
}
|
||||
|
||||
public void setErrmsg(String errmsg) {
|
||||
this.errmsg = errmsg;
|
||||
}
|
||||
|
||||
public String getAccess_token() {
|
||||
return access_token;
|
||||
}
|
||||
|
||||
public void setAccess_token(String access_token) {
|
||||
this.access_token = access_token;
|
||||
}
|
||||
|
||||
public String getExpires_in() {
|
||||
return expires_in;
|
||||
}
|
||||
|
||||
public void setExpires_in(String expires_in) {
|
||||
this.expires_in = expires_in;
|
||||
}
|
||||
|
||||
public Date getGetTokenTime() {
|
||||
return getTokenTime;
|
||||
}
|
||||
|
||||
public void setGetTokenTime(Date getTokenTime) {
|
||||
this.getTokenTime = getTokenTime;
|
||||
}
|
||||
|
||||
public String getAesKey() {
|
||||
return aesKey;
|
||||
}
|
||||
|
||||
public void setAesKey(String aesKey) {
|
||||
this.aesKey = aesKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WechatAccessToken{" +
|
||||
"sid='" + sid + '\'' +
|
||||
", corpId='" + corpId + '\'' +
|
||||
", agentId='" + agentId + '\'' +
|
||||
", secrect='" + secret + '\'' +
|
||||
", errcode='" + errcode + '\'' +
|
||||
", errmsg='" + errmsg + '\'' +
|
||||
", access_token='" + access_token + '\'' +
|
||||
", expires_in='" + expires_in + '\'' +
|
||||
", getTokenTime=" + getTokenTime +
|
||||
", aesKey='" + aesKey + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package com.ruoyi.bps.mapper;
|
||||
|
||||
import com.ruoyi.bps.domain.WechatAccessToken;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
/**
|
||||
* 企业微信获取Access Token 的Mapper接口
|
||||
*
|
||||
* @author Bo
|
||||
* @date 2021-07-21
|
||||
*/
|
||||
public interface WechatAccessTokenMapper
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 根据SID查询Access Token
|
||||
*
|
||||
* @param sid
|
||||
* @return 企业微信Access Token信息
|
||||
*/
|
||||
public WechatAccessToken selectWechatAccessTokenById(Long sid);
|
||||
|
||||
|
||||
/**
|
||||
* 查询Access Token
|
||||
* @param wechatAccessToken
|
||||
* @return 企业微信Access Token信息
|
||||
*/
|
||||
public List<WechatAccessToken> selectWechatAccessTokenList(WechatAccessToken wechatAccessToken);
|
||||
|
||||
/**
|
||||
* 新增Access Token
|
||||
*
|
||||
* @param wechatAccessToken Access Token
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertWechatAccessToken(WechatAccessToken wechatAccessToken);
|
||||
|
||||
/**
|
||||
* 修改Access Token
|
||||
*
|
||||
* @param wechatAccessToken Access Token
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateWechatAccessToken(WechatAccessToken wechatAccessToken);
|
||||
|
||||
/**
|
||||
* 删除Access Token
|
||||
*
|
||||
* @param sid Access TokenID
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteWechatAccessTokenById(Long sid);
|
||||
|
||||
/**
|
||||
* 批量删除Access Token
|
||||
*
|
||||
* @param sids 需要删除的数据ID
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteWechatAccessTokenByIds(String[] sids);
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package com.ruoyi.bps.service;
|
||||
|
||||
public interface IWechatApiService {
|
||||
//获取Access Token
|
||||
public String GetAccessToken();
|
||||
}
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
package com.ruoyi.bps.service.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.ruoyi.bps.domain.WechatAccessToken;
|
||||
import com.ruoyi.bps.mapper.WechatAccessTokenMapper;
|
||||
import com.ruoyi.bps.service.IWechatApiService;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.http.HttpUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class WechatApiServiceImpl implements IWechatApiService {
|
||||
@Value("${wechat.corpId}")
|
||||
private String corpId;
|
||||
@Value("${wechat.secret}")
|
||||
private String secret;
|
||||
@Value("${wechat.agentId}")
|
||||
private String agentId;
|
||||
|
||||
@Autowired
|
||||
WechatAccessTokenMapper wechatAccessTokenMapper;
|
||||
|
||||
/**
|
||||
* 获取企业微信Access Token
|
||||
* @return Access Token
|
||||
*/
|
||||
@Override
|
||||
public String GetAccessToken() {
|
||||
//获取本地数据库中的Token
|
||||
WechatAccessToken wat = new WechatAccessToken();
|
||||
wat.setCorpId(corpId);
|
||||
wat.setSecret(secret);
|
||||
List<WechatAccessToken> list= wechatAccessTokenMapper.selectWechatAccessTokenList(wat);
|
||||
WechatAccessToken returnWat;
|
||||
//如果数据库中没有对应corpId+corpSecret的数据,或者数据多于一条,则从企业微信获取Token,并插入数据库
|
||||
if(list.isEmpty() || list.size() <=0)
|
||||
{
|
||||
returnWat= getAccessTokenFromWechat(corpId,secret,agentId);
|
||||
wechatAccessTokenMapper.insertWechatAccessToken(returnWat);
|
||||
return returnWat.getAccess_token();
|
||||
}
|
||||
|
||||
//如果数据库中存在多条corpId+corpSecret的数据,则全部删除再从企业微信获取Token,并插入数据库
|
||||
if(list.size()>1 || StringUtils.isEmpty(list.get(0).getAccess_token()))
|
||||
{
|
||||
for(WechatAccessToken token:list){
|
||||
wechatAccessTokenMapper.deleteWechatAccessTokenById(token.getSid());
|
||||
}
|
||||
returnWat= getAccessTokenFromWechat(corpId,secret,agentId);
|
||||
wechatAccessTokenMapper.insertWechatAccessToken(returnWat);
|
||||
return returnWat.getAccess_token();
|
||||
}
|
||||
|
||||
//如果获取到的数据中,Token或更新时间为空,或者离过期时间小于1000秒,则从企业微信获取Token,并更新数据库
|
||||
int a= differenceSecond(DateUtils.getNowDate(),list.get(0).getGetTokenTime())-Integer.parseInt(list.get(0).getExpires_in())-1000;
|
||||
if(StringUtils.isEmpty(list.get(0).getAccess_token()) || list.get(0).getGetTokenTime() ==null
|
||||
|| differenceSecond(DateUtils.getNowDate(),list.get(0).getGetTokenTime())>Integer.parseInt(list.get(0).getExpires_in())-1000){
|
||||
returnWat= getAccessTokenFromWechat(corpId,secret,agentId);
|
||||
returnWat.setSid(list.get(0).getSid());
|
||||
wechatAccessTokenMapper.updateWechatAccessToken(returnWat);
|
||||
return returnWat.getAccess_token();
|
||||
}
|
||||
|
||||
//如果以上情况皆不是,则返回本地数据库的token
|
||||
return list.get(0).getAccess_token();
|
||||
|
||||
}
|
||||
|
||||
//根据corpId与corpSecret获取Token
|
||||
private WechatAccessToken getAccessTokenFromWechat(String corpId,String secret, String agentId){
|
||||
String param ="corpid=" + corpId + "&corpsecret=" + secret;
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
|
||||
WechatAccessToken wechatAccessToken= new Gson().fromJson(HttpUtils.sendGet(url,param),WechatAccessToken.class);
|
||||
wechatAccessToken.setCorpId(corpId);
|
||||
wechatAccessToken.setSecret(secret);
|
||||
wechatAccessToken.setAgentId(agentId);
|
||||
wechatAccessToken.setGetTokenTime(DateUtils.getNowDate());
|
||||
return wechatAccessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param minuendDate 被减数日期
|
||||
* @param subtractionDate 减数日期
|
||||
* @return 相差秒数
|
||||
*/
|
||||
public int differenceSecond(Date minuendDate, Date subtractionDate ) {
|
||||
return (int)((minuendDate.getTime() - subtractionDate.getTime()) / 1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ruoyi.bps.mapper.WechatAccessTokenMapper">
|
||||
|
||||
<resultMap type="WechatAccessToken" id="WechatAccessTokenResult">
|
||||
<result property="sid" column="sid" />
|
||||
<result property="corpId" column="corp_id" />
|
||||
<result property="agentId" column="agent_id" />
|
||||
<result property="secret" column="secret" />
|
||||
<result property="errcode" column="errcode" />
|
||||
<result property="errmsg" column="errmsg" />
|
||||
<result property="access_token" column="access_token" />
|
||||
<result property="expires_in" column="expires_in" />
|
||||
<result property="getTokenTime" column="get_token_time" />
|
||||
<result property="aesKey" column="aes_key" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectWechatAccessTokenVo">
|
||||
select sid, corp_id, agent_id, secret, errcode, errmsg, access_token, expires_in, get_token_time, aes_key from wechat_access_token
|
||||
</sql>
|
||||
|
||||
<select id="selectWechatAccessTokenList" parameterType="WechatAccessToken" resultMap="WechatAccessTokenResult">
|
||||
<include refid="selectWechatAccessTokenVo"/>
|
||||
<where>
|
||||
<if test="corpId != null and corpId != ''"> and corp_id = #{corpId}</if>
|
||||
<if test="agentId != null and agentId != ''"> and agent_id = #{agentId}</if>
|
||||
<if test="secret != null and secret != ''"> and secret = #{secret}</if>
|
||||
<if test="access_token != null and access_token != ''"> and access_token = #{access_token}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectWechatAccessTokenById" parameterType="Long" resultMap="WechatAccessTokenResult">
|
||||
<include refid="selectWechatAccessTokenVo"/>
|
||||
where sid = #{sid}
|
||||
</select>
|
||||
|
||||
<insert id="insertWechatAccessToken" parameterType="WechatAccessToken" useGeneratedKeys="true" keyProperty="sid">
|
||||
insert into wechat_access_token
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="corpId != null">corp_id,</if>
|
||||
<if test="agentId != null">agent_id,</if>
|
||||
<if test="secret != null">secret,</if>
|
||||
<if test="errcode != null">errcode,</if>
|
||||
<if test="errmsg != null">errmsg,</if>
|
||||
<if test="access_token != null">access_token,</if>
|
||||
<if test="expires_in != null">expires_in,</if>
|
||||
<if test="getTokenTime != null">getToken_time,</if>
|
||||
<if test="aesKey != null">aes_key,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="corpId != null">#{corpId},</if>
|
||||
<if test="agentId != null">#{agentId},</if>
|
||||
<if test="secret != null">#{secret},</if>
|
||||
<if test="errcode != null">#{errcode},</if>
|
||||
<if test="errmsg != null">#{errmsg},</if>
|
||||
<if test="access_token != null">#{access_token},</if>
|
||||
<if test="expires_in != null">#{expires_in},</if>
|
||||
<if test="getTokenTime != null">#{getTokenTime},</if>
|
||||
<if test="aesKey != null">#{aesKey},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="updateWechatAccessToken" parameterType="WechatAccessToken">
|
||||
update wechat_access_token
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="corpId != null">corp_id = #{corpId},</if>
|
||||
<if test="agentId != null">agent_id = #{agentId},</if>
|
||||
<if test="secret != null">secret = #{secret},</if>
|
||||
<if test="errcode != null">errcode = #{errcode},</if>
|
||||
<if test="errmsg != null">errmsg = #{errmsg},</if>
|
||||
<if test="access_token != null">access_token = #{access_token},</if>
|
||||
<if test="expires_in != null">expires_in = #{expires_in},</if>
|
||||
<if test="getTokenTime != null">get_token_time = #{getTokenTime},</if>
|
||||
<if test="aesKey != null">aes_key = #{aesKey},</if>
|
||||
</trim>
|
||||
where sid = #{sid}
|
||||
</update>
|
||||
|
||||
<delete id="deleteWechatAccessTokenById" parameterType="Long">
|
||||
delete from wechat_access_token where sid = #{sid}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteWechatAccessTokenByIds" parameterType="String">
|
||||
delete from wechat_access_token where sid in
|
||||
<foreach item="sid" collection="array" open="(" separator="," close=")">
|
||||
#{sid}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
||||
</mapper>
|
||||
|
|
@ -141,3 +141,11 @@ xss:
|
|||
swagger:
|
||||
# 是否开启swagger
|
||||
enabled: true
|
||||
|
||||
#企业微信
|
||||
wechat:
|
||||
corpId: ww4ed3771457e5f463
|
||||
agentId: 1000080
|
||||
secret: drtHKYabI9_EgjJQ8aqDPTQkY1WUYeWUTMkYw7D_z64
|
||||
token: 111
|
||||
aesKey: 111
|
||||
Loading…
Reference in New Issue