Ejemplo n.º 1
0
        private FormItemTagHelper CreateItemTag(string itemName, string itemTitle, TextboxTagHelper contentTag, int itemSort = int.MaxValue)
        {
            var tag = new FormItemTagHelper();

            tag.Field      = itemName;
            tag.Title      = itemTitle;
            tag.Sort       = itemSort;
            tag.ContentTag = contentTag;
            if (ItemWidth.HasValue && !tag.ColSpan.HasValue && contentTag.IsMultiline != true)
            {
                tag.Width = $"{ItemWidth}px";
            }
            return(tag);
        }
Ejemplo n.º 2
0
        private void CreateFormItem()
        {
            if (ModelTagHelper.HasModel)
            {
                var remarkName = nameof(IHasRemark.Remark);
                var index      = 0;
                foreach (var property in ModelTagHelper.PropertyInfos)
                {
                    var name         = property.Name;
                    var isId         = name == nameof(WebConsts.Id);
                    var propertyType = property.PropertyType;
                    if (property.IsDefined(typeof(NotFormItemAttribute), false))
                    {
                        continue;
                    }
                    var isFileType = propertyType == typeof(IFormFile);
                    if (!propertyType.IsValueType() && !isFileType)
                    {
                        continue;
                    }
                    if (isId && !HasId)
                    {
                        continue;
                    }
                    if (property.IsDefined(typeof(HideFormItemAttribute), false) || isId && !HasId)
                    {
                        _hideItems.Add(property.Name);
                        continue;
                    }

                    var attributes = property.GetCustomAttributes(true);
                    var itemAttr   = GetAttribute <FormItemAttribute>(attributes);

                    if (name == remarkName && ModelTagHelper.IsRemarkType)
                    {
                        var remarkTag = new TextboxTagHelper();
                        remarkTag.IsMultiline = true;
                        remarkTag.MaxLength   = DomainConsts.MaxDescLength;

                        var textAreaTag = CreateItemTag(remarkName, WebConsts.DisplayName_Remark, remarkTag);

                        textAreaTag.Width = itemAttr == null || itemAttr.Width.IsNullOrWhiteSpace() ? "100%" : itemAttr.Width;
                        _textAreas.Add(textAreaTag);

                        continue;
                    }

                    index++;


                    var valueType    = propertyType.GetValueType();
                    var isNullType   = propertyType.IsNullableType();
                    var isEnum       = valueType.IsEnum;
                    var isValueType  = propertyType.IsValueType && !isNullType;
                    var isName       = ModelTagHelper.IsNameType && name == nameof(IHasName.Name);
                    var isDisable    = GetAttribute <DisableAttribute>(attributes) != null || !property.CanWrite;
                    var colNameAttr  = GetAttribute <DisplayNameAttribute>(attributes);
                    var requiredAttr = GetAttribute <RequiredAttribute>(attributes);
                    var comboboxAttr = GetAttribute <ComboboxAttribute>(attributes);

                    var isCombo = comboboxAttr != null || propertyType == typeof(bool) || propertyType == typeof(bool?) || valueType.IsEnum;
                    var colName = colNameAttr == null ? name : colNameAttr.DisplayName;


                    if (name == colName)
                    {
                        if (isFileType)
                        {
                            colName = "文件";
                        }
                        else if (isEnum)
                        {
                            colName = valueType.GetDescription();
                        }
                        else
                        {
                            switch (name)
                            {
                            case nameof(IHasRemark.Remark):
                                colName = WebConsts.DisplayName_Remark;
                                break;

                            case nameof(IHasName.Name):
                                colName = WebConsts.DisplayName_Name;
                                break;

                            case nameof(WebConsts.IsActive):
                                colName = WebConsts.IsAble_Name;
                                break;

                            case nameof(IHasSort.Sort):
                                colName = WebConsts.Sort_Name;
                                break;

                            case nameof(WebConsts.Id):
                                colName = IdName;
                                break;

                            default:
                                if (valueType.IsEnum)
                                {
                                    colName = valueType.GetDisplayName();
                                }
                                break;
                            }
                        }
                    }

                    var isTextArea = false;
                    var isRequired = isId || (isName && !IsSearch) || isValueType || requiredAttr != null;
                    int maxLength  = DomainConsts.DefaultStringLength;

                    TextboxTagHelper itemTag = null;



                    if (isCombo)
                    {
                        var defaultValueAttr = GetAttribute <DefaultValueAttribute>(attributes);
                        var defaultIndexAttr = GetAttribute <DefaultIndexAttribute>(attributes);


                        var tag = new ComboboxTagHelper
                        {
                            DefaultValue = defaultValueAttr?.Value,
                            DefaultIndex = defaultIndexAttr?.Index
                        };

                        if (isEnum)
                        {
                            tag.ModelType = valueType;
                        }
                        else if (comboboxAttr != null)
                        {
                            if (comboboxAttr.IsLoadData)
                            {
                                tag.ModelType = comboboxAttr.Type;

                                var comboAttr = GetAttribute <ComboboxAttribute>(attributes);


                                if (comboAttr != null)
                                {
                                    if (comboAttr.WhereField.IsNotNullOrWhiteSpace())
                                    {
                                        tag.WhereField = comboAttr.WhereField;
                                        tag.WhereValue = comboAttr.WhereValue;
                                        tag.WhereOper  = comboAttr.WhereOper;
                                    }

                                    tag.IsEdit = !comboAttr.IsReadOnly;
                                }
                            }

                            if (colNameAttr == null && comboboxAttr.DisplayName != null)
                            {
                                colName = comboboxAttr.DisplayName;
                            }
                        }
                        else if (valueType == typeof(bool))
                        {
                            tag.Class.Add("boolCombobox");
                        }

                        itemTag = tag;
                    }
                    else if (propertyType.IsNumberType())
                    {
                        var precisionAttr = GetAttribute <NumberFormatAttribute>(attributes);
                        itemTag = new NumberboxTagHelper
                        {
                            Precision = precisionAttr?.Precision
                        };
                    }
                    else if (valueType == typeof(DateTime))
                    {
                        var datetimeAttr = GetAttribute <DateFormatAttribute>(attributes);
                        if (datetimeAttr == null)
                        {
                            itemTag = new DateboxTagHelper();
                        }
                        else
                        {
                            itemTag = new DateTimeboxTagHelper();
                        }
                    }
                    else if (isFileType)
                    {
                        itemTag = new FileTagHelper();
                    }
                    else
                    {
                        var maxLengthAttr = GetAttribute <MaxLengthAttribute>(attributes);
                        isTextArea = GetAttribute <TextAreaAttribute>(attributes) != null;

                        var tag = new TextboxTagHelper();
                        if (maxLengthAttr != null)
                        {
                            maxLength = maxLengthAttr.Length;
                        }

                        var stringLength = GetAttribute <StringLengthAttribute>(attributes)?.MaximumLength;
                        if (stringLength.HasValue)
                        {
                            maxLength = stringLength.Value;
                        }

                        var isEmail = GetAttribute <EmailAddressAttribute>(attributes) != null;
                        if (isEmail)
                        {
                            tag.ValidTypes.Add("email");
                        }

                        var isIdCard = GetAttribute <IdCardAttribute>(attributes) != null;
                        if (isIdCard)
                        {
                            tag.ValidTypes.Add("idCard");
                        }

                        var isPhone = GetAttribute <PhoneAttribute>(attributes) != null || GetAttribute <PhoneNumberAttribute>(attributes) != null;
                        if (isPhone)
                        {
                            tag.ValidTypes.Add("phone");
                        }

                        var isMobilePhone = GetAttribute <MobilePhoneAttribute>(attributes) != null;
                        if (isMobilePhone)
                        {
                            tag.ValidTypes.Add("mobile");
                        }

                        if (isTextArea)
                        {
                            tag.IsMultiline = true;
                            if (maxLengthAttr == null)
                            {
                                maxLength = DomainConsts.MaxDescLength;
                            }
                        }

                        itemTag = tag;
                    }



                    if (isDisable)
                    {
                        itemTag.IsDisable = isDisable;
                    }

                    itemTag.IsRequired = isRequired;
                    itemTag.MaxLength  = maxLength;

                    FormItemTagHelper formItem;

                    if (isTextArea)
                    {
                        formItem = CreateItemTag(name, colName, itemTag, index);
                        _textAreas.Add(formItem);
                    }
                    else
                    {
                        formItem = CreateItemTag(name, colName, itemTag, isId ? 0 : index);
                        _formItems.Add(formItem);
                    }


                    if (itemAttr != null)
                    {
                        if (itemAttr.After.IsNotNullOrWhiteSpace())
                        {
                            formItem.After = itemAttr.After;
                        }
                        if (itemAttr.Before.IsNotNullOrWhiteSpace())
                        {
                            formItem.Before = itemAttr.Before;
                        }
                        if (itemAttr.ColSpan.HasValue)
                        {
                            formItem.ColSpan = itemAttr.ColSpan;
                        }
                        if (itemAttr.Group.IsNotNullOrWhiteSpace())
                        {
                            formItem.Group = itemAttr.Group;
                        }
                        if (itemAttr.Width.IsNotNullOrWhiteSpace())
                        {
                            formItem.Width = itemAttr.Width;
                        }
                    }
                }
            }
        }