From 2b46b751f7618b2be3210cf2c6ab51d3540c3ebc Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Wed, 07 Sep 2022 11:36:08 +0800 Subject: [PATCH] 20220902 --- src/test/java/cc/mrbird/febs/ProfitTest.java | 255 +++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 197 insertions(+), 58 deletions(-) diff --git a/src/test/java/cc/mrbird/febs/ProfitTest.java b/src/test/java/cc/mrbird/febs/ProfitTest.java index 6071375..3784730 100644 --- a/src/test/java/cc/mrbird/febs/ProfitTest.java +++ b/src/test/java/cc/mrbird/febs/ProfitTest.java @@ -5,17 +5,12 @@ import cc.mrbird.febs.common.enumerates.FlowTypeEnum; import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum; import cc.mrbird.febs.common.enumerates.OrderStatusEnum; -import cc.mrbird.febs.common.properties.XcxProperties; -import cc.mrbird.febs.common.utils.LoginUserUtil; import cc.mrbird.febs.common.utils.MallUtils; import cc.mrbird.febs.common.utils.RedisUtils; -import cc.mrbird.febs.common.utils.SpringContextHolder; -import cc.mrbird.febs.mall.dto.ApiLeaderOrderConfirmDto; import cc.mrbird.febs.mall.dto.ApiLeaderRefundOrderDto; import cc.mrbird.febs.mall.entity.*; import cc.mrbird.febs.mall.mapper.*; import cc.mrbird.febs.mall.service.*; -import cc.mrbird.febs.mall.vo.ApiLeaderProfitVo; import cc.mrbird.febs.pay.model.WxGenerateQrCodeDto; import cc.mrbird.febs.pay.service.IXcxPayService; import cc.mrbird.febs.pay.util.WechatConfigure; @@ -23,37 +18,33 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import lombok.RequiredArgsConstructor; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.io.*; import java.math.BigDecimal; -import java.net.HttpURLConnection; +import java.net.URLConnection; +import java.nio.charset.Charset; import java.util.*; -import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintWriter; -import java.net.HttpURLConnection; import java.net.URL; -import java.util.Base64; import java.util.HashMap; import java.util.Map; @@ -379,61 +370,209 @@ } } - private static final String GET_WXACODE ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="; /** - * 生成小程序码返回base64字符串 - * @param sceneStr 要携带的参数 - * @param accessToken 上面方法得到的token - * @param page 要跳转的小程序页面(必须是已发布的) - * @return - */ - - private static final String GETWXACODEUNLIMIT_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";// 生成小程序码地址 - private static final String URL_GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";//获取access_token地址 - private static final String APP_ID = "wx0b515f652282158e";// 小程序appid - private static final String APP_SECRET = "8d3d3c14221f7dc37650b861dc0fc570"; // 小程序秘钥 - - public static final String PATH_HOME = ""; - private static final String BASE_PREFIX = "data:image/png;base64,"; // base64图片固定前缀 - public static String getminiqrQr(String sceneStr, String accessToken,String page) { - HttpURLConnection httpURLConnection = null; + * 获取小程序菊花码 + * @Description //TODO + * @Author zhaowx + * @Date 2020/3/25 15:23 + **/ + @Test + public void getWXCode() { try { - URL url = new URL(String.format(GETWXACODEUNLIMIT_URL,accessToken)); - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestMethod("POST");// 提交模式 - // 发送POST请求必须设置如下两行 - httpURLConnection.setDoOutput(true); - httpURLConnection.setDoInput(true); - // 获取URLConnection对象对应的输出流 - PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); - // 发送请求参数 - JSONObject paramJson = new JSONObject(); - paramJson.put("scene", sceneStr); - paramJson.put("page", page); - printWriter.write(paramJson.toString()); - // flush输出流的缓冲 - printWriter.flush(); - //开始获取数据 + //这里调用的是上面的获取access_token方法 + String access_token = redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY).toString(); + String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token; + String scene = "15"; + Map<String, String> param = new HashMap<>(); + param.put("scene",scene); + //这里的page如果没有的话可以不写,默认是跳主页,如果写了没有的页面的话,会返回错误信息 + param.put("page","pages/product/details"); + String json = JSON.toJSONString(param); + ByteArrayInputStream inputStream = sendPost(url, json); - BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); - try (InputStream is = httpURLConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();){ - byte[] buffer = new byte[1024]; - int len = -1; - while ((len = is.read(buffer)) != -1) { - baos.write(buffer, 0, len); + System.out.println(inputStream); + //这里判断的是返回的图片还是错误信息,一般错误信息不会大于200 + if (inputStream.available() <= 200){ + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + int i; + byte[] buffer = new byte[200]; + while ((i = inputStream.read(buffer)) != -1){ + byteArrayOutputStream.write(buffer,0,i); } - return BASE_PREFIX+ Base64.getEncoder().encodeToString(baos.toByteArray()); + String str = new String(byteArrayOutputStream.toByteArray()); + System.out.println(str); + //错误信息的格式在官方文档里有 + JSONObject jsonObject = JSONObject.parseObject(str); + if ("41030".equals(jsonObject.getString("errcode"))){ + System.out.println("所传page页面不存在,或者小程序没有发布"); + + }else if ("45009".equals(jsonObject.getString("errcode"))){ + System.out.println("调用分钟频率受限"); + } + byteArrayOutputStream.close(); + } + //输出到本地的代码 + FileOutputStream fileOutputStream = new FileOutputStream("D:/123.png"); + int i; + byte[] buffer = new byte[200]; + while ((i = inputStream.read(buffer)) != -1){ + fileOutputStream.write(buffer,0,i); + } + fileOutputStream.flush(); + fileOutputStream.close(); + + inputStream.close(); + }catch (Exception e){ + } + } + + + public static ByteArrayInputStream sendPost(String URL, String json) { + InputStream inputStream = null; + ByteArrayInputStream byteArrayInputStream = null; + // 创建默认的httpClient实例. + CloseableHttpClient httpclient = HttpClients.createDefault(); + // 创建httppost + HttpPost httppost = new HttpPost(URL); + httppost.addHeader("Content-type", "application/json; charset=utf-8"); + httppost.setHeader("Accept", "application/json"); + try { + StringEntity s = new StringEntity(json, Charset.forName("UTF-8")); + s.setContentEncoding("UTF-8"); + httppost.setEntity(s); + HttpResponse response = httpclient.execute(httppost); + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ + // 获取相应实体 + HttpEntity entity = response.getEntity(); + inputStream = entity.getContent(); + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + // 创建一个Buffer字符串 + byte[] buffer = new byte[1024]; + // 每次读取的字符串长度,如果为-1,代表全部读取完毕 + int len = 0; + // 使用一个输入流从buffer里把数据读取出来 + while ((len = inputStream.read(buffer)) != -1) { + // 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 + outStream.write(buffer, 0, len); + } + // 关闭输入流 + inputStream.close(); + // 把outStream里的数据写入内存 + byteArrayInputStream = new ByteArrayInputStream(outStream.toByteArray()); } } catch (Exception e) { e.printStackTrace(); - }finally { - if (httpURLConnection != null){ - httpURLConnection.disconnect(); + } finally { + // 关闭连接,释放资源 + try { + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); } } - return null; + return byteArrayInputStream; } + + /** + * 传递自定义参数获取二维码保存后并生成url给前端访问 + * @Author yeafel + * @param scene + * @param page + * @param is_hyaline + * @param auto_color + * @return + */ + @Test + public void getQrCodeImgUrl() { + String scene = "pages/product/details"; + String page = "15"; + Boolean is_hyaline = true; + Boolean auto_color = true; + RestTemplate restTemplate = new RestTemplate(); + //首先获取ACCESS_TOKEN + String accessToken = redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY).toString(); + + //然后调用微信官方api生成二维码 + String createQrCodeUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken; + //此处我是使用的阿里巴巴的fastJson + JSONObject createQrParam = new JSONObject(); + createQrParam.put("scene", scene); + createQrParam.put("page", page); + createQrParam.put("is_hyaline", is_hyaline); + createQrParam.put("auto_color", auto_color); + + PrintWriter out = null; + InputStream in = null; + try { + URL realUrl = new URL(createQrCodeUrl); + // 打开和URL之间的连接 + URLConnection conn = realUrl.openConnection(); + // 设置通用的请求属性 + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + // 获取URLConnection对象对应的输出流 + out = new PrintWriter(conn.getOutputStream()); + // 发送请求参数,利用connection的输出流,去写数据到connection中,我的参数数据流出我的电脑内存到connection中,让connection把参数帮我传到URL中去请求。 + out.print(createQrParam); + // flush输出流的缓冲 + out.flush(); + //获取URL的connection中的输入流,这个输入流就是我请求的url返回的数据,返回的数据在这个输入流中,流入我内存,我将从此流中读取数据。 + in = conn.getInputStream(); + //定义个空字节数组 + byte[] data = null; + // 读取图片字节数组 + try { + //创建字节数组输出流作为中转仓库,等待被写入数据 + ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); + byte[] buff = new byte[100]; + int rc = 0; + while ((rc = in.read(buff, 0, 100)) > 0) { + //向中转的输出流循环写出输入流中的数据 + swapStream.write(buff, 0, rc); + } + //此时connection的输入流返回给我们的请求结果数据已经被完全地写入了我们定义的中转输出流swapStream中 + data = swapStream.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + String base64Code = new String(Objects.requireNonNull(Base64.getEncoder().encode(data))); + //Base64转byte[]数组 + System.out.println(base64Code); + } catch (Exception e) { + System.out.println("发送 POST 请求出现异常!" + e); + e.printStackTrace(); + } + + // 使用finally块来关闭输出流、输入流 + finally { + try { + if (out != null) { + out.close(); + } + if (in != null) { + in.close(); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + + public static void main(String[] args) { BigDecimal amount = new BigDecimal("0.15").setScale(2,BigDecimal.ROUND_DOWN); System.out.println(amount); -- Gitblit v1.9.1