收藏
回答

获取小程序码base64后无法显示,请给出方法或思路,别让大家在这里瞎折腾好吗?

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后图片并不能显示出来,

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

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

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

最后一次编辑于  2018-06-07  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏关注问题回答

6 个回答

  • 阳光H
    阳光H
    2018-06-07

    //小程序二维码生成

    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();

        }


    2018-06-07
    赞同 11
    回复 2
    • 风云
      风云
      2018-06-08
      public class MyX509TrustManager implements X509TrustManager {
          /*
           * The default X509TrustManager returned by SunX509.  We'll delegate
           * decisions to it, and fall back to the logic in this class if the
           * default X509TrustManager doesn't trust it.
           */
          X509TrustManager sunJSSEX509TrustManager;
          MyX509TrustManager() throws Exception {
              // create a "default" JSSE X509TrustManager.
              KeyStore ks = KeyStore.getInstance("JKS");
              ks.load(new FileInputStream("trustedCerts"),
                  "passphrase".toCharArray());
              TrustManagerFactory tmf =
              TrustManagerFactory.getInstance("SunX509", "SunJSSE");
              tmf.init(ks);

      这里这个文件"trustedCerts"如何获得?

      或者我如何生成?谢谢啦哈~~~

      2018-06-08
      赞同
      回复
    • 风云
      风云
      2018-06-09回复风云

      发现其实不需要这么费劲,还是wx的文档没有说的清楚,摸索的过程最痛苦,但收获也最大


      谢谢你,朋友~~~

      2018-06-09
      赞同
      回复
  • 风云
    风云
    2018-06-07

    谢谢阳光H

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

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

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

    2018-06-07
    赞同
    回复
  • 大治
    大治
    2018-11-12

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

    2018-11-12
    赞同
    回复 1
    • 风云
      风云
      03-15

      微信不提倡在小程序端获取小程序码,应该在服务器端获取小程序码后在小程序端进行显示~

      03-15
      赞同
      回复