Skip to content

Commit

Permalink
fix: base64 decode
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 28, 2024
1 parent 1dc0042 commit fec0965
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion web/src/components/common/CodeEditor.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import file from '@/api/panel/file'
import { decodeBase64 } from '@/utils'
import { languageByPath } from '@/utils/file'
import Editor from '@guolao/vue-monaco-editor'
Expand All @@ -21,7 +22,7 @@ const get = async () => {
await file
.content(props.path)
.then((res) => {
data.value = atob(res.data.content)
data.value = decodeBase64(res.data.content)
window.$message.success('获取成功')
})
.catch(() => {
Expand Down
17 changes: 17 additions & 0 deletions web/src/utils/common/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function decodeBase64(base64: string): string {
const binary = atob(base64)
const bytes = new Uint8Array(binary.length)
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i)
}
return new TextDecoder('utf-8').decode(bytes)
}

export function encodeBase64(str: string): string {
const bytes = new TextEncoder().encode(str)
let binary = ''
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i])
}
return btoa(binary)
}
1 change: 1 addition & 0 deletions web/src/utils/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './base64'
export * from './color'
export * from './common'
export * from './icon'
Expand Down
4 changes: 2 additions & 2 deletions web/src/views/task/CronView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NButton, NDataTable, NInput, NPopconfirm, NSwitch, NTag } from 'naive-u
import cron from '@/api/panel/cron'
import file from '@/api/panel/file'
import { formatDateTime, renderIcon } from '@/utils'
import { decodeBase64, formatDateTime, renderIcon } from '@/utils'
import type { CronTask } from '@/views/task/types'
import { CronNaive } from '@vue-js-cron/naive-ui'
Expand Down Expand Up @@ -209,7 +209,7 @@ const handleEdit = async (row: any) => {
editTask.value.id = row.id
editTask.value.name = row.name
editTask.value.time = row.time
editTask.value.script = atob(res.data.content)
editTask.value.script = decodeBase64(res.data.content)
editModal.value = true
})
})
Expand Down

0 comments on commit fec0965

Please sign in to comment.