// @ts-nocheck export class RequseParmeter { constructor (name: string | undefined = undefined, value: any[] | undefined = undefined) { this.parameter = { fieldGroup: { fields: [] }, ordSort: [], pageable: {} } if (name !== undefined && value !== undefined) { this.AndFields(name, value) } this.Pageable() } // 原型上的方法 AndFields (name, value: any[] = [], oper = 'EQUAL', andOr = 'AND') { const type = ['EQUAL', 'NOT_EQUAL', 'LIKE', 'NOT_LIKE', 'BETWEEN', 'NOT_BETWEEN', 'GREATER_THAN', 'GREATER_THAN_OR_EQUAL', 'LESS_THAN', 'LESS_THAN_OR_EQUAL', 'IS_NULL', 'NOT_NULL', 'IN', 'NOT_IN'] if (!['AND', 'OR'].includes(andOr)) { throw new Error('andOr 不等于 AND || OR' + '传入的值是: ' + andOr) } if (!type.includes(oper)) { throw new Error('oper 不等于' + type + '传入的值是:' + oper) } // value 值 后端是需要的一个数组, 但是为了方便, 前端可以传递字符串和数组 // 只允许 传递 字符串 和 数组 || 数值 if (!['[object String]', '[object Date]', '[object Array]', '[object Number]'].includes(Object.prototype.toString.call(value))) { throw new Error('value 不是 数组 || 字符串 || 数值, 传入的是:' + Object.prototype.toString.call(value)) } // 处理 value if (Object.prototype.toString.call(value) === '[object Array]') { if (value.length === 0) { throw new Error('数组的值不能为空!!!') } if (oper === 'LIKE') { for (const index in value) { value[index] = `%${value[index]}%` } } } else { if (oper === 'LIKE') { value = [`%${value}%`] } else { value = [value] } } this.parameter.fieldGroup.fields.push({ andOr: andOr, name: name, oper: oper, value: value }) return this } Sort (name, order) { if (order !== 'ASC' && order !== 'DESC') { throw new Error('排序的 value 不等于 ASC || DESC') } this.parameter.ordSort.push({ name: name, order: order }) return this } Pageable (pageNum = 1, pageSize = 20, pageable = true) { if (pageable === false) { this.parameter.pageable = { pageable: pageable } return this } // 只允许输入整数 const reg = /^\d+$/ if (!(reg.test(pageNum) && reg.test(pageSize))) { throw new Error('只能是整数') } if (Object.prototype.toString.call(pageable) !== '[object Boolean]') { throw new Error('分页的开关必须是 布尔值') } this.parameter.pageable = { pageNum: pageNum, pageSize: pageSize, pageable: pageable } return this } }