无关风月
7 天以前 7fd053651ac11db87fe4f6c57e65eed3b9a59452
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ruoyi.other;
 
import com.ruoyi.common.core.domain.R;
import com.ruoyi.other.RuoYiOtherApplication;
import com.ruoyi.other.util.MyQrCodeUtil;
import com.ruoyi.other.util.ObsUploadUtil;
import com.ruoyi.other.util.QRCodeUtil;
import com.ruoyi.other.util.UUIDUtil;
import com.ruoyi.other.util.payment.wx.WechatPayService;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = RuoYiOtherApplication.class)
public class RuoYiOtherApplicationTests {
    @Resource
    private WechatPayService wechatPayService;
    @Test
    public void contextLoads() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        Map<String,String> r = wechatPayService.unifiedOrder(code, "0.01", "积分充值", "/other/wx/integralCallback");
        System.err.println(r);
        String codeUrl = r.get("code_url");
        MyQrCodeUtil.createCodeToFile(codeUrl);
        BufferedImage blueImage = QRCodeUtil.createImage(codeUrl);
        MultipartFile blueFile = convert(blueImage, new Date().getTime() + UUIDUtil.getRandomCode(3) + ".PNG");
        String s = ObsUploadUtil.obsUpload(blueFile);
        System.err.println(s);
 
    }
    public static MultipartFile convert(BufferedImage bufferedImage, String fileName) throws IOException {
        // 将 BufferedImage 转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", baos);
        byte[] bytes = baos.toByteArray();
 
        // 创建 ByteArrayResource
        ByteArrayResource resource = new ByteArrayResource(bytes);
 
        // 创建 MockMultipartFile
        MockMultipartFile multipartFile = new MockMultipartFile(
                "file",
                fileName,
                "image/png",
                resource.getInputStream()
        );
 
        return multipartFile;
    }
}