|
|
|
@ -1,7 +1,9 @@ |
|
|
|
package org.jeecg.modules.applet.service.impl; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
import lombok.extern.log4j.Log4j2; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.jeecg.common.api.IAppletUserService; |
|
|
|
import org.jeecg.common.exception.JeecgBootException; |
|
|
|
@ -10,17 +12,33 @@ import org.jeecg.common.system.vo.AppletUser; |
|
|
|
import org.jeecg.modules.applet.entity.StatisticsVo; |
|
|
|
import org.jeecg.modules.applet.service.AppletApiWaterService; |
|
|
|
import org.jeecg.modules.common.IdUtils; |
|
|
|
import org.jeecg.modules.common.wxUtils.WxHttpUtils; |
|
|
|
import org.jeecg.modules.demo.appletConfig.service.IAppletConfigService; |
|
|
|
import org.jeecg.modules.demo.appletWater.entity.AppletWater; |
|
|
|
import org.jeecg.modules.demo.appletWater.service.IAppletWaterService; |
|
|
|
import org.jeecg.modules.demo.appletWithdrawal.entity.AppletWithdrawal; |
|
|
|
import org.jeecg.modules.demo.appletWithdrawal.service.IAppletWithdrawalService; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.HttpEntity; |
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.List; |
|
|
|
import java.util.UUID; |
|
|
|
import java.net.URL; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
@Log4j2 |
|
|
|
@Service |
|
|
|
public class AppletApiWaterServiceImpl implements AppletApiWaterService { |
|
|
|
@Autowired |
|
|
|
@ -29,6 +47,113 @@ public class AppletApiWaterServiceImpl implements AppletApiWaterService { |
|
|
|
private IAppletUserService appletUserService; |
|
|
|
@Autowired |
|
|
|
private IAppletWithdrawalService appletWithdrawalService; |
|
|
|
@Autowired |
|
|
|
private IAppletConfigService appletConfigService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxHttpUtils wxHttpUtils; |
|
|
|
|
|
|
|
@Override |
|
|
|
public byte[] getInviteCode(){ |
|
|
|
AppletUser user = AppletUserUtil.getCurrentAppletUser(); |
|
|
|
|
|
|
|
// 获取环境配置 |
|
|
|
String vsion = appletConfigService.getContentByCode("invite_env_version"); |
|
|
|
Integer vsionStr = Integer.parseInt(vsion); |
|
|
|
String trial = "release"; |
|
|
|
if(vsionStr == 0){ |
|
|
|
trial= "release"; |
|
|
|
}else if(vsionStr == 1){ |
|
|
|
trial= "trial"; |
|
|
|
}else{ |
|
|
|
trial= "develop"; |
|
|
|
} |
|
|
|
|
|
|
|
// 获取必要的配置信息 |
|
|
|
String xcxSharePage = appletConfigService.getContentByCode("xcxSharePage"); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
// 准备微信API请求参数 |
|
|
|
String key = "shareId=" + user.getId(); |
|
|
|
Map<String, Object> param = new HashMap<>(); |
|
|
|
param.put("path", xcxSharePage + "?" + key); |
|
|
|
param.put("scene", user.getId()); |
|
|
|
param.put("width", 150); |
|
|
|
param.put("auto_color", false); |
|
|
|
param.put("env_version", trial); |
|
|
|
|
|
|
|
Map<String, Object> line_color = new HashMap<>(); |
|
|
|
line_color.put("r", 0); |
|
|
|
line_color.put("g", 0); |
|
|
|
line_color.put("b", 0); |
|
|
|
param.put("line_color", line_color); |
|
|
|
|
|
|
|
|
|
|
|
// param.put("is_hyaline", true); |
|
|
|
|
|
|
|
// 获取微信小程序码 |
|
|
|
String accessToken = wxHttpUtils.getAccessToken(); |
|
|
|
String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken; |
|
|
|
|
|
|
|
// 请求微信API获取二维码图片数据 |
|
|
|
RestTemplate rest = new RestTemplate(); |
|
|
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); |
|
|
|
HttpEntity requestEntity = new HttpEntity(JSON.toJSONString(param), headers); |
|
|
|
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); |
|
|
|
byte[] qrCodeBytes = entity.getBody(); |
|
|
|
|
|
|
|
return qrCodeBytes; |
|
|
|
// return this.generateAndCombineImagesFromUrl2(qrCodeBytes, backgroundImageUrl); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public byte[] generateAndCombineImagesFromUrl2(byte[] qrCodeImageByte, String backgroundUrl) { |
|
|
|
File file = null; |
|
|
|
try { |
|
|
|
// 从字节数组加载小程序码图像 |
|
|
|
BufferedImage qrCodeImage = ImageIO.read(new ByteArrayInputStream(qrCodeImageByte)); |
|
|
|
|
|
|
|
// 从URL加载背景图像 |
|
|
|
URL backgroundImageUrl = new URL(backgroundUrl); |
|
|
|
BufferedImage backgroundImage = ImageIO.read(backgroundImageUrl); |
|
|
|
|
|
|
|
// 创建一个新的BufferedImage来保存合并后的图像 |
|
|
|
Graphics2D g2d = backgroundImage.createGraphics(); |
|
|
|
|
|
|
|
int wh = backgroundImage.getWidth() / 3; |
|
|
|
|
|
|
|
// 计算小程序码放置的位置(这里以中心位置为例) |
|
|
|
int qrCodeX = (backgroundImage.getWidth() - wh) / 2; |
|
|
|
int qrCodeY = (int) ((backgroundImage.getHeight() - wh) * 0.6); |
|
|
|
|
|
|
|
// 绘制小程序码图像 |
|
|
|
g2d.drawImage(qrCodeImage, qrCodeX, qrCodeY, wh, wh, null); |
|
|
|
|
|
|
|
// 释放Graphics2D资源 |
|
|
|
g2d.dispose(); |
|
|
|
|
|
|
|
// 将合并后的图像保存到临时文件 |
|
|
|
file = File.createTempFile("combined_", ".png"); |
|
|
|
ImageIO.write(backgroundImage, "png", file); |
|
|
|
|
|
|
|
// 上传到阿里云OSS |
|
|
|
// return this.uploadAliYunOss(Files.readAllBytes(file.toPath()), file.getName()); |
|
|
|
return Files.readAllBytes(file.toPath()); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("生成合并图片失败", e); |
|
|
|
throw new RuntimeException("生成合并图片失败", e); |
|
|
|
} finally { |
|
|
|
// 删除临时文件 |
|
|
|
if (file != null && file.exists()) { |
|
|
|
file.delete(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|