通过SpringBoot生成微信小程序二维码,跳转指定页面

以下通过两种方法实现生成微信小程序二维码保存

 通过华为存储obs服务

通过(IO流:字符流的使用、读取字符流、字符流写入)

微信小程序获取二维码参数

onLoad: function (options) {
	console.log(options);
   //方式二 微信官方提供了这个这个扫码是使用微信那边普通网址链接
	if (options.mid) { 
		//  解析二维码中地址中的参数   mid为二维码中地址带的参数名 如login/login?mid=xxx
        console.log(decodeURIComponent(options.mid));
	}
   //接受到参数 方式二
    //不解析也可以获取到二维码参数
	console.log(options.mid);
}

源码

@PostMapping("save")
    public AjaxJson save(WechatCode wechatCode, Model model) throws Exception {
     
//通过该链接请求携带自己的APPID +SECRET 获取access_token
 HttpResponse response = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WechatUtil.APPID + "&secret=" + WechatUtil.SECRET + "").execute();
        JSONObject tokenJson = JSON.parseObject(response.body());
        if (!tokenJson.containsKey("access_token")) {
            return AjaxJson.error("access_token为空");
        }
//得到的accessToken 
        String accessToken = tokenJson.get("access_token").toString();
        String urls = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;
        Map<String, Object> param = new HashMap<>();
        //mid:页面携带的参数mid=" + 生成的参数
        param.put("path", "pages/login/login?mid=" + wechatCode.getM().getId());
        param.put("width", 430);
        param.put("auto_color", false);
        Map<String, Object> line_color = new HashMap<>();
        line_color.put("r", 0);
        line_color.put("g", 0);
        line_color.put("b", 0);
        param.put("line_color", line_color);
        try {
            URL url = new URL(urls);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
            httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            printWriter.write(JSON.toJSONString(param));
            // flush输出流的缓冲
            printWriter.flush();
            
             //HuaweiObsUtil使用华为存储bos(自行封装)
            String name = System.currentTimeMillis() + ".png";
            InputStream inputStream=httpURLConnection.getInputStream();
            String path=HuaweiObsUtil.upload(inputStream, name);
            log.info("存储服务器返回的路径:{},",path)

         //不使用云存储通过字符流写入到磁盘

            /*
* 
* BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File("二维码生成地址"));
            int len;
            //设置缓冲写入
            byte[] arr = new byte[2048];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
* */


          
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.error("操作异常"+e);
        }

   return AjaxJson.success("保存成功");
    }

华为云OBS对象存储工具类


import com.obs.services.ObsClient;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 华为云OBS对象存储工具类
 */
public class HuaweiObsUtil {
/* 初始化OBS客户端所需的参数 */

    //获取服务终端节点
    private static final String Endpoint = "";
   //域名的一个访问
    private static final String URL = "";  
    //创建桶的名称
    private static final String bucketName = ""; 
    //Obs的访问秘钥【ak,sk】
    private static final String AK = ""; 
    private static final String SK = ""; 

    //File 
    public static String upload(File file, String filename) {
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(AK, SK, Endpoint);

        // localfile为待上传的本地文件路径,需要指定到具体的文件名
        obsClient.putObject(bucketName, filename, file);
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return URL + filename;
    }
    //InputStream 
    public static String upload(InputStream fis, String filename) {
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(AK, SK, Endpoint);

        // 待上传的本地文件路径,需要指定到具体的文件名
        obsClient.putObject(bucketName, filename, fis);
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return URL + filename;
    }
<dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>esdk-obs-java</artifactId>
	<version>3.20.6.1</version>
</dependency>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值