C#公共类
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.0 KiB

using Common.Helper.Response;
using Common.Helper.WeChat.Models;
using Common.Helper.WeChat.Response;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Common.Helper.WeChat
{
public class WeChat
{
/// <summary>
/// 小程序通过code获取openId
/// </summary>
/// <param name="appId"></param>
/// <param name="secret"></param>
/// <param name="code"></param>
/// <returns></returns>
public static string GetOpenId(string appId, string secret, string code)
{
var url = $"https://api.weixin.qq.com/sns/jscode2session?appid={appId}&secret={secret}&js_code={code}&grant_type=authorization_code";
return Request.RequestHelper.GetString(url).Result;
// var client = new HttpClient();
// var response = await client.GetAsync(uri);
// response.EnsureSuccessStatusCode();
// return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// 获取token
/// </summary>
/// <param name="appId"></param>
/// <param name="secret"></param>
/// <returns></returns>
public static TokenModel GetToken(string appId, string secret)
{
var url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appId}&secret={secret}";
return Request.RequestHelper.GetEntity<TokenModel>(url).Result;
}
/// <summary>
/// 微信支付码
/// </summary>
/// <param name="httpIp"></param>
/// <param name="weChatPay"></param>
/// <returns></returns>
public static string QrCodePay(string httpIp,FormUrlEncodedContent weChatPay)
{
return Request.RequestHelper.PostFormDataEntity<ResultModel>($"{httpIp}/wx/pay/qrCodePay.do",weChatPay).Result.Data.ToString();
}
/// <summary>
/// 小程序码
/// </summary>
/// <param name="token"></param>
/// <param name="unlimitedQrCode"></param>
/// <returns></returns>
public static Stream UnlimitedQrCode(string token,UnlimitedQrCodeModel unlimitedQrCode)
{
return Request.RequestHelper
.PostFileStream<UnlimitedQrCodeModel>(
$"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={token}", unlimitedQrCode).Result;
}
/// <summary>
/// 一次性订阅消息
/// </summary>
/// <param name="token"></param>
/// <param name="subscribeMessage"></param>
/// <returns></returns>
public static WeChatResponse SubscribeMessage(string token,SubscribeMessageModel subscribeMessage)
{
return Request.RequestHelper
.PostEntity<WeChatResponse, SubscribeMessageModel>(
$"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={token}", subscribeMessage).Result;
}
}
}