package com.xzx.gc.user.service; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.xzx.gc.common.constant.Constants; import com.xzx.gc.common.exception.RestException; import com.xzx.gc.common.utils.BusinessUtil; import com.xzx.gc.common.utils.ExceptionUtils; import com.xzx.gc.common.utils.image.GraphicsUtils; import com.xzx.gc.common.utils.wxpay.WxUtil; import com.xzx.gc.entity.UserInfo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; @Service @Slf4j public class PosterService { @Autowired private WxUtil wxUtil; @Autowired private BusinessUtil businessUtil; @Autowired private GraphicsUtils graphicsUtils; public Map generate(UserInfo userInfo){ //海报图名称 String userId=userInfo.getUserId(); String imageName="/user_" + userId + "_poster.jpg"; //二维码图名称 String imgName="/user_" + userId + "_acode.jpg"; String s1 = wxUtil.generateAcode("poster=" + userInfo.getMobilePhone(), "pages/login/login", imgName, "800px"); if(StrUtil.isBlank(s1)){ throw new RestException("生成分享海报失败"); } String path=Constants.IMG_UPLOAD_PATH+imgName; if(StrUtil.isBlank(userInfo.getAvatar())){ userInfo.setAvatar(Constants.IMG_TEMPALRE_VIEW+"/xzx-qr.png"); } userInfo.setMobilePhone(businessUtil.changeMobile(userInfo.getMobilePhone())); String backgroundUrl=userInfo.getBgPoster(); if(StrUtil.isBlank(backgroundUrl)) { backgroundUrl = Constants.IMG_TEMPALRE_PATH + "/user_share_poster_bg.png"; } Map s = null; try { AtomicInteger atomicInteger=new AtomicInteger(1); while (1==1) { if(atomicInteger.get()>10){ throw new RestException("生成分享海报大小异常"); } s = graphicsUtils.generatePoster(imageName, path, userInfo.getAvatar(), backgroundUrl, userInfo.getMobilePhone(), 100, 340, 57, 94, 230, 160, 40); if(s!=null){ break; } log.debug("海报格式异常,重新生成:{},重试次数:{}",userId,atomicInteger.get()); atomicInteger.getAndIncrement(); } } catch (RestException e){ ExceptionUtils.err(e.getMsg(),e); }catch (Exception e) { ExceptionUtils.err("生成分享海报失败",e); } return s; } // // public Map generateByUi(UserInfo userInfo){ // log.trace("开始生成海报图:{}",userInfo.getUserId()); // //海报图名称 // String userId=userInfo.getUserId(); // String imageName="/user_" + userId + "_poster.jpg"; // //二维码图名称 // String imgName="/user_" + userId + "_acode.jpg"; // String s1 = wxUtil.generateAcode("poster=" + userInfo.getMobilePhone(), "pages/login/login", imgName, "800px"); // if(StrUtil.isBlank(s1)){ // throw new RestException("生成分享海报失败"); // } // String path=Constants.IMG_UPLOAD_PATH+imgName; // if(StrUtil.isBlank(userInfo.getAvatar())){ // userInfo.setAvatar(Constants.IMG_TEMPALRE_VIEW+"/xzx-qr.png"); // } // userInfo.setMobilePhone(businessUtil.changeMobile(userInfo.getMobilePhone())); // String backgroundUrl=userInfo.getBgPoster(); // if(StrUtil.isBlank(backgroundUrl)) { // backgroundUrl = Constants.IMG_TEMPALRE_PATH + "/user_share_poster_bg2.png"; // } // Map s = null; // try { // s = graphicsUtils.generatePoster(imageName, path, userInfo.getAvatar(), backgroundUrl, userInfo.getMobilePhone(),100,160,null,null,670,954,34); // } catch (Exception e) { // ExceptionUtils.err("生成分享海报失败",e); // } // return s; // } }