import { ENCODE_RULE_ELEMENT, EncodeRuleElement } from './decorators'; import { Provider, Injectable, PlainPro } from '@notadd/core'; import { randomNumberString } from './utils/encode.utils' import { Uuid, StringUtil } from '@notadd/platform'; import { format } from 'date-fns'; import { v4 } from 'uuid'; import randomString from 'crypto-random-string'; @Injectable() export class StringEncodeRuleElement implements EncodeRuleElement { constructor(){} @PlainPro() title: string = '前/后缀'; @PlainPro() format: string = ``; async handler() { return this.format; } } @Injectable() export class SplitEncodeRuleElement implements EncodeRuleElement { @PlainPro() title: string = '分隔符'; @PlainPro() format: string = ``; @PlainPro() items: string[] = ['-', '_', ':', '.', ' ', '%', '*'] async handler() { return this.format; } } @Injectable() export class RandomStringEncodeRuleElement implements EncodeRuleElement { @PlainPro() title: string = '随机字符'; @PlainPro() length: number = 6; async handler() { return randomString({ length: this.length }); } } @Injectable() export class RandomNumberEncodeRuleElement implements EncodeRuleElement { @PlainPro() title: string = '随机数字'; @PlainPro() length: number = 6; async handler() { return randomNumberString(this.length); } } @Injectable() export class DateEncodeRuleElement implements EncodeRuleElement { @PlainPro() title: string = '时间'; @PlainPro() format: string = 'yyyyMMdd'; @PlainPro() items: { title: string, value: string }[] = [{ title: '年月日', value: 'yyyyMMdd' }, { title: '年月日时分', value: 'yyyyMMddHHMM' }, { title: '年月日时分秒', value: 'yyyyMMddHHmmss' }]; async handler() { return format(new Date(), this.format); } } @Injectable() export class TimeEncodeRuleElement implements EncodeRuleElement { @PlainPro() title: string = '时间戳'; /** * 是否带毫秒 */ @PlainPro() isMillisecond: boolean = true; async handler(): Promise { const now = new Date().getTime() return `${this.isMillisecond ? now : Math.floor(now / 1000)}`; } } @Injectable() export class UuidEncodeRuleElement implements EncodeRuleElement { constructor(){} @PlainPro() title: string = 'uuid'; @PlainPro() length: number = 16 async handler(): Promise { const uuid = v4(); return uuid.substr(0, this.length) } } @Injectable() export class PropertyEncodeRuleElement implements EncodeRuleElement { @PlainPro() title: string = '属性'; /** * 初始流水号 */ @PlainPro() property: string; async handler(instance: any): Promise { let value = instance; const properties = this.property.split('.') let property = properties.unshift(); while (property) { value = Reflect.get(instance, value) property = properties.unshift(); } return value; } } export const encodeRuleElements: Provider[] = [ PropertyEncodeRuleElement, { provide: ENCODE_RULE_ELEMENT, useClass: SplitEncodeRuleElement, multi: true }, { provide: ENCODE_RULE_ELEMENT, useClass: StringEncodeRuleElement, multi: true }, { provide: ENCODE_RULE_ELEMENT, useClass: DateEncodeRuleElement, multi: true }, { provide: ENCODE_RULE_ELEMENT, useClass: UuidEncodeRuleElement, multi: true }, { provide: ENCODE_RULE_ELEMENT, useClass: TimeEncodeRuleElement, multi: true }, { provide: ENCODE_RULE_ELEMENT, useClass: RandomNumberEncodeRuleElement, multi: true }, { provide: ENCODE_RULE_ELEMENT, useClass: RandomStringEncodeRuleElement, multi: true } ]