<% ' +---------------------------------------------------------------------- ' | POPASP [ ASP MVC ] ' +---------------------------------------------------------------------- ' | Copyright (c) 2016 http://popasp.com All rights reserved. ' +---------------------------------------------------------------------- ' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) ' +---------------------------------------------------------------------- ' | Author: popasp <1737025626@qq.com> ' +---------------------------------------------------------------------- Class POPASP_CONTROLLER public template,success_tip,error_tip private tplShowTime ' 请求类型是否为GET Function isGet() isGet = POP_MVC.isGet End Function ' 请求类型是否为POST Function isPost() isPost = POP_MVC.isPost End Function ' 是否为ajax请求 Function isAjax() isAjax = POP_MVC.isAjax End Function '判断网址是否从本网站的一个链接而来,而非直接打开页面而来。 Public Property Get isSelfOrigin() isSelfOrigin = POP_MVC.String.iStartsWith( POP_MVC.String.reg_replace( Request.ServerVariables("HTTP_REFERER") , "" , "^https?://" , "i" ),POP_MVC.String.Before(Request.ServerVariables("HTTP_HOST"),":") ) End Property '注销对象,可连贯操作 Public Function u( ByRef obj ) Call POP_MVC.Unset(obj) set u = Me End Function ' 生成验证码 Public Property Get Verify P_("VERIFY").output End Property '快速生成只有一个键值对的Dictionary对象 Public Function dict( key ,value ) dim d set d = D_ if isObject( value ) then set d(key) = value else d(key) = value end if set dict = d end Function '显示文件 Public Property get Display( tpl ) template.Display(tpl) End Property '分配变量 public Sub Assign( tpl_var, value ) Call template.Assign(tpl_var,value) End Sub '是否过期,没有过期,自动加载数据 Public Function expired(id) expired = template.expired(id) End Function '是否缓存 Public Property Get is_cached( id ) is_cached = template.is_cached(id) end Property Public Property Get [Load]( arg ) if isArray( arg ) then if ubound( arg ) = 0 then call template.Load( arg(0) ,"" ) elseif ubound( arg ) > 0 then call template.Load( arg(0) ,arg(1) ) else POP_MVC.Exit( "POPASP_CONTROLLER.Load参数分配错误!!" ) end if else call template.Load( arg , "" ) end if End Property Public Property Get Cache() Call template.Cache() End Property '注销对象 Public Sub Unset( ByRef obj ) Call POP_MVC.Unset(obj) End Sub '获取来源网址,"http://127.0.0.1:2222/26/find1.asp" Function getReferer() getReferer = Request.ServerVariables("HTTP_REFERER") End Function '获取来源服务器,"http://127.0.0.1:2222" Function getOrigin() getReferer = Request.ServerVariables("HTTP_ORIGIN") End Function ' 页面跳转 Public Property Get Redirect(args) if not isArray(args) then if args = "" then success( array("","",0) ) else Call R_(args) end if else Call R_(args) end if End Property ' 根据键名获取 Request.QueryString中的值 Public Property Get [Get]( key ) [GEt] = POP_MVC.get( key ) End Property ' 根据键名获取 Request.Form中的值 Public Property Get Form(key) Form = POP_MVC.Form( key ) End Property '存在则返回Request.ServerVariables的键值,不存在返回Empty Public Property Get Server( key ) Server = Request.ServerVariables(key) End Property ' 根据键名获取 Request.Form中的值 Private Function post( key ) on error resume next if key = "" OR key="-1" then set post = POP_MVC.post("") else set post = POP_MVC.post("") if isObject( post(key) ) Then set post = post(key) else post = post(key) end if end If POP_MVC.ajax err.description End Function '存在则返回Request.ServerVariables的键值,不存在返回Empty Public Property Get var( key ) var = Request.ServerVariables(key) End Property ' Ajax返回 ' 分配变量时采用的步骤如下 ' set dict = D_ '一定采用如下分配顺序 ' dict(K_) = "data" ' dict(K_) = "info" ' dict(K_) = "status" ' 或者只传参data ' 如果data是Recordset,函数会自动处理的 Public Property Get AjaxReturn ( byRef dict ) on error resume next dim args,i,key set args = D_ args("0") = "" 'data args("1") = "" 'info args("2") = 1 'status if isArray( dict ) Then for i = 0 to ubound(dict) POP_MVC.dict.edit args,CStr(i),dict(i) next elseif TypeName(dict) = "Dictionary" Then if dict.Exists("0") Then set args = POP_MVC.dict.Merge(args,dict) else POP_MVC.dict.edit args,"0",dict end if Else POP_MVC.dict.edit args,"0",dict End If POP_MVC.dict.edit args,"data",args("0") POP_MVC.dict.edit args,"info",args("1") POP_MVC.dict.edit args,"status",args("2") ' 0 1 2 这三个键/值对是临时的 args.Remove("0") : args.Remove("1") : args.Remove("2") if TypeName( args("data") ) = "Recordset" Then if is_empty( args("data") ) Then args("data") = array() else set args("data") = P_("POPASP_TEMPLATE").rs2dict( args("data") ) end If End If call Response.AddHeader("content-type","application/json; charset=utf-8") Response.Write js_encode( args ) call L_("") Response.end End Property ''' ' 操作失败后跳转的快捷方法 ' @param string message 提示信息 ' @param string jump_url 页面跳转地址 ' @param mixed ajax 是否为Ajax方式 当数字时指定跳转时间 ' @return void ''' Public Property Get [Error]( args ) dim dict,i set dict = D_ dict(0) = "" 'message dict(1) = "" 'jump_url dict(2) = false 'ajax if isArray( args ) Then for i = 0 to ubound(args) dict(i) = args(i) next else dict(0) = args end If message = dict(0) : jump_url = dict(1) : ajax = dict(2) set dict = nothing call dispatchJump( message , 0, jump_url , ajax ) End Property ''' ' 操作成功跳转的快捷方法 ' @param string message 提示信息 ' @param string jump_url 页面跳转地址 ' @param mixed ajax 是否为Ajax方式 当数字时指定跳转时间 ' @return void ''' Public Property Get Success( args ) dim dict,i set dict = D_ dict(0) = "" 'message dict(1) = "" 'jump_url dict(2) = false 'ajax if isArray( args ) Then for i = 0 to ubound(args) dict(i) = args(i) next else dict(0) = args end If message = dict(0) : jump_url = dict(1) : ajax = dict(2) set dict = nothing call dispatchJump( message , 1, jump_url , ajax ) End Property '如果为错误的跳转 Public Sub [Err]( ByRef condition,ByVal tip ) if tip="" then tip = error_tip if get_err_bool( condition ) then Call Me.error( tip ) End Sub '如果为成功的跳转 Public Sub Suc( ByVal condition,ByVal tip ) if tip = "" then tip = success_tip if get_jump_bool( condition ) then Call Me.Success( tip ) End Sub '操作处理 Public Property Get Jump( arg ) if not isArray( arg ) then if get_suc_bool( arg ) then Me.success( Me.success_tip ) else Me.error( Me.error_tip ) end if else dim bound : bound = ubound( arg ) '如果只有一个元素 if bound = 0 then Me.Jump( arg(0) ) exit Property end if if get_suc_bool(arg(0)) then if not isNull( arg(1) ) then Me.Success( arg(1) ) end if else if bound = 1 then Me.error( Me.error_tip ) else if not isNull( arg(2) ) then Me.error( arg(2) ) end if end if end if end if End Property ' Private sub dispatchJump(message,status,jumpUrl,ajax) on error resume next If NOt is_empty( C_("STOP_PAGE_JUMP") ) then Exit Sub End If dim dict,tpl,waitSecond,sRand,msgTitle,tplContent,face if true = ajax OR isAjax() then 'AJAX提交 set dict = D_ dict("0") = "" dict("1") = message dict("2") = status call ajaxReturn(dict) exit sub End If 'ajax所代表的是等待秒数 if is_numeric( ajax ) Then waitSecond = ajax else waitSecond = 3 End if if waitSecond <1 then Response.redirect server("HTTP_REFERER") Response.end end if ' 提示标题 if is_empty(status) Then msgTitle = "操作失败" Else msgTitle = "操作成功" End If '如果设置了关闭窗口,则提示完毕后自动关闭窗口 if not is_empty(Me.get("closeWin")) Then jumpUrl = "javascript:window.close();" End If if not is_empty( status ) Then '操作正确时的提示 if is_empty( jumpUrl ) Then jumpUrl = server("HTTP_REFERER") end if face = C_("TMPL_SUCCESS_FACE") message = "

