/// <summary> /// 生成TsCode /// </summary> public TsResultModel GetTsResultModel(string itemId, string itemType = "") { TsResultModel result = null; if (itemType.ToLower() == TsCreateType.Controller.ToString().ToLower()) { ControllerModel controller = JcApiHelper.GetController(itemId); if (controller == null) { throw new Exception("无效的ItemId."); } result = GetTsResultModel(controller); } else if (itemType.ToLower() == TsCreateType.Action.ToString().ToLower()) { ActionModel action = JcApiHelper.GetAction(itemId); if (action == null) { throw new Exception("无效的ItemId."); } result = GetTsResultModel(action); } else { PTypeModel ptype = JcApiHelper.GetPTypeModel(itemId); if (ptype == null) { throw new Exception("无效的ItemId."); } result = GetTsResultModel(ptype); } return(result); }
/// <summary> /// Ctor /// 枚举属性使用 /// 性构造ParamModel /// </summary> /// <param name="fi">FieldInfo</param> /// <param name="index"></param> private static ParamModel GetParam(FieldInfo fi, int index = 0) { PTypeModel ptype = GetPType(fi.FieldType); int? value = null; if (fi.FieldType.IsEnum) { try { value = Convert.ToInt32(Enum.Parse(fi.FieldType, fi.Name)); } catch { //如转换失败,忽略不做处理 } } string fiId = null; if (fi.DeclaringType != null) { fiId = $"F:{fi.DeclaringType.ToString()}.{fi.Name}"; } ParamModel param = new ParamModel() { Name = fi.Name, Id = fiId, PType = ptype, ParamValue = value, CustomerAttrList = fi.CustomAttributes.Select(a => GetCustomerAttribute(a)).ToList(), Position = index + 1 }; return(param); }
/// <summary> /// Ctor /// 类属性使用 /// 构造ParamModel /// </summary> /// <param name="pi">参数信息</param> /// <param name="index"></param> private static ParamModel GetParam(PropertyInfo pi, int index = 0) { PTypeModel ptype = GetPType(pi.PropertyType); string piId = null; if (pi.DeclaringType != null) { string declaringTypeName = pi.DeclaringType.ToString(); if (declaringTypeName.IndexOf("`") != -1) { //泛型属性Id结构如下:P:Jc.Core.Robj`1.Result declaringTypeName = declaringTypeName.Substring(0, declaringTypeName.IndexOf("`") + 2); } piId = $"P:{declaringTypeName}.{pi.Name}"; } ParamModel param = new ParamModel() { Name = pi.Name, Id = piId, PType = ptype, CustomAttrList = pi.CustomAttributes.Select(a => GetCustomAttribute(a)).ToList(), Position = index + 1 }; if (pi.CustomAttributes.Count() > 0) { } return(param); }
/// <summary> /// 生成TsService /// </summary> private string GetTsServiceCommonCode(ActionModel action) { StringBuilder strBuilder = new StringBuilder(); string actionRouteName = (string.IsNullOrEmpty(action.AreaName) ? "" : $"{action.AreaName}/") + $"{action.ControllerName}/{action.ActionName}"; string inputParamStr = ""; string ajaxParamStr = ""; string returnParamTypeStr = ""; if (action.InputParameters != null && action.InputParameters.Count > 0) { if (action.InputParameters.Count == 1 && action.InputParameters[0].HasPiList == true) { inputParamStr = $"{action.InputParameters[0].Name}:{GetTsType(action.InputParameters[0].PType)}"; ajaxParamStr = $",{action.InputParameters[0].Name}"; } else if (action.InputParameters.Any(param => param.Name.ToLower().Contains("index")) && action.InputParameters.Any(param => param.Name.ToLower().Contains("size"))) { //处理分页查询方法 string queryObjTypeName = "PgQueryObj"; if (action.ReturnParameter.PType.PiList?.Count > 0) { for (int i = 0; i < action.ReturnParameter.PType.PiList.Count; i++) { if (action.ReturnParameter.PType.PiList[i].IsIEnumerable) { PTypeModel enumPType = JcApiHelper.GetPTypeModel(action.ReturnParameter.PType.PiList[i].EnumItemId); queryObjTypeName = GetTsType(enumPType) + "QueryObj"; break; } } } inputParamStr = $"{FirstToLower(queryObjTypeName)}: {queryObjTypeName}"; ajaxParamStr = $",{FirstToLower(queryObjTypeName)}"; } else { ajaxParamStr = ",{"; for (int i = 0; i < action.InputParameters.Count; i++) { if (i > 0) { inputParamStr += ","; ajaxParamStr += ","; } inputParamStr += $"{action.InputParameters[i].Name}: {GetTsType(action.InputParameters[i].PType)}"; ajaxParamStr += $"{action.InputParameters[i].Name}: {action.InputParameters[i].Name}"; } ajaxParamStr += "}"; } } returnParamTypeStr = GetTsType(action.ReturnParameter.PType); strBuilder.AppendLine($" /*{(string.IsNullOrEmpty(action.Summary) ? action.ActionName : action.Summary)}*/"); strBuilder.AppendLine($" public { FirstToLower(action.ActionName)}({inputParamStr}): Observable<{returnParamTypeStr}>{{"); strBuilder.AppendLine($" return this.http.post('{actionRouteName}'{ajaxParamStr});"); strBuilder.AppendLine(" }"); return(strBuilder.ToString()); }
/// <summary> /// Ctor /// 根据数据类型 自定义参数名 /// 构造ParamModel /// </summary> /// <param name="type">类型</param> /// <param name="index">序号</param> private static ParamModel GetParam(Type type, int index = 0) { PTypeModel ptype = GetPType(type); ParamModel param = new ParamModel() { Name = $"p{index + 1}", PType = ptype, Position = index + 1 }; return(param); }
/// <summary> /// Ctor /// Attribute使用 /// 根据Attribute NamedArgument 命名参数 /// 构造命名参数ParamModel /// </summary> /// <param name="namedArgument"></param> /// <param name="index">序号</param> private static ParamModel GetParam(CustomAttributeNamedArgument namedArgument, int index = 0) { PTypeModel ptype = GetPType(namedArgument.TypedValue.ArgumentType); ParamModel param = new ParamModel() { Name = $"p{index + 1}", PType = ptype, ParamValue = namedArgument.TypedValue.Value, Position = index }; return(param); }
/// <summary> /// Ctor /// 根据参数信息 输入输出参数 /// 构造ParamModel /// </summary> /// <param name="paramInfo">参数信息</param> private static ParamModel GetParam(ParameterInfo paramInfo) { PTypeModel ptype = GetPType(paramInfo.ParameterType); ParamModel param = new ParamModel() { Name = paramInfo.Name, PType = ptype, DefaultValue = paramInfo.DefaultValue?.ToString(), Position = paramInfo.Position + 1 }; return(param); }
/// <summary> /// 生成TsCode /// </summary> public TsResultModel GetTsResultModel(PTypeModel ptype) { TsResultModel result = new TsResultModel() { Id = ptype.Id, Name = ptype.TypeName, Summary = ptype.Summary }; result.TsModelList = new List <TsModel>(); FillTsModelList(result.TsModelList, ptype); return(result); }
/// <summary> /// Ctor /// 根据参数信息 输入输出参数 /// 构造ParamModel /// </summary> /// <param name="paramInfo">参数信息</param> private static ParamModel GetParam(ParameterInfo paramInfo) { PTypeModel ptype = GetPType(paramInfo.ParameterType); ParamModel param = new ParamModel() { Name = paramInfo.Name, PType = ptype, DefaultValue = paramInfo.DefaultValue?.ToString(), CustomAttrList = paramInfo.GetCustomAttributes().Select(a => GetCustomAttribute(a)).ToList(), Position = paramInfo.Position + 1 }; return(param); }
/// <summary> /// Ctor /// Attribute使用 /// 根据AttributeData /// 构造CustomAttrModel /// </summary> private static CustomAttrModel GetCustomAttribute(Attribute customAttribute, int index = 0) { PTypeModel ptype = GetPType(customAttribute.GetType()); CustomAttrModel model = new CustomAttrModel() { Name = ptype.TypeName.Replace("Attribute", ""), PType = ptype, Position = index, //ConstructorArgumentsList = customAttribute.ConstructorArguments.Select((a, i) => { return GetParam(a, i); }).ToList(), //NamedArgumentsList = customAttribute.NamedArguments.Select((a, i) => { return GetParam(a, i); }).ToList() }; return(model); }
/// <summary> /// 生成TsCode /// </summary> private string GetTsModelCode(PTypeModel ptype) { StringBuilder strBuilder = new StringBuilder(); string tsType = GetTsType(ptype); strBuilder.AppendLine($"/*{(string.IsNullOrEmpty(ptype.Summary) ? tsType : ptype.Summary)}*/"); strBuilder.AppendLine("export class " + tsType + " {"); for (int i = 0; i < ptype.PiList.Count; i++) { strBuilder.AppendLine($" { FirstToLower(ptype.PiList[i].Name)}: {GetTsType(ptype.PiList[i].PType)}; // " + ptype.PiList[i].Summary); } strBuilder.AppendLine("}"); strBuilder.AppendLine(); return(strBuilder.ToString()); }
/// <summary> /// 获取TsModelList /// </summary> /// <param name="list"></param> /// <param name="ptype"></param> private void FillTsModelList(List <TsModel> list, PTypeModel ptype) { ptype = JcApiHelper.GetPTypeModel(ptype.Id); //读取Ptype注释内容 if (list.Any(tsPtype => tsPtype.Id == ptype.Id)) { //已添加过,不再添加 return; } if (ptype.IsIEnumerable) { //枚举类型 添加其泛型类型 PTypeModel enumItemPtype = JcApiHelper.GetPTypeModel(ptype.EnumItemId); //读取Ptype注释内容 if (enumItemPtype?.PiList?.Count > 0) { FillTsModelList(list, enumItemPtype); } return; } TsModel tsPType = new TsModel() { Id = ptype.Id, Name = GetTsType(ptype), Summary = ptype.Summary, TsModelCode = GetTsModelCode(ptype), PgQueryModelCode = GetTsQueryModelCode(ptype), PiList = new List <TsPi>() }; list.Add(tsPType); for (int i = 0; i < ptype.PiList?.Count; i++) { TsPi tsPi = new TsPi() { Name = ptype.PiList[i].Name, Summary = ptype.PiList[i].Summary, TsType = GetTsType(ptype.PiList[i].PType) }; tsPType.PiList.Add(tsPi); if (ptype.PiList[i].PType.PiList?.Count > 0) { FillTsModelList(list, ptype.PiList[i].PType); } } }
public IActionResult GetPType([FromForm] string typeId) { Robj <PTypeModel> robj = new Robj <PTypeModel>(); try { if (string.IsNullOrEmpty(typeId)) { throw new Exception("参数typeId不能为空"); } PTypeModel ptype = JcApiHelper.GetPTypeModel(typeId); robj.Result = ptype; } catch (Exception ex) { robj.Error(ex.Message); } return(new JsonResult(robj)); }
/// <summary> /// 获取TsQueryModel /// </summary> private string GetTsQueryModelCode(PTypeModel ptype) { if (ptype.IsEnum || ptype.IsGeneric) { //排除枚举和泛型类型 return(""); } string tsType = GetTsType(ptype); StringBuilder strBuilder = new StringBuilder(); strBuilder.AppendLine($"/*{(string.IsNullOrEmpty(ptype.Summary) ? tsType : ptype.Summary)} QueryObj 分页查询对象*/"); strBuilder.AppendLine($"export class { tsType }QueryObj extends { tsType } implements IPage {{"); strBuilder.AppendLine(" pageIndex: number;"); strBuilder.AppendLine(" pageSize: number;"); strBuilder.AppendLine(" sort: string;"); strBuilder.AppendLine(" order: string;"); strBuilder.AppendLine(" constructor() {"); strBuilder.AppendLine(" super();"); strBuilder.AppendLine(" }"); strBuilder.AppendLine("}"); strBuilder.AppendLine(); return(strBuilder.ToString()); }
/// <summary> /// 为ptype设置注释 /// </summary> /// <param name="ptype"></param> private static void SetPTypeNote(PTypeModel ptype) { #region 设置PType注释 string baseDir = AppDomain.CurrentDomain.BaseDirectory; DirectoryInfo dirInfo = new DirectoryInfo(baseDir); string xmlNoteFileName = ptype.ModuleName.Replace(".dll", ".xml"); FileInfo fileInfo = dirInfo.GetFiles(xmlNoteFileName, SearchOption.AllDirectories).FirstOrDefault(); if (fileInfo != null) { AssemblyNoteModel noteModel = AssemblyHelper.GetAssemblyNote(fileInfo.FullName); if (noteModel != null) { //Controller 注释 ptype.Summary = noteModel.MemberList.FirstOrDefault(member => member.Name == ptype.Id)?.Summary; foreach (ParamModel param in ptype.PiList) { param.Summary = noteModel.MemberList.FirstOrDefault(member => member.Name == param.Id)?.Summary; } } } #endregion }
/// <summary> /// c#中的数据类型与TsType对照 /// </summary> /// <param name="ptype"></param> /// <returns></returns> private string GetTsType(PTypeModel ptype) { if (ptype == null) { return(""); } string tsTypeStr = ""; Type type = ptype.SourceType; if (type == typeof(Microsoft.AspNetCore.Mvc.IActionResult)) { tsTypeStr = "any"; } else if (ptype.IsIEnumerable) { //枚举类型特殊处理 PTypeModel enumPType = JcApiHelper.GetPTypeModel(ptype.EnumItemId); tsTypeStr = $"{GetTsType(enumPType)}[]"; } else { tsTypeStr = GetTsType(ptype.TypeName); } return(tsTypeStr); }
/// <summary> /// 获取PType /// </summary> /// <param name="type"></param> private static PTypeModel GetPType(Type type) { PTypeModel ptype = null; string id = TypeHelper.GetModuleMark(type); if (PTypeDic.Keys.Contains(id)) { ptype = PTypeDic[id]; } else { ptype = new PTypeModel(type); PTypeDic.Add(ptype.Id, ptype); if (!IsIgnoreType(type)) { //忽略系统类型 忽略类型,不生成属性列表 自定义特性列表忽略 //ptype.CustomAttrList = GetCustomAttributes(type); if (type.IsEnum) { ptype.PiList = GetEnumPiList(type); } else if (ptype.IsIEnumerable) { //实现了枚举方法 使用子类属性 if (!IsIgnoreType(type.GenericTypeArguments[0])) { PTypeModel enumPType = GetPType(type.GenericTypeArguments[0]); ptype.PiList = enumPType.PiList; } } else { ptype.PiList = GetClassObjPiList(type); } } } return(ptype); }
/// <summary> /// 根据Id获取TypeModel /// </summary> /// <param name="typeId">typeId</param> /// <returns></returns> public static PTypeModel GetPTypeModel(string typeId) { PTypeModel ptype = null; if (PTypeDic.ContainsKey(typeId)) { PTypeModel sourcePType = PTypeDic[typeId]; SetPTypeNote(sourcePType); //new 新对象,因为属性列表需要过滤 IsJsonIgnore != true ptype = new PTypeModel() { Id = sourcePType.Id, TypeName = sourcePType.TypeName, Summary = sourcePType.Summary, SourceType = sourcePType.SourceType, IsIEnumerable = sourcePType.IsIEnumerable, EnumItemId = sourcePType.EnumItemId, EnumItemName = sourcePType.EnumItemName, PiList = sourcePType.PiList?.Where(pi => pi.IsJsonIgnore != true).ToList() }; } return(ptype); }