利用weixin-java-miniapp生成小程序码并直接返回图片文件流

有时候我们可能需要在其他的网页上展示我们自己的小程序中某些页面的小程序码,这种时候,我们需要用到小程序的生成小程序码的相关接口。工具选型我们仍然选用简单方便的weixin-java-miniapp来完成此功能。项目配置详见我们的另一篇文章点此进入生成小程序码的相关类型小程序码的其他生成方式以及相关类型在这篇文章点此进入中介绍的较为详细,此处不再赘述,以下仅以生成不限制张数的这种类型来做一个示例。生成小程序码图片先获取小程序的service实例wxMaService。再获取二维码相关操作的service实例// 获取小程序服务实例WxMaService wxMaService = WxMaConfiguration.getWxMaService();// 获取小程序二维码生成实例WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService();// 设置小程序二维码线条颜色为黑色WxMaCodeLineColor lineColor = new WxMaCodeLineColor(“0”, “0”, “0”);// 生成二维码图片字节流(此处也可以生成File类型,如果想将图片文件保存到服务器就生成File类型,此处生成byte[]类型,方便直接返回文件流到前端)byte[] qrCodeBytes = null;qrCodeBytes = wxMaQrcodeService.createWxaCodeUnlimitBytes(String.valueOf(id), null, 430, false, lineColor, false);返回文件流将文件流写到response中,相关示例代码如下:@RestController@RequestMapping("/qrCode")public class QrCodeController { private static final Logger logger = LoggerFactory.getLogger(QrCodeController.class); @GetMapping("/getMiniappQrCode/{id}") public void getMiniappQrCode(@PathVariable(“id”) Long id, HttpServletRequest request, HttpServletResponse response) throws Exception{ // 获取小程序服务实例 WxMaService wxMaService = WxMaConfiguration.getWxMaService(); // 获取小程序二维码生成实例 WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService(); // 设置小程序二维码线条颜色为黑色 WxMaCodeLineColor lineColor = new WxMaCodeLineColor(“0”, “0”, “0”); // 生成二维码图片字节流 byte[] qrCodeBytes = null; try{ qrCodeBytes = wxMaQrcodeService.createWxaCodeUnlimitBytes(String.valueOf(id), null, 430, false, lineColor, false); } catch(Exception e){ logger.error(“生成小程序码出错”, e); } // 设置contentType response.setContentType(“image/png”); // 写入response的输出流中 OutputStream stream = response.getOutputStream(); stream.write(qrCodeBytes); stream.flush(); stream.close(); }} ...

March 28, 2019 · 1 min · jiezi

使用weixin-java-miniapp配置进行单个小程序的配置

在进行小程序后端接口开发方面,使用weixin-java-tools中的weixin-java-miniapp模块,往往可以事半功倍。引入weixin-java-tools在https://mvnrepository.com/中搜索weixin-java-miniapp,进入微信小程序 Java SDK这个项目中。选择相应正式版本来进行使用。maven中在依赖中添加如下配置项:<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>3.3.0</version></dependency>gradle中添加如下配置项:compile(“com.github.binarywang:weixin-java-miniapp:3.3.0”)注意:以上我用的版本是3.3.0,实际中根据你要使用的版本来用。配置文件配置文件中主要配置四项参数,分别是:appIdsecrettokenaesKey配置初始化:weixin-java-miniapp可以使用注解来进行配置,具体步骤如下:在config包中创建WxMaConfiguration类。使用@Configuration注解来进行小程序相关的参数配置,可参考以下代码。该代码示例中是单个小程序配置示例,如果需要配置多个小程序的参数,请参考官方案例点击进入。package com.diboot.miniapp.config;import cn.binarywang.wx.miniapp.api.WxMaService;import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;import dibo.framework.config.BaseConfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class WxMaConfiguration { // 此处获取配置的方式可以改成你自己的方式,也可以注解等方式获取配置等。 private static final String appId = BaseConfig.getProperty(“wechat.appId”); private static final String secret = BaseConfig.getProperty(“wechat.secret”); private static final String token = BaseConfig.getProperty(“wechat.token”); private static final String aesKey = BaseConfig.getProperty(“wechat.aesKey”); private static WxMaService wxMaService = null; @Bean public Object services(){ WxMaInMemoryConfig config = new WxMaInMemoryConfig(); config.setAppid(appId); config.setSecret(secret); config.setToken(token); config.setAesKey(aesKey); wxMaService = new WxMaServiceImpl(); wxMaService.setWxMaConfig(config); return Boolean.TRUE; } public static WxMaService getWxMaService(){ return wxMaService; }}开始使用在需要使用小程序相关接口的地方,只需要通过该配置类中的静态方法getWxMaService()来获取到wxMaService即可开始使用,如: // 获取小程序服务实例WxMaService wxMaService = WxMaConfiguration.getWxMaService();// 获取小程序二维码生成实例WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService();// 便可以开始使用wxMaQrcodeService来进行二维码相关的处理了…. ...

March 28, 2019 · 1 min · jiezi