package com.ygj.yuemum.controller.pay; import com.ygj.yuemum.component.Constant; import com.ygj.yuemum.domain.order.YSOrder; import com.ygj.yuemum.domain.pay.CustomerPay; import com.ygj.yuemum.domain.pay.PayMents; import com.ygj.yuemum.service.order.YSOrderService; import com.ygj.yuemum.service.pay.CustomerPayService; import io.swagger.annotations.Api; import io.swagger.models.auth.In; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.HashMap; import java.util.Map; @Api(tags = "支付订单相关接口") @RestController public class CreatePayQrCodeController { public static final String RESUMEFILE = Constant.RESUMEFILE; @Autowired private CustomerPayService customerPayService; @Autowired private YSOrderService ysOrderService; @RequestMapping(value = "/createPayQrCode", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") @ResponseBody public String createPayQrCode(@RequestBody Map params) throws Exception { String orderNo = params.get("orderNo"); String channel= params.get("channel"); float price= Float.parseFloat(params.get("price")); float originPrice= Float.parseFloat(params.get("originPrice")); String title= params.get("title"); String productName= params.get("productName"); Integer productId= Integer.parseInt(params.get("productId")); String payment_steps = params.get("payment_steps"); String out_trade_no = orderNo + "SEQ" + payment_steps; //check 本流水号是否已经付过款 CustomerPay customerPay = customerPayService.checkTrade(out_trade_no); if (customerPay != null) { //check 订单是否已经支付完成 YSOrder ysOrder = ysOrderService.selectByOdNo(orderNo); if (ysOrder.getOd_order_pay_amount() == 0) { return "all"; } else { out_trade_no = orderNo + "SEQ" + (Integer.parseInt(params.get("payment_steps")) + 1); } } BufferedImage bufferedImage = customerPayService.createPayQrCode(out_trade_no,channel,price,originPrice,title,productName,productId); //保存一下 // ByteArrayOutputStream os = new ByteArrayOutputStream(); // ImageIO.write(bufferedImage, "jpg", os); // FileUtils.writeByteArrayToFile(new File(RESUMEFILE+"666.jpg"), os.toByteArray()); // os.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "png", baos); byte[] bytes = baos.toByteArray();//转换成字节 BASE64Encoder encoder = new BASE64Encoder(); String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串 png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", ""); System.out.println(png_base64); Map tableData = new HashMap<>(); tableData.put("img",png_base64); return png_base64; } }