admin
2020-01-03 ecb361733a4f90ad1672c422577292a18c0fd2bb
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.yeshi.fanli.service.impl.user;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;
 
import javax.annotation.Resource;
 
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 {
 
    @Resource
    private ShareMapper shareMapper;
 
    @Resource
    private SpreadUserImgService spreadUserImgService;
 
    @Override
    public String getPortrait(Long uid) {
        return shareMapper.getPortrait(uid);
    }
 
    @Override
    public String drawInviteQrCode(String url, Long uid, String portrait, String inviteCode) throws IOException {
        if (url != null && !url.equals("")) {
            String targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg";
            String erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg";
 
            String erCode = ("http://" + Constant.wxGZConfig.getLoginHost() + "/"
                    + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
            // 生成
            try {
                QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景
            InputStream portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像
            InputStream erCodeInputStream = new FileInputStream(new File(erCodeTempPath)); // 二维码
 
            // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
            ImageUtil.inviteFriendImg(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, inviteCode);
 
            if (new File(targetPath).exists() && new File(targetPath).length() > 0) {
                // 上传文件
 
                // 将url 转为 md5 让到下面中添加 在比较md5的值
 
                int index = url.lastIndexOf("/");
                String newUrl = url.substring(index + 1);
                String urlMd5 = newUrl.substring(0, newUrl.lastIndexOf("."));
 
                String imgUrl = COSManager.getInstance()
                        .uploadFile(new File(targetPath),
                                FilePathEnum.ercode.getPath() + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg")
                        .getUrl();
 
                if (new File(erCodeTempPath).exists())
                    new File(erCodeTempPath).delete();
 
                if (new File(targetPath).exists())
                    new File(targetPath).delete();
 
                return imgUrl;
            }
        }
        return null;
    }
 
    @Override
    public String drawInviteQrCode(String url, Long uid, String portrait, int erCodePostionX, int erCodePostionY,
            int erCodeSize, String inviteCode) throws IOException {
        if (!StringUtil.isNullOrEmpty(url)) {
            String targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg";
            String erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg";
 
            String erCode = ("http://" + Constant.wxGZConfig.getLoginHost() + "/"
                    + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
            // 生成
            try {
                QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景
            InputStream portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像
            InputStream erCodeInputStream = new FileInputStream(new File(erCodeTempPath)); // 二维码
 
            // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
            ImageUtil.inviteFriendImg(urlInputStream, portraitInputStream, erCodeInputStream, targetPath,
                    erCodePostionX, erCodePostionY, erCodeSize, inviteCode);
 
            if (new File(targetPath).exists() && new File(targetPath).length() > 0) {
                // 上传文件
 
                // 将url 转为 md5 让到下面中添加 在比较md5的值
 
                int index = url.lastIndexOf("/");
                String newUrl = url.substring(index + 1);
                String urlMd5 = newUrl.substring(0, newUrl.lastIndexOf("."));
 
                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();
 
                if (new File(targetPath).exists())
                    new File(targetPath).delete();
 
                return imgUrl;
            }
        }
        return null;
    }
 
    @Override
    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 portraitInputStream = null; // 头像
//        InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景
 
        String uuid = UUID.randomUUID().toString().replace("-", "");
        if (uid == null) {
            targetPath = FileUtil.getCacheDir() + "/share_" + uuid + "_" + System.currentTimeMillis() + ".jpg";
            // 官方 邀请码(无效的)
            inviteCode = "FLQAPP";
            // 官方二维码
            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 = UserInviteUtil.getShareUrl(uid);
            // 生成
            try {
                QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像
            erCodeInputStream = new FileInputStream(new File(erCodeTempPath)); // 二维码
        }
 
        // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
        ImageUtil.inviteFriendImgWhitecustom(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, pX, pY,
                size, inviteCode);
 
        // 删除二维码文件
        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) {
            String fileUrl = null;
            if (uid == null) {
                fileUrl = FilePathEnum.ercode.getPath() + "ercode_" + uuid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            } else {
                fileUrl = FilePathEnum.ercode.getPath() + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            }
 
            // 上传文件
            String imgUrl = COSManager.getInstance().uploadFile(new File(targetPath), fileUrl).getUrl();
 
            // 删除本地缓存文件
            new File(targetPath).delete();
 
            return imgUrl;
        }
        return null;
    }
 
    @Override
    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 portraitInputStream = null; // 头像
        InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景
 
        String uuid = UUID.randomUUID().toString().replace("-", "");
        if (uid == null) {
            targetPath = FileUtil.getCacheDir() + "/share_" + uuid + "_" + System.currentTimeMillis() + ".jpg";
            // 官方 邀请码(无效的)
            inviteCode = "FLQAPP";
            // 官方二维码
            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 = ("http://" + Constant.wxGZConfig.getLoginHost() + "/"
                    + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid);
            // 生成
            try {
                QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            erCodeInputStream = new FileInputStream(new File(erCodeTempPath));
            portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像
        }
 
        // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片
        ImageUtil.drawInviteToGreet(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, inviteCode,
                content, date);
 
        // 删除二维码文件
        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;
            if (uid == null) {
                fileUrl = FilePathEnum.ercode.getPath() + "ercode_" + uuid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            } else {
                fileUrl = FilePathEnum.ercode.getPath() + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg";
            }
 
            // 上传
            String imgUrl = COSManager.getInstance().uploadFile(new File(targetPath), fileUrl).getUrl();
            // 删除缓存文件
            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);
    }
 
}