视图类配置项:

  1. /**
  2. * 视图配置
  3. */
  4. export class ViewConfig {
  5. /**
  6. * 配置名称
  7. */
  8. Name: string = "";
  9. /**
  10. * 标题字段
  11. */
  12. Title: string = "";
  13. /**
  14. * 配置代码
  15. */
  16. Code: string = "";
  17. /**
  18. * 配置描述
  19. */
  20. Desc: string = "";
  21. /**
  22. * 区域字段名称
  23. */
  24. AreaCodeField: string = "";
  25. /**
  26. *
  27. */
  28. Fields: ViewConfigFields[] = [];
  29. /**
  30. * 默认对象,支持传入个Class类
  31. */
  32. Default: any = {};
  33. /**
  34. * 主键
  35. */
  36. PK: string = "";
  37. /**
  38. * 关键词查询
  39. */
  40. Keyword: ViewConfigKeyword = new ViewConfigKeyword
  41. /**
  42. * 默认查询条件
  43. */
  44. Where: SearchWhere = new SearchWhere()
  45. /**
  46. * 返回的结果集
  47. */
  48. Result: SearchResult<any> = new SearchResult();
  49. /**
  50. * 导出过滤方法
  51. */
  52. ExportMethod?: (d: any) => { [index: string]: string | number };
  53. /**
  54. * 格式化函数,传入当前列的数据部分,返回格式化后的内容字符串
  55. */
  56. Format?: Function;
  57. /**
  58. * 查询请求,传入SearchWhere结构,要求返回SearchResult结构
  59. */
  60. Search?: (w: SearchWhere) => Promise<SearchResult<any>>
  61. /**
  62. * 保存对象接口,传入整个对象数据,返回true/false,
  63. * 返回后会自动更新列表
  64. * @param d
  65. */
  66. Save(d: any) { return true; }
  67. /**
  68. * 删除指定对象,传入对象数据,返回boolean
  69. * 自动更新列表
  70. */
  71. Del?: (d: number[]) => boolean
  72. Dels?: (d: number[]) => boolean
  73. /**
  74. * 添加接口,常用于导入或单个添加,
  75. */
  76. Adds?: (d: any[]) => any[]
  77. /**
  78. * 数据结果过滤函数
  79. */
  80. Filter?: (d: any[]) => any[]
  81. /**
  82. * 配置显示模式
  83. */
  84. Show: ViewConfigShow = new ViewConfigShow
  85. constructor(conf?: ViewConfig | any) {
  86. if ('object' == typeof conf && conf !== undefined) {
  87. for (let x in conf) {
  88. (<any>this)[x] = conf[x];
  89. }
  90. }
  91. }
  92. /**
  93. * 链式调用设定属性,
  94. * @param key 属性名称
  95. * @param val 属性值
  96. */
  97. $set(key: string, val: any) {
  98. (<any>this)[key] = val;
  99. return this;
  100. }
  101. }

