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 { /// /// 小程序通过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 Request.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 Request.RequestHelper.GetEntity(url).Result; } /// /// 微信支付码 /// /// /// /// public static string QrCodePay(string httpIp,FormUrlEncodedContent weChatPay) { return Request.RequestHelper.PostFormDataEntity($"{httpIp}/wx/pay/qrCodePay.do",weChatPay).Result.Data.ToString(); } /// /// 小程序码 /// /// /// /// public static Stream UnlimitedQrCode(string token,UnlimitedQrCodeModel unlimitedQrCode) { return Request.RequestHelper .PostFileStream( $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={token}", unlimitedQrCode).Result; } /// /// 一次性订阅消息 /// /// /// /// public static WeChatResponse SubscribeMessage(string token,SubscribeMessageModel subscribeMessage) { return Request.RequestHelper .PostEntity( $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={token}", subscribeMessage).Result; } } }