| @@ -110,35 +110,14 @@ public class WxUserGrantController extends BaseController { | |||||
| if(StringUtils.isBlank(wxAppinfo.getAccessToken())) { | if(StringUtils.isBlank(wxAppinfo.getAccessToken())) { | ||||
| // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 | // 如果没有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()); | |||||
| } | |||||
| updateAppAccessToken(wxAppinfo, wxMaService); | |||||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | wxMaService = getWeappServiceByAppInfo(wxAppinfo); | ||||
| } else { | } else { | ||||
| // 检查token是否已过期, 小时就重新获取 | // 检查token是否已过期, 小时就重新获取 | ||||
| Date curDate = new Date(); | Date curDate = new Date(); | ||||
| if(curDate.getTime() > wxAppinfo.getLastTokenTime().getTime() + 3600000) { | if(curDate.getTime() > wxAppinfo.getLastTokenTime().getTime() + 3600000) { | ||||
| 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()); | |||||
| } | |||||
| updateAppAccessToken(wxAppinfo, wxMaService); | |||||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | wxMaService = getWeappServiceByAppInfo(wxAppinfo); | ||||
| } | } | ||||
| } | } | ||||
| @@ -244,6 +223,20 @@ public class WxUserGrantController extends BaseController { | |||||
| return new ResultData(resultMap); | return new ResultData(resultMap); | ||||
| } | } | ||||
| private void updateAppAccessToken(WxAppinfo wxAppinfo, WxMaService wxMaService) { | |||||
| 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()); | |||||
| } | |||||
| } | |||||
| /** | /** | ||||
| * 获取用户的昵称等信息 | * 获取用户的昵称等信息 | ||||
| * @param map | * @param map | ||||