优化商品详情加载时间过长,拆分详情海报单独请求

This commit is contained in:
hupeng
2020-05-01 17:37:28 +08:00
parent 492b0ef306
commit 9541d11f35
5 changed files with 136 additions and 54 deletions

View File

@ -1,5 +1,6 @@
package co.yixiang.modules.shop.service;
import co.yixiang.modules.shop.entity.YxStoreProduct;
import co.yixiang.modules.shop.web.dto.ProductDTO;
import java.awt.*;
@ -7,5 +8,5 @@ import java.io.IOException;
public interface CreatShareProductService {
String creatProductPic(ProductDTO productDTO,String qrcode,String spreadPicName,String spreadPicPath,String apiUrl) throws IOException, FontFormatException;
String creatProductPic(YxStoreProduct productDTO, String qrcode, String spreadPicName, String spreadPicPath, String apiUrl) throws IOException, FontFormatException;
}

View File

@ -28,6 +28,8 @@ import java.util.List;
*/
public interface YxStoreProductService extends BaseService<YxStoreProduct> {
YxStoreProduct getProductInfo(int id);
void incProductStock(int num,int productId,String unique);

View File

@ -2,6 +2,7 @@ package co.yixiang.modules.shop.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import co.yixiang.modules.shop.entity.YxStoreProduct;
import co.yixiang.modules.shop.service.CreatShareProductService;
import co.yixiang.modules.shop.web.dto.ProductDTO;
import co.yixiang.modules.user.entity.YxSystemAttachment;
@ -27,7 +28,7 @@ public class CreatShareProductServiceImpl implements CreatShareProductService {
private final YxSystemAttachmentService systemAttachmentService;
public String creatProductPic(ProductDTO productDTO,String shareCode,String spreadPicName,String spreadPicPath,String apiUrl) throws IOException, FontFormatException {
public String creatProductPic(YxStoreProduct productDTO, String shareCode, String spreadPicName, String spreadPicPath, String apiUrl) throws IOException, FontFormatException {
YxSystemAttachment attachmentT = systemAttachmentService.getInfo(spreadPicName);
String spreadUrl = "";
if(ObjectUtil.isNull(attachmentT)){
@ -45,7 +46,7 @@ public class CreatShareProductServiceImpl implements CreatShareProductService {
//读取互联网图片
BufferedImage priductUrl = null;
try {
priductUrl = ImageIO.read(new URL(productDTO.getStoreInfo().getImage_base()));
priductUrl = ImageIO.read(new URL(productDTO.getImage()));
} catch (IOException e) {
e.printStackTrace();
}
@ -59,11 +60,11 @@ public class CreatShareProductServiceImpl implements CreatShareProductService {
g.setFont(font.deriveFont(Font.BOLD,34));
g.setColor(new Color(29,29,29));
//绘制文字
g.drawString(productDTO.getStoreInfo().getStoreName(), 31, 638);
g.drawString(productDTO.getStoreName(), 31, 638);
//文案
g.setFont(font.deriveFont(Font.PLAIN,30));
g.setColor(new Color(47,47,47));
int fontlen = getWatermarkLength(productDTO.getStoreInfo().getStoreInfo(), g);
int fontlen = getWatermarkLength(productDTO.getStoreInfo(), g);
//文字长度相对于图片宽度应该有多少行
int line = fontlen / (back.getWidth() - 90);
//高度
@ -76,8 +77,8 @@ public class CreatShareProductServiceImpl implements CreatShareProductService {
//单行字符总长度临时计算
int tempLineLen = 0;
StringBuffer sb =new StringBuffer();
for(int i=0; i < productDTO.getStoreInfo().getStoreInfo().length(); i++) {
char tempChar = productDTO.getStoreInfo().getStoreInfo().charAt(i);
for(int i=0; i < productDTO.getStoreInfo().length(); i++) {
char tempChar = productDTO.getStoreInfo().charAt(i);
tempCharLen = getCharLen(tempChar, g);
tempLineLen += tempCharLen;
if(tempLineLen >= (back.getWidth()-90)) {
@ -116,19 +117,19 @@ public class CreatShareProductServiceImpl implements CreatShareProductService {
//价格
g.setFont(font.deriveFont(Font.PLAIN,50));
g.setColor(new Color(249,64,64));
g.drawString("¥" +productDTO.getStoreInfo().getPrice(), 29, 1162);
g.drawString("¥" +productDTO.getPrice(), 29, 1162);
//原价
g.setFont(font.deriveFont(Font.PLAIN,36));
g.setColor(new Color(171,171,171));
String price = "¥" + productDTO.getStoreInfo().getOtPrice();
String price = "¥" + productDTO.getOtPrice();
g.drawString(price, 260, 1160);
g.drawLine(250,1148,260+150,1148);
//商品名称
g.setFont(font.deriveFont(Font.PLAIN,32));
g.setColor(new Color(29,29,29));
g.drawString(productDTO.getStoreInfo().getStoreName(), 30, 1229);
g.drawString(productDTO.getStoreName(), 30, 1229);
//生成二维码返回链接
String url = shareCode;

View File

@ -128,6 +128,18 @@ public class YxStoreProductServiceImpl extends BaseServiceImpl<YxStoreProductMap
}
@Override
public YxStoreProduct getProductInfo(int id) {
QueryWrapper<YxStoreProduct> wrapper = new QueryWrapper<>();
wrapper.eq("is_del",0).eq("is_show",1).eq("id",id);
YxStoreProduct storeProduct = yxStoreProductMapper.selectOne(wrapper);
if(ObjectUtil.isNull(storeProduct)){
throw new ErrorRequestException("商品不存在或已下架");
}
return storeProduct;
}
@Override
public ProductDTO goodsDetail(int id, int type,int uid,String latitude,String longitude) {
QueryWrapper<YxStoreProduct> wrapper = new QueryWrapper<>();

View File

@ -19,6 +19,7 @@ import co.yixiang.common.api.ApiResult;
import co.yixiang.common.web.controller.BaseController;
import co.yixiang.enums.AppFromEnum;
import co.yixiang.enums.ProductEnum;
import co.yixiang.modules.shop.entity.YxStoreProduct;
import co.yixiang.modules.shop.service.*;
import co.yixiang.modules.shop.web.dto.ProductDTO;
import co.yixiang.modules.shop.web.param.YxStoreProductQueryParam;
@ -114,17 +115,14 @@ public class StoreProductController extends BaseController {
}
/**
* 普通商品详情
* 商品详情海报
*/
@Log(value = "查看商品详情",type = 1)
@GetMapping("/product/detail/{id}")
@ApiOperation(value = "普通商品详情",notes = "普通商品详情")
public ApiResult<ProductDTO> detail(@PathVariable Integer id,
@RequestParam(value = "",required=false) String latitude,
@RequestParam(value = "",required=false) String longitude,
@RequestParam(value = "",required=false) String from) throws IOException, FontFormatException {
@GetMapping("/product/poster/{id}")
@ApiOperation(value = "商品详情海报",notes = "商品详情海报")
public ApiResult<String> prodoctPoster(@PathVariable Integer id) throws IOException, FontFormatException {
int uid = SecurityUtils.getUserId().intValue();
ProductDTO productDTO = storeProductService.goodsDetail(id,0,uid,latitude,longitude);
YxStoreProduct storeProduct = storeProductService.getProductInfo(id);
// 海报
String siteUrl = systemConfigService.getData("site_url");
if(StrUtil.isEmpty(siteUrl)){
@ -139,42 +137,110 @@ public class StoreProductController extends BaseController {
if(!userType.equals(AppFromEnum.ROUNTINE.getValue())) {
userType = AppFromEnum.H5.getValue();
}
String name = id+"_"+uid + "_"+userType+"_product_detail_wap.jpg";
YxSystemAttachment attachment = systemAttachmentService.getInfo(name);
String fileDir = path+"qrcode"+ File.separator;
String qrcodeUrl = "";
if(ObjectUtil.isNull(attachment)){
File file = FileUtil.mkdir(new File(fileDir));
//如果类型是小程序
if(userType.equals(AppFromEnum.ROUNTINE.getValue())){
//h5地址
siteUrl = siteUrl+"/product/";
//生成二维码
QrCodeUtil.generate(siteUrl+"?productId="+id+"&spread="+uid+"&pageType=good&codeType="+AppFromEnum.ROUNTINE.getValue(), 180, 180,
FileUtil.file(fileDir+name));
}
else if(userType.equals(AppFromEnum.APP.getValue())){
//h5地址
siteUrl = siteUrl+"/product/";
//生成二维码
QrCodeUtil.generate(siteUrl+"?productId="+id+"&spread="+uid+"&pageType=good&codeType="+AppFromEnum.APP.getValue(), 180, 180,
FileUtil.file(fileDir+name));
}else{//如果类型是h5
//生成二维码
QrCodeUtil.generate(siteUrl+"detail/"+id+"?spread="+uid, 180, 180,
FileUtil.file(fileDir+name));
}
systemAttachmentService.attachmentAdd(name,String.valueOf(FileUtil.size(file)),
fileDir+name,"qrcode/"+name);
qrcodeUrl = apiUrl + "/api/file/qrcode/"+name;
}else{
qrcodeUrl = apiUrl + "/api/file/" + attachment.getSattDir();
String name = id+"_"+uid + "_"+userType+"_product_detail_wap.jpg";
YxSystemAttachment attachment = systemAttachmentService.getInfo(name);
String fileDir = path+"qrcode"+ File.separator;
String qrcodeUrl = "";
if(ObjectUtil.isNull(attachment)){
File file = FileUtil.mkdir(new File(fileDir));
//如果类型是小程序
if(userType.equals(AppFromEnum.ROUNTINE.getValue())){
//h5地址
siteUrl = siteUrl+"/product/";
//生成二维码
QrCodeUtil.generate(siteUrl+"?productId="+id+"&spread="+uid+"&pageType=good&codeType="+AppFromEnum.ROUNTINE.getValue(), 180, 180,
FileUtil.file(fileDir+name));
}
String spreadPicName = id+"_"+uid + "_"+userType+"_product_user_spread.jpg";
String spreadPicPath = fileDir+spreadPicName;
String rr = creatShareProductService.creatProductPic(productDTO,qrcodeUrl,spreadPicName,spreadPicPath,apiUrl);
productDTO.getStoreInfo().setCodeBase(rr);
else if(userType.equals(AppFromEnum.APP.getValue())){
//h5地址
siteUrl = siteUrl+"/product/";
//生成二维码
QrCodeUtil.generate(siteUrl+"?productId="+id+"&spread="+uid+"&pageType=good&codeType="+AppFromEnum.APP.getValue(), 180, 180,
FileUtil.file(fileDir+name));
}else{//如果类型是h5
//生成二维码
QrCodeUtil.generate(siteUrl+"detail/"+id+"?spread="+uid, 180, 180,
FileUtil.file(fileDir+name));
}
systemAttachmentService.attachmentAdd(name,String.valueOf(FileUtil.size(file)),
fileDir+name,"qrcode/"+name);
qrcodeUrl = apiUrl + "/api/file/qrcode/"+name;
}else{
qrcodeUrl = apiUrl + "/api/file/" + attachment.getSattDir();
}
String spreadPicName = id+"_"+uid + "_"+userType+"_product_user_spread.jpg";
String spreadPicPath = fileDir+spreadPicName;
String rr = creatShareProductService.creatProductPic(storeProduct,qrcodeUrl,
spreadPicName,spreadPicPath,apiUrl);
//productDTO.getStoreInfo().setCodeBase(rr);
return ApiResult.ok(rr);
}
/**
* 普通商品详情
*/
@Log(value = "查看商品详情",type = 1)
@GetMapping("/product/detail/{id}")
@ApiOperation(value = "普通商品详情",notes = "普通商品详情")
public ApiResult<ProductDTO> detail(@PathVariable Integer id,
@RequestParam(value = "",required=false) String latitude,
@RequestParam(value = "",required=false) String longitude,
@RequestParam(value = "",required=false) String from) {
int uid = SecurityUtils.getUserId().intValue();
ProductDTO productDTO = storeProductService.goodsDetail(id,0,uid,latitude,longitude);
// 海报
// String siteUrl = systemConfigService.getData("site_url");
// if(StrUtil.isEmpty(siteUrl)){
// return ApiResult.fail("未配置h5地址");
// }
// String apiUrl = systemConfigService.getData("api_url");
// if(StrUtil.isEmpty(apiUrl)){
// return ApiResult.fail("未配置api地址");
// }
// YxUserQueryVo userInfo = yxUserService.getYxUserById(uid);
// String userType = userInfo.getUserType();
// if(!userType.equals(AppFromEnum.ROUNTINE.getValue())) {
// userType = AppFromEnum.H5.getValue();
// }
// String name = id+"_"+uid + "_"+userType+"_product_detail_wap.jpg";
// YxSystemAttachment attachment = systemAttachmentService.getInfo(name);
// String fileDir = path+"qrcode"+ File.separator;
// String qrcodeUrl = "";
// if(ObjectUtil.isNull(attachment)){
// File file = FileUtil.mkdir(new File(fileDir));
// //如果类型是小程序
// if(userType.equals(AppFromEnum.ROUNTINE.getValue())){
// //h5地址
// siteUrl = siteUrl+"/product/";
// //生成二维码
// QrCodeUtil.generate(siteUrl+"?productId="+id+"&spread="+uid+"&pageType=good&codeType="+AppFromEnum.ROUNTINE.getValue(), 180, 180,
// FileUtil.file(fileDir+name));
// }
// else if(userType.equals(AppFromEnum.APP.getValue())){
// //h5地址
// siteUrl = siteUrl+"/product/";
// //生成二维码
// QrCodeUtil.generate(siteUrl+"?productId="+id+"&spread="+uid+"&pageType=good&codeType="+AppFromEnum.APP.getValue(), 180, 180,
// FileUtil.file(fileDir+name));
// }else{//如果类型是h5
// //生成二维码
// QrCodeUtil.generate(siteUrl+"detail/"+id+"?spread="+uid, 180, 180,
// FileUtil.file(fileDir+name));
// }
// systemAttachmentService.attachmentAdd(name,String.valueOf(FileUtil.size(file)),
// fileDir+name,"qrcode/"+name);
//
// // qrcodeUrl = apiUrl + "/api/file/qrcode/"+name;
// }else{
// // qrcodeUrl = apiUrl + "/api/file/" + attachment.getSattDir();
// }
// //String spreadPicName = id+"_"+uid + "_"+userType+"_product_user_spread.jpg";
// // String spreadPicPath = fileDir+spreadPicName;
//// String rr = creatShareProductService.creatProductPic(productDTO,qrcodeUrl,spreadPicName,spreadPicPath,apiUrl);
//// productDTO.getStoreInfo().setCodeBase(rr);
return ApiResult.ok(productDTO);
}