java实现⽣成⼩程序码
流程:
1、导⼊依赖
2、在properties⽂件中配置appId、secret参数
3、获取配置⽂件中的参数到实例类
4、配置相关参数⾄WxMaService中
5、书写Service,逻辑代码编写
6、书写Controller,并完成测试
//代码演⽰
1、导⼊依赖
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.6.0</version>
</dependency>
2、在properties⽂件中配置appId、secret参数
/*⼩程序相关参数*/
miniapp.appid=
miniapp.secret=
3、获取配置⽂件中的参数到实体类
import lombok.Data;
import org.t.properties.ConfigurationProperties;
/**
* 相关配置信息
* @author: create by luotj
* date: 2020-03-23 16:46
**/
@ConfigurationProperties(prefix = "miniapp")
@Data
public class WxMaProperties {
/
*⼩程序app_id*/
private String appid;
/*⼩程序app_secret*/
private String secret;
}
4、配置相关参数⾄WxMaService中
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.fig.WxMaConfig;
import cn.binarywang.fig.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dition.ConditionalOnClass; import org.springframework.dition.ConditionalOnMissingBean; import org.t.properties.EnableConfigurationProperties; import t.annotation.Bean;
import t.annotation.Configuration;
/**
* ⼩程序配置⽂件
* @author: create by luotj
* date: 2020-03-23 16:42
**/
@Configuration
@ConditionalOnClass(WxMaService.class)
@EnableConfigurationProperties(WxMaProperties.class)
public class WeChatMaConfig {
/*注⼊⼩程序相关配置*/
@Autowired
private WxMaProperties properties;
/**
* 配置默认参数
*/
@Bean
@ConditionalOnMissingBean
public WxMaConfig wxMaConfig(){
WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl(); //设置默认参数-appid,secret
wxMaDefaultConfig.setAppid(Appid());
wxMaDefaultConfig.setSecret(Secret());
return wxMaDefaultConfig;
}
/**
* 配置WxMaService
*/
@Bean
@ConditionalOnMissingBean
public WxMaService wxMaService(WxMaConfig wxMaConfig){
WxMaService wxMaService = new WxMaServiceImpl();
wxMaService.setWxMaConfig(wxMaConfig);
return wxMaService;
}
}
5、书写Service,逻辑代码编写
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
/**
* @author: create by luotj
* date: 2020-03-23 16:34
**/
@Slf4j
@Service
public class WeChatServiceImpl implements WeChatService {
@Autowired
private WxMaService wxMaService;
/**
* 该实例获取的是不限制次数的⼩程序码
*
* @param response
*/
@Override
public void queryWeChatCode(HttpServletResponse response) {
OutputStream outputStream = null;
try {
WxMaQrcodeService wxMaQrcodeService = QrcodeService(); /* 获取⼆维码字节数组 */
入门的java游戏小程序byte[] bytes = ateWxaCodeUnlimitBytes
("hello,test", "", 100, true, new WxMaCodeLineColor("0", "0", "0"), false); /* 设置返回⽂件类型,不设置前端识别不了 */
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
outputStream = OutputStream();
outputStream.write(bytes);
outputStream.flush();
} catch (Exception ex) {
log.info("⽣成⼩程序码失败,message:{}", ex.getMessage());
} finally {
try {
outputStream.close();
} catch (Exception ex) {
<("关闭IO流失败,msg:{}", ex.getMessage());
}
}
}
}
6、书写Controller,并完成测试
package com.mine.ller.api.wechat.ma;
import com.mine.luotj.Service.wechat.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
/**
* ⼩程序相关接⼝测试
* @author: create by luotj
* date: 2020-03-23 16:27
**/
@RestController
@RequestMapping("/api/wechat")
@Api(tags = "03. 相关接⼝")
public class WeChatController {
@Autowired
private WeChatService wechatService;
@ApiOperation("获取⼩程序码")
@GetMapping("/code")
@ResponseBody
public void queryWeChatCode(HttpServletResponse response){ wechatService.queryWeChatCode(response);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论