今天花了一早上的时间实现了accesstoken的数据库存储,现在做个总结。
首先,access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。
因此数据库中只需要一条数据足矣,过期了再更新就是。
1.建立数据库t_accesstoken
字段:ID(自增)| accesstoken(varchar,255) | updatetime(date)
2.建立accesstoken类
package com.saibei.plat.model;
import java.util.Date;
public class AccessToken {
// 凭证有效时间,单位:秒
private int expiresin;
public int getExpiresin() {
return expiresin;
}
public void setExpiresin(int expiresin) {
this.expiresin = expiresin;
}
/**
* This field was generated by MyBatis Generator. This field corresponds to the
* database column t_accesstoken.ID
*
* @mbggenerated
*/
private Integer id;
/**
* This field was generated by MyBatis Generator. This field corresponds to the
* database column t_accesstoken.accesstoken
*
* @mbggenerated
*/
private String accesstoken;
/**
* This field was generated by MyBatis Generator. This field corresponds to the
* database column t_accesstoken.updatetime
*
* @mbggenerated
*/
private Date updatetime;
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column t_accesstoken.ID
*
* @return the value of t_accesstoken.ID
*
* @mbggenerated
*/
public Integer getId() {
return id;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column t_accesstoken.ID
*
* @param id
* the value for t_accesstoken.ID
*
* @mbggenerated
*/
public void setId(Integer id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column t_accesstoken.accesstoken
*
* @return the value of t_accesstoken.accesstoken
*
* @mbggenerated
*/
public String getAccesstoken() {
return accesstoken;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column t_accesstoken.accesstoken
*
* @param accesstoken
* the value for t_accesstoken.accesstoken
*
* @mbggenerated
*/
public void setAccesstoken(String accesstoken) {
this.accesstoken = accesstoken == null ? null : accesstoken.trim();
}
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column t_accesstoken.updatetime
*
* @return the value of t_accesstoken.updatetime
*
* @mbggenerated
*/
public Date getUpdatetime() {
return updatetime;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column t_accesstoken.updatetime
*
* @param updatetime
* the value for t_accesstoken.updatetime
*
* @mbggenerated
*/
public void setUpdatetime(Date updatetime) {
this.updatetime = updatetime;
}
}
3.在发送模板消息的工具类中实现accesstoken的存储
实现逻辑:
- 从数据库查出来access token信息,若没有,则重新获取access token
- 判断数据库中access token的更新时间和现在的时间差
- 若超过90分钟,就重新获取access token,并把新的access token数据更新至数据库中
- 若未超过,使用数据库中的access token
public static String sendWechatMsgToUser(String touser, String templatId, String clickurl, String topcolor,
JSONObject data) {
/**
* 做个查询,access token是否过期
*
*/
SqlSession sqlsession = null;
sqlsession = SqlSessionFactoryUtils.openSqlSession();
AccessTokenMapperDao accessTokenMapperDao = sqlsession.getMapper(AccessTokenMapperDao.class);
AccessToken access_token = accessTokenMapperDao.selectByPrimaryKey(1);
String accesstoken = "";
if (access_token != null) {
Date updatetime = access_token.getUpdatetime();
Date now = new Date();
long diff = now.getTime() - updatetime.getTime();// 这样得到的差值是微秒级别
long seconds = diff / (1000);
if (seconds >= 5400) {
AccessToken accessToken = new AccessToken();
accesstoken = WX_TokenUtil.getWXToken().getAccesstoken();
accessToken.setAccesstoken(accesstoken);
accessToken.setUpdatetime(now);
accessToken.setId(1);
int flag = accessTokenMapperDao.updateByPrimaryKeySelective(accessToken);
sqlsession.commit();
} else {
accesstoken = access_token.getAccesstoken();
}
} else {
accesstoken = WX_TokenUtil.getWXToken().getAccesstoken();
}
String tmpurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accesstoken;
JSONObject json = new JSONObject();
json.put("touser", touser);
json.put("template_id", templatId);
json.put("url", clickurl);
json.put("topcolor", topcolor);
json.put("data", data);
try {
JSONObject result = WX_HttpsUtil.httpsRequest(tmpurl, "POST", json.toString());
JSONObject resultJson = new JSONObject(result);
log.info("发送微信消息返回信息:" + resultJson.get("errcode"));
String errmsg = (String) resultJson.get("errmsg");
if (!"ok".equals(errmsg)) { // 如果为errmsg为ok,则代表发送成功,公众号推送信息给用户了。
return "error";
}
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "success";
}
大致过程就是这样,有什么疑惑可以联系我。
邮箱地址:[email protected]