视图字段配置项:


    /**
     * 字段配置
     */
    export class ViewConfigFields {
        /**
         * 字段名称
         */
        Name: string = "";
        /**
         * 显示的字段名称
         */
        Title: string = "";
        /**
         * 主键
         */
        Primary: boolean = false;
        /**
         * 可导入,是否显示到导入列中
         */
        Importable: boolean = true;
        /**
         * 排序
         */
        Sort: ViewConfigFieldsSort = new ViewConfigFieldsSort;
        /**
         * 是否可排序
         */
        Sortable: boolean = false;
        /**
         * 字段代码
         */
        Code: string = "";
        /**
         * 字段描述
         */
        Desc: string = "";
        /**
         * 计量单位
         */
        Unit: string = "";
        /**
         * 类型
         */
        _Type: ViewConfigFieldsType = ViewConfigFieldsType.String;

        get Type() {
            return this._Type;
        }
        set Type(t) {
            if (t == ViewConfigFieldsType.Sex) {
                this.Enum = [
                    { Name: '男', Code: 1 },
                    { Name: '女', Code: 0 },
                    { Name: '未知', Code: -1 },
                ]
                // this.Type=ViewConfigFieldsType
            }
            this._Type = t;
        }
        /**
         * 开始支持Render函数
         */
        Render?: (h: CreateElement, Data: any, Field: ViewConfigFields, Result: SearchResult<any>) => CreateElement[]
        /**
         * 默认值
         */
        Default: Function | string = "";
        /**
         * 规则验证,支持正则表达式或函数,回调参数为整个对象及键
         */
        Validator: Function | RegExp = /.+/;
        /**
         * 是否可编辑,支持布尔值或函数,回调参数为整个对象
         */
        Editable: Function | boolean | ViewConfigFieldsShow = true
        /**
         * 显示控制
         */
        Show: ViewConfigFieldsShow = new ViewConfigFieldsShow
        /**
         * 在编辑和详情的情况下做响应式
         */
        Span: ViewConfigFieldsSpan = new ViewConfigFieldsSpan
        /**
         * 字典数据设定
         */
        $Enum: string | Function | any[] = '';
        get Enum() { return this.$Enum }
        set Enum(e: string | Function | any[]) {
            if ('string' == typeof e) {
                let i = 0;
                e = e.split(',').map((o) => {
                    return {
                        Name: o,
                        Code: i++
                    }
                })
            } else if (e instanceof Function) {
                (async () => {
                    let rs = await e();
                    this.$Enum = rs;
                })()
            }
            this.$Enum = e;
        }
        /**
         * 临时
         */
        Options: any[] = []
        /**
         * 直接传递给编辑模式下的组件的
         */
        Props: any = {}
        /**
         * 文件上传的
         */
        Accept: string = "";
        /**
         * 格式化
         */
        Format: string | Function = "";

        Filter?: (data: any[]) => any[];
        /**
         * 是否必填,作用于输入框
         */
        Required: boolean = false;
        /**
         * 输入框
         */
        Placeholder: string = ""
        /**
         * 外键配置
         */
        Foreign?: ViewConfig

        constructor(conf?: any) {
            if ('object' == typeof conf && conf !== undefined) {
                for (let x in conf) {
                    switch (x) {
                        case 'Show':
                            this.Show = Object.assign(new ViewConfigFieldsShow(), conf.Show)
                            break;
                        case 'Span':
                            this.Span = Object.assign(new ViewConfigFieldsSpan(), conf.Span)
                            break;
                        case 'Sort':
                            this.Sort = Object.assign(new ViewConfigFieldsSort(), conf.Sort)
                            break;
                        default:
                            (<any>this)[x] = conf[x]; break;
                    }
                }
            }
        }
        /**
         * 动态属性设置
         * @param key 
         * @param val 
         */
        $set(key: string, val: any) {
            (<any>this)[key] = val;
            return this;
        }
    }

控制视图的显示范围:


    export class ViewConfigFieldsShow {
        /**
         * 该字段是否在列表中显示
         */
        List: boolean | Function = true;
        /**
         * 该字段是否在编辑模式下显示
         */
        Edit: boolean | Function = true;
        /**
         * 该字段是否在添加模式下显示
         */
        Add: boolean | Function = true;
        /**
         * 该字段是否在详情模式下显示
         */
        Detail: boolean | Function = true;
        /**
         * 该字段是否在查询条件模式下显示
         */
        Where: boolean = false;

        constructor(conf?: any) {
            if ('object' == typeof conf && conf !== undefined) {
                for (let x in conf) {
                    (<any>this)[x] = conf[x];
                }
            }
        }
        /**
         * 
         * @param key 
         * @param val 
         */
        $set(key: string, val: any) {
            (<any>this)[key] = val;
            return this;
        }
    }

