l =
"https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+
"&redirect_uri="
+ reurl +
"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"
;
Response.Redirect(url);
}
else
{
url =
"https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid +
"&redirect_uri="
+ reurl +
"&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
;
//不弹出授权页面
Response.Redirect(url);
}
}
}
}
///需跳转页面
public
partial
class
Index : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
string
code =
""
;
if
(Request.QueryString[
"code"
] !=
null
&& Request.QueryString[
"code"
] !=
""
)
{
//获取微信回传的code
code = Request.QueryString[
"code"
].ToString();
string
openid = WeiXinCommom.Get_token(code);
//获取token
if
(!
string
.IsNullOrWhiteSpace(openid)) {
WeiXinService WeiXinService =
new
WeiXinService();
Entity UserInfo= WeixinDA.GetShowUserWeiXinInfo(openid);
//获取用户信息
LogHelper.WriteFileLog(
"UserInfo"
, UserInfo.IsNull.ToString());
bool
SaveUser =
true
;
if
(UserInfo.IsNull) {
//如果不存在用户信息,用户已经关注了公众号,但是未注册到用户数据,重新注册
int
CareUsCode = 11000;
//关注公众号赠送积分的codeid
Entity CareUsData = WeixinDA.GetCategoryCodesByCodeID(CareUsCode);
int
CareUsStatus = CareUsData.GetValue(
"Status"
).TryInt32();
SaveUser= WeiXinService.SaveUserInfo(openid);
//返回fasle,用户未关注公众号
if
(SaveUser) {
if
(CareUsStatus == (
int
)BaseEnum.OnOff.On)
//积分规则打开
{
decimal
Integration = CareUsData.GetValue(
"Value"
).TryDecimal();
WeiXinService.AddOpenIDIntegration(openid, (
int
)BaseEnum.IntegrationType.CareUs, Integration);
//第一次关注公众号赠送的积分
}
}
}
if
(SaveUser)
{
string
WeiXinInfoString = WeiXinService.GetShowUserWeiXinInfoHadTokenString(openid);
ClientScript.RegisterStartupScript(ClientScript.GetType(),
"orderList"
,
"<script>UserProfile.GetWeixinCode('"
+ WeiXinInfoString +
"');</script>"
);
}
else
{
string
WeiXinInfoString = WeiXinService.GetShowUserWeiXinInfoHadTokenString(openid);
ClientScript.RegisterStartupScript(ClientScript.GetType(),
"orderList"
,
"<script>UserProfile.GetWeixinCode('');</script>"
);
}
}
}
}
}
//根据appid,secret,code获取微信openid、access token信息
public
static
string
Get_token(
string
Code)
{
//获取微信回传的openid、access token
string
WeixinData = WeiXinCommom.GetData(
"https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+ appid +
"&secret="
+ appsecret +
"&code="
+ Code +
"&grant_type=authorization_code"
);
string
openid =
string
.IsNullOrWhiteSpace(WeixinData) ?
string
.Empty : JObject.Parse(WeixinData)[
"openid"
].TryString();
return
openid;
}
上面的这些代码就可以实现授权页面了
关于本站 | 网站地图 | 手机版 | XML地图 | 反馈 | 版权申明 皖ICP备13006370号-1