Browse Source

[微信accessToken缓存][新增]:定时获取accessToken及用户登录获取accessToken并存储

release_toaliyun_real
Stormeye.Wu 7 years ago
parent
commit
58b7e58237
3 changed files with 83 additions and 1 deletions
  1. +21
    -0
      mallinkCApi/src/main/java/com/iformall/controller/WxUserGrantController.java
  2. +57
    -0
      mallinkSchedule/src/main/java/com/iformall/schedule/WeChatAccessTokenSchedule.java
  3. +5
    -1
      mallinkService/src/main/java/com/iformall/utils/MaUtil.java

+ 21
- 0
mallinkCApi/src/main/java/com/iformall/controller/WxUserGrantController.java View File

@@ -64,6 +64,9 @@ public class WxUserGrantController extends BaseController {
@Autowired
WxCUserTagsService wxCUserTagsService;

@Autowired
WxAppinfoService wxAppinfoService;

/**
* 用户登录
* @param map
@@ -96,6 +99,24 @@ public class WxUserGrantController extends BaseController {
WxAppinfo wxAppinfo = getAppInfo(appId);
WxMaService wxMaService = getWeappServiceByAppInfo(wxAppinfo);

if(StringUtils.isBlank(wxAppinfo.getAccessToken())) {
// 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用
try {
String accessToken = wxMaService.getAccessToken(true);
wxAppinfo.setAccessToken(accessToken);
wxAppinfo.setLastTokenTime(new Date());
wxAppinfo.setExpiresIn(7200);
wxAppinfoService.saveOrUpdate(wxAppinfo);
} catch (WxErrorException e) {
logger.error(e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
}

wxMaService = getWeappServiceByAppInfo(wxAppinfo);

}

String token = null;
String session_key = null;
String openId = null;


+ 57
- 0
mallinkSchedule/src/main/java/com/iformall/schedule/WeChatAccessTokenSchedule.java View File

@@ -0,0 +1,57 @@
package com.iformall.schedule;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.mapper.WxAppinfoMapper;
import com.iformall.utils.MaUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class WeChatAccessTokenSchedule {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
WxAppinfoMapper wxAppinfoMapper;


@Scheduled(cron = "0 1 * * * ?") // 每小时第一分钟执行
public void getAccessTokens() {

logger.info("微信访问AccessToken获取定时任务启动");

WxAppinfo appinfoQ = new WxAppinfo();
List<WxAppinfo> appList = wxAppinfoMapper.findList(appinfoQ);

for (WxAppinfo appinfo : appList) {
resetAccessToken(appinfo);
}

logger.info("微信访问AccessToken获取定时任务结束");
}

public void resetAccessToken(WxAppinfo appinfo) {
WxMaService wxMaService = MaUtil.getWeappService(appinfo);
try {
String accessToken = wxMaService.getAccessToken(true);
appinfo.setAccessToken(accessToken);
appinfo.setLastTokenTime(new Date());
appinfo.setExpiresIn(7200);
wxAppinfoMapper.updateByPrimaryKey(appinfo);
} catch (WxErrorException e) {
logger.error(e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
}

}

}

+ 5
- 1
mallinkService/src/main/java/com/iformall/utils/MaUtil.java View File

@@ -4,6 +4,7 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.iformall.domain.po.WxAppinfo;
import org.apache.commons.lang3.StringUtils;

public class MaUtil {

@@ -14,7 +15,10 @@ public class MaUtil {
config.setToken(appinfo.getToken());
config.setAesKey(appinfo.getAesKey());
config.setMsgDataFormat(appinfo.getMsgDataFormat());

if (StringUtils.isBlank(appinfo.getAccessToken())) {
config.setAccessToken(appinfo.getAccessToken());
config.setExpiresTime(appinfo.getLastTokenTime().getTime()+appinfo.getExpiresIn());
}
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(config);
return service;


Loading…
Cancel
Save