获取小程序码base64后无法显示,请给出方法或思路,别让大家在这里瞎折腾好吗?
发布于 6 年前 作者 hufang 14757 次浏览 来自 问答
wx.request({ //获取小程序码
  url: "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + getApp().globalData.access_token, //获取小程序码
  header: {
    'content-type': 'application/json'
  },
  data:{
    scene: json.content,
    page:"pages/news/detial",
    width:430
  },
  method:"POST",
  success: function (res) {
    console.log(res);
    var json = res.data;
    console.log("json.length=" + json.length);
    console.log("content length=" + res.header['Content-Length']);
    var a =new Uint8Array(json.length)
    for(var i=0;i<json.length;++i){
      a[i]=json.charCodeAt(i);
    }
    var b = wx.arrayBufferToBase64(a);
    console.log(b.substring(0,100))
    that.setData({ img: b })
    wx.hideNavigationBarLoading();
  }
})

console.log返回的结果:

{data: “����”, header: {…}, statusCode: 200, errMsg: “request:ok”}

json.length=91729

content length=96702

/f39/QAQSkZJRgABAQAAAQABAAD9/QBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB

可以看出:

res.data是字符串格式的图片数据,然而转换成base64后图片并不能显示出来,

请问微信的技术人员,该如何显示潘慧的小程序码图片呢?

如果现在显示不出来,能不能说一声,避免大家浪费数据在这里瞎测试,

如果能显示,请给出方法或思路,别让大家在这里瞎折腾好吗????

3 回复

楼主你好, 我是在java后台获取小程序二维码的, 也是base64编码, 跟你一样也是显示不出来.请问你是怎么解决的

//小程序二维码生成

private void addxiaoweima(int id) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {

JSONObject json = new JSONObject();

 

Map<String, String> params = new HashMap<String, String>();

params.put(“appid”, “12121”);

params.put(“secret”, “1212121”);

params.put(“grant_type”, “client_credential”);

String res = HttpKit.get(“https://api.weixin.qq.com/cgi-bin/token”,

params);

json = JSONObject.parseObject(res);

String access_token = json.getString(“access_token”);

// 柜子列表

//List<Tbl_cabinet> list = findall();

/*if (list != null) {

// 遍历列表

for (int i = 0; i < list.size(); i++) {

id = list.get(i).getIdtbl_cabinet();

*/ params.clear();

params.put(“path”, “pages/auth/index?cid=” + id);

params.put(“width”, “430”);

String post = JSONObject.toJSONString(params);

res = HttpKit.postAndSaveImg(

https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=

+ access_token, post, “/eop/upload/”, id + “.png”);

/*

}

}*/

}

public static String postAndSaveImg(String url, String params,String path,String imgName) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {

        TrustManager[] tm = { new MyX509TrustManager() };

        SSLContext sslContext = SSLContext.getInstance(“SSL”, “SunJSSE”);

        sslContext.init(null, tm, new java.security.SecureRandom());

        // 从上述SSLContext对象中得到SSLSocketFactory对象  

        SSLSocketFactory ssf = sslContext.getSocketFactory();

        URL urlGet = new URL(url);

        HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();

        // 连接超时

        http.setConnectTimeout(25000);

        // 读取超时 --服务器响应比较慢,增大时间

        http.setReadTimeout(25000);

        http.setRequestMethod(“POST”);

        http.setRequestProperty(“Content-Type”,“application/x-www-form-urlencoded”);

        http.setSSLSocketFactory(ssf);

        http.setDoOutput(true);

        http.setDoInput(true);

        http.connect();

        OutputStream out = http.getOutputStream();

        out.write(params.getBytes(“UTF-8”));

        out.flush();

        out.close();

        InputStream in = http.getInputStream();

 

        Integer status = saveToImgByInputStream(in, path, imgName);  

 

        in.close();

        if (http != null) {

            // 关闭连接

            http.disconnect();

        }

        return status.toString();

    }

谢谢阳光H

你是说在服务器端编程吗?

先不说安全性问题,就是我上面代码的思路,是哪里错了,造成图片显示不出来呢?

你的回复我再慢慢消化一下,再次表示感谢?

回到顶部