以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  下面的代码如何写入自定义函数中?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=158855)

--  作者:wh420
--  发布时间:2020/12/4 15:59:00
--  下面的代码如何写入自定义函数中?
下面代码如何写入自定义函数中,看过帮助中“简化HttpRequest事件代码”但这个不太明白如何应用,当到处都要用到“Dim wb As New weui”时怎么写入函数中

Static UserTable As DataTable \'定义一个变量,用于存储用户随机身份ID,以及最后一次活动时间.
Static ClearTime As Date
If UserTable Is Nothing Then \'创建用于记录登录信息的临时表
    ClearTime = Date.Now()
    Dim dtb As New DataTableBuilder("UserInfos")
    dtb.AddDef("UserName", Gettype(String), 90)
    dtb.AddDef("UserID",Gettype(String),16)
    dtb.AddDef("ActiveTime",Gettype(Date))
    UserTable = dtb.Build(True) \'true代表使用完自动销毁表且不显示表
End If
If (Date.Now - ClearTime).TotalMinutes >= 30 Then \'清除超过30分钟没有操作的登录信息
    UserTable.DeleteFor("ActiveTime < #" & Date.Now.AddMinutes(-30) & "#")
    ClearTime = Date.Now()
End If

Dim wb As New weui
\'身份验证
Dim UserName As String
Dim Password As String
Dim UserID As String
If e.Path = "logon.htm" \'验证用户名和密码
    If e.PostValues.ContainsKey("username") AndAlso e.PostValues.ContainsKey("password")  Then
        Dim Verified As Boolean \'用于标记用户是否通过了身份验证
        UserName = e.PostValues("username")
        Password = e.PostValues("password")
        Dim dr1 As DataRow = DataTables("Table_User").SQLFind("UseName=\'" & EncryptText(UserName,"1","123") & "\'")
        If dr1 IsNot Nothing  Then
            If dr1("UserPWd") = EncryptText(Password ,"1","123") Then
                Verified  = True
            End If
        End If
        
        If Verified Then
            UserID = Rand.NextString(16) \'生成随机用户ID
            UserName = EncryptText(UserName,"whblq","whblq") \'将用户名加密.
            Dim dr As DataRow = UserTable.Find("UserName = \'" & UserName & "\'")
            If  dr IsNot Nothing Then \'如果是重复登录,删除以前的登录信息
                dr.Delete()
            End If
            dr = UserTable.AddNew()
            dr("UserName") = UserName
            dr("UserID") = UserId
            dr("ActiveTime") = Date.Now \'记录登录时间
            wb.AppendCookie("username",UserName) \'将用户名和密码写入cookie
            wb.AppendCookie("userid",UserID)
            wb.InsertHTML("<meta http-equiv=\'Refresh\' c>") \'直接跳转到首页
            e.WriteString(wb.Build) \'生成网页
            Return \'必须的
        End If
    End If
Else \'其它页面从Cookie提取登录信息进行验证
    UserName = e.Cookies("username")  \'从cookie中获取用户名
    UserID = e.Cookies("userid")  \'从cookie中获取 随机ID
    Dim dr As DataRow = UserTable.Find("UserName = \'" & UserName & "\'")
    If dr IsNot Nothing AndAlso dr("UserID") = UserID Then  \'如果通过验证,更新活动时候,继续访问其它页面.
        dr("ActiveTime") = Date.Now \'更新活动时间
    Else \'如果验证失败
        wb.InsertHTML("<meta http-equiv=\'Refresh\' c>") \'那么直接跳转到登录页面
        e.WriteString(wb.Build) \'生成网页
        Return \'必须的
    End If
End If
\'开始生成网页
Select Case e.path
    Case "logon.htm" \'登录页面
        wb.AddPageTitle("","pageheader","个人应用系统","--夜 语--")
        \'wb.AddPageFooter("","pf1","Copyright &copy; 2008-2016 foxtable.com").AddLink("底部链接","http://www.foxtable.com")
        wb.AddPageFooter("","pf1","京")
        If e.PostValues.ContainsKey("username") AndAlso e.PostValues.ContainsKey("password")  Then \'判断是否是验证失败后的重新登录
            wb.AddTopTips("","toptip1","用户名或密码错误!").msec = 2000 \'如果用户通过登录按钮访问,则给用户一个2秒的提示.
        End If
        wb.AddForm("","form1","logon.htm")
        With wb.AddInputGroup("form1","ipg1")
            .AddInput("username","户名:","text")
            .AddInput("password","密码:","password")
        End With
        With wb.AddButtonGroup("form1","btg1",True)
            .Add("btn1", "登录", "submit")
        End With
    Case "exit.htm" \'退出登录
        wb.DeleteCookie("username") \'清除cookie中原来的用户名和密码
        wb.DeleteCookie("password")
        wb.InsertHTML("<meta http-equiv=\'Refresh\' c>") \'然后直接跳转到登录页面
    Case "", "default.htm" \'首页
        wb.AddPageTitle("","pageheader","个人系统")
        wb.AddPageFooter("","pf1","号-1")

        With wb.AddGrid("","g1")
            .Add("c1","增加订单", "./images/button.png").Attribute = ""
            .Add("c12","退出", "./images/exit.png", "exit.htm") \'退出登录
        End With
End Select
e.WriteString(wb.Build)

--  作者:有点蓝
--  发布时间:2020/12/4 16:40:00
--  
\'开始生成网页前面这一段建议就保留在httprequest好了。

各个case里的内容再放到函数里,函数里自己定义自己的【Dim wb As New weui】,然后都调用e.WriteString

比如logon函数
Dim e As RequestEventArgs = args(0)
Dim wb As New weui
    wb.AddPageTitle("","pageheader","个人应用系统","--夜 语--")
        \'wb.AddPageFooter("","pf1","Copyright &copy; 2008-2016 foxtable.com").AddLink("底部链接","http://www.foxtable.com")
        wb.AddPageFooter("","pf1","京")
        If e.PostValues.ContainsKey("username") AndAlso e.PostValues.ContainsKey("password")  Then \'判断是否是验证失败后的重新登录
            wb.AddTopTips("","toptip1","用户名或密码错误!").msec = 2000 \'如果用户通过登录按钮访问,则给用户一个2秒的提示.
        End If
        wb.AddForm("","form1","logon.htm")
        With wb.AddInputGroup("form1","ipg1")
            .AddInput("username","户名:","text")
            .AddInput("password","密码:","password")
        End With
        With wb.AddButtonGroup("form1","btg1",True)
            .Add("btn1", "登录", "submit")
        End With
e.WriteString(wb.Build)

…………………………
Select Case e.path
    Case "logon.htm" \'登录页面
        Functions.Execute("logon",e)
    Case "exit.htm" \'退出登录
        Functions.Execute("exit",e)
    Case "", "default.htm" \'首页
        Functions.Execute("default",e)
End Select
‘这里最后一句必须去掉e.WriteString(wb.Build)

--  作者:wh420
--  发布时间:2020/12/5 1:19:00
--  
收到,谢谢老师。我研究一下