|
|
using BWP.B3WeChat.WeiChatObjs;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
using System.Web;
|
|
|
using System.Web.Script.Serialization;
|
|
|
|
|
|
namespace BWP.B3WeChat.Utils
|
|
|
{
|
|
|
public static class WeChatPageUtil
|
|
|
{
|
|
|
const string template = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={APPID}&redirect_uri={REDIRECT_URI}&response_type=code&scope=snsapi_base&state={STATE}#wechat_redirect";
|
|
|
public static string GetWeChatUrl(string url,string state="1")
|
|
|
{
|
|
|
|
|
|
var config = new B3WeChatConfig();
|
|
|
var result = template.Replace("{APPID}", config.AppID.Value)
|
|
|
.Replace("{REDIRECT_URI}", HttpUtility.UrlEncode(url))
|
|
|
.Replace("{STATE}", state);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
const string getAccessTokenTemplate = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={APPID}&secret={SECRET}&code={CODE}&grant_type=authorization_code";
|
|
|
public static string QueryOpenID(string code)
|
|
|
{
|
|
|
var config = new B3WeChatConfig();
|
|
|
var url = getAccessTokenTemplate.Replace("{APPID}", config.AppID.Value)
|
|
|
.Replace("{SECRET}", config.AppSecret.Value)
|
|
|
.Replace("{CODE}", code);
|
|
|
|
|
|
var json = new WebClient() { Encoding = Encoding.UTF8 }.DownloadString(url);
|
|
|
|
|
|
var response = AuthorizationCodeResponse.Parse(json);
|
|
|
return response.openid;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|