" & message & "

" if is_empty(C_("TMPL_ACTION_SUCCESS")) Then tpl = POP_MVC.mvc_dir & "Tpl/dispatch_jump.html" else tpl = C_("TMPL_ACTION_SUCCESS") End If Else '操作错误时的提示 if is_empty( jumpUrl ) then jumpUrl = "javascript:history.back(-1);" end if if is_empty(C_("TMPL_ACTION_ERROR")) Then tpl = POP_MVC.mvc_dir & "Tpl/dispatch_jump.html" else tpl = C_("TMPL_ACTION_ERROR") End If face = C_("TMPL_ERROR_FACE") message = "

" & message & "

" End If if not POP_MVC.file.isExists( tpl ) then tpl = POP_MVC.mvc_dir & "Tpl/dispatch_jump.html" end if tplContent = POP_MVC.file_get_contents( tpl ) tplContent = replace( tplContent, "{$msgTitle}" , msgTitle ) tplContent = replace( tplContent, "{$face}" , face ) tplContent = replace( tplContent, "{$message}" , message ) tplContent = replace( tplContent, "{$jumpUrl}" , jumpUrl ) tplContent = replace( tplContent, "{$version}" , POP_MVC.version ) tplContent = replace( tplContent, "{$waitSecond}" , waitSecond ) Response.write( tplContent ) Response.flush() POP_MVC.quit End Sub Public Property Get echo( str ) Response.Write str End Property Private Sub Class_Initialize set template = P_( "POPASP_TEMPLATE" ) success_tip = "操作成功!" error_tip = "操作失败!" End Sub Private Sub Class_Terminate if isObject(template) Then set template = nothing End If End Sub ' 将Recordset结果集转化成Dictionary,每行记录的键名均为数字类型,且为Dictionary类型 Public Function rs2dict(rs) set rs2dict = POP_MVC.rs2dict(rs) End Function ' 中止程序 Public Sub [exit]() Response.End End Sub Public Sub Empty_ Call A_( C_("EMPTY_MODULE") , C_("DEFAULT_ACTION") ) end sub Private Function isTheRequest(key) isTheRequest = ( LCase(key) = LCase(getRequestMethod)) End Function ' 获取请求类型 Private Function getRequestMethod() getRequestMethod = Request.ServerVariables("REQUEST_METHOD") End Function '使用了POPASP_AUTOVALIDATE.Check进行验证 Public Function Check( value,rule ) if isArray( rule ) then if ubound( rule ) = 0 then Check = P_("POPASP_AUTOVALIDATE").Check( value,rule(0) , "regex" ) else Check = P_("POPASP_AUTOVALIDATE").Check( value,rule(0) , rule(1) ) end if else Check = P_("POPASP_AUTOVALIDATE").Check( value,rule,"regex" ) end if End Function '仅对request.form数据进行mode=3时的验证,validate可以为数组,也可为数组所在文件 Public Property Get asp_check( byval arg ) dim bool bool = false if isArray( arg ) then dim validate validate = arg bool = true elseif typename( arg ) = "String" then arg = replace( arg,"\" , "/" ) dim file file = POP_MVC.appPath & "/Check/" & POP_MVC.String.Ltrim(arg,"/") if POP_MVC.file.isFile( file ) then execute POP_MVC.asp_get_contents( file ) bool = true end if end if if not bool then POP_MVC.exit( "验证条件错误,请网站开发者重新修订验证条件" ) end if '自动验证有三个参数 if not P_("autovalidate").handle( validate,request.form , 3 ) then Me.error( P_("autovalidate").error ) end if End Property 'js验证 Public sub js_check( byref arg,byref mode ) dim bool bool = false if isArray( arg ) then dim validate validate = arg bool = true elseif typename( arg ) = "String" then arg = replace( arg,"\" , "/" ) dim file file = POP_MVC.appPath & "/Check/" & POP_MVC.String.Ltrim(arg,"/") if POP_MVC.file.isFile( file ) then execute POP_MVC.asp_get_contents( file ) bool = true end if end if if not bool then POP_MVC.exit( "验证条件错误,请网站开发者重新修订验证条件" ) end if if is_empty( mode ) then mode = 3 end if '不显示控制台 call C_( Array( "SHOW_PAGE_TRACE" , 0 ) ) '修改头信息为js类型 call Response.AddHeader("Content-Type","application/x-javascript; charset=UTF-8") '输出js文件 if Me.get("type") = "" or Me.get("type") = "file" then '输出popasp_check.js文件内容 Response.write POP_MVC.file_get_contents(POP_MVC.mvc_dir & "Tpl/popasp_check.js") '换行 Response.write vbcrlf end if '输出数据 if isEmpty(Me.get("type")) or Me.get("type") = "data" then POP_MVC.dJS( "check_validate" ) = validate POP_MVC.dJS( "check_mode" ) = mode POP_MVC.dJS( "check_patchValidate" ) = P_("POPASP_AUTOVALIDATE").patchValidate POP_MVC.dJS( "SHOW_PAGE_TRACE" ) = C_( "SHOW_PAGE_TRACE" ) POP_MVC.dJS( "DEBUG" ) = C_( "DEBUG" ) Response.write "var POPASP_CHECK = " & js_encode( POP_MVC.dJS ) & ";" end if '结束,以便不显示模板文件内容 Response.end End sub 'js自动填充 Public Property get js_auto( byref arg ) '不显示控制台 call C_( Array( "SHOW_PAGE_TRACE" , 0 ) ) '修改头信息为js类型 call Response.AddHeader("Content-Type","application/x-javascript; charset=UTF-8") '输出js文件 if Me.get("type") = "" or Me.get("type") = "file" then '输出popasp_check.js文件内容 Response.write POP_MVC.file_get_contents(POP_MVC.mvc_dir & "Tpl/popasp_auto.js") '换行 Response.write vbcrlf end if '输出数据 if isEmpty(Me.get("type")) or Me.get("type") = "data" then if typename( arg ) = "Recordset" then set POP_MVC.dJS( "auto_data" ) = POP_MVC.rs2dict( arg ) elseif typename( arg ) = "Dictionary" then set POP_MVC.dJS( "auto_data" ) = arg end if POP_MVC.dJS( "SHOW_PAGE_TRACE" ) = C_( "SHOW_PAGE_TRACE" ) POP_MVC.dJS( "DEBUG" ) = C_( "DEBUG" ) Response.write "var POPASP_AUTO = " & js_encode( POP_MVC.dJS ) & ";" & vbcrlf Response.write POP_MVC.file_get_contents(POP_MVC.mvc_dir & "Tpl/popasp_auto_jump.js") end if '结束,以便不显示模板文件内容 Response.end End Property function get_err_bool( condition ) get_err_bool = false if isNull( condition ) then get_err_bool = false elseif typename( condition ) = "Dictionary" then 'Dictionary对象 if condition.count < 1 then Me.u( condition ) get_err_bool = true end if elseif typename( condition ) = "Recordset" then 'Recordset对象 if condition.bof and condition.eof then Me.u( condition ) get_err_bool = true end if else get_err_bool = CBool(condition) end if End Function function get_suc_bool( condition ) get_suc_bool = false if isNull( condition ) then get_suc_bool = false elseif typename( condition ) = "Dictionary" then 'Dictionary对象 if condition.count > 0 then Me.u( condition ) get_suc_bool = true end if elseif typename( condition ) = "Recordset" then 'Recordset对象 if not (condition.bof and condition.eof) then Me.u( condition ) get_suc_bool = true end if else get_suc_bool = CBool(condition) end if End function End Class %>