微信小程序获取二维码接口整理,.Net Core后台获取小程序二维码

一、关于二维码接口说明

参考:https://my.oschina.net/tianma3798/blog/1811307

获取二维码分两步:

1.获取Accesstoken

2.根据token和二维码参数后台请求,返回二维码图片

二、后台获取二维码示例

1.前台整合二维码参数,请求下载二维码图片

var link = url.getMini('getqrcode');
//整合二维码参数
var param = {
  scene: id,
  page: page,
  width: 300,
  auto_color: false,
  is_hyaline: false
};
//下载图片到本地
wx.downloadFile({
  url: link + '?param=' + JSON.stringify(param),
  success: res => {
    console.info(res);
    if (onSuccess)
      onSuccess(res.tempFilePath);
  }
});

下载成功后显示

  _this.setData({
    tempPath: data
  });

wxml

<image src='{{tempPath}}' mode='widthFix'></image>

2.后台获取二维码

#region 获取小程序二维码
/// <summary>
/// 获取小程序二维码
/// </summary>
/// <param name="param">指定二维码参数</param>
/// <returns></returns>
[HttpGet("getqrcode")]
public async Task<IActionResult> GetQrCode(string param)
{
    //1.指定token获取连接
    string link = new LinkManage().GetQrCode();
    //2.获取二维码
    HttpClient client = new HttpClient();
    byte[] bytes = Encoding.UTF8.GetBytes(param); //注:参数格式必须UTF8
    HttpContent data = new ByteArrayContent(bytes);
    HttpResponseMessage resp = await client.PostAsync(link, data);
    if (resp.StatusCode == HttpStatusCode.OK)
    {
        byte[] dataT = await resp.Content.ReadAsByteArrayAsync();
        //3.处理返回提前台image图片
        FileContentResult img = new FileContentResult(dataT, "images/jpeg");
        img.FileDownloadName = "qrcode.jpg";
        return img;
    }
    else
    {
        string msg = await resp.Content.ReadAsStringAsync();
        throw new Exception(msg);
    }
}
#endregion

更多:

微信小程序获取二维码接口整理,前台获取二维码

微信小程序image图片实现高度自适应

微信小程序下拉刷新使用整理

猜你喜欢

转载自my.oschina.net/tianma3798/blog/1811347