关于应用模块页面的生成
应用模块页面函数
如:
public partial class userblog : BaseJsonMode{
[ModeMethod("{CanNoLogin:true,CanEnable:true}")]
static public ReturnJson index(HttpContext ctx, Object m_Parame)
{
ReturnJson m_ReturnJson = new ReturnJson();
try
{
Hashtable m_PageHH = new Hashtable();
//获得页面参数
JsonHelper.OBJToHashTable(m_Parame, m_PageHH);
m_ReturnJson.bOK=true;
//获得渲染页面的内容,传递页面数据m_PageHH
m_ReturnJson.m_ReturnOBJ = RenderPageView(ctx, m_JsonModePath, "index", m_PageHH);
}
catch(Exception)
{
m_ReturnJson.bOK=false;
m_ReturnJson.sMsg="渲染页面异常";
}
return m_ReturnJson;
}
通过浏览器访问该页面
http://../userblog/index.aspx?a=a
其中m_PageHH将获得参数
string a=m_PageHH["a"].ToString();
渲染页面函数
static protected string RenderPageView(HttpContext m_ctx, string m_ModePath, string m_TamplateCTL, Object m_Model)
m_ModePath:页面文件所在位置一般为m_JsonModePath
如是渲染PC端页面一般为Viewamazeuizh-CNindex.aspx
如是渲染手机端页面一般为Viewdeviceamazeuizh-CNindex.aspx
我们也可以自己再加自己的路径
如m_ModePath设置为m_JsonModePath+“/xn”;
那么找到的页面路径则为
如是渲染PC端页面一般为Viewamazeuizh-CNxnindex.aspx
如是渲染手机端页面一般为Viewdeviceamazeuizh-CNixnndex.aspx
路径分解
对于PC端:View+UI库名+语言+添加路径+页面文件
对于手机端:View+device+UI库名+语言+添加路径+页面文件
我们采用aspx作为页面模板的原因,因为ASP.NET开发人员对于aspx页面嵌套服务端逻辑代码是比较熟悉的,便于很快上手
对于aspx模板页面派生自
XNWebEngine.JsonBase.MyBasePageView
典型代码如下:
<%@ Page Language="C#" AutoEventWireup="true" Inherits="XNWebEngine.JsonBase.MyBasePageView" %>
<%@ Assembly Name="CoreSYS" %>
<%@ Import Namespace="XNWebEngine.JsonBase" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="CoreSYS" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="XNWebEngine" %>
<script language="C# " runat="server">
public Object getModel()
{
return this.Model;
}
</script>
其中getModel()获得RenderPageView传过来的页面数据
如:
<%
Hashtable m_PageHH=(Hashtable)getModel();
string a="";
try
{
if(m_PageHH.ContAInsKey("a"))
{
a=m_PageHH["a"].ToString();
}
}
catch
{
}
%>
后续的代码和正常的aspx页面写法一致
MyBasePageView的类函数可在页面使用的可见之前的章节
MyBasePageView常用函数和参数
获得文件上传参数
static public OneUpFileWEB GetUpFileURL(HttpContext ctx)
读取cookie
static protected string ReadCookie(HttpContext ctx, string name)
static protected HttpCookie GetCookie(HttpContext ctx, string sKey)
获得当前登录信息
protected SessionLink GetSessionLink()
判断当前用户权限
public bool HavePower(string ClassFullName, string FunctionName, bool bClient, out string sError)
判断指定角色权限
public bool HavePower(string sys_js_uuid, string username, string ClassFullName, string FunctionName, bool bClient, out string sError)
调用指定应用模块函数
public bool CallModeFunction(string classfullname, string functionname, object m_InParame, out object m_OutParame, out string sError)
执行指定模块的函数返回html
public string RenderModeHTML(string classfullname, string functionname, object m_InParame)
保存cookie
protected bool SaveCookie(string name, string value)
读取cookie
protected string ReadCookie( string name)
#region 读写Cache
protected bool SaveCache<T>(string sKey, T m_T,bool bSlidTime=true)
protected bool ReadCache<T>(string sKey, out T m_T, int nDelayMinute = -1)
protected bool SaveCache<T>(string sKey, T m_T, TimeSpan m_nOverTime, bool bSlidTime=true)
protected bool ClearCache(string sKey)
#endregion
#region 清除cookie
protected void ClearCookie(string sKey)
#endregion
其他
protected bool SaveCookie( string name, string value, TimeSpan m_T)
protected bool SaveUserShareCache<T>(string sKey, T m_T, bool bSlidTime=true)
protected bool SaveUserShareCache<T>(string sKey, T m_T, TimeSpan m_nOverTime, bool bSlidTime=true)
protected bool ReadUserShareCache<T>(string sKey, out T m_T, int nDelayMinute = -1)
protected bool ClearUserShareCache<T>( string sKey)
protected bool SaveUserCache<T>(string sKey, T m_T, bool bSlidTime=true)
protected bool SaveUserCache<T>(string sKey, T m_T, TimeSpan m_nOverTime, bool bSlidTime=true)
protected bool ReadUserCache<T>(string sKey, out T m_T, int nDelayMinute = -1)
protected bool ClearUserCache<T>(string sKey)
读取key
static protected string GetConfigKeyString(string sKeyName)
获得资源url
static protected string GetResourceUrl(HttpContext ctx)
获得页面前缀
static protected string GetUrlBefore(HttpContext ctx)
protected static string GetUrlHead(HttpContext ctx)
判断是否是PC端
protected bool IsPCClient()
判断是否HTTPS
protected bool IsHTTPS()
以上函数在页面里都可调用
常用功能及函数为
1.获得页面数据
<script language="C# " runat="server">
public Object getModel()
{
return this.Model;
}
</script>
<%
Hashtable m_HH=(Hashtable)getModel();
%>
2.权限判断
<%
string sError;
if (HavePower("CodeFactory_0_base", "Addcode_adcodes", false, out sError))
{
}
%>
3.获得当前登录信息
<%
SessionLink m_SessionLink = GetSessionLink();
%>
4.获得应用模块的view Url路径
<%
string ModeUrl = urlBefore + MainViewPath;
%>
<script src="<%=ModeUrl%>amazeui/public/js/clipboard.min.js" type="text/JAVAscript"></script>
5.获得资源的URL路径
<%
GetResourceUrl(HttpContext.Current);
%>
//应用例子
<script src="<%=GetResourceUrl(HttpContext.Current)%>/public/amazeui/amazeui/js/jquery.min.js"></script>
6.获得页面前缀
<%
GetHeadUrl(HttpContext.Current);
%>
//应用例子
ar options = {
iniUrl: "<%=GetUrlHead(HttpContext.Current)%>
weixin/initLayUI_RawJson.ajax", // 初始化接口
clearUrl: "<%=GetUrlHead(HttpContext.Current)%>
weixin/clearLayUI_RawJson.ajax", // 缓存清理接口
renderPageVersion: true, // 初始化页面是否加版本号
bgColorDefault: false, // 主题默认配置
multiModule: true, // 是否开启多模块
menuChildOpen: false, // 是否默认展开菜单
loadingTime: 0, // 初始化加载时间
pageAnim: true, // 切换菜单动画
};
页面渲染函数的内部实现
static protected string RenderPageView(HttpContext m_ctx, string m_ModePath, string m_TamplateCTL, Object m_Model)
该页面渲染函数支持对aspx的渲染和shtml采用开源JNTemplate渲染
aspx页面渲染核心代码
//构建Page pageVirtualPath:aspx文件路径
Page handler = BuildManager.CreateInstanceFromVirtualPath(pageVirtualPath, typeof(System.Web.UI.Page)) as Page;
//设置页面数据
SetPageModel(handler, m_Model, ViewPath);
StringWriter output = new StringWriter();
//执行输出页面
m_ctx.Server.Execute(handler, output, false);
//返回页面字符串
return output.ToString();
shtml页面JNTemplate渲染核心代码
string output="";
using (JNTemplateEngine m_JNTemplateEngine = new JNTemplateEngine(m_ctx, m_ModePath, FName, m_Model, true))
{
m_JNTemplateEngine.Render(out output);
}
对于开源JNTemplate的使用可查阅相关资料
其中JNTemplateEngine封装的渲染引擎代码如下
class JNTemplateEngine: IDisposable
{
JinianNet.JNTemplate.ITemplate ITemplate = null;
HttpContext ctx = null;
PageEngine m_PageEngine = null;
public JNTemplateEngine(HttpContext ctx, string m_ModePath, string src, Object m_Model, bool bFile = true)
{
this.ctx = ctx;
if (bFile)
ITemplate = Engine.LoadTemplate(src);
else
{
ITemplate = Engine.CreateTemplate(src);
}
ITemplate.Set("model", m_Model);
m_PageEngine = new PageEngine(ctx, m_ModePath);
ITemplate.Set("engine", m_PageEngine);
}
public JNTemplateEngine(HttpContext ctx, string m_ModePath, string src, Object m_Model, Type m_Type,bool bFile = true)
{
this.ctx = ctx;
if (bFile)
ITemplate = Engine.LoadTemplate(src);
else
{
ITemplate = Engine.CreateTemplate(src);
}
ITemplate.Set("model", m_Model);
m_PageEngine = new PageEngine(ctx, m_ModePath);
ITemplate.Set("engine", m_PageEngine);
ITemplate.SetStaticType("insertClass", m_Type);
}
public void Dispose()
{
if(ITemplate!=null)
{
ITemplate=null;
}
if(m_PageEngine!=null)
{
m_PageEngine = null;
}
}
public bool Render(out string sText)
{
sText = "";
try
{
sText = ITemplate.Render();
return true;
}
catch (Exception e)
{
sText = e.ToString();
return false;
}
}
}
public class PageEngine : BaseJsonMode
{
HttpContext ctx = null;
string m_ModePath,ModeUrl="";
public PageEngine(HttpContext ctx, string m_ModePath)
{
this.ctx = ctx;
this.m_ModePath = m_ModePath;
string ViewPath = "~\" + m_ModePath + "\View\";
ViewPath = ViewPath.Replace("\", "/");
int nPos = ViewPath.IndexOf("View/");
string MainViewPath = "";
if (nPos != -1)
{
MainViewPath = ViewPath.Substring(1, nPos + 4);
}
ModeUrl = GetResourceUrl() + MainViewPath;
}
public SessionLink GetSessionLink()
{
return BaseJsonMode.GetSessionLink(ctx);
//SessionLink m_SessionLink = new SessionLink();
//m_SessionLink.username = "xuneng";
//return m_SessionLink;
}
public bool HavePower(string FullClassName, string FunctionName)
{
return BaseJsonMode.HavePower(ctx, FullClassName, FunctionName, true).bOK;
//return true;
}
public bool SaveCookie(string name, string value)
{
return BaseJsonMode.SaveCookie(ctx, name, value);
//return false;
}
public string ReadCookie(string name)
{
return BaseJsonMode.ReadCookie(ctx, name);
//return "";
}
public void ClearCookie(string name)
{
BaseJsonMode.ClearCookie(ctx, name);
//return;
}
public ReturnJson CallJsonModeFunction(string ClassFullName, string FunctionName, Object m_InParame)
{
ReturnJson m_ReturnJson = new ReturnJson();
//return m_ReturnJson;
if (BaseJsonMode.CallJsonModeFunction(ctx, ClassFullName, FunctionName, m_InParame, true, out m_ReturnJson.m_ReturnOBJ, out m_ReturnJson.sMsg))
{
m_ReturnJson.bOK = true;
return m_ReturnJson;
}
else
{
m_ReturnJson.bOK = false;
return m_ReturnJson;
}
}
public string RenderModeHTML(string classfullname, string functionname, object m_InParame)
{
//return "";
ReturnJson m_ReturnJson = CallJsonModeFunction(classfullname, functionname, m_InParame);
if (m_ReturnJson.bOK)
{
return m_ReturnJson.m_ReturnOBJ.ToString();
}
else
{
return m_ReturnJson.sMsg;
}
}
public string GetResourceUrl()
{
return BaseJsonMode.GetResourceUrl(ctx);
}
public OneUpFileWEB GetUpFileURL()
{
return BaseJsonMode.GetUpFileURL(ctx);
}
public bool HaveCrossPower(string sClassFullName, string sFunctionName, string sys_companys_nodeuuid)
{
return BaseJsonMode.HaveCrossPower(ctx, sClassFullName, sFunctionName, sys_companys_nodeuuid, true).bOK;
}
protected List<string> GetUrlDotListString()
{
if (ctx.Items.Contains("headDot"))
{
return (List<string>)ctx.Items["headDot"];
}
string[] sDotArray = ctx.Request.FilePath.Split('/');
int nCnt = sDotArray.Length;
List<string> m_List = new List<string>();
if (nCnt >= 3)
{
if (nCnt == 3)
{
ctx.Items["headDot"] = m_List;
return m_List;
}
else
{
//string[] m_ReturnDotArray = new string[nCnt - 3];
for (int i = 1; i < 1 + nCnt - 3; i++)
{
m_List.Add(sDotArray[i]);
}
ctx.Items["headDot"] = m_List;
return m_List;
}
}
else
{
ctx.Items["headDot"] = m_List;
return m_List;
}
}
public string GetUrlHead()
{
List<string> m_HeadList = GetUrlDotListString(ctx);
if (m_HeadList.Count > 0)
{
string str = "";
for (int i = 0; i < m_HeadList.Count; i++)
{
str += "/" + m_HeadList[i];
}
str += "/";
return str;
}
else
{
return "/";
}
}
public string GetModeUrl()
{
return ModeUrl;
}
public bool IsPCClient()
{
return BaseJsonMode.IsPCClient(ctx);
}
public bool isHTTPS()
{
return ctx.Request.IsSecureConnection;
}
public Hashtable OBJToHashTable(Object m_Parame)
{
/*
Hashtable m_HH = new Hashtable();
JsonHelper.OBJToHashTable(m_Parame, m_HH);
return m_HH;
*/
try
{
return JsonHelper.OBJToType<Hashtable>(m_Parame);
}
catch
{
return new Hashtable();
}
}
public List<Hashtable>OBJToListHashtable(Object m_Parame)
{
return JsonHelper.OBJToType<List<Hashtable>>(m_Parame);
}
public Hashtable JsonToHashtable(string jsonString)
{
/*
Hashtable m_HH = new Hashtable();
JsonHelper.JsonToHashtable(jsonString, m_HH);
return m_HH;
*/
return JsonHelper.JsonStrToOBJ<Hashtable>(jsonString);
}
}
使用JNTemplate的页面例子
页面函数:
static public ReturnJson uniAppTestPage(HttpContext ctx, Object m_Parame)
{
Hashtable m_HH = new Hashtable();
JsonHelper.OBJToHashTable(m_Parame, m_HH);
ReturnJson m_ReturnJson = new ReturnJson();
try
{
string type = "";
if (KeyIsOK(m_HH, "type"))
{
type = m_HH["type"].ToString();
}
m_ReturnJson.bOK = true;
oneData m_oneData = new oneData();
m_oneData.m_HH = new Hashtable();
m_oneData.A = "1";
m_oneData.B = 2;
m_oneData.m_HH["A"] = type;
m_oneData.m_oneData = new oneData();
m_oneData.m_oneData.m_HH = new Hashtable();
m_oneData.m_oneData.A = "11";
m_oneData.m_oneData.B = 22;
m_oneData.m_oneData.m_HH["A"] = "下级测试数据";
oneData m_oneData0 = new oneData();
m_oneData0.A = "c1";
m_oneData.m_HH["m_oneData"] = m_oneData0;//错误Hashtable内只能包含基本数据类型如int string float double 不能包含对象否则模板访问失败
int i;
List<Hashtable> m_HHList = new List<Hashtable>();
for (i = 0; i < 10; i++)
{
Hashtable m_oneHH = new Hashtable();
m_oneHH["pos"] = i;
m_HHList.Add(m_oneHH);
}
m_oneData.m_oneData.m_HHList = m_HHList;
List<oneRow> m_RowArray = new List<oneRow>();
for (i = 0; i < 10; i++)
{
oneRow m_oneRow = new oneRow();
m_oneRow.pos = i;
m_RowArray.Add(m_oneRow);
}
m_oneData.m_oneData.m_HH["array"] = m_RowArray.ToArray();
m_oneData.m_oneData.m_HH["array2"] = m_HHList;
m_oneData.m_oneData.m_RowArray = m_RowArray;
m_ReturnJson.m_ReturnOBJ = RenderPageView(ctx, m_JsonModePath, "uniappTestPage.shtml", m_oneData,typeof(menuClass));
}
catch
{
m_ReturnJson.bOK = false;
m_ReturnJson.sMsg = "渲染uniappTestPage失败";
}
return m_ReturnJson;
}
uniappTestPage.shtml的页面内容纯测试模板语法所以里面未出现html标签
m_oneData.A:${model.A},m_oneData.B:${model.B},m_oneData.m_HH["A"]:${model.m_HH["A"]},m_oneData.m_oneData.A:${model.m_oneData.A},m_oneData.m_oneData.B:${model.m_oneData.B},m_oneData.m_oneData.m_HH["A"]:${model.m_oneData.m_HH["A"]},m_SessionLink.username:${engine.GetSessionLink().username}
权限判断:CoreSYS.SYS/AddCompany:${if(engine.HavePower("CoreSYS.SYS","AddCompany"))}有权限执行${else}无权限执行${end}
对象列表数据:${foreach(one in
model.m_oneData.m_RowArray)}序号-${one.pos} ${end}<br />
Hashtable的List列表数据:${foreach(one in model.m_oneData.m_HHList)}序号-${one["pos"]} ${end}</br>
Hashtable中对象${model.m_HH["m_oneData"]}</br>
引号内</br>
Hashtable的List列表数据:"${foreach(one in model.m_oneData.m_HHList)}序号-${one["pos"]} ${end}"</br>
模板页内变量使用<br />
${set(a=123)}<br />
输出:${a}
<br />
<ul>
定义ii变量
${set(ii=0)}
${for(ii=1;ii<4;ii++)}
<li>${ii}</li>
${end}
</ul>
</br>
复杂语法测试<br />
${set(m_SessionLink=engine.GetSessionLink())}
${if(model.m_oneData.m_HH.ContainsKey("BB"))}
有BB
${else}
无BB
${end}
<br />
复杂测试
Hashtable对象${set(m_oneData=model.m_HH["m_oneData"])}
${set(hh=engine.OBJToHashTable(m_oneData))}
${hh["A"]}
<br />
Hashtable对象
${engine.OBJToHashTable(model.m_HH["m_oneData"])["A"]}
<br />
Hashtable包含Hashtable数组
${set(m_List=model.m_oneData.m_HH["array2"])}
${set(m_HHList=engine.OBJToListHashtable(m_List))}
${foreach(rr in m_HHList)}
${rr["pos"]},
${end}
<br />
Hashtable包含Hashtable数组
${foreach(rr2 in engine.OBJToListHashtable(model.m_oneData.m_HH["array2"]))}
${rr2["pos"]},
${end}
下集将针对应用模块开发页面UI常用代码总结