HttpRequest事件代码设置为:
Dim wb As New weui
Dim OpenID As String
wb.InsertHTML("<meta name='viewport' content='width=device-width,initial-scale=1,user-scalable=1'>")
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,"wx8acdb7df5beb68fd","a5a3b06a790d4200a151e6b13c3dc263",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")
wb.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 ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8acdb7df5beb68fd&redirect_uri=http%3a%2f%2fwexin.foxtable.com&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
wb.InsertHTML("<meta http-equiv='Refresh' content='0; url=" & ul & "'>") '跳转到授权链接
e.WriteString(sb.ToString)
Return
End If
If Verified = False Then
e.WriteString("你无权访问本系统")
Else
Select Case e.path
Case "exit.htm" '退出登录
wb.DeleteCookie("openid") '清除cookie
wb.InsertHTML("<meta http-equiv='Refresh' content='0; url=/logon.htm'>") '然后直接跳转到登录页面
Case "", "default.htm" '首页
wb.AddPageTitle("","pageheader","销售系统","由湛江辉迅基于Foxtable开发")
With wb.AddGrid("","g1")
.Add("c1","增加订单", "./images/button.png").Attribute = "onclick='javascript:alert(""你单击了我!"")'"
.Add("c2","客户管理", "./images/cell.png", "http://www.foxtable.com")
……其它页面
.Add("c12","退出", "./images/exit.png", "exit.htm") '退出登录
End With
Case "xxx.htm"
……
End Select
e.WriteString(wb.Build) '生成网页
End If