Quellcode durchsuchen

图片返回处理

wuyi vor 1 Monat
Ursprung
Commit
4ac93e2c77

+ 4 - 2
src/controllers/file.controller.ts

@@ -98,11 +98,13 @@ export class FileController {
         maxSize: 15 * 1024 * 1024 // 15MB
       })
 
+      const signedUrl = await this.fileService.getSignedUrl(result.key)
+
       return reply.send({
         message: '图片上传成功',
         data: {
-          ...result,
-          dateFolder: new Date().toISOString().split('T')[0] // YYYY-MM-DD格式
+          key: result.key,
+          url: signedUrl
         }
       })
     } catch (error) {

+ 21 - 0
src/controllers/person-info.controller.ts

@@ -21,6 +21,13 @@ export class PersonInfoController {
     try {
       const { qrCode, ...data } = request.body
 
+      if (data.photoUrl) {
+        try {
+          const urlObj = new URL(data.photoUrl)
+          data.photoUrl = urlObj.pathname.substring(1)
+        } catch (error) {}
+      }
+
       const personInfo = await this.personInfoService.create(qrCode, data)
 
       return reply.code(201).send({
@@ -37,6 +44,13 @@ export class PersonInfoController {
     try {
       const { qrCode, maintenanceCode, ...data } = request.body
 
+      if (data.photoUrl) {
+        try {
+          const urlObj = new URL(data.photoUrl)
+          data.photoUrl = urlObj.pathname.substring(1)
+        } catch (error) {}
+      }
+
       const personInfo = await this.personInfoService.update(qrCode, maintenanceCode, data)
 
       return reply.send({
@@ -78,6 +92,13 @@ export class PersonInfoController {
         return reply.code(400).send({ message: '请提供二维码ID' })
       }
 
+      if (data.photoUrl) {
+        try {
+          const urlObj = new URL(data.photoUrl)
+          data.photoUrl = urlObj.pathname.substring(1)
+        } catch (error) {}
+      }
+
       const personInfo = await this.personInfoService.adminUpdate(qrCodeId, data)
 
       return reply.send({

+ 21 - 0
src/controllers/pet-info.controller.ts

@@ -16,6 +16,13 @@ export class PetInfoController {
     try {
       const { qrCode, ...data } = request.body
 
+      if (data.photoUrl) {
+        try {
+          const urlObj = new URL(data.photoUrl)
+          data.photoUrl = urlObj.pathname.substring(1)
+        } catch (error) {}
+      }
+
       const petInfo = await this.petInfoService.create(qrCode, data)
 
       return reply.code(201).send({
@@ -32,6 +39,13 @@ export class PetInfoController {
     try {
       const { qrCode, maintenanceCode, ...data } = request.body
 
+      if (data.photoUrl) {
+        try {
+          const urlObj = new URL(data.photoUrl)
+          data.photoUrl = urlObj.pathname.substring(1)
+        } catch (error) {}
+      }
+
       const petInfo = await this.petInfoService.update(qrCode, maintenanceCode, data)
 
       return reply.send({
@@ -73,6 +87,13 @@ export class PetInfoController {
         return reply.code(400).send({ message: '请提供二维码ID' })
       }
 
+      if (data.photoUrl) {
+        try {
+          const urlObj = new URL(data.photoUrl)
+          data.photoUrl = urlObj.pathname.substring(1)
+        } catch (error) {}
+      }
+
       const petInfo = await this.petInfoService.adminUpdate(qrCodeId, data)
 
       return reply.send({

+ 7 - 1
src/services/person-info.service.ts

@@ -57,7 +57,13 @@ export class PersonInfoService {
     })
 
     if (!personInfo) {
-      throw new Error('人员信息不存在')
+      const created = this.personInfoRepository.create({
+        ...data,
+        qrCodeId: qrCodeEntity.id
+      })
+      const result = await this.personInfoRepository.save(created)
+      await this.qrCodeService.activateQrCode(qrCodeEntity.id)
+      return result
     }
 
     await this.personInfoRepository.update(personInfo.id, data)