'----------------------以下代码是企业微信认证-------------------------------------------
If e.host = "xjxhsd.vicp.net:21166" Then '需要授权才能访问的域名
Dim UserId As String
Dim UserName As String
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.GetValues.ContainsKey("code") Then '如果通过授权链接跳转而来,就根据传递过来的code参数调用接口,获取用户的UserId
Dim ul As String = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}"
ul = CExp(ul,Functions.Execute("GetQYAccessToken"),e.GetValues("code"))
Dim hc As new HttpClient(ul)
Dim jo As JObject = JObject.Parse(hc.GetData)
If jo("UserId") IsNot Nothing Then
UserId = jo("UserId")
End If
Else
UserId = e.Cookies("userid") '否则从cookie中提取userid和username
End If
Dim Verified As Boolean
Dim dr As DataRow
dr = DataTables("Users").Find("userid ='" & UserId & "'") '根据openid找出对应的行
If dr IsNot Nothing AndAlso UserId > "" AndAlso dr("permit") = True '授权成功
Verified = True
UserName = dr("name")
e.AppendCookie("userid",UserId) '将userid和username存储在Cookie中
e.AppendCookie("UserName",UserName)
ElseIf e.GetValues.ContainsKey("code") = False Then '如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcbfda17a529a96a6&redirect_uri=http%3a%2f%2fxjxhsd.vicp.net:21166&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(sb.ToString)
Return
End If
If Verified = False AndAlso e.Path <> "logon.htm" Then '如果用户身份验证失败,且访问的不是登录页面
e.WriteString("<meta http-equiv='Refresh' c>") '那么直接跳转到登录页面
Else
sb.AppendLine("<meta http-equiv='Refresh' c>") '直接跳转到首页
End If
e.WriteString(sb.ToString)
End If
Select Case e.path
Case "logon.htm"
Dim wb As New WeUI
wb.InsertHTML("<h3 align='center' style='margin-top:5px'>用户登录</h3>")
wb.AddForm("","form1","logon.htm")
'提示用名和密码
If e.PostValues.ContainsKey("username") AndAlso e.PostValues.ContainsKey("password") Then '判断是否是验证失败后的重新登录
wb.AddTopTips("","toptip1","用户名或密码错误!").msec = 2000 '如果用户通过登录按钮访问,则给用户一个2秒的提示.
End If
With wb.AddInputGroup("form1","ipg1")
.Attribute = "style='margin-top:5px'"
.AddInput("xm","用户名","text")
.AddInput("pw","密码","password")
End With
With wb.AddButtonGroup("form1","btg1",True)
.Add("btn1", "确定", "submit")
End With
e.WriteString(wb.Build)
End Select