admin
2020-02-18 ddf5db7d31af51fcf697eac0cee7dd46ff73bcc1
fanli/src/main/java/com/yeshi/fanli/service/impl/user/QrCodeServiceImpl.java
@@ -12,15 +12,20 @@
import org.springframework.stereotype.Service;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.QRCodeUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
import com.yeshi.fanli.dao.mybatis.share.ShareMapper;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.service.inter.user.QrCodeService;
import com.yeshi.fanli.service.inter.user.SpreadUserImgService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.FileUtil;
import com.yeshi.fanli.util.ImageUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.UserInviteUtil;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
@Service
public class QrCodeServiceImpl implements QrCodeService {
@@ -42,7 +47,7 @@
         String targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg";
         String erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg";
         String erCode = HttpUtil.getShortLink("http://" + Constant.wxGZConfig.getLoginHost() + "/"
         String erCode = ("http://" + Constant.wxGZConfig.getLoginHost() + "/"
               + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
         // 生成
         try {
@@ -69,7 +74,7 @@
            String imgUrl = COSManager.getInstance()
                  .uploadFile(new File(targetPath),
                        "ercode/" + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg")
                        FilePathEnum.ercode.getPath() + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg")
                  .getUrl();
            if (new File(erCodeTempPath).exists())
@@ -91,7 +96,7 @@
         String targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg";
         String erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg";
         String erCode = HttpUtil.getShortLink("http://" + Constant.wxGZConfig.getLoginHost() + "/"
         String erCode = ("http://" + Constant.wxGZConfig.getLoginHost() + "/"
               + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
         // 生成
         try {
@@ -117,8 +122,8 @@
            String newUrl = url.substring(index + 1);
            String urlMd5 = newUrl.substring(0, newUrl.lastIndexOf("."));
            String imgUrl = COSManager.getInstance()
                  .uploadFile(new File(targetPath), "ercode/" + "ercode_" + uid + "_" + urlMd5 + ".jpg").getUrl();
            String imgUrl = COSManager.getInstance().uploadFile(new File(targetPath),
                  FilePathEnum.ercode.getPath() + "ercode_" + uid + "_" + urlMd5 + ".jpg").getUrl();
            if (new File(erCodeTempPath).exists())
               new File(erCodeTempPath).delete();
@@ -131,21 +136,20 @@
      }
      return null;
   }
   @Override
   public String drawInviteQrCodeNew(String url, Long uid, String portrait, Integer pX, Integer pY, Integer size, String inviteCode) throws IOException {
      if (StringUtil.isNullOrEmpty(url)) {
   public String drawInviteQrCodeNew(InputStream urlInputStream, String urlMd5, Long uid, String portrait, Integer pX, Integer pY, Integer size,
         String inviteCode) throws IOException {
      if (urlInputStream == null) {
         return null;
      }
      String targetPath = null;
      String erCodeTempPath = null;
      InputStream erCodeInputStream  = null; // 二维码
      InputStream erCodeInputStream = null; // 二维码
      InputStream portraitInputStream = null; // 头像
      InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景
//      InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景
      String uuid = UUID.randomUUID().toString().replace("-", "");
      if (uid == null) {
         targetPath = FileUtil.getCacheDir() + "/share_" + uuid + "_" + System.currentTimeMillis() + ".jpg";
@@ -155,53 +159,47 @@
         erCodeInputStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/official.png");
         // 官方默认头像
         portraitInputStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/official_icon.png");
      } else {
         targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg";
         erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg";
         String erCode = HttpUtil.getShortLink("http://" + Constant.wxGZConfig.getLoginHost() + "/"
               + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
         String erCode = UserInviteUtil.getShareUrl(uid);
         // 生成
         try {
            QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath);
         } catch (Exception e) {
            e.printStackTrace();
         }
         urlInputStream = HttpUtil.getAsInputStream(url); // 背景
         portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像
         erCodeInputStream = new FileInputStream(new File(erCodeTempPath)); // 二维码
      }
      // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
      ImageUtil.inviteFriendImgWhitecustom(urlInputStream, portraitInputStream, erCodeInputStream, targetPath,
          pX, pY, size, inviteCode);
      ImageUtil.inviteFriendImgWhitecustom(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, pX, pY,
            size, inviteCode);
      // 删除二维码文件
      if(erCodeInputStream != null) {
      if (erCodeInputStream != null) {
         erCodeInputStream.close();
      }
      if (erCodeTempPath != null && new File(erCodeTempPath).exists()) {
         new File(erCodeTempPath).delete();
      }
      // 上传文件相对位置
      if (new File(targetPath).exists() && new File(targetPath).length() > 0) {
         int index = url.lastIndexOf("/");
         String newUrl = url.substring(index + 1);
         String urlMd5 = newUrl.substring(0, newUrl.lastIndexOf("."));
         String fileUrl= null;
         String fileUrl = null;
         if (uid == null) {
            fileUrl = "ercode/" + "ercode_" + uuid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            fileUrl = FilePathEnum.invitePictureDemo.getPath() + "ercode_" + uuid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
         } else {
            fileUrl = "ercode/" + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            fileUrl = FilePathEnum.ercode.getPath() + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
         }
         // 上传文件
         String imgUrl = COSManager.getInstance().uploadFile(new File(targetPath), fileUrl).getUrl();
         // 删除本地缓存文件
         new File(targetPath).delete();
@@ -209,21 +207,21 @@
      }
      return null;
   }
   @Override
   public String drawInviteToGreet(String url, Long uid, String portrait, String inviteCode, String content,
         Date date) throws IOException {
   public String drawInviteToGreet(String url, Long uid, String portrait, String inviteCode, String content, Date date)
         throws IOException {
      if (StringUtil.isNullOrEmpty(url)) {
         return null;
      }
      String targetPath = null;
      String erCodeTempPath = null;
      InputStream erCodeInputStream  = null; // 二维码
      InputStream erCodeInputStream = null; // 二维码
      InputStream portraitInputStream = null; // 头像
      InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景
      String uuid = UUID.randomUUID().toString().replace("-", "");
      if (uid == null) {
         targetPath = FileUtil.getCacheDir() + "/share_" + uuid + "_" + System.currentTimeMillis() + ".jpg";
@@ -233,11 +231,11 @@
         erCodeInputStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/official.png");
         // 官方默认头像
         portraitInputStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/official_icon.png");
      } else {
         targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg";
         erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg";
         String erCode = HttpUtil.getShortLink("http://" + Constant.wxGZConfig.getLoginHost() + "/"
         String erCode = ("http://" + Constant.wxGZConfig.getLoginHost() + "/"
               + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
         // 生成
         try {
@@ -245,23 +243,22 @@
         } catch (Exception e) {
            e.printStackTrace();
         }
         erCodeInputStream = new FileInputStream(new File(erCodeTempPath));
         erCodeInputStream = new FileInputStream(new File(erCodeTempPath));
         portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像
      }
      // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
      ImageUtil.drawInviteToGreet(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, inviteCode, content, date);
      // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
      ImageUtil.drawInviteToGreet(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, inviteCode,
            content, date);
      // 删除二维码文件
      if(erCodeInputStream != null) {
      if (erCodeInputStream != null) {
         erCodeInputStream.close();
      }
      if (erCodeTempPath != null && new File(erCodeTempPath).exists()) {
         new File(erCodeTempPath).delete();
      }
      // 上传文件
      if (new File(targetPath).exists() && new File(targetPath).length() > 0) {
         int index = url.lastIndexOf("/");
@@ -269,11 +266,11 @@
         String urlMd5 = newUrl.substring(0, newUrl.lastIndexOf("."));
         // 上传文件相对位置
         String fileUrl= null;
         String fileUrl = null;
         if (uid == null) {
            fileUrl = "ercode/" + "ercode_" + uuid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            fileUrl = FilePathEnum.invitePictureDemo.getPath() + "ercode_" + uuid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
         } else {
            fileUrl = "ercode/" + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            fileUrl = FilePathEnum.ercode.getPath() + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
         }
         // 上传
@@ -282,8 +279,54 @@
         new File(targetPath).delete();
         return imgUrl;
      }
      }
      return null;
   }
   @Override
   public FileUploadResult drawGoodsPoster(String erCodeUrl, String portrait, GoodsDetailVO goods) {
      // 二维码流
      InputStream erCodeStream = null;
      try {
         erCodeStream = QRCodeUtil.getInstance(250).encode(erCodeUrl);
      } catch (Exception e1) {
         e1.printStackTrace();
      }
      // 头像
      InputStream portraitStream = null;
      if (!StringUtil.isNullOrEmpty(portrait)) {
         try {
            portraitStream = HttpUtil.getAsInputStream(portrait);
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
      if (portraitStream == null) {
         portraitStream = ImageUtil.class.getClassLoader().getResourceAsStream("image/official_default_head.jpg");
      }
      // 画图
      InputStream drawStream = ImageUtil.drawGoodsShareSingle(erCodeStream, portraitStream, goods);
      // 上传位置
      String uuid = UUID.randomUUID().toString().replace("-", "");
      String upPath = FilePathEnum.shareXCX.getPath() + uuid + "_" + goods.getGoodsId() + "_" + System.currentTimeMillis() + ".png";
      // 上传文件
      return COSManager.getInstance().uploadInputStream(drawStream, upPath);
   }
   @Override
   public FileUploadResult drawGoodsPosterXCX(InputStream erCodeStream,UserInfo user, GoodsDetailVO goods) {
      // 画图
      InputStream drawStream = ImageUtil.drawGoodsShareXCX(erCodeStream, user, goods);
      // 上传位置
      String uuid = UUID.randomUUID().toString().replace("-", "");
      String upPath = FilePathEnum.shareXCX.getPath() + uuid + "_" + goods.getGoodsId() + "_" + System.currentTimeMillis() + ".png";
      // 上传文件
      return COSManager.getInstance().uploadInputStream(drawStream, upPath);
   }
}