diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/WxAppinfoController.java b/mallinkAdmin/src/main/java/com/iformall/controller/WxAppinfoController.java index 780a88a65..67fcc5a35 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/WxAppinfoController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/WxAppinfoController.java @@ -1,6 +1,7 @@ package com.iformall.controller; import com.github.pagehelper.PageInfo; +import com.iformall.common.ErrorCode; import com.iformall.common.Result; import com.iformall.common.ResultData; import com.iformall.domain.po.WxAppinfo; @@ -8,11 +9,16 @@ import com.iformall.service.WxAppinfoService; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Map; + @RestController @RequestMapping("wxAppinfo") public class WxAppinfoController extends BaseController { @@ -64,4 +70,56 @@ public class WxAppinfoController extends BaseController { } + @ApiOperation("更新AccessToken, 无限制二维码生成需要更新token") + @GetMapping("/accessTokenUpdate") + @ApiImplicitParam(name = "appId", value = "c端小程序appId", dataType = "String", paramType = "query", required = true) + public ResultData tokenUpdate(String appId) { + try { + wxAppinfoService.tokenUpdate(appId); + } catch (Exception e) { + logger.error(e.getMessage()); + } + + return new ResultData(); + } + + @ApiOperation(value = "下载二维码", notes = "参数{\"appId\":\"String\", \"pageUrl\":\"String\", \"sceneParam\":\"二维码参数\", \"type\":\"0:有限二维码,1:无限二维码\",\"withText\":\"int(0:不带字, 1:加一行字,2:加两行字)\",\"text1\":\"String\",\"text2\":\"String\"") + @PostMapping("/downQrCode") + public ResultData downQrCode(@RequestBody Map params, HttpServletRequest request, HttpServletResponse response) { + String appId = (String) params.get("appId"); + String pageUrl = (String) params.get("pageUrl"); + String sceneParam = (String) params.get("sceneParam"); + int type = 0; + try { + type = (int) params.get("type"); + } catch (Exception e) { + type = 0; + } + + int withText = 0; + try { + withText = (int) params.get("withText"); + } catch (Exception e) { + withText = 0; + } + String text1 = (String) params.get("text1"); + String text2 = (String) params.get("text2"); + if (StringUtils.isBlank(appId)) { + return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空"); + } + if (StringUtils.isBlank(pageUrl)) { + return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "pageUrl不能为空"); + } + try { + wxAppinfoService.exportQrcode(request, response, + appId, type, pageUrl, sceneParam, + withText, text1, text2); + } catch (Exception e) { + logger.error(e.getMessage()); + } + + return new ResultData(); + } + + } diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/WxMallController.java b/mallinkAdmin/src/main/java/com/iformall/controller/WxMallController.java index 74504a6ab..398af5a23 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/WxMallController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/WxMallController.java @@ -1,40 +1,38 @@ package com.iformall.controller; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - import com.github.pagehelper.PageInfo; import com.iformall.common.Result; import com.iformall.common.ResultData; - import com.iformall.domain.po.WxMall; import com.iformall.service.WxMallService; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("wxMall") public class WxMallController extends BaseController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - @Autowired + @Autowired private WxMallService wxMallService; - - @ApiOperation("分页列表接口") + + @ApiOperation("分页列表接口") @GetMapping("list") - @ApiImplicitParams({ - @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true), - @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)}) - public ResultData list(@ModelAttribute WxMall wxMall,Integer pageNum, Integer pageSize) { - if (null == wxMall) wxMall = new WxMall(); + @ApiImplicitParams({ + @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true), + @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)}) + public ResultData list(@ModelAttribute WxMall wxMall, Integer pageNum, Integer pageSize) { + if (null == wxMall) wxMall = new WxMall(); final PageInfo page = wxMallService.listAsPage(wxMall, pageNum, pageSize); return new ResultData(page); } - @ApiOperation("新增接口") + @ApiOperation("新增接口") @PostMapping("add") public ResultData add(@RequestBody WxMall wxMall) { //Assert.notNull(wxMall.getName(), "角色名不能为空"); @@ -52,17 +50,17 @@ public class WxMallController extends BaseController { @ApiOperation("根据id删除接口") @GetMapping("/del") - @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) + @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) public ResultData delete(Long id) { wxMallService.deleteById(id); return new ResultData(Result.SUCCESS, "删除成功", null); } - - @ApiOperation("根据id查询接口") - @GetMapping("/findById") - @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) + + @ApiOperation("根据id查询接口") + @GetMapping("/findById") + @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) public ResultData findById(Long id) { - return new ResultData(Result.SUCCESS,"查询成功",wxMallService.getById(id)); + return new ResultData(Result.SUCCESS, "查询成功", wxMallService.getById(id)); } @ApiOperation("查询当前mall的信息") @@ -70,5 +68,6 @@ public class WxMallController extends BaseController { public ResultData mallinfo() { return new ResultData(wxMallService.getByTenantId(getTenantId())); } - + + } diff --git a/mallinkAdmin/src/main/java/com/iformall/utils/Constant.java b/mallinkAdmin/src/main/java/com/iformall/utils/Constant.java deleted file mode 100644 index 7c1762971..000000000 --- a/mallinkAdmin/src/main/java/com/iformall/utils/Constant.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iformall.utils; - -public class Constant { - - public static final String fileDirectory="./uploads"; -} diff --git a/mallinkService/src/main/java/com/iformall/service/WxAppinfoService.java b/mallinkService/src/main/java/com/iformall/service/WxAppinfoService.java index 7df44c5c1..5ad807b40 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxAppinfoService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxAppinfoService.java @@ -3,6 +3,9 @@ package com.iformall.service; import com.github.pagehelper.PageInfo; import com.iformall.domain.po.WxAppinfo; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + public interface WxAppinfoService { /** @@ -44,12 +47,19 @@ public interface WxAppinfoService { * @param id */ void deleteById(Long id); - - - - - - - + + /** + * 更新accessToken + * @param appId + */ + void tokenUpdate(String appId); + + /** + * 位置码/导购码生成 + */ + void exportQrcode(HttpServletRequest request, HttpServletResponse response, + String appId, int type, String pageUrl, String sceneParam, + int withText, String text1, String text2); + } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxAppinfoServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxAppinfoServiceImpl.java index 020c2bc4b..537a80708 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxAppinfoServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxAppinfoServiceImpl.java @@ -1,16 +1,34 @@ package com.iformall.service.impl; +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; +import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor; +import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import com.iformall.common.ErrorCode; import com.iformall.domain.po.WxAppinfo; +import com.iformall.exception.MallinkException; import com.iformall.mapper.WxAppinfoMapper; import com.iformall.service.WxAppinfoService; +import com.iformall.utils.Constant; +import com.iformall.utils.QRCodeUtils; +import me.chanjar.weixin.common.error.WxErrorException; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.iformall.common.IdWorker; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; + @Service public class WxAppinfoServiceImpl implements WxAppinfoService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -51,10 +69,133 @@ public class WxAppinfoServiceImpl implements WxAppinfoService { } - - - - - - + @Override + public void tokenUpdate(String appId) { + WxAppinfo appinfo = wxAppinfoMapper.findByAppId(appId); + if (appinfo == null) { + logger.error("APPID Not found: ", appId); + throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); + } + // get access_token + WxMaInMemoryConfig config = new WxMaInMemoryConfig(); + config.setAppid(appinfo.getAppId()); + config.setSecret(appinfo.getSecret()); + if (null != appinfo.getAccessToken()) { + config.setAccessToken(appinfo.getAccessToken()); + config.setExpiresTime(appinfo.getLastTokenTime().getTime()); + } + WxMaService wxMaService = new WxMaServiceImpl(); + wxMaService.setWxMaConfig(config); + + String access_token = ""; + try{ + access_token = wxMaService.getAccessToken(); + } catch (Exception e) { + logger.error(e.getMessage()); + } + Date date = new Date(); + // update db + appinfo.setAccessToken(access_token); + appinfo.setLastTokenTime(date); + wxAppinfoMapper.updateByPrimaryKey(appinfo); + } + + private void downFile(String filePath, String filename, HttpServletResponse response, HttpServletRequest req) throws IOException { + try { + response.reset(); + response.setContentType("bin"); + String agent = req.getHeader("user-agent"); + if (agent.contains("Firefox")) { + response.setHeader("Content-disposition", + "attachment; filename=" + + new String(filename.getBytes("GB2312"),"ISO-8859-1")); + } else { + response.setHeader("Content-disposition", + "attachment; filename=" + + java.net.URLEncoder.encode(filename,"UTF-8")); + } + // 循环取出流中的数据 + byte[] b = new byte[1024]; + int len; + InputStream inStream = new FileInputStream(filePath); + while ((len = inStream.read(b)) > 0) + response.getOutputStream().write(b, 0, len); + inStream.close(); + } catch (Exception e) { + logger.error(e.getMessage()); + } + } + + @Override + public void exportQrcode(HttpServletRequest request, HttpServletResponse response, + String appId, int type, String pageUrl, String sceneParam, + int withText, String text1, String text2 ) { + // type 0 有限二维码, 1 无限制二维码 + // withText 0 不带字 1. 带下面的字,2 带下面的两行字 + WxAppinfo appinfo = wxAppinfoMapper.findByAppId(appId); + if (appinfo == null) { + logger.error("APPID Not found: ", appId); + return; + } + boolean autoColor = false; + boolean isHyaline = true; + WxMaCodeLineColor color = new WxMaCodeLineColor("0", "0", "0"); + + WxMaInMemoryConfig config = new WxMaInMemoryConfig(); + config.setAppid(appinfo.getAppId()); + config.setSecret(appinfo.getSecret()); + WxMaService wxMaService = new WxMaServiceImpl(); + wxMaService.setWxMaConfig(config); + + String pathStr = ""; + if (StringUtils.isNotBlank(sceneParam)) { + pathStr = pageUrl + "?scene="+sceneParam; + } else { + pathStr = pageUrl; + } + + + String filepath = Constant.fileDirectory; + String fileName = sceneParam + ".png"; + String destPath = filepath + fileName; + + File dest = new File(destPath); + File pDest = dest.getParentFile(); + if (!pDest.exists()) { + pDest.mkdirs(); + } + try { + Date imgDate = new Date(); + if(StringUtils.isBlank(sceneParam)) { + final File wxCode = wxMaService.getQrcodeService().createQrcode(pathStr, QRCodeUtils.QR_WIDTH); + if (type == 1) { + QRCodeUtils.graphicsGeneration(wxCode, dest, text1); + } else if (type == 2) { + QRCodeUtils.graphicsGeneration2(wxCode, dest, text1, text2); + } + wxCode.renameTo(dest); + } else { + final File wxCode = wxMaService.getQrcodeService().createWxaCodeUnlimit(sceneParam, pageUrl, QRCodeUtils.QR_WIDTH, autoColor, color, isHyaline); + if (type == 1) { + QRCodeUtils.graphicsGeneration(wxCode, dest, text1); + } else if (type == 2) { + QRCodeUtils.graphicsGeneration2(wxCode, dest, text1, text2); + } + wxCode.renameTo(dest); + } + + } catch (WxErrorException e) { + logger.error(e.getMessage()); + } catch (Exception e) { + logger.error(e.getMessage()); + } + + try { + downFile(filepath, fileName, response, request); + org.apache.commons.io.FileUtils.forceDelete(dest); + } catch (Exception e) { + logger.error(e.getMessage()); + } + + } } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java index e593e9787..b7688591f 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java @@ -25,6 +25,7 @@ import com.iformall.mapper.WxCUserMapper; import com.iformall.mapper.WxCUserTagsMapper; import com.iformall.mapper.WxTagsMapper; import com.iformall.service.WxCUserBasicInfoService; +import com.iformall.utils.Constant; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; @@ -140,7 +141,7 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { List memberlist = wxCUserBasicInfoMapper.findList(basicInfoQ); XSSFWorkbook workbook; - String filepath = "./uploads/"; + String filepath = Constant.fileDirectory; File savefile = new File(filepath); if (!savefile.exists()) { savefile.mkdirs(); @@ -283,7 +284,7 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { public void exportTemplate(HttpServletRequest request, HttpServletResponse response, String tenantId) { XSSFWorkbook workbook; - String filepath = "./uploads/"; + String filepath = Constant.fileDirectory; File savefile = new File(filepath); if (!savefile.exists()) { savefile.mkdirs(); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java index fb447311c..4be9a4218 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponOrderServiceImpl.java @@ -21,6 +21,7 @@ import com.iformall.enums.*; import com.iformall.exception.MallinkException; import com.iformall.mapper.*; import com.iformall.service.*; +import com.iformall.utils.Constant; import com.iformall.utils.DateUtils; import com.iformall.utils.MaUtil; import me.chanjar.weixin.common.error.WxErrorException; @@ -260,7 +261,7 @@ public class WxCouponOrderServiceImpl implements WxCouponOrderService { WxMall wxMall = new WxMall(); wxMall.setTenantId(tenantId); wxMall = wxMallMapper.findList(wxMall).get(0); - String filepath = "./uploads/"; + String filepath = Constant.fileDirectory; File savefile = new File(filepath); if (!savefile.exists()) { savefile.mkdirs(); diff --git a/mallinkService/src/main/java/com/iformall/utils/Constant.java b/mallinkService/src/main/java/com/iformall/utils/Constant.java new file mode 100644 index 000000000..11377db3d --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/utils/Constant.java @@ -0,0 +1,6 @@ +package com.iformall.utils; + +public class Constant { + + public static final String fileDirectory="./uploads/"; +} diff --git a/mallinkService/src/main/java/com/iformall/utils/QRCodeUtils.java b/mallinkService/src/main/java/com/iformall/utils/QRCodeUtils.java new file mode 100644 index 000000000..7ec0c38b3 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/utils/QRCodeUtils.java @@ -0,0 +1,139 @@ +package com.iformall.utils; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.font.FontRenderContext; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.io.File; + +/** + * Created by Stormeye on 2017/8/7. + */ +public class QRCodeUtils { + + public static String PAGE_URL = "pages/index/index"; + + public static final int QR_WIDTH = 430; + public static final int QR_HIGHT = 430; + public static final int QR_FONT_SIZE = 19; + public static final int QR_H_TIP = 90; + public static final int QR_H_TIP2 = 120; + + /** + * 二维码 + 文字1 + * @param oldimg + * @param newimg + * @param pressText + */ + public static void graphicsGeneration(File oldimg,File newimg,String pressText) { + int H_tip = QR_H_TIP; // 文字的高度 + int imageWidth = QR_WIDTH; // 图片的宽度 + int imageHeight = QR_HIGHT + QR_H_TIP; // 图片的高度 + + BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); + // 设置图片的背景色 + Graphics2D main = image.createGraphics(); + main.setColor(Color.white); + main.fillRect(0, 0, imageWidth, imageHeight); + + Graphics mainPic = image.getGraphics(); + BufferedImage bimg = null; + try { + bimg = ImageIO.read(oldimg); + if (bimg != null) { + mainPic.drawImage(bimg, 0, 0, QR_WIDTH, QR_HIGHT, null); + mainPic.dispose(); + } + + Graphics2D tip = image.createGraphics(); + // 设置区域颜色 + tip.setColor(Color.white); + // 填充区域并确定区域大小位置 + tip.fillRect(0, QR_HIGHT, QR_WIDTH, H_tip); + + // 设置字体颜色,先设置颜色,再填充内容 + tip.setColor(Color.black); + // 设置字体 + Font tipFont = new Font("宋体", Font.PLAIN, QR_FONT_SIZE); + tip.setFont(tipFont); + FontRenderContext context = tip.getFontRenderContext(); + Rectangle2D bounds = tipFont.getStringBounds(pressText, context); + double x = (QR_WIDTH - bounds.getWidth()) / 2; + tip.drawString(pressText, (int)x, QR_HIGHT + 50); + + // 分割线 + tip.drawLine(0, QR_HIGHT, QR_WIDTH, QR_HIGHT); + + //FileOutputStream out = new FileOutputStream(newimg); + ImageIO.write(image, "png", newimg); + //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); + //encoder.encode(image); + //out.close(); + + }catch (Exception e) { + e.printStackTrace(); + System.out.println(e); + } + + } + + /** + * 二维码 + 文字1 + 文字2 + * @param oldimg + * @param newimg + * @param pressText1 + * @param pressText2 + */ + public static void graphicsGeneration2(File oldimg,File newimg,String pressText1, String pressText2) { + int H_tip = QR_H_TIP2; // 文字的高度 + int imageWidth = QR_WIDTH; // 图片的宽度 + int imageHeight = QR_HIGHT + QR_H_TIP2; // 图片的高度 + + BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); + // 设置图片的背景色 + Graphics2D main = image.createGraphics(); + main.setColor(Color.white); + main.fillRect(0, 0, imageWidth, imageHeight); + + Graphics mainPic = image.getGraphics(); + BufferedImage bimg = null; + try { + bimg = ImageIO.read(oldimg); + if (bimg != null) { + mainPic.drawImage(bimg, 0, 0, QR_WIDTH, QR_HIGHT, null); + mainPic.dispose(); + } + + Graphics2D tip = image.createGraphics(); + // 设置区域颜色 + tip.setColor(Color.white); + // 填充区域并确定区域大小位置 + tip.fillRect(0, QR_HIGHT, QR_WIDTH, H_tip); + // 设置字体颜色,先设置颜色,再填充内容 + tip.setColor(Color.black); + + // 设置字体 + Font tipFont = new Font("粗体", Font.BOLD, QR_FONT_SIZE); + tip.setFont(tipFont); + FontRenderContext context = tip.getFontRenderContext(); + Rectangle2D bounds1 = tipFont.getStringBounds(pressText1, context); + double x = (QR_WIDTH - bounds1.getWidth()) / 2; + tip.drawString(pressText1, (int)x, QR_HIGHT + 50); + + Rectangle2D bounds2 = tipFont.getStringBounds(pressText2, context); + x = (QR_WIDTH - bounds2.getWidth()) / 2; + tip.drawString(pressText2, (int)x, QR_HIGHT + 90); + //FileOutputStream out = new FileOutputStream(newimg); + ImageIO.write(image, "png", newimg); + //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); + //encoder.encode(image); + //out.close(); + + }catch (Exception e) { + e.printStackTrace(); + System.out.println(e); + } + + } +}