From 8837db3e3de94a4d1760488b06e4729008614f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E5=B7=9E?= Date: Fri, 9 Jul 2021 16:06:20 +0800 Subject: [PATCH] =?UTF-8?q?'=E6=B7=BB=E5=8A=A0electron-storez=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=85=B3=E9=97=AD=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=90=8E?= =?UTF-8?q?=E4=BB=8D=E8=83=BD=E4=BF=9D=E5=AD=98=E6=95=B0=E6=8D=AE'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++ electron/main.js | 4 ++ electron/preload.js | 9 ++- electron/storage.js | 68 +++++++++++++++++++ package.json | 2 + src/app.ts | 9 ++- .../components/userCenter/UserCenter.tsx | 3 + src/global.d.ts | 4 ++ src/pages/login/index.tsx | 25 +++++++ src/services/system.ts | 21 ++++-- src/utils/storage.ts | 54 +++++++++++++-- 11 files changed, 190 insertions(+), 14 deletions(-) create mode 100644 electron/storage.js diff --git a/.gitignore b/.gitignore index bee1cf6..d3252dc 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,8 @@ /src/.umi-production /src/.umi-test /.env.local + + +# +/logs +/文档 \ No newline at end of file diff --git a/electron/main.js b/electron/main.js index 4abc869..cc94fc1 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,6 +1,7 @@ const { app, BrowserWindow, dialog, ipcMain, shell } = require('electron'); const path = require('path'); const url = require('url'); +const { initialStorageEvents } = require('./storage'); let mainWindow; @@ -84,6 +85,9 @@ ipcMain.handle('open-browser', (event, url) => { shell.openExternal(url); }); +// 初始化electron-store相关API +initialStorageEvents(ipcMain); + app.on('ready', () => { createWindow(); diff --git a/electron/preload.js b/electron/preload.js index 39bf1aa..c85b57d 100644 --- a/electron/preload.js +++ b/electron/preload.js @@ -1,6 +1,11 @@ const electron = require('electron'); const { contextBridge, ipcRenderer } = electron; - -// console.log(electron.ipcRenderer.on); +const { storage } = require('./storage'); contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer); +contextBridge.exposeInMainWorld('initialStorage', storage.getAllItem()); + +/** + * todo: + * websocket事件处理 + */ diff --git a/electron/storage.js b/electron/storage.js new file mode 100644 index 0000000..3ea121c --- /dev/null +++ b/electron/storage.js @@ -0,0 +1,68 @@ +const ElectronStore = require('electron-store'); +const eStore = new ElectronStore(); + +const PERSIST_KEY_NAME = '_persistKeys'; + +const keyList = { + add(key) { + const keys = this.get(); + if (this.has(key)) { + return; + } + keys.push(key); + eStore.set(PERSIST_KEY_NAME, keys.join(',')); + }, + get() { + return eStore + .get(PERSIST_KEY_NAME, '') + .split(',') + .filter((a) => a); + }, + has(key, keys) { + return (keys || this.get()).find((iKey) => iKey === key); + }, + remove(key) { + const keys = this.get(); + eStore.set(PERSIST_KEY_NAME, keys.filter((iKey) => iKey !== key).join(',')); + }, +}; + +const storage = { + getItem(key) { + return eStore.get(key); + }, + setItem(key, value) { + if (key === PERSIST_KEY_NAME) return; + keyList.add(key); + return eStore.set(key, value); + }, + removeItem(key) { + keyList.remove(key); + return eStore.delete(key); + }, + getAllItem() { + const keys = keyList.get(); + const hash = { + keyList: keys, + }; + keys.forEach((key) => { + hash[key] = eStore.get(key); + }); + return hash; + }, +}; + +module.exports.storage = storage; +module.exports.initialStorageEvents = function initialStorageEvents(ipcMain) { + ipcMain.handle('storage:set', (_, { key, value }) => { + storage.setItem(key, value); + }); + + ipcMain.handle('storage:remove', (_, { key }) => { + storage.removeItem(key); + }); + + // ipcMain.handle('storage:getAll', () => { + // return storage.getAllItem(); + // }); +}; diff --git a/package.json b/package.json index 518dcbc..bf1c4fe 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "dependencies": { "@ant-design/icons": "^4.6.2", "@ant-design/pro-layout": "^6.5.0", + "@ant-design/pro-table": "^2.43.4", "@types/lodash": "^4.14.170", "@umijs/preset-react": "1.x", "ahooks": "^2.10.6", @@ -36,6 +37,7 @@ "dayjs": "^1.10.5", "electron": "^13.1.4", "electron-packager": "^15.2.0", + "electron-store": "^8.0.0", "lodash": "^4.17.21", "umi": "^3.4.25" }, diff --git a/src/app.ts b/src/app.ts index cbde4d3..975e5a1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,6 +4,7 @@ import { notification } from 'antd'; import { firstCharToLowerCase, handleRequest } from './utils/tool'; import { isObject } from 'lodash'; import { logout, queryCurrent } from './services/user'; +import storage from './utils/storage'; const codeMessage = { 200: '服务器成功返回请求的数据。', @@ -102,6 +103,11 @@ export async function getInitialState(): Promise<{ currentUser?: DATA.User | null; }> { async function fetchUserInfo() { + const accountId = storage.get('accountId'); + if (!accountId) { + logout(); + return null; + } const res = await queryCurrent(); handleRequest(res) .error(() => logout()) @@ -116,9 +122,6 @@ export async function getInitialState(): Promise<{ history.location.pathname !== '/~docs' ) { const currentUser = await fetchUserInfo(); - if (!currentUser) { - logout(); - } return { fetchUserInfo, isLogin: !!currentUser, diff --git a/src/components/AppHeader/components/userCenter/UserCenter.tsx b/src/components/AppHeader/components/userCenter/UserCenter.tsx index 8f4a043..7ad344e 100644 --- a/src/components/AppHeader/components/userCenter/UserCenter.tsx +++ b/src/components/AppHeader/components/userCenter/UserCenter.tsx @@ -13,6 +13,7 @@ import styles from './UserCenter.less'; import defaultAvatorImg from '@/assets/avator_default.svg'; import FolderSetModal from './FolderSetModal'; import { isClient } from '@/services/system'; +import storage from '@/utils/storage'; interface UserCenterProps { className?: string; @@ -66,6 +67,8 @@ function PopContent(props: PopContentProps) { const tryLogout = useCallback(() => { if (hidePop) hidePop(); + storage.remove('account'); + storage.remove('password'); confirm({ onOk() { logout(); diff --git a/src/global.d.ts b/src/global.d.ts index 597c707..37aa09a 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -3,5 +3,9 @@ import { IpcRenderer } from 'electron'; declare global { interface Window { ipcRenderer: IpcRenderer; + initialStorage?: { + keyList: string[]; + [key: string]: any; + }; } } diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 78d7ec8..79fdee1 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -13,6 +13,10 @@ import classNames from 'classnames'; import SmsInputer from './SmsInputer'; import { memoize } from 'lodash'; import { Rule } from 'antd/lib/form'; +import { isClient } from '@/services/system'; +import storage from '@/utils/storage'; +import { useRef } from 'react'; +import { useLayoutEffect } from 'react'; export default function Login() { const [errText, setErrText] = useState(''); @@ -21,6 +25,7 @@ export default function Login() { const [loading, setLoading] = useState(false); const [regModalVisible, setRegModalVisible] = useState(false); const { refresh } = useModel('@@initialState'); + const buttonRef = useRef(null); // const { loading, signin } = useModel('useAuthModel'); const onLogin = useCallback(async () => { @@ -40,6 +45,11 @@ export default function Login() { setErrText(res.message!); }) .success(() => { + // 记录账号密码 + if (isClient) { + storage.set('account', account, true); + storage.set('password', password, true); + } // history.push('/'); // window.location.href = '/'; history.replace('/'); @@ -52,6 +62,20 @@ export default function Login() { setErrText(''); }, [account, password]); + useLayoutEffect(() => { + if (isClient) { + const iAccount = storage.get('account'); + const iPass = storage.get('password'); + if (iAccount && iPass) { + setAccount(iAccount); + setPassword(iPass); + setTimeout(() => { + buttonRef.current?.click(); + }, 500); + } + } + }, [buttonRef]); + return (