using System.IO; using System.Net.Http; using PEIS.Common.Helper.Request; using PEIS.Common.Helper.Response; using PEIS.Common.Helper.WeChat.Models; using PEIS.Common.Helper.WeChat.Response; namespace PEIS.Common.Helper.WeChat { public class WeChat { /// /// 小程序通过code获取openId /// /// /// /// /// 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 RequestHelper.GetString(url).Result; // var client = new HttpClient(); // var response = await client.GetAsync(uri); // response.EnsureSuccessStatusCode(); // return await response.Content.ReadAsStringAsync(); } /// /// 获取token /// /// /// /// 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 RequestHelper.GetEntity(url).Result; } /// /// 微信支付码 /// /// /// /// public static string QrCodePay(string httpIp,FormUrlEncodedContent weChatPay) { return RequestHelper.PostFormDataEntity($"{httpIp}/wx/pay/qrCodePay.do",weChatPay).Result.Data.ToString(); } /// /// 小程序码 /// /// /// /// public static Stream UnlimitedQrCode(string token,UnlimitedQrCodeModel unlimitedQrCode) { return RequestHelper .PostFileStream( $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={token}", unlimitedQrCode).Result; } /// /// 一次性订阅消息 /// /// /// /// public static WeChatResponse SubscribeMessage(string token,SubscribeMessageModel subscribeMessage) { return RequestHelper .PostEntity( $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={token}", subscribeMessage).Result; } } }