Skip to content

Commit 88a00bd

Browse files
committed
add mysql
1 parent 045706e commit 88a00bd

File tree

10 files changed

+136
-63
lines changed

10 files changed

+136
-63
lines changed

electron/main/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import path from 'node:path'
55
import os from 'node:os'
66
import { update } from './update'
77

8+
import '../tools/db'
9+
810
const require = createRequire(import.meta.url)
911
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1012

electron/preload/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ipcRenderer, contextBridge } from 'electron'
22

3+
4+
35
// --------- Expose some API to the Renderer process ---------
46
contextBridge.exposeInMainWorld('ipcRenderer', {
57
on(...args: Parameters<typeof ipcRenderer.on>) {
@@ -18,9 +20,6 @@ contextBridge.exposeInMainWorld('ipcRenderer', {
1820
const [channel, ...omit] = args
1921
return ipcRenderer.invoke(channel, ...omit)
2022
},
21-
22-
// You can expose other APTs you need here.
23-
// ...
2423
})
2524

2625
// --------- Preload scripts loading ---------
@@ -115,4 +114,4 @@ window.onmessage = (ev) => {
115114
ev.data.payload === 'removeLoading' && removeLoading()
116115
}
117116

118-
setTimeout(removeLoading, 4999)
117+
setTimeout(removeLoading, 4999)

electron/tools/db.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import { ipcMain } from 'electron';
3+
import mysql, { Connection } from 'mysql';
4+
5+
const db: Connection = mysql.createConnection({
6+
host: '47.121.143.53',
7+
user: 'root',
8+
password: 'MM1314520m',
9+
database: 'yami_shops',
10+
});
11+
12+
// 开启数据库连接
13+
db.connect();
14+
15+
console.log(db.emit);
16+
17+
18+
19+
20+
// 导出包含正删改查操作的方法供渲染进程调用
21+
ipcMain.handle('mysqlQuery', async (event, query) => {
22+
return new Promise((resolve, reject) => {
23+
db.query(query, (error, results) => {
24+
if (error) {
25+
reject(error);
26+
} else {
27+
resolve(results);
28+
}
29+
});
30+
});
31+
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
},
2121
"dependencies": {
2222
"antd": "^5.18.0",
23-
"electron-updater": "^6.1.8"
23+
"electron-updater": "^6.1.8",
24+
"mysql": "^2.18.1"
2425
},
2526
"devDependencies": {
2627
"@playwright/test": "^1.42.1",
28+
"@types/mysql": "^2.15.26",
2729
"@types/react": "^18.2.64",
2830
"@types/react-dom": "^18.2.21",
2931
"@vitejs/plugin-react": "^4.2.1",

src/App.tsx

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,89 @@
1-
import { useState } from 'react'
2-
import UpdateElectron from '@/components/update'
3-
import logoVite from './assets/logo-vite.svg'
4-
import logoElectron from './assets/logo-electron.svg'
5-
import './App.css'
6-
import { Button } from 'antd'
1+
import React, { useEffect, useState } from 'react';
2+
import {
3+
MenuFoldOutlined,
4+
MenuUnfoldOutlined,
5+
UploadOutlined,
6+
UserOutlined,
7+
VideoCameraOutlined,
8+
} from '@ant-design/icons';
9+
import { Button, Layout, Menu, theme } from 'antd';
10+
import { invokeIpcMethod } from './utils/ipcUtils';
711

12+
const { Header, Sider, Content } = Layout;
813

9-
function App() {
10-
const [count, setCount] = useState(0)
14+
const App: React.FC = () => {
15+
const [collapsed, setCollapsed] = useState(false);
16+
const {
17+
token: { colorBgContainer, borderRadiusLG },
18+
} = theme.useToken();
1119

12-
const communication=()=>{
13-
window.ipcRenderer.send("message-from-renderer",{type:"test",params:{
14-
id:"1",
15-
name:"zc",
16-
age:18
17-
}})
18-
}
20+
useEffect(() => {
21+
const getAreaList=async()=>{
22+
const mysql =await invokeIpcMethod("mysqlQuery","SELECT * from tz_basket tb ")
23+
console.log(mysql);
24+
}
25+
getAreaList();
26+
}, [])
1927

2028
return (
21-
<div className='App'>
22-
<Button onClick={()=>communication()}>开始通讯</Button>
23-
<div className='logo-box'>
24-
<a href='https://github.com/electron-vite/electron-vite-react' target='_blank'>
25-
<img src={logoVite} className='logo vite' alt='Electron + Vite logo' />
26-
<img src={logoElectron} className='logo electron' alt='Electron + Vite logo' />
27-
</a>
28-
</div>
29-
<h1>Electron + Vite + React</h1>
30-
<div className='card'>
31-
<button onClick={() => setCount((count) => count + 1)}>
32-
count is {count}
33-
</button>
34-
<p>
35-
Edit <code>src/App.tsx</code> and save to test HMR
36-
</p>
37-
</div>
38-
<p className='read-the-docs'>
39-
Click on the Electron + Vite logo to learn more
40-
</p>
41-
<div className='flex-center'>
42-
Place static files into the<code>/public</code> folder <img style={{ width: '5em' }} src='./node.svg' alt='Node logo' />
43-
</div>
29+
<Layout>
30+
<Button onClick={async () => {
31+
const mysql = await invokeIpcMethod('SELECT * from tz_area ta ')
32+
console.log(mysql);
4433

45-
<UpdateElectron />
46-
</div>
47-
)
48-
}
34+
}}>mysql</Button>
35+
<Sider trigger={null} collapsible collapsed={collapsed}>
36+
<div className="demo-logo-vertical" />
37+
<Menu
38+
theme="dark"
39+
mode="inline"
40+
defaultSelectedKeys={['1']}
41+
items={[
42+
{
43+
key: '1',
44+
icon: <UserOutlined />,
45+
label: 'nav 1',
46+
},
47+
{
48+
key: '2',
49+
icon: <VideoCameraOutlined />,
50+
label: 'nav 2',
51+
},
52+
{
53+
key: '3',
54+
icon: <UploadOutlined />,
55+
label: 'nav 3',
56+
},
57+
]}
58+
/>
59+
</Sider>
60+
<Layout>
61+
<Header style={{ padding: 0, background: colorBgContainer }}>
62+
<Button
63+
type="text"
64+
icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
65+
onClick={() => setCollapsed(!collapsed)}
66+
style={{
67+
fontSize: '16px',
68+
width: 64,
69+
height: 64,
70+
}}
71+
/>
72+
</Header>
73+
<Content
74+
style={{
75+
margin: '24px 16px',
76+
padding: 24,
77+
minHeight: 280,
78+
background: colorBgContainer,
79+
borderRadius: borderRadiusLG,
80+
}}
81+
>
82+
Content
83+
</Content>
84+
</Layout>
85+
</Layout>
86+
);
87+
};
4988

50-
export default App
89+
export default App;

src/components/update/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import './update.css'
77
const Update = () => {
88
const [checking, setChecking] = useState(false)
99
const [updateAvailable, setUpdateAvailable] = useState(false)
10-
const [versionInfo, setVersionInfo] = useState<VersionInfo>()
11-
const [updateError, setUpdateError] = useState<ErrorType>()
10+
const [versionInfo, setVersionInfo] = useState<any>()
11+
const [updateError, setUpdateError] = useState<any>()
1212
const [progressInfo, setProgressInfo] = useState<Partial<ProgressInfo>>()
1313
const [modalOpen, setModalOpen] = useState<boolean>(false)
1414
const [modalBtn, setModalBtn] = useState<{
@@ -36,7 +36,7 @@ const Update = () => {
3636
}
3737
}
3838

39-
const onUpdateCanAvailable = useCallback((_event: Electron.IpcRendererEvent, arg1: VersionInfo) => {
39+
const onUpdateCanAvailable = useCallback((_event: Electron.IpcRendererEvent, arg1: any) => {
4040
setVersionInfo(arg1)
4141
setUpdateError(undefined)
4242
// Can be update
@@ -53,7 +53,7 @@ const Update = () => {
5353
}
5454
}, [])
5555

56-
const onUpdateError = useCallback((_event: Electron.IpcRendererEvent, arg1: ErrorType) => {
56+
const onUpdateError = useCallback((_event: Electron.IpcRendererEvent, arg1: any) => {
5757
setUpdateAvailable(false)
5858
setUpdateError(arg1)
5959
}, [])

src/type/electron-updater.d.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/utils/index.ts

Whitespace-only changes.

src/utils/ipcUtils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
export async function invokeIpcMethod<T>(channel: string, ...args: any[]): Promise<T> {
4+
try {
5+
return await window.ipcRenderer.invoke(channel, ...args);
6+
} catch (error) {
7+
console.error(`Error invoking IPC method ${channel}:`, error);
8+
throw error; // 你可以选择处理错误或重新抛出
9+
}
10+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"skipLibCheck": true,
88
"esModuleInterop": false,
99
"allowSyntheticDefaultImports": true,
10-
"strict": true,
10+
"strict": false,
1111
"forceConsistentCasingInFileNames": true,
1212
"module": "ESNext",
1313
"moduleResolution": "Node",

0 commit comments

Comments
 (0)