diff --git a/app.go b/app.go deleted file mode 100644 index a63e2dd..0000000 --- a/app.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "context" - "os" -) - -// App struct -type App struct { - ctx context.Context -} - -// NewApp creates a new App application struct -// NewApp 创建一个新的 App 应用程序 -func NewApp() *App { - return &App{} -} - -// startup is called at application startup -// startup 在应用程序启动时调用 -func (a *App) startup(ctx context.Context) { - // Perform your setup here - // 在这里执行初始化设置 - a.ctx = ctx -} - -// domReady is called after the front-end dom has been loaded -// domReady 在前端Dom加载完毕后调用 -func (a *App) domReady(ctx context.Context) { - // Add your action here - // 在这里添加你的操作 -} - -// beforeClose is called when the application is about to quit, -// either by clicking the window close button or calling runtime.Quit. -// Returning true will cause the application to continue, -// false will continue shutdown as normal. -// beforeClose在单击窗口关闭按钮或调用runtime.Quit即将退出应用程序时被调用. -// 返回 true 将导致应用程序继续,false 将继续正常关闭。 -func (a *App) beforeClose(ctx context.Context) (prevent bool) { - return false -} - -// shutdown is called at application termination -// 在应用程序终止时被调用 -func (a *App) shutdown(ctx context.Context) { - // Perform your teardown here - // 在此处做一些资源释放的操作 -} - -func (a *App) Exit() { - os.Exit(0) -} diff --git a/frontend/package.json b/frontend/package.json index 610e0b0..d14e5a0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -19,6 +19,7 @@ "@toast-ui/vue-editor": "^3.2.3", "@types/prismjs": "^1.26.3", "@vueuse/core": "^10.7.2", + "axios": "^1.6.7", "element-plus": "^2.5.5", "eruda": "^3.0.1", "pinia": "^2.0.21", diff --git a/frontend/package.json.md5 b/frontend/package.json.md5 index 76251ad..0114ee1 100644 --- a/frontend/package.json.md5 +++ b/frontend/package.json.md5 @@ -1 +1 @@ -ed9d649d63f25396331411c05754112e \ No newline at end of file +c5ef7295ec7f4bb6126fdb8f810346b1 \ No newline at end of file diff --git a/frontend/src/App.vue b/frontend/src/App.vue index b59cda4..fd1c5c6 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,16 +1,26 @@ diff --git a/frontend/src/main.ts b/frontend/src/main.ts index fac5fd4..6467c02 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -1,13 +1,14 @@ import { createApp } from "vue"; import { createPinia } from "pinia"; +import "./style.scss"; import App from "./App.vue"; import router from "./router"; import i18n from "./i18n"; import ElementPlus from "element-plus"; import "element-plus/dist/index.css" -import "./style.scss"; + // import "./assets/main.css"; diff --git a/frontend/src/utils/axios/index.ts b/frontend/src/utils/axios/index.ts new file mode 100644 index 0000000..4eeb8c8 --- /dev/null +++ b/frontend/src/utils/axios/index.ts @@ -0,0 +1,63 @@ +import type {AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig} from "axios"; +import axios from "axios"; +import {ElNotification} from "element-plus"; + +export function getAxiosInstance(axiosRequestConfig: AxiosRequestConfig): AxiosInstance { + let instance = axios.create(axiosRequestConfig); + // 全局 请求拦截器 + instance.interceptors.request.use(async (config: InternalAxiosRequestConfig) => { + let headers = config.headers || {} + return config; + }) + // 全局 响应拦截器 + instance.interceptors.response.use(successCB, rejectCB); + return instance; +} + + +// 请求成功回调 +export function successCB(response: AxiosResponse | Promise): AxiosResponse | Promise { + function processResponse(response: AxiosResponse) { + if (response.status === 200) { // 响应成功 + if (response.data.code === 401) { + ElNotification.warning({ + message: "登录状态失效, 请重新登录", + }) + console.log(response,response.data.msg) + } else if (response.data.code === 403) { + setTimeout(() => { + console.log(response,response.data.msg) + }, 0) + } + return response; + } else { + return Promise.reject(response); + } + } + + if (response instanceof Promise) { + let resp: AxiosResponse + return new Promise((resolve)=>{ + response.then((r: AxiosResponse) => { + resp = r; + resolve(processResponse(resp)) + }); + }) + } else { + return processResponse(response); + } +} + +// 请求失败回调 +export function rejectCB(error: any): any { + let response = error.response + if(response.status === 401){ + return response; + } else if(response.status === 403){ + setTimeout(() => { + console.log(response.data.msg) + }, 0) + return response; + } + return Promise.reject(error); +} diff --git a/frontend/src/utils/file/file.ts b/frontend/src/utils/file/file.ts new file mode 100644 index 0000000..4a8d86d --- /dev/null +++ b/frontend/src/utils/file/file.ts @@ -0,0 +1,92 @@ +export interface FileUnitType { + size: number, + unit: string +} + +export const FileUnit: Record = { + B: { + size: 1, + unit: "B", + }, + KB: { + size: 2 ** 10, + unit: "KB", + }, + MB: { + size: 2 ** (10 * 2), + unit: "MB", + }, + GB: { + size: 2 ** (10 * 3), + unit: "GB", + }, + TB: { + size: 2 ** (10 * 4), + unit: "TB", + }, + PB: { + size: 2 ** (10 * 5), + unit: "PB", + }, + EB: { + size: 2 ** (10 * 6), + unit: "EB", + }, + ZB: { + size: 2 ** (10 * 7), + unit: "ZB", + }, + YB: { + size: 2 ** (10 * 8), + unit: "YB", + }, + DB: { + size: 2 ** (10 * 9), + unit: "DB", + }, + NB: { + size: 2 ** (10 * 10), + unit: "NB", + }, +} + +export function convertToFileSizeWithUnit(anySize:string, defaultUnit:FileUnitType = FileUnit.B){ + let units = Object.values(FileUnit).map(item=>item.unit) + units = units.concat(Object.values(FileUnit).slice(1).map(item=>item.unit.replace("B",""))) + let unitsReg = units.toString().replace(/,/g,"|") + let reg = new RegExp(`^(\\d+\\.?\\d*)(\\s*(${unitsReg})?)?$`,"i") + + let matchResult = anySize.match(reg) + if(!matchResult){ + return "未知大小" + } else { + if(!matchResult[3]){ + return bytesToSizeWithUnit(Number(matchResult[1])) + } else { + return calcSize(Number(matchResult[1]), defaultUnit) + } + } +} + +export function bytesToSizeWithUnit(byteSize?:number){ + if(byteSize == null || byteSize < 0){ + return `0${FileUnit.B.unit}` + } + + let targetUnitIndex = Object.values(FileUnit).findIndex(item => item.size > byteSize) || 0 + let targetUnit:FileUnitType + if(targetUnitIndex > 0){ + targetUnit = Object.values(FileUnit)[targetUnitIndex - 1] + } else { + targetUnit = Object.values(FileUnit)[0] + } + return `${calcSize(byteSize,targetUnit)} ${targetUnit.unit}` +} + +export function numberToHz(number:number){ + return bytesToSizeWithUnit(number).replace(FileUnit.B.unit,"") +} + +export function calcSize(byteSize:number,targetUnit:FileUnitType){ + return Number((byteSize / targetUnit.size).toFixed(2)) +} diff --git a/frontend/wailsjs/go/core/App.d.ts b/frontend/wailsjs/go/core/App.d.ts index c83e972..5da4bab 100644 --- a/frontend/wailsjs/go/core/App.d.ts +++ b/frontend/wailsjs/go/core/App.d.ts @@ -1,9 +1,17 @@ // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT import {context} from '../models'; +import {config} from '../models'; +import {frontend} from '../models'; export function Ctx():Promise; export function ForceClose():Promise; +export function GetConfig():Promise; + +export function OpenFileDialog(arg1:frontend.OpenDialogOptions):Promise; + export function ReloadApp():Promise; + +export function SaveFileDialog(arg1:frontend.SaveDialogOptions):Promise; diff --git a/frontend/wailsjs/go/core/App.js b/frontend/wailsjs/go/core/App.js index 3d538d4..49cc835 100644 --- a/frontend/wailsjs/go/core/App.js +++ b/frontend/wailsjs/go/core/App.js @@ -10,6 +10,18 @@ export function ForceClose() { return window['go']['core']['App']['ForceClose'](); } +export function GetConfig() { + return window['go']['core']['App']['GetConfig'](); +} + +export function OpenFileDialog(arg1) { + return window['go']['core']['App']['OpenFileDialog'](arg1); +} + export function ReloadApp() { return window['go']['core']['App']['ReloadApp'](); } + +export function SaveFileDialog(arg1) { + return window['go']['core']['App']['SaveFileDialog'](arg1); +} diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts new file mode 100644 index 0000000..f53e0ac --- /dev/null +++ b/frontend/wailsjs/go/models.ts @@ -0,0 +1,64 @@ +export namespace config { + + export class DebuggerConfig { + enable: boolean; + + static createFrom(source: any = {}) { + return new DebuggerConfig(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.enable = source["enable"]; + } + } + export class ServerConfig { + host: string; + port: number; + + static createFrom(source: any = {}) { + return new ServerConfig(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.host = source["host"]; + this.port = source["port"]; + } + } + export class Config { + server?: ServerConfig; + debug?: DebuggerConfig; + + static createFrom(source: any = {}) { + return new Config(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.server = this.convertValues(source["server"], ServerConfig); + this.debug = this.convertValues(source["debug"], DebuggerConfig); + } + + convertValues(a: any, classs: any, asMap: boolean = false): any { + if (!a) { + return a; + } + if (a.slice) { + return (a as any[]).map(elem => this.convertValues(elem, classs)); + } else if ("object" === typeof a) { + if (asMap) { + for (const key of Object.keys(a)) { + a[key] = new classs(a[key]); + } + return a; + } + return new classs(a); + } + return a; + } + } + + +} + diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 245a5ee..8c2db7f 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -897,6 +897,11 @@ async-validator@^4.2.5: resolved "https://registry.npmmirror.com/async-validator/-/async-validator-4.2.5.tgz#c96ea3332a521699d0afaaceed510a54656c6339" integrity sha512-7HhHjtERjqlNbZtqNqy2rckN/SpOOlmDliet+lP7k+eKZEjPk3DgyeU9lIXLdeLz0uBbbVp+9Qdow9wJWgwwfg== +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.npmmirror.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== + autoprefixer@^10.4.12: version "10.4.17" resolved "https://registry.npmmirror.com/autoprefixer/-/autoprefixer-10.4.17.tgz#35cd5695cbbe82f536a50fa025d561b01fdec8be" @@ -914,6 +919,15 @@ available-typed-arrays@^1.0.5, available-typed-arrays@^1.0.6: resolved "https://registry.npmmirror.com/available-typed-arrays/-/available-typed-arrays-1.0.6.tgz#ac812d8ce5a6b976d738e1c45f08d0b00bc7d725" integrity sha512-j1QzY8iPNPG4o4xmO3ptzpRxTciqD3MgEHtifP/YnJpIo58Xu+ne4BejlbkuaLfXn/nz6HFiw29bLpj2PNMdGg== +axios@^1.6.7: + version "1.6.7" + resolved "https://registry.npmmirror.com/axios/-/axios-1.6.7.tgz#7b48c2e27c96f9c68a2f8f31e2ab19f59b06b0a7" + integrity sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA== + dependencies: + follow-redirects "^1.15.4" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + babel-plugin-prismjs@^2.1.0: version "2.1.0" resolved "https://registry.npmmirror.com/babel-plugin-prismjs/-/babel-plugin-prismjs-2.1.0.tgz#ade627896106326ad04d6d77fba92877618de571" @@ -1053,6 +1067,13 @@ color-name@~1.1.4: resolved "https://registry.npmmirror.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.npmmirror.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + commander@^4.0.0: version "4.1.1" resolved "https://registry.npmmirror.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068" @@ -1133,6 +1154,11 @@ define-properties@^1.1.3, define-properties@^1.2.0, define-properties@^1.2.1: has-property-descriptors "^1.0.0" object-keys "^1.1.1" +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.npmmirror.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== + didyoumean@^1.2.2: version "1.2.2" resolved "https://registry.npmmirror.com/didyoumean/-/didyoumean-1.2.2.tgz#989346ffe9e839b4555ecf5666edea0d3e8ad037" @@ -1620,6 +1646,11 @@ flatted@^3.2.9: resolved "https://registry.npmmirror.com/flatted/-/flatted-3.2.9.tgz#7eb4c67ca1ba34232ca9d2d93e9886e611ad7daf" integrity sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ== +follow-redirects@^1.15.4: + version "1.15.5" + resolved "https://registry.npmmirror.com/follow-redirects/-/follow-redirects-1.15.5.tgz#54d4d6d062c0fa7d9d17feb008461550e3ba8020" + integrity sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw== + for-each@^0.3.3: version "0.3.3" resolved "https://registry.npmmirror.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e" @@ -1635,6 +1666,15 @@ foreground-child@^3.1.0: cross-spawn "^7.0.0" signal-exit "^4.0.1" +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.npmmirror.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + fraction.js@^4.3.7: version "4.3.7" resolved "https://registry.npmmirror.com/fraction.js/-/fraction.js-4.3.7.tgz#06ca0085157e42fda7f9e726e79fefc4068840f7" @@ -2187,6 +2227,18 @@ micromatch@^4.0.4, micromatch@^4.0.5: braces "^3.0.2" picomatch "^2.3.1" +mime-db@1.52.0: + version "1.52.0" + resolved "https://registry.npmmirror.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" + integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== + +mime-types@^2.1.12: + version "2.1.35" + resolved "https://registry.npmmirror.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" + integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== + dependencies: + mime-db "1.52.0" + minimatch@^3.0.4, minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2: version "3.1.2" resolved "https://registry.npmmirror.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b" @@ -2583,6 +2635,11 @@ prosemirror-view@^1.18.7, prosemirror-view@^1.27.0, prosemirror-view@^1.31.0: prosemirror-state "^1.0.0" prosemirror-transform "^1.1.0" +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.npmmirror.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + punycode@^2.1.0: version "2.3.1" resolved "https://registry.npmmirror.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5" diff --git a/main.go b/main.go index 21d4a41..83e9f05 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,6 @@ func main() { // Create application with options // 使用选项创建应用 err := wails.Run(&options.App{ - Title: "wails-app-dock", Width: 1024, Height: 768, diff --git a/pkg/core/app.go b/pkg/core/app.go index 6ba7d3e..d36d8a9 100644 --- a/pkg/core/app.go +++ b/pkg/core/app.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "os" + "skapp/pkg/config" "skapp/pkg/config/toml" "skapp/pkg/global" @@ -130,3 +131,15 @@ func (a *App) Shutdown(ctx context.Context) { logger.Log.Fatalf("Server Shutdown:", err) } } + +func (a *App) GetConfig() *config.Config { + return global.Config +} + +func (a *App) OpenFileDialog(options runtime.OpenDialogOptions) (string, error) { + return runtime.OpenFileDialog(a.ctx, options) +} + +func (a *App) SaveFileDialog(options runtime.SaveDialogOptions) (string, error) { + return runtime.SaveFileDialog(a.ctx, options) +}