需求:生成一个商品详细页面的二维码(带分享者的id参数),别人扫码后到达指定商品页面,并获取参数中的分享者id。
服务器端使用.net调用微信的api接口,使用的代码如下
string postUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string options = "{\"scene\":\"" + scene + "\",\"page\":\"pages/index/index\",\"width\":\"1024\"}";
byte[] payload = Encoding.UTF8.GetBytes(options);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();
List<byte> bytes = new List<byte>();
int temp = stream.ReadByte();
while (temp != -1)
{
bytes.Add((byte)temp);
temp = stream.ReadByte();
}
byte[] result = bytes.ToArray();
string base64 = Convert.ToBase64String(result);//将byte[]转为base64
return "{\"errcode\":0,\"errmsg\":\"获取成功\",\"buffer\":\"" + base64 + "\"}";
========================
接着在微信小程序端用以下代码
onLoad: function (options) {
var that=this;
var para = {
action: 'wx_ewm',
scene: '',
}
wx.request({
url: app.config.hostUrl + 'wx/config.aspx',
data: para,
success: function (res) {
var base64 = res.data.buffer.replace(/[\r\n]/g, "");
that.setData({
ewm_url: "data:image/png;base64," + base64
})
},
fail: function () {
wx.hideToast();
console.log('error!!!!!!!!!!!!!!')
}
})
},
在wxml用<image src="{{ewm_url}}" width="300" height="300"></image>显示不出图片
1、是哪里出了问题吗?
2、在模拟器、真机调试都无法显示出图片。
请教大家。谢谢