字段类型设定:


    /**
     * 字段类型配置
     */
    export enum ViewConfigFieldsType {
        /**
         * 普通字符串
         */
        String = "String",
        /**
         * 区域代码 如:511111001
         */
        AreaCode = "AreaCode",
        /**
         * 身份证
         */
        IDCard = "IDCard",
        /**
         * 性别 0 男 1 女 -1 未知
         */
        Sex = "Sex",
        /**
         * 选择框,Select选择
         */
        SelectMap = "Select.Map",
        /**
         * 选择框,按钮选择
         */
        ButtonMap = "ButtonMap",
        /**
         * HTML内容,用v-html渲染
         */
        HTML = "HTML",
        /**
         * 单图
         */
        Img = "Img",
        /**
         * 多图
         */
        Imgs = "Imgs",
        /**
         * 外键
         */
        ForeignTable = "foreign.table",
        /**
         * 外键-一次性提取全部数据,适用于数量小于1000的情况下
         */
        ForeignTableAll = "foreign.table.all",
        /**
         * 时间日期
         */
        DateTime = "DateTime",
        /**
         * 头像
         */
        Avatar = "Avatar",
        /**
         * 金额
         */
        Money = "Money",
        /**
         * 密码输入框
         */
        PWD = "PWD",
        /**
         * 富文本编辑器
         */
        Editor = "Editor",
        /**
         * 数字
         */
        Number = "Number",
        /**
         * 分割线
         */
        Divider = "Divider",
        /**
         * 多行文本
         */
        Textarea = "Textarea",
    }

字段类型示例图:

string

image.png

textarea

image.png

ButtonMap

image.png

Img Imgs

image.png

外键 ForeignTable ForeignTableAll

image.png

富文本编辑器 Editor

image.png

便捷化视图配置辅助方法:

配置视图字段:


    /**
     * 配置字段
     * @param Name 
     * @param Code 
     * @param Desc 
     * @param Default 
     * @param Type 
     * @param Show 
     */
    export function configFields(Name: string, Code: string, Desc: string = '', Default: string | number | Function = '', Type: ViewConfigFieldsType = ViewConfigFieldsType.String, Show?: ViewConfigFieldsShow) {
        return new ViewConfigFields({
            Name, Code, Desc, Default, Type, Show
        })
    }

配置视图:


    /**
     * 配置生成ViewConfig
     * @param Name 
     * @param Code 
     * @param PK 
     * @param Fields 
     * @param Methods 
     * @param Conf 
     */
    export function configConfig<T>(Name: string, Code: string, PK: string, Fields: ViewConfigFields[], Methods: ViewConfigMethods<T>, Conf: { Where?: SearchWhere, Default?: any } = {}) {
        return Object.assign(new ViewConfig, { Name, Code, PK, Fields, Where: Conf.Where || new SearchWhere, Default: Conf.Default || {} }, Methods)
    }

配置视图显示控制:


    /**
     * 生成Show配置
     * @param List 
     * @param Edit 
     * @param Add 
     * @param Detail 
     * @param Where 
     */
    export function configShow(List: boolean = false, Edit: boolean = false, Add: boolean = false, Detail: boolean = false, Where: boolean = false) {
        return new ViewConfigFieldsShow({
            List, Edit, Add, Where, Detail
        })
    }

配置案例1 随便举例的个人认证数据表的显示:

数据表结构:

image.png

配置对象:

import Ctsy from "@ctsy/template";
import { SearchWhere } from '@ctsy/api-sdk/dist/lib';

var conf = Ctsy.configConfig('个人认证', 'Personal', 'PID', [
    Ctsy.configFields('用户编号', 'UID', '', '', Ctsy.ViewConfigFieldsType.ForeignTable).$set('Forgeign', {
        Name: "创建人",
        Code: "User",
        PK: "UID",
        Title: "Nick",
        Fields: [Ctsy.configFields("名称", "Name")],
    }),
    Ctsy.configFields('姓名', 'Name'),
    Ctsy.configFields('手机号', 'Phone'),
    Ctsy.configFields('身份证', 'IDCard'),
    Ctsy.configFields('审核状态', 'Status', '', '', Ctsy.ViewConfigFieldsType.ButtonMap).$set('Enum', [
        { Name: '通过', Code: 1 },
        { Name: '拒绝', Code: 0 }
    ]),
], {
    Adds: () => { throw new Error('请实现添加接口') },
    Save: () => { throw new Error('请实现保存接口') },
    Del: () => { throw new Error('请实现删除接口') },
    Search: (w: SearchWhere) => { throw new Error('请实现查询接口') },
}, {
    Where: Object.assign(new SearchWhere(), {
        W: {
            //额外的查询条件,如设定状态为通过
            Status: 1
        }
    }),
    Default: {
        UID: 0,
        PID: 0,
        JID: 0,
        Name: '',
        Phone: '',
        IDCard: '',
        Status: 0,
    }
})