Skip to content

公众号二维码

csharp
 async Task<string> CreateQrCodeAsync()
 {
     string appId = "xxx";
     var options = wechat.UseAppId(appId);
     var token = await wechatTokenManager.GetAccessTokenAsync(options.AppId);

     var scene = $"wxlogin:{Context.ConnectionId}";
     var wxQrcodeRequest = new WxQrCodeRequest(token, scene, 300);
     var qrcodeResult = await wechat.SendAsync<WxQrCodeResponse>(wxQrcodeRequest);
     return JsonSerializer.Serialize(qrcodeResult);
 }

使用https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket= 拼接 ticket值

小程序二维码

csharp
public async Task<object>GetMiniPargramQrcodeAsync(string scene,string page)
{

    var options = wechatOptions.FirstOrDefault();
    wechat.UseAppId(options.AppId);
    var tokenInfo = await wechat.SendAsync<AppAccessTokenResponse>(new AppAccessTokenRequest(options.AppId, options.AppSecret));

    var res = await wechat.SendAsync(new GetUnlimitedRequest()
                                     {
                                         AccessToken = tokenInfo.AccessToken,
                                         Scene = scene,
                                         Width = 280,
                                         Page = page,
                                     });
    if (res.StatusCode == System.Net.HttpStatusCode.OK)
    {
        byte[] data = await res.Content.ReadAsByteArrayAsync();
        FileContentResult img = new FileContentResult(data, "images/jpeg");
        img.FileDownloadName = "qrcode.jpg";
        return img;
    }
    else
    {
        using (var responseStream = await res.Content.ReadAsStreamAsync())
        {
            return await JsonSerializer.DeserializeAsync
                <GetUnlimitedResponse>(responseStream);
        }
    }
}

signalr接收收小程序生成的二维码

js
vm.qrcode = "data:image/png;base64," + JSON.parse(ret).FileContents;