请求测试

This commit is contained in:
icezhb 2024-07-14 20:01:34 +08:00
parent 6191e9c171
commit be379395f8
5 changed files with 30 additions and 4 deletions

View File

@ -123,7 +123,8 @@ function createWindow() {
...(process.platform === 'linux' ? { icon } : {}), ...(process.platform === 'linux' ? { icon } : {}),
webPreferences: { webPreferences: {
preload: join(__dirname, '../preload/index.js'), preload: join(__dirname, '../preload/index.js'),
sandbox: false sandbox: false,
webSecurity: false
} }
}) })

View File

@ -1,6 +1,8 @@
import electron, { contextBridge, ipcRenderer } from 'electron' import electron, { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload' import { electronAPI } from '@electron-toolkit/preload'
const http = require('http')
// Custom APIs for renderer // Custom APIs for renderer
const api = {} const api = {}
@ -11,6 +13,7 @@ if (process.contextIsolated) {
try { try {
contextBridge.exposeInMainWorld('electron', electronAPI) contextBridge.exposeInMainWorld('electron', electronAPI)
contextBridge.exposeInMainWorld('api', api) contextBridge.exposeInMainWorld('api', api)
contextBridge.exposeInMainWorld('http', http)
contextBridge.exposeInMainWorld('baseConfig', { contextBridge.exposeInMainWorld('baseConfig', {
getBaseConfig: () => ipcRenderer.invoke('getBaseConfig') getBaseConfig: () => ipcRenderer.invoke('getBaseConfig')
}) })
@ -23,6 +26,7 @@ if (process.contextIsolated) {
} else { } else {
window.electron = electronAPI window.electron = electronAPI
window.api = api window.api = api
window.http = http
window.electronAPI = { window.electronAPI = {
getBaseConfig: () => ipcRenderer.invoke('getBaseConfig') getBaseConfig: () => ipcRenderer.invoke('getBaseConfig')
} }

View File

@ -2,11 +2,12 @@
<html> <html>
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<!-- <meta http-equiv="Content-Security-Policy" content="default-src 'elf'; img-src https://*; child-src 'none';"/> -->
<title>Electron</title> <title>Electron</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta <meta
http-equiv="Content-Security-Policy" http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:" content="default-src 'self';connect-src 'self' https://127.0.0.1:53266; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:"
/> />
</head> </head>

View File

@ -1,5 +1,6 @@
<script setup> <script setup>
import Versions from './components/Versions.vue' import Versions from './components/Versions.vue'
import instance from './utils/http'
const getBaseConfig = async () => { const getBaseConfig = async () => {
return await window.baseConfig.getBaseConfig() return await window.baseConfig.getBaseConfig()
@ -24,6 +25,11 @@ const intervalId = setInterval(async () => {
console.log('检测到客户端运行') console.log('检测到客户端运行')
} }
}, 500) }, 500)
instance.get('/lol-gameflow/v1/gameflow-phase')
.then(res => console.info(res))
.catch(err => console.info(err))
</script> </script>
<template> <template>

View File

@ -1,13 +1,27 @@
import axios from 'axios'; import axios from 'axios';
const base_url = '' const baseConfig = async () => {
return await window.baseConfig.getBaseConfig()
}
const baseInfo = await baseConfig()
const base_url = baseInfo['BASE_URL']
const auth = baseInfo['Auth']
// const http = window.http;
// 创建 axios 实例 // 创建 axios 实例
const instance = axios.create({ const instance = axios.create({
baseURL: base_url, baseURL: base_url,
timeout: 10000, timeout: 10000,
headers: {'X-Custom-Header': 'foobar'} // 默认请求头 auth: auth
}); });
// instance.defaults.adapter = http;
// 添加请求拦截器 // 添加请求拦截器
instance.interceptors.request.use(function (config) { instance.interceptors.request.use(function (config) {