| @@ -64,6 +64,9 @@ public class WxUserGrantController extends BaseController { | |||||
| @Autowired | @Autowired | ||||
| WxCUserTagsService wxCUserTagsService; | WxCUserTagsService wxCUserTagsService; | ||||
| @Autowired | |||||
| WxAppinfoService wxAppinfoService; | |||||
| /** | /** | ||||
| * 用户登录 | * 用户登录 | ||||
| * @param map | * @param map | ||||
| @@ -96,6 +99,24 @@ public class WxUserGrantController extends BaseController { | |||||
| WxAppinfo wxAppinfo = getAppInfo(appId); | WxAppinfo wxAppinfo = getAppInfo(appId); | ||||
| WxMaService wxMaService = getWeappServiceByAppInfo(wxAppinfo); | 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 token = null; | ||||
| String session_key = null; | String session_key = null; | ||||
| String openId = null; | String openId = null; | ||||
| @@ -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()); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -4,6 +4,7 @@ import cn.binarywang.wx.miniapp.api.WxMaService; | |||||
| import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; | import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; | ||||
| import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; | import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; | ||||
| import com.iformall.domain.po.WxAppinfo; | import com.iformall.domain.po.WxAppinfo; | ||||
| import org.apache.commons.lang3.StringUtils; | |||||
| public class MaUtil { | public class MaUtil { | ||||
| @@ -14,7 +15,10 @@ public class MaUtil { | |||||
| config.setToken(appinfo.getToken()); | config.setToken(appinfo.getToken()); | ||||
| config.setAesKey(appinfo.getAesKey()); | config.setAesKey(appinfo.getAesKey()); | ||||
| config.setMsgDataFormat(appinfo.getMsgDataFormat()); | config.setMsgDataFormat(appinfo.getMsgDataFormat()); | ||||
| if (StringUtils.isBlank(appinfo.getAccessToken())) { | |||||
| config.setAccessToken(appinfo.getAccessToken()); | |||||
| config.setExpiresTime(appinfo.getLastTokenTime().getTime()+appinfo.getExpiresIn()); | |||||
| } | |||||
| WxMaService service = new WxMaServiceImpl(); | WxMaService service = new WxMaServiceImpl(); | ||||
| service.setWxMaConfig(config); | service.setWxMaConfig(config); | ||||
| return service; | return service; | ||||