package com.matrix.system.shopXcx.api.action; import com.matrix.system.common.init.UserCacheManager; import com.matrix.component.tools.ImageUtil; import com.matrix.component.tools.WxacodeUtil; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.tools.*; import com.matrix.system.common.constance.AppConstance; import com.matrix.system.common.dao.BusParameterSettingsDao; import com.matrix.system.hive.bean.SysVipInfo; import com.matrix.system.hive.dao.SysShopInfoDao; import com.matrix.system.hive.dao.SysVipInfoDao; import com.matrix.system.hive.plugin.util.CollectionUtils; import com.matrix.system.shopXcx.api.WeChatApiTools; import com.matrix.system.shopXcx.api.pojo.QrcodeImgParam; import com.matrix.system.shopXcx.api.vo.QrcodeVo; import com.matrix.system.shopXcx.api.vo.WxActivitiesGroupBuyVO; import com.matrix.system.shopXcx.bean.*; import com.matrix.system.shopXcx.dao.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; /** * @author pengliang * @description 商品查询 * @date 2019-06-05 19:00 */ @Controller @RequestMapping(value = "/wxapi/shareProduct") @CrossOrigin(origins = "*", maxAge = 3600) public class WxShareProductAction { @Autowired WxacodeUtil wxacodeUtil; @Value("${file_storage_path}") private String fileStoragePath; @Value("${static_resource_url}") private String staticResourceUrl; @Autowired WeChatApiTools weChatApiTools; @Autowired private ShopProductDao shopProductDao; @Autowired private UserCacheManager userCacheManager; @Autowired private ShopShareQrcordDao shareQrcordDao; @Autowired private SysShopInfoDao shopInfoDao; @Value(value = "${qrcodeBackgroundImgPath}") private String qrcodeBackgroundImgPath; @Value(value = "${qrcodeFrontImgPath}") private String qrcodeFrontImgPath; @Autowired ShopActivitiesGroupInfoDao shopActivitiesGroupInfoDao; @Autowired SysVipInfoDao sysVipInfoDao; @Autowired ShopActivitiesGroupPriceDao shopActivitiesGroupPriceDao; @Autowired ShopActivitiesDao shopActivitiesDao; @Autowired ShopActivitiesSeckillInfoDao shopActivitiesSeckillInfoDao; @Autowired BusParameterSettingsDao busParameterSettingsDao; /** * 生成二维码 */ @Transactional(rollbackFor = Exception.class) @RequestMapping(value = "/getProductShareQrcode") public @ResponseBody AjaxResult getwxacode1(@RequestBody QrcodeVo qrcodeVo) throws Exception { SysVipInfo loginUser = userCacheManager.getLoginUser(); loginUser = sysVipInfoDao.selectById(loginUser.getId()); //活动类型 ShopActivities activities=null; //是否为拼团分享 boolean isGroupProduct = qrcodeVo.getActId() != null; if(isGroupProduct){ activities= shopActivitiesDao.selectById(qrcodeVo.getActId()); } ShopShareQrcord queryOrcode = new ShopShareQrcord(); queryOrcode.setProductId(qrcodeVo.getProductId()); queryOrcode.setUserId(loginUser.getId()); if (isGroupProduct ) { queryOrcode.setProductId(Integer.parseInt(qrcodeVo.getActId() + "")); queryOrcode.setStatus(1); } else { queryOrcode.setStatus(2); queryOrcode.setShopId(qrcodeVo.getShopId()); } List queryOrcodeList = shareQrcordDao.selectByModel(queryOrcode); if (CollectionUtils.isEmpty(queryOrcodeList)) { ShopShareQrcord shareQrcord = new ShopShareQrcord(); //如果未查询到就先插入数据再更新数据 shareQrcord.setCreateBy(loginUser.getOpenId()); shareQrcord.setUpdateBy(loginUser.getOpenId()); shareQrcord.setUserId(loginUser.getId()); shareQrcord.setShopId(qrcodeVo.getShopId()); //拼团活动 if (isGroupProduct ) { queryOrcode.setStatus(1); shareQrcord.setProductId(Integer.parseInt(qrcodeVo.getActId() + "")); } else { queryOrcode.setStatus(2); shareQrcord.setProductId(qrcodeVo.getProductId()); } shareQrcordDao.insert(shareQrcord); QrcodeImgParam qrcodeImgParam = null; if (isGroupProduct && ShopActivities.ACTIVITIES_TYPE_GROUP==activities.getActType()) { qrcodeImgParam = getGroupQrcodeImgParam(loginUser, qrcodeVo); shareQrcord.setProductId(Integer.parseInt(qrcodeVo.getActId() + "")); } if (isGroupProduct && ShopActivities.ACTIVITIES_TYPE_SECKILL==activities.getActType()) { qrcodeImgParam = getSeckillQrcodeImgParam(loginUser, qrcodeVo); shareQrcord.setProductId(Integer.parseInt(qrcodeVo.getActId() + "")); } else { qrcodeImgParam = getQrcodeImgParam(loginUser, qrcodeVo); shareQrcord.setProductId(qrcodeVo.getProductId()); } qrcodeImgParam.logo=""; //绘制二维码 drawQrcode(qrcodeImgParam); //更新分享记录 shareQrcord.setRemark(qrcodeImgParam.targetImg); shareQrcord.setQrcodeUrl(qrcodeImgParam.urlPath); shareQrcordDao.updateByModel(shareQrcord); return new AjaxResult(AjaxResult.STATUS_SUCCESS, Arrays.asList(shareQrcord)); } else { //查询到就返回 return new AjaxResult(AjaxResult.STATUS_SUCCESS, queryOrcodeList); } } /** * 秒杀分享图片参数 * @param loginUser * @param qrcodeVo * @return */ private QrcodeImgParam getSeckillQrcodeImgParam(SysVipInfo loginUser, QrcodeVo qrcodeVo) throws Exception { ShopActivitiesSeckillInfo seckillInfo = shopActivitiesSeckillInfoDao.selectById(qrcodeVo.getSkillId()); ShopProduct shopProduct = shopProductDao.selectById(seckillInfo.getGoodsId().intValue()); String qrcodeSavePath = wxacodeUtil.getWxacode(qrcodeVo.getScene(), qrcodeVo.getPath(), UUIDUtil.getRandomID() ,weChatApiTools.getAppid(loginUser.getCompanyId()),weChatApiTools.getSecret(loginUser.getCompanyId())); LogUtil.debug("qrcodeSavePath={}", qrcodeSavePath); // 图片保存目录路径 String baseSavePath = fileStoragePath; // 图片访问URL String baseSaveUrl = staticResourceUrl; String productImg = shopProduct.getImgMobile(); productImg = productImg.replace(baseSaveUrl, baseSavePath); String targetImg = UUIDUtil.getRandomID() + ".png"; ShopActivitiesGroupPrice price=shopActivitiesGroupPriceDao.selectById(qrcodeVo.getGpId()); QrcodeImgParam qrcodeImgParam = new QrcodeImgParam(); qrcodeImgParam.qrcodeSavePath = qrcodeSavePath; qrcodeImgParam.qrcodeBackgroundImgPath = qrcodeBackgroundImgPath; qrcodeImgParam.qrcodeFrontImgPath = qrcodeFrontImgPath; qrcodeImgParam.productImgPath = productImg; qrcodeImgParam.urlPath = baseSaveUrl + "/" + "wxacode" + "/" + targetImg; qrcodeImgParam.targetImg = baseSavePath + "/" + "wxacode" + "/" + targetImg; qrcodeImgParam.title = "【秒杀】" + shopProduct.getTitle(); qrcodeImgParam.price = "¥" +seckillInfo.getSiPrice(); qrcodeImgParam.couponName = loginUser.getNickName() + "邀您体验"; return qrcodeImgParam; } /** * 组装拼团图片参数 * * @param loginUser * @param qrcodeVo * @return */ private QrcodeImgParam getGroupQrcodeImgParam(SysVipInfo loginUser, QrcodeVo qrcodeVo) throws Exception { WxActivitiesGroupBuyVO groupBuyVO = shopActivitiesGroupInfoDao.selectGroupInfoWithPriceByActId(qrcodeVo.getActId()); ShopProduct shopProduct = shopProductDao.selectById(groupBuyVO.getGoodsId().intValue()); String qrcodeSavePath =wxacodeUtil.getWxacode(qrcodeVo.getScene(), qrcodeVo.getPath(), UUIDUtil.getRandomID() ,weChatApiTools.getAppid(loginUser.getCompanyId()),weChatApiTools.getSecret(loginUser.getCompanyId())); LogUtil.debug("qrcodeSavePath={}", qrcodeSavePath); // 图片保存目录路径 String baseSavePath =fileStoragePath; // 图片访问URL String baseSaveUrl =staticResourceUrl; String productImg = shopProduct.getImgMobile(); productImg = productImg.replace(baseSaveUrl, baseSavePath); String targetImg = UUIDUtil.getRandomID() + ".png"; ShopActivitiesGroupPrice price=shopActivitiesGroupPriceDao.selectById(qrcodeVo.getGpId()); QrcodeImgParam qrcodeImgParam = new QrcodeImgParam(); qrcodeImgParam.qrcodeSavePath = qrcodeSavePath; qrcodeImgParam.qrcodeBackgroundImgPath = qrcodeBackgroundImgPath; qrcodeImgParam.qrcodeFrontImgPath = qrcodeFrontImgPath; qrcodeImgParam.productImgPath = productImg; qrcodeImgParam.urlPath = baseSaveUrl + "/" + "wxacode" + "/" + targetImg; qrcodeImgParam.targetImg = baseSavePath + "/" + "wxacode" + "/" + targetImg; qrcodeImgParam.title = "【拼团】" + shopProduct.getTitle(); qrcodeImgParam.price = "¥" + price.getGpPrice(); qrcodeImgParam.couponName = loginUser.getNickName() + "邀您体验"; return qrcodeImgParam; } private QrcodeImgParam getQrcodeImgParam(SysVipInfo loginUser, QrcodeVo qrcodeVo) throws Exception { ShopProduct shopProduct = shopProductDao.selectById(qrcodeVo.getProductId()); String qrcodeSavePath = wxacodeUtil.getWxacode(qrcodeVo.getScene(), qrcodeVo.getPath(), UUIDUtil.getRandomID() ,weChatApiTools.getAppid(loginUser.getCompanyId()),weChatApiTools.getSecret(loginUser.getCompanyId())); LogUtil.debug("qrcodeSavePath={}", qrcodeSavePath); // 图片保存目录路径 String baseSavePath =fileStoragePath; // 图片访问URL String baseSaveUrl =staticResourceUrl; String productImg = shopProduct.getImgMobile(); String productPath = baseSavePath + "wxacode" + File.separatorChar + MD5Util.strToMD5(loginUser.getOpenId()) + "poster.png"; ImageUtil.downloadPicture(productImg, productPath); // productImg = productImg.replace(baseSaveUrl, baseSavePath); String targetImg = UUIDUtil.getRandomID() + ".png"; QrcodeImgParam qrcodeImgParam = new QrcodeImgParam(); qrcodeImgParam.qrcodeSavePath = qrcodeSavePath; qrcodeImgParam.qrcodeBackgroundImgPath = qrcodeBackgroundImgPath; qrcodeImgParam.qrcodeFrontImgPath = qrcodeFrontImgPath; qrcodeImgParam.productImgPath = productPath; qrcodeImgParam.urlPath = baseSaveUrl + "/" + "wxacode" + "/" + targetImg; qrcodeImgParam.targetImg = baseSavePath + "/" + "wxacode" + "/" + targetImg; qrcodeImgParam.title = shopProduct.getTitle(); qrcodeImgParam.price = "¥" + shopProduct.getPrice().toString(); ShopCoupon shopCoupon = shopProduct.getShopCoupon(); qrcodeImgParam.couponName = loginUser.getNickName() + "邀您体验"; if (shopCoupon != null) { String beginTiem = DateUtil.dateFormatStr(shopCoupon.getBeginTime(), DateUtil.DATE_FORMAT_SPLITE_DD); String endTiem = DateUtil.dateFormatStr(shopCoupon.getEndTime(), DateUtil.DATE_FORMAT_SPLITE_DD); qrcodeImgParam.couponDate = "有效期:" + beginTiem + "~" + endTiem; } return qrcodeImgParam; } /** * 绘制二维码宣传海报 * * @param qrcodeImgParam * @throws IOException */ public static void drawQrcode(QrcodeImgParam qrcodeImgParam) throws IOException { LogUtil.debug(qrcodeImgParam.toString()); File targetImgFile = new File(qrcodeImgParam.targetImg); BufferedImage productImgBuf = ImageIO.read(new File(qrcodeImgParam.productImgPath)); BufferedImage qrcordImgBuf = ImageIO.read(new File(qrcodeImgParam.qrcodeSavePath)); BufferedImage backgroundImgBuf = ImageIO.read(new File(qrcodeImgParam.qrcodeBackgroundImgPath)); BufferedImage frontImagBuf = ImageIO.read(new File(qrcodeImgParam.qrcodeFrontImgPath)); //绘制背景+产品 Graphics2D g = backgroundImgBuf.createGraphics(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1)); //计算产品等比宽度 Double height = new Double(productImgBuf.getHeight()) * (350D / productImgBuf.getWidth()); int py = 750 - 260 - height.intValue(); g.drawImage(productImgBuf, 75, py, 350, height.intValue(), null); g.dispose(); ImageIO.write(backgroundImgBuf, "png", new File(qrcodeImgParam.targetImg)); //绘制前景图片 BufferedImage b1Img = ImageIO.read(targetImgFile); g = b1Img.createGraphics(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1)); g.drawImage(frontImagBuf, 0, 0, 500, 750, null); ImageIO.write(b1Img, "png", targetImgFile); //绘制二维码 BufferedImage b2Img = ImageIO.read(targetImgFile); g = b2Img.createGraphics(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1)); g.drawImage(qrcordImgBuf, 320, 550, 140, 140, null); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); //标题 g.setColor(new Color(0xF10D0A0A, true)); Font font = new Font("微软雅黑", Font.PLAIN, 24); g.setFont(font); g.drawString(qrcodeImgParam.title, 40, 540); //价格 g.setColor(new Color(0xD78008)); g.drawString(qrcodeImgParam.price, 40, 580); //LOGO g.setColor(new Color(0xFED69311, true)); g.setFont(new Font("微软雅黑", Font.PLAIN, 12)); g.drawString(qrcodeImgParam.logo, 200, 730); g.drawString(qrcodeImgParam.couponDate, 40, 620); //活动名称 g.setColor(new Color(0xAC834C00, true)); g.setFont(new Font("微软雅黑", Font.BOLD, 20)); g.drawString(qrcodeImgParam.couponName, 40, 60); ImageIO.write(b2Img, "png", targetImgFile); } public static void main(String[] args) throws Exception { QrcodeImgParam qrcodeImgParam = new QrcodeImgParam(); qrcodeImgParam.qrcodeSavePath = "C:\\Users\\Administrator\\Desktop\\ewm.png"; qrcodeImgParam.qrcodeBackgroundImgPath = "C:\\Users\\Administrator\\Desktop\\dd\\bj1.png"; qrcodeImgParam.qrcodeFrontImgPath = "C:\\Users\\Administrator\\Desktop\\dd\\qj2.png"; // qrcodeImgParam.productImgPath = "C:\\Users\\Administrator\\Desktop\\dd\\banner.jpg"; qrcodeImgParam.productImgPath = "C:\\Users\\Administrator\\Desktop\\6cbfee5df0474fe0b864be142bc474ab.jpg"; qrcodeImgParam.targetImg = "C:\\Users\\Administrator\\Desktop\\dd\\banner111.jpg"; qrcodeImgParam.title = "盐酸米多君片"; qrcodeImgParam.price = "¥30"; qrcodeImgParam.logo = "新诚智慧药房"; qrcodeImgParam.couponName = "姜友瑶送您一张零食免单券快来领取吧"; qrcodeImgParam.couponDate = "有效期:2019.01.02~2019.03.03"; drawQrcode(qrcodeImgParam); } }