Skip to content

Commit

Permalink
Merge pull request #48 from besscroft/dev
Browse files Browse the repository at this point in the history
feat: 新增备份数据功能
  • Loading branch information
besscroft authored Apr 13, 2024
2 parents 07aa513 + 3a2e401 commit 00892ed
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
51 changes: 50 additions & 1 deletion pages/admin/system.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script setup lang="ts">
import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'
import { breakpointsTailwind, useBreakpoints, useDateFormat, useNow } from '@vueuse/core'
import type { FormError, FormSubmitEvent } from '#ui/types'
const breakpoints = useBreakpoints(breakpointsTailwind)
const mdAndLarger = breakpoints.greaterOrEqual('md')
const toast = useToast()
const userLoading = ref<boolean>(false)
const storageLoading = ref<boolean>(false)
const backupLoading = ref<boolean>(false)
const storageInfo = ref()
const user = useUserStore()
const showS3Modal = ref<boolean>(false)
Expand Down Expand Up @@ -171,6 +172,39 @@ const updateAlist = async () => {
}
}
const backupHandle = async () => {
try {
backupLoading.value = true
const res = await $fetch('/api/getImageJson', {
method: 'get',
headers: {
Authorization: `${user.tokenName} ${user.token}`,
},
})
if (res?.code === 200) {
toast.add({ title: '备份数据获取成功!', timeout: 2000 })
const blob = new Blob([JSON.stringify(res?.data)], {
type: 'application/json',
})
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD-HH-mm-ss')
a.download = `kamera-backup-${formatted.value}.json`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
} else {
toast.add({ title: '备份数据获取失败!', timeout: 2000, color: 'red' })
}
} catch (e) {
console.log(e)
toast.add({ title: '备份数据获取失败!', timeout: 2000, color: 'red' })
} finally {
backupLoading.value = false
}
}
onMounted(async () => {
await getStorageInfo()
})
Expand Down Expand Up @@ -205,6 +239,21 @@ definePageMeta({
</UButton>
</UForm>
</div>
<UDivider
label="备份"
my-2
:ui="{ label: 'text-primary-500 dark:text-primary-400' }"
/>
<UButton
icon="i-carbon-data-backup"
size="sm"
color="white"
variant="solid"
label="备份"
:trailing="false"
@click="backupHandle"
:loading="backupLoading"
/>
</el-card>
</div>
</template>
Expand Down
19 changes: 19 additions & 0 deletions server/api/getImageJson.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sql from '~/config/db'

export default defineEventHandler(async (event) => {
const data = await sql`
SELECT
*
FROM
public.kamera_image
WHERE
del = 0
ORDER BY sort DESC, create_time DESC, update_time DESC
`

if (!data) {
return Response.json({ code: 500, message: '没有数据!', data: null})
}

return Response.json({ code: 200, message: 'success', data: data})
})

1 comment on commit 00892ed

@vercel
Copy link

@vercel vercel bot commented on 00892ed Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.