xiaoyong931011
2021-07-16 886265d9085ce5324f98e52c1826cfee99637045
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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<String,String> 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<String, String> 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<String,String> 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<String, String> 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;
//    }
 
 
 
}