java实现生成微信小程序码

接口文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
效果图:
java实现生成微信小程序码_第1张图片


 

流程:

1、导入依赖
2、在properties文件中配置appId、secret参数
3、获取配置文件中的参数到实例类
4、配置相关参数至WxMaService中
5、书写Service,逻辑代码编写
6、书写Controller,并完成测试

//代码演示
1、导入依赖

        
            com.github.binarywang
            weixin-java-miniapp
            3.6.0
        

2、在properties文件中配置appId、secret参数

/*微信小程序相关参数*/
miniapp.appid=
miniapp.secret=

java实现生成微信小程序码_第2张图片

3、获取配置文件中的参数到实体类

package com.mine.luotj.config.wechat.ma;

import lombok.Data;
import org.springframework.boot.context.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中

package com.mine.luotj.config.wechat.ma;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.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(this.properties.getAppid());
        wxMaDefaultConfig.setSecret(this.properties.getSecret());
        return wxMaDefaultConfig;
    }

    /**
     * 配置WxMaService
     */
    @Bean
    @ConditionalOnMissingBean
    public WxMaService wxMaService(WxMaConfig wxMaConfig){
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaConfig);
        return wxMaService;
    }


}


5、书写Service,逻辑代码编写

package com.mine.luotj.Service.wechat;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import lombok.extern.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 = wxMaService.getQrcodeService();
            /* 获取二维码字节数组 */
            byte[] bytes = wxMaQrcodeService.createWxaCodeUnlimitBytes
                    ("hello,test", "", 100, true, new WxMaCodeLineColor("0", "0", "0"), false);
            /* 设置返回文件类型,不设置前端识别不了 */
            response.setContentType(MediaType.IMAGE_JPEG_VALUE);
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
        } catch (Exception ex) {
            log.info("生成小程序码失败,message:{}", ex.getMessage());
        } finally {
            try {
                outputStream.close();
            } catch (Exception ex) {
                log.error("关闭IO流失败,msg:{}", ex.getMessage());
            }
        }
    }
}


6、书写Controller,并完成测试

package com.mine.luotj.ui.controller.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);
    }
}


 

你可能感兴趣的:(java,微信小程序码)