| | |
| | | package com.stylefeng.guns.modular.system.util; |
| | | |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.apache.http.entity.ContentType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLDecoder; |
| | | import java.nio.charset.Charset; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 微信工具类 |
| | |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | |
| | | |
| | |
| | | String ticket = JSONObject.parseObject(backData).getString("ticket"); |
| | | return ticket; |
| | | } |
| | | |
| | | |
| | | |
| | | public String getWxAppletsAccessToken(){ |
| | | String wxAppletsAccessToken = redisUtil.getValue("wxAppletsAccessToken"); |
| | | if(ToolUtil.isNotEmpty(wxAppletsAccessToken)){ |
| | | return wxAppletsAccessToken; |
| | | } |
| | | String appletsAccessToken = getAppletsAccessToken(); |
| | | redisUtil.setStrValue("wxAppletsAccessToken", appletsAccessToken, 7200); |
| | | return appletsAccessToken; |
| | | } |
| | | |
| | | /** |
| | | * 获取微信小程序token |
| | | * @return |
| | | */ |
| | | public String getAppletsAccessToken() { |
| | | String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret; |
| | | HttpRequest get = HttpUtil.createGet(url); |
| | | HttpResponse response = get.execute(); |
| | | if(response.getStatus() != 200){ |
| | | return ""; |
| | | } |
| | | JSONObject jsonObject = JSON.parseObject(response.body()); |
| | | return jsonObject.getString("access_token"); |
| | | } |
| | | |
| | | /*** |
| | | * 获取acess_token (公众号) |
| | |
| | | * @return |
| | | */ |
| | | public String getAccessToken(){ |
| | | String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc89ecab90d24edd8&secret=bca863174d22736a4a9758a67484947e"; |
| | | String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret; |
| | | String backData = restTemplate.getForObject(url, String.class); |
| | | String accessToken = JSONObject.parseObject(backData).getString("access_token"); |
| | | return accessToken; |
| | |
| | | } |
| | | |
| | | public String rid(String path, Integer driverId) throws Exception{ |
| | | // 获取access_token |
| | | String accessToken = getAccessToken(); |
| | | // 替换CREATE_CODE_URL |
| | | String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken; |
| | | // 发送请求 |
| | | String stringApiResult = null; |
| | | InputStream release = getwxacodeunlimit(path, "driverId=" + driverId, EnvVersion.RELEASE.getVersion()); |
| | | String png = ObsUploadUtil.obsUpload("png", release); |
| | | return png; |
| | | } |
| | | |
| | | |
| | | |
| | | public InputStream getwxacodeunlimit(String page, String scene, String envVersion) throws Exception{ |
| | | try { |
| | | HashMap<String, String> map = new HashMap<>(); |
| | | map.put("path", path+"?driverId="+driverId); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | HttpEntity requestEntity = new HttpEntity(map, headers); |
| | | ResponseEntity<byte[]> rep = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class); |
| | | byte[] bytes = rep.getBody(); |
| | | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); |
| | | MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), byteArrayInputStream); |
| | | String s = ObsUploadUtil.obsUpload(file); |
| | | System.err.println("===url"+s); |
| | | return s; |
| | | |
| | | } catch (Exception e) { |
| | | String wxAppletsAccessToken = getWxAppletsAccessToken(); |
| | | String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + wxAppletsAccessToken; |
| | | Map<String, Object> param = new HashMap<>(); |
| | | param.put("scene", scene); |
| | | param.put("page", page); |
| | | param.put("env_version", envVersion); |
| | | HttpHeaders httpHeaders = new HttpHeaders(); |
| | | MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8"); |
| | | httpHeaders.setContentType(type); |
| | | HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(param, httpHeaders); |
| | | ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); |
| | | byte[] body = entity.getBody(); |
| | | String s = new String(body, Charset.defaultCharset()); |
| | | System.err.println(s); |
| | | return new ByteArrayInputStream(body); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | return stringApiResult; |
| | | return null; |
| | | } |
| | | |
| | | } |