视图类配置项:
/** * 视图配置 */ export class ViewConfig { /** * 配置名称 */ Name: string = ""; /** * 标题字段 */ Title: string = ""; /** * 配置代码 */ Code: string = ""; /** * 配置描述 */ Desc: string = ""; /** * 区域字段名称 */ AreaCodeField: string = ""; /** * */ Fields: ViewConfigFields[] = []; /** * 默认对象,支持传入个Class类 */ Default: any = {}; /** * 主键 */ PK: string = ""; /** * 关键词查询 */ Keyword: ViewConfigKeyword = new ViewConfigKeyword /** * 默认查询条件 */ Where: SearchWhere = new SearchWhere() /** * 返回的结果集 */ Result: SearchResult<any> = new SearchResult(); /** * 导出过滤方法 */ ExportMethod?: (d: any) => { [index: string]: string | number }; /** * 格式化函数,传入当前列的数据部分,返回格式化后的内容字符串 */ Format?: Function; /** * 查询请求,传入SearchWhere结构,要求返回SearchResult结构 */ Search?: (w: SearchWhere) => Promise<SearchResult<any>> /** * 保存对象接口,传入整个对象数据,返回true/false, * 返回后会自动更新列表 * @param d */ Save(d: any) { return true; } /** * 删除指定对象,传入对象数据,返回boolean * 自动更新列表 */ Del?: (d: number[]) => boolean Dels?: (d: number[]) => boolean /** * 添加接口,常用于导入或单个添加, */ Adds?: (d: any[]) => any[] /** * 数据结果过滤函数 */ Filter?: (d: any[]) => any[] /** * 配置显示模式 */ Show: ViewConfigShow = new ViewConfigShow constructor(conf?: ViewConfig | 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 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
textarea
ButtonMap
Img Imgs
外键 ForeignTable ForeignTableAll
富文本编辑器 Editor

便捷化视图配置辅助方法:
配置视图字段:
/**
* 配置字段
* @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 随便举例的个人认证数据表的显示:
数据表结构:
配置对象:
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,
}
})