Skip to content

Commit

Permalink
修复跨域请求
Browse files Browse the repository at this point in the history
  • Loading branch information
super_wang committed Jul 12, 2024
1 parent e0325c1 commit 703544a
Show file tree
Hide file tree
Showing 10 changed files with 4,195 additions and 254 deletions.
1 change: 0 additions & 1 deletion .env.dev

This file was deleted.

1 change: 0 additions & 1 deletion .env.prod

This file was deleted.

4,311 changes: 4,107 additions & 204 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.0.1",
"scripts": {
"dev": "vite --mode dev",
"prod": "vite --mode prod",
"build": "vite build --mode prod",
"generate": "vite-ssg build",
"preview": "vite preview",
Expand All @@ -17,7 +18,10 @@
"@vue-js-cron/element-plus": "^3.0.1",
"ace-builds": "^1.35.2",
"axios": "^1.4.0",
"axios-tauri-api-adapter": "^0.2.1",
"element-plus": "^2.7.6",
"install": "^0.13.0",
"npm": "^10.8.2",
"nprogress": "^0.2.0",
"pinia": "^2.1.7",
"qs": "^6.12.2",
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ winapi = { version = "0.3", features = [
"winuser",
"synchapi",
] }
tauri = { version = "1.6.1", features = ["dialog-all", "shell-open", "system-tray"] }
tauri = { version = "1.6.1", features = ["api-all", "system-tray"] }
local-ip-address = "0.6.1"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
65 changes: 37 additions & 28 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use chrono::Local;
use local_ip_address::local_ip;
use log::{info, Level, LevelFilter, Log, Metadata, Record};
use std::ptr;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -48,6 +49,12 @@ struct AppState {
http_server: HttpServer,
}

#[tauri::command]
async fn ip() -> Result<String, String> {
let local = local_ip().map_err(|e| e.to_string())?;
Ok(String::from(local.to_string()))
}

#[tauri::command]
async fn is_http_server_running(
state: tauri::State<'_, Arc<Mutex<AppState>>>,
Expand Down Expand Up @@ -130,32 +137,32 @@ fn handle_system_tray_event(app_handle: &tauri::AppHandle, event: tauri::SystemT
}
}

fn init_window(window: tauri::Window) {
window.hide().unwrap();
if let Ok(Some(monitor)) = window.primary_monitor() {
let monitor_size = monitor.size();
if let Ok(window_size) = window.outer_size() {
let x = (monitor_size.width as i32 - window_size.width as i32) / 2;
let y = (monitor_size.height as i32 - window_size.height as i32) / 2;
window
.set_position(tauri::Position::Logical(tauri::LogicalPosition {
x: x.into(),
y: y.into(),
}))
.unwrap();
} else {
let x = (monitor_size.width as i32 - 640) / 2;
let y = (monitor_size.height as i32 - 320) / 2;
window
.set_position(tauri::Position::Logical(tauri::LogicalPosition {
x: x.into(),
y: y.into(),
}))
.unwrap();
}
}
window.show().unwrap();
}
// fn init_window(window: tauri::Window) {
// window.hide().unwrap();
// if let Ok(Some(monitor)) = window.primary_monitor() {
// let monitor_size = monitor.size();
// if let Ok(window_size) = window.outer_size() {
// let x = (monitor_size.width as i32 - window_size.width as i32) / 2;
// let y = (monitor_size.height as i32 - window_size.height as i32) / 2;
// window
// .set_position(tauri::Position::Logical(tauri::LogicalPosition {
// x: x.into(),
// y: y.into(),
// }))
// .unwrap();
// } else {
// let x = (monitor_size.width as i32 - 640) / 2;
// let y = (monitor_size.height as i32 - 320) / 2;
// window
// .set_position(tauri::Position::Logical(tauri::LogicalPosition {
// x: x.into(),
// y: y.into(),
// }))
// .unwrap();
// }
// }
// window.show().unwrap();
// }

fn init_log(handle: AppHandle) {
log::set_boxed_logger(Box::new(FrontendLogger { app_handle: handle }))
Expand Down Expand Up @@ -188,8 +195,9 @@ fn main() {

let app = tauri::Builder::default()
.setup(|app| {
init_window(app.get_window("main").unwrap());
// init_window(app.get_window("main").unwrap());
init_log(app.app_handle());
// app.get_window("main").unwrap().open_devtools();
Ok(())
})
.on_window_event(move |event| match event.event() {
Expand All @@ -211,7 +219,8 @@ fn main() {
start_server,
stop_server,
confirm_exit,
is_http_server_running
is_http_server_running,
ip
]);

app.run(tauri::generate_context!())
Expand Down
22 changes: 19 additions & 3 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@
"iconAsTemplate": true
},
"allowlist": {
"all": false,
"all": true,
"dialog": {
"all": true
},
"http": {
"all": true,
"request": true,
"scope": ["http://**", "https://**"]
},
"protocol": {
"asset": true,
"assetScope": ["**"]
},
"shell": {
"all": false,
"open": true
Expand All @@ -37,13 +46,20 @@
],
"resources": [
".\\src\\wcferry\\lib"
]
],
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": "",
"wix": {
"language": ["zh-CN"]
}
}
},
"security": {
"csp": null
},
"windows": [{
"maximized": false,
"fullscreen": false,
"minHeight": 800,
"resizable": true,
Expand Down
5 changes: 5 additions & 0 deletions src/command/wcf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { tauri, event } from '@tauri-apps/api';

/** 获取内网IP */
async function ip() {
return await tauri.invoke('ip');
}

/** 开启服务 */
async function start_server(host, port, cburl) {
Expand All @@ -22,6 +26,7 @@ async function exit() {
}

export default {
ip,
start_server,
stop_server,
is_http_server_running,
Expand Down
20 changes: 11 additions & 9 deletions src/utils/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ import NProgress from "../progress";
import { useWechatStore } from "~/store";
import { RequestMethods } from "./types";
import { ElMessage } from "element-plus";
import qs from 'qs';

console.log(import.meta.env.VITE_BASE_URL);
import axiosTauriApiAdapter from 'axios-tauri-api-adapter';
import wcf from "~/command/wcf";

// 相关配置请参考:www.axios-js.com/zh-cn/docs/#axios-request-config-1
const defaultConfig: AxiosRequestConfig = {
baseURL: import.meta.env.VITE_BASE_URL,
baseURL: 'http://' + (await (async () => {
return await wcf.ip();
})()) + ':10010',
adapter: axiosTauriApiAdapter,
// 请求超时时间
timeout: 10000,
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json;charset=UTF-8",
"X-Requested-With": "XMLHttpRequest"
},
// headers: {
// Accept: "application/json, text/plain, */*",
// "Content-Type": "application/json;charset=UTF-8",
// "X-Requested-With": "XMLHttpRequest"
// },
// 数组格式参数序列化(https://github.com/axios/axios/issues/5142)
paramsSerializer: {
serialize: stringify as unknown as CustomParamsSerializer
Expand Down
17 changes: 10 additions & 7 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ export default defineConfig({
},
server: {
port: 5555,
proxy: {
"/api": {
"target": 'http://127.0.0.1:10010/',
"changeOrigin": true,
rewrite: (p) => p.replace(/^\/api/, ""),
},
}
// proxy: {
// "/api": {
// "target": 'http://127.0.0.1:10010/',
// "changeOrigin": true,
// rewrite: (p) => p.replace(/^\/api/, ""),
// },
// }
},
build: {
target: ['edge90', 'chrome90', 'firefox90', 'safari15'],
},
plugins: [
vue(),
Expand Down

0 comments on commit 703544a

Please sign in to comment.