| | |
| | | package com.panzhihua.common.utlis; |
| | | |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | 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.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.panzhihua.common.constants.SecurityConstants; |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.fileupload.FileItem; |
| | | import org.apache.commons.fileupload.FileItemFactory; |
| | | import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
| | | import org.apache.http.entity.ContentType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.data.redis.core.ValueOperations; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.commons.CommonsMultipartFile; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.panzhihua.common.constants.SecurityConstants; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.Resource; |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Slf4j |
| | | @Component |
| | |
| | | private RedisUtils redisUtils; |
| | | |
| | | private static WxXCXTempSend wxXCXTempSend; |
| | | |
| | | /** |
| | | * url转变为 MultipartFile对象 |
| | | * |
| | |
| | | private static MultipartFile createFileItem(String url, String fileName) throws Exception { |
| | | FileItem item = null; |
| | | try { |
| | | HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection(); |
| | | HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); |
| | | conn.setReadTimeout(30000); |
| | | conn.setConnectTimeout(30000); |
| | | // 设置应用程序要从网络连接读取数据 |
| | |
| | | FileItemFactory factory = new DiskFileItemFactory(16, null); |
| | | String textFieldName = "uploadfile"; |
| | | item = |
| | | factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName); |
| | | factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName); |
| | | OutputStream os = item.getOutputStream(); |
| | | |
| | | int bytesRead = 0; |
| | |
| | | * @return |
| | | */ |
| | | public String getAccessToken() throws Exception { |
| | | String accessToken = ""; |
| | | String accessToken = ""; |
| | | 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); |
| | | }*/ |
| | | accessToken = wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:" + appId).get(); |
| | | return accessToken; |
| | | } |
| | | |
| | | |
| | | 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); |
| | | 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 { |
| | | 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); |
| | | 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); |
| | |
| | | //如果Access_token过期也重新获取 |
| | | if (resultMap.containsKey("errcode")) { |
| | | accessToken = getAppAccessToken(appId, appSecret); |
| | | wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME,TimeUnit.HOURS); |
| | | 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 { |
| | | public String getAppAccessToken(String appId, String appSecret) throws Exception { |
| | | String accessToken = "0"; |
| | | try { |
| | | log.info("获取微信token参数:appid=" + appId + ",appSecret=" + appSecret); |
| | |
| | | |
| | | /** |
| | | * 发布消息时重新设置access_token 防止过期 |
| | | * |
| | | * @param appId |
| | | * @param appSecret |
| | | * @throws Exception |
| | | */ |
| | | public void setAppAccessTokenToCache(String appId,String appSecret) throws Exception{ |
| | | public void setAppAccessTokenToCache(String appId, String appSecret) throws Exception { |
| | | String accessToken = "0"; |
| | | try { |
| | | log.info("获取微信token参数:appid=" + appId + ",appSecret=" + appSecret); |
| | |
| | | Map<String, Object> resultMap = JSON.parseObject(result, Map.class); |
| | | if (resultMap.containsKey("access_token")) { |
| | | accessToken = resultMap.get("access_token").toString(); |
| | | wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME,TimeUnit.HOURS); |
| | | wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME, TimeUnit.HOURS); |
| | | } |
| | | } catch (IOException ioe) { |
| | | log.error("小程序http请求异常"); |
| | |
| | | } |
| | | } |
| | | |
| | | public String getAccessTokenValid(String appId,Integer index) throws Exception { |
| | | if (index % 1000 == 0){ |
| | | public String getAccessTokenValid(String appId, Integer index) throws Exception { |
| | | if (index % 1000 == 0) { |
| | | return getAccessToken(appId); |
| | | }else { |
| | | return wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+appId).get(); |
| | | } else { |
| | | return wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:" + appId).get(); |
| | | } |
| | | |
| | | } |
| | | |
| | | public String getWsAccessToken() throws Exception { |
| | | String accessToken = "0"; |
| | | try { |
| | | // log.info("获取微信token参数:appid=" + APP_ID + ",appSecret=" + APP_SECRET); |
| | | String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=wx98d62711dfbd8425"+ "&secret=97a2a10b990c2774ed279724337b5337"; |
| | | String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=wx98d62711dfbd8425" + "&secret=97a2a10b990c2774ed279724337b5337"; |
| | | String result = HttpClientUtil.httpGet(accessTokenUrl, null, null); |
| | | Map<String, Object> resultMap = JSON.parseObject(result, Map.class); |
| | | if (resultMap.containsKey("access_token")) { |
| | |
| | | } |
| | | return accessToken; |
| | | } |
| | | |
| | | /** |
| | | * 获取西区社区通微信token |
| | | * @return 西区社区通微信token |
| | | * |
| | | * @return 西区社区通微信token |
| | | * @throws Exception 异常 |
| | | */ |
| | | public String getXQAppAccessToken() throws Exception { |
| | |
| | | mediaId = getAppMediaId(accessToken); |
| | | valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID, mediaId); |
| | | valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME, |
| | | System.currentTimeMillis() + 259000000 + ""); |
| | | System.currentTimeMillis() + 259000000 + ""); |
| | | } else { |
| | | mediaId = valueOperations.get(SecurityConstants.APPLETS_ACCESS_MEDIA_ID); |
| | | } |
| | |
| | | mediaId = getAppMediaId(accessToken); |
| | | valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID, mediaId); |
| | | valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME, |
| | | System.currentTimeMillis() + 259000000 + ""); |
| | | System.currentTimeMillis() + 259000000 + ""); |
| | | } |
| | | return mediaId; |
| | | } |
| | |
| | | * |
| | | * @param file |
| | | * @return |
| | | * @author yixiu |
| | | * @throws Exception |
| | | * @author yixiu |
| | | */ |
| | | public String uploadFile(MultipartFile file, String access_token) throws Exception { |
| | | String url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + access_token + "&type=image"; |
| | | String result = null; |
| | | String fileName = file.getOriginalFilename(); |
| | | URL urlObj = new URL(url); |
| | | HttpURLConnection con = (HttpURLConnection)urlObj.openConnection(); |
| | | HttpURLConnection con = (HttpURLConnection) urlObj.openConnection(); |
| | | con.setRequestMethod("POST"); |
| | | con.setDoInput(true); |
| | | con.setDoOutput(true); |
| | |
| | | public void init() { |
| | | wxXCXTempSend = this; |
| | | wxXCXTempSend.redisTemplate = this.redisTemplate; |
| | | wxXCXTempSend.stringRedisTemplate=this.stringRedisTemplate; |
| | | wxXCXTempSend.redisUtils=this.redisUtils; |
| | | wxXCXTempSend.stringRedisTemplate = this.stringRedisTemplate; |
| | | wxXCXTempSend.redisUtils = this.redisUtils; |
| | | |
| | | } |
| | | |