Dim wb As New weui
wb.InsertHTML("<meta name='viewport' c>")
Dim UserName As String
Dim OpenID As String
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,"wxaecf5rre23235e0","3a232323232323cfc0e3479",e.GetValues("code"))
Dim hc As new HttpClient(ul)
Dim jo As JObject = JObject.Parse(hc.GetData)
If jo("openid") IsNot Nothing Then '如果获取openid成功(成功的话,还会同时返回一个accesstiken,用于获取用户详情)
OpenID = jo("openid")
Dim dr As DataRow = DataTables("WXUsers").SqlFind("openid ='" & Openid & "'")
If dr IsNot Nothing Then
UserName = dr("nickname")
Else
ul = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN "
'根据openid和accesstoken获取用户详情,注意这里这个accesstoken不是普通accesston,只能用于网页授权
hc = New HttpClient(CExp(ul, jo("access_token"), OpenId))
jo = jo.Parse(hc.GetData)
If jo("openid") IsNot Nothing Then
UserName = jo("nickname")
dr = DataTables("WXUsers").AddNew()
Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl"} '""
For Each nm As String In nms
dr(nm) = jo(nm)
Next
dr.Save
Else
e.WriteString(jo.ToString) '在用户浏览器显示错误信息
Return
End If
End If
wb.AppendCookie("username",UserName) '用户名和openid存储在Cookie中
wb.AppendCookie("openid",OpenID)
If openid="omr44444444444s" Then
wb.InsertHTML("跳转到list.htm")
Else
wb.InsertHTML("跳转到list2.htm")
End If
e.WriteString(wb.Build) '在用户浏览器显示错误信息
Return
Else
e.WriteString(jo.ToString) '在用户浏览器显示错误信息
Return
End If
Else
UserName = e.Cookies("username") '从cookie获取用户名和openid
OpenID = e.Cookies("openid")
If userName = "" OrElse OpenID = "" Then
'msgbox("没取到COOKIES")
Dim p As String
If openid="omu1u644444444444cjs" Then
p="list.htm"
Else
p="list2.htm"
End If
Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaecf57229ed535e0&redirect_uri=http%3a%2f%2fweixin.5gzvip.idcfengye.com%2f" & p & "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
wb.InsertHTML("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(wb.Build)
Return
Else
If DataTables("GrantTable").sqlfind("useropenid='omu1u644444444444cjs' and htmname='" & e.path &"'") IsNot Nothing Then
If e.path = "list2.htm"
wb.InsertHTML(跳转到list.htm)
e.WriteString(wb.Build)
Return
End If
Else
If DataTables("GrantTable").sqlfind("useropenid is null and htmname='" & e.path &"'") IsNot Nothing Then
wb.InsertHTML("跳转到list2.htm")
e.WriteString(wb.Build)
Return
End If
End If
End If
End If