Case "hello.htm"
PopMessage(Functions.Execute("getopenid",e,"hello.htm")) ‘ 这样PopMessage 出来的也是空的、
内部函数,
Dim e As RequestEventArgs = Args(0)
Dim url As String = args(1)
Dim wb As New weui
Dim OpenID As String
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.GetValues.ContainsKey("code") Then '如果通过授权链接跳转而来,就根据传递过来的code参数调用接口,获取用户的OpenID
Dim ul As String = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
ul = CExp(ul,"AppID","AppSecret",e.GetValues("code"))
Dim hc As new HttpClient(ul)
Dim jo As JObject = JObject.Parse(hc.GetData)
If jo("openid") IsNot Nothing Then
OpenID = jo("openid")
e.AppendCookie("openid",OpenID) '将openid存储在Cookie中
End If
Else
OpenId = e.Cookies("openid") '否则从cookie中提取openid
End If
Dim Verified As Boolean
Dim dr As DataRow = DataTables("WXUsers").Find("openid ='" & OpenID & "'") '根据openid找出对应的行
If OpenId > "" AndAlso dr IsNot Nothing AndAlso dr("permit") = True '授权成功
Verified = True
ElseIf e.GetValues.ContainsKey("code") = False Then '如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
Dim ul1 As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect"
Dim ul2 As String = UrlEncode(www.xxx.com/ & args(1) & url) '这里是不是要改为自己的网址
ul1 = CExp(ul1,"AppID",ul2,"123")
sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(sb.ToString)
Return OpenId
End If
[此贴子已经被作者于2019/11/30 16:15:40编辑过]