| | |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.panzhihua.common.controller.BaseController; |
| | | import com.panzhihua.common.model.vos.R; |
| | | import com.panzhihua.common.model.vos.user.SysAppConfigVO; |
| | | import com.panzhihua.common.redis.RedisUtils; |
| | | import com.panzhihua.common.service.user.UserService; |
| | | import org.apache.commons.fileupload.FileItem; |
| | | import org.apache.commons.fileupload.FileItemFactory; |
| | |
| | | private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; |
| | | private static String APP_IMAGE_URL = "https://www.psciio.com//idcard/8fa82cfba258498eab2fa818220fb592.jpg"; |
| | | private static String APP_IMAGE_NAME = "5.jpg"; |
| | | private static String WX_CALL_BACK = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token="; |
| | | //2小时 |
| | | private static final Long EXPIRE_TIME = 2L; |
| | | @Resource |
| | | private RedisTemplate redisTemplate; |
| | | @Resource |
| | | private StringRedisTemplate stringRedisTemplate; |
| | | UserService userService; |
| | | @Resource |
| | | private RedisUtils redisUtils; |
| | | |
| | | private static WxXCXTempSend wxXCXTempSend; |
| | | /** |
| | |
| | | */ |
| | | public String getAccessToken() throws Exception { |
| | | String accessToken = ""; |
| | | accessToken=wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+this.getAppId()).get(); |
| | | return accessToken; |
| | | } |
| | | public String getAccessToken(String appid) throws Exception { |
| | | String accessToken = ""; |
| | | accessToken=wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+appid).get(); |
| | | String appId = this.getAppId(); |
| | | accessToken=wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+ appId).get(); |
| | | if (appId.equals(APP_ID)){ |
| | | accessToken = validAccessToken(accessToken, appId,APP_SECRET); |
| | | } |
| | | |
| | | return accessToken; |
| | | } |
| | | |
| | | public String getAppAccessToken() throws Exception { |
| | | |
| | | public String getAccessToken(String appId) throws Exception { |
| | | String accessToken = ""; |
| | | accessToken=wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+appId).get(); |
| | | if (appId.equals(APP_ID)){ |
| | | accessToken = validAccessToken(accessToken, appId,APP_SECRET); |
| | | } |
| | | return accessToken; |
| | | } |
| | | |
| | | private String validAccessToken(String accessToken, String appId,String appSecret) throws Exception { |
| | | if (StrUtil.isEmpty(accessToken)) { |
| | | //重新获取并设置到缓存 该方法暂时只用在花城 |
| | | accessToken = getAppAccessToken(appId, appSecret); |
| | | wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME,TimeUnit.HOURS); |
| | | } else { |
| | | String accessTokenUrl = WX_CALL_BACK + accessToken; |
| | | String result = HttpClientUtil.httpGet(accessTokenUrl, null, null); |
| | | Map<String, Object> resultMap = JSON.parseObject(result, Map.class); |
| | | //如果Access_token过期也重新获取 |
| | | if (resultMap.containsKey("errcode")) { |
| | | accessToken = getAppAccessToken(appId, appSecret); |
| | | wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME,TimeUnit.HOURS); |
| | | } |
| | | } |
| | | return accessToken; |
| | | } |
| | | |
| | | public String getAppAccessToken(String appId,String appSecret) throws Exception { |
| | | String accessToken = "0"; |
| | | try { |
| | | log.info("获取微信token参数:appid=" + APP_ID + ",appSecret=" + APP_SECRET); |
| | | String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + APP_ID + "&secret=" + APP_SECRET; |
| | | log.info("获取微信token参数:appid=" + appId + ",appSecret=" + appSecret); |
| | | String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + appSecret; |
| | | String result = HttpClientUtil.httpGet(accessTokenUrl, null, null); |
| | | Map<String, Object> resultMap = JSON.parseObject(result, Map.class); |
| | | if (resultMap.containsKey("access_token")) { |
| | |
| | | wxXCXTempSend = this; |
| | | wxXCXTempSend.redisTemplate = this.redisTemplate; |
| | | wxXCXTempSend.stringRedisTemplate=this.stringRedisTemplate; |
| | | wxXCXTempSend.redisUtils=this.redisUtils; |
| | | |
| | | } |
| | | |
| | | } |