CreatePayQrCodeController.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.ygj.yuemum.controller.pay;
  2. import com.ygj.yuemum.component.Constant;
  3. import com.ygj.yuemum.service.pay.CustomerPayService;
  4. import io.swagger.annotations.Api;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.*;
  7. import sun.misc.BASE64Encoder;
  8. import javax.imageio.ImageIO;
  9. import java.awt.image.BufferedImage;
  10. import java.io.ByteArrayOutputStream;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. @Api(tags = "支付订单相关接口")
  14. @RestController
  15. public class CreatePayQrCodeController {
  16. public static final String RESUMEFILE = Constant.RESUMEFILE;
  17. @Autowired
  18. private CustomerPayService customerPayService;
  19. @RequestMapping(value = "/createPayQrCode", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
  20. @ResponseBody
  21. public String createPayQrCode(@RequestBody Map<String,String> params) throws Exception {
  22. String orderNo = params.get("orderNo");
  23. String channel= params.get("channel");
  24. float price= Float.parseFloat(params.get("price"));
  25. float originPrice= Float.parseFloat(params.get("originPrice"));
  26. String title= params.get("title");
  27. String productName= params.get("productName");
  28. Integer productId= Integer.parseInt(params.get("productId"));
  29. String payment_steps = params.get("payment_steps");
  30. String out_trade_no = orderNo + "SEQ" + payment_steps;
  31. BufferedImage bufferedImage = customerPayService.createPayQrCode(out_trade_no,channel,price,originPrice,title,productName,productId);
  32. //保存一下
  33. // ByteArrayOutputStream os = new ByteArrayOutputStream();
  34. // ImageIO.write(bufferedImage, "jpg", os);
  35. // FileUtils.writeByteArrayToFile(new File(RESUMEFILE+"666.jpg"), os.toByteArray());
  36. // os.close();
  37. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  38. ImageIO.write(bufferedImage, "png", baos);
  39. byte[] bytes = baos.toByteArray();//转换成字节
  40. BASE64Encoder encoder = new BASE64Encoder();
  41. String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
  42. png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");
  43. System.out.println(png_base64);
  44. Map<String, Object> tableData = new HashMap<>();
  45. tableData.put("img",png_base64);
  46. return png_base64;
  47. }
  48. }