欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > Java生成微信小程序码及小程序短链接

Java生成微信小程序码及小程序短链接

2025/5/5 7:37:03 来源:https://blog.csdn.net/zf14840/article/details/147595813  浏览:    关键词:Java生成微信小程序码及小程序短链接

使用wx-java-miniapp-spring-boot-starter 生成微信小程序码及小程序短链接

  1. 在pom.xml文件中引入依赖
		<dependency><groupId>com.github.binarywang</groupId><artifactId>wx-java-miniapp-spring-boot-starter</artifactId><version>4.7.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
  1. application.yml中配置小程序参数
spring:application:name: wx-posterdata:redis:host: 192.168.1.111port: 6379password: 123456database: 0wx:miniapp:configs:scm:appid: wx1234567890abcdefsecret: test-567890abcdef1234567890abcdefmsgDataFormat: JSONmall:appid: wxe8db97a0603abcdesecret: test-35f85ebb2135bdcd9e154de12345msgDataFormat: JSON
  1. 加载配置及实例化服务类
@Data
@Configuration
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxMaProperties {private Map<String, Config> configs;@Datapublic static class Config {private String appid;private String secret;private String msgDataFormat;}
}@Configuration
public class WxMaConfiguration {@Autowiredprivate WxMaProperties properties;@Autowiredprivate StringRedisTemplate redisTemplate;@Beanpublic Map<String, WxMaConfig> wxMaConfigs() {Map<String, WxMaConfig> configMap = new HashMap<>();WxRedisOps wxRedisOps = new RedisTemplateWxRedisOps(redisTemplate);properties.getConfigs().forEach((key, config) -> {WxMaRedisBetterConfigImpl maConfig = new WxMaRedisBetterConfigImpl(wxRedisOps, "wechat");maConfig.setAppid(config.getAppid());maConfig.setSecret(config.getSecret());maConfig.setMsgDataFormat(config.getMsgDataFormat());configMap.put(key, maConfig);});return configMap;}@Beanpublic WxMaService wxMaService() {WxMaService service = new WxMaServiceImpl();service.setMultiConfigs(wxMaConfigs());return service;}
}
  1. 根据微信小程序标识及页面路径,生成小程序码
@Slf4j
@Service
public class WxMaManagerService {@Autowiredprivate WxMaService wxMaService;/*** 生成小程序码*/@SneakyThrowspublic byte[] createWxaCode(WechatGenerateQrCodeParam param) {WxMaService service = wxMaService.switchoverTo(param.getAppKey());try {return service.getQrcodeService().createWxaCodeUnlimitBytes(param.getScene(), param.getPage(), param.getCheckPath(), param.getEnvVersion(),param.getWidth(), param.getAutoColor(), param.getLineColor(), param.getIsHyaline());} catch (Exception e) {if (isTokenInvalid(e)) {// 如果token无效,刷新一次后重试service.getWxMaConfig().expireAccessToken();return service.getQrcodeService().createWxaCodeUnlimitBytes(param.getScene(), param.getPage(), param.getCheckPath(), param.getEnvVersion(),param.getWidth(), param.getAutoColor(), param.getLineColor(), param.getIsHyaline());}throw new RuntimeException("生成小程序码失败", e);}}/*** 生成短链接*/@SneakyThrowspublic String createShortLink(WechatGenerateShortLinkParam param) {WxMaService service = wxMaService.switchoverTo(param.getAppKey());// 短期有效GenerateShortLinkRequest shortLinkRequest = GenerateShortLinkRequest.builder().pageUrl(param.getPageUrl()).isPermanent(false).build();try {return service.getLinkService().generateShortLink(shortLinkRequest);} catch (Exception e) {if (isTokenInvalid(e)) {// 如果token无效,刷新一次后重试service.getWxMaConfig().expireAccessToken();return service.getLinkService().generateShortLink(shortLinkRequest);}throw new RuntimeException("生成短链接失败", e);}}/*** 判断是否是token无效的错误*/private boolean isTokenInvalid(Exception e) {String errorMsg = e.getMessage();return StringUtils.containsIgnoreCase(errorMsg, "access_token")&& (StringUtils.containsIgnoreCase(errorMsg, "invalid")|| StringUtils.containsIgnoreCase(errorMsg, "expired"));}
}

完整代码地址 https://gitee.com/galen.zhang/wx-poster

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词