Skip to content

Commit

Permalink
清理 require、process(此版本暂时不可用)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stapxs committed Nov 21, 2024
1 parent fbb393a commit 3cdd8b6
Show file tree
Hide file tree
Showing 28 changed files with 295 additions and 82 deletions.
14 changes: 14 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 功能 API 相关设置
VITE_APP_LINK_VIEW=https://api.stapxs.cn/tool/page-info/

# 用户统计 umami 相关设置
VITE_APP_MU_ADDRESS=https://status.stapxs.cn
VITE_APP_MU_ID=fa20b37d-93a3-41d5-a180-dd66fbbcc573
VITE_APP_MU_SHARE=https://status.stapxs.cn/share/dxwhBcriczpA7wWQ/Stapxs%20QQ%20Lite%202.0

# 高德地图相关设置
VITE_APP_AMAP_KEY=99b8849d7b67f05e9f834bde4108f0f9
VITE_APP_AMAP_SECRET=e9ac5aac621defdd6bb111b55fb75d73

# 后端连接相关设置
VITE_APP_BACKEND_CONNECT=true
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export PYTHON_PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin/python
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"electron-store": "^10.0.0",
"electron-window-state": "^5.0.3",
"log4js": "^6.9.1",
"pofile": "^1.1.4",
"vite-plugin-vue-devtools": "^7.6.4",
"ws": "^8.18.0"
},
Expand Down
7 changes: 7 additions & 0 deletions src/preload/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ElectronAPI } from '@electron-toolkit/preload'
import { Shell } from 'electron'

declare module '@electron-toolkit/preload' {
interface ElectronAPI {
shell: Shell
}
}

declare global {
interface Window {
Expand Down
12 changes: 9 additions & 3 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { contextBridge } from 'electron'
import { contextBridge, shell } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {}

// 拓展 electronAPI
const extendedElectronAPI = {
...electronAPI,
shell: shell
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
try {
contextBridge.exposeInMainWorld('electron', electronAPI)
contextBridge.exposeInMainWorld('electron', extendedElectronAPI)
contextBridge.exposeInMainWorld('api', api)
} catch (error) {
// eslint-disable-next-line no-console
console.error(error)
}
} else {
// @ts-ignore (define in dts)
window.electron = electronAPI
window.electron = extendedElectronAPI
// @ts-ignore (define in dts)
window.api = api
}
34 changes: 15 additions & 19 deletions src/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@
},
data() {
return {
dev: process.env.NODE_ENV == 'development',
dev: import.meta.env.DEV,
Connector: Connector,
defineAsyncComponent: defineAsyncComponent,
save: Option.runASWEvent,
Expand Down Expand Up @@ -411,14 +411,13 @@
window.onload = async () => {
// 初始化全局参数
runtimeData.tags.isElectron = window.electron != undefined
// const reader = electron ? electron.ipcRenderer : null
// runtimeData.reader = reader
// if (reader) {
// runtimeData.tags.platform =
// await reader.invoke('sys:getPlatform')
// runtimeData.tags.release =
// await reader.invoke('sys:getRelease')
// }
runtimeData.reader = window.electron.ipcRenderer
if (runtimeData.reader) {
runtimeData.tags.platform =
await runtimeData.reader.invoke('sys:getPlatform')
runtimeData.tags.release =
await runtimeData.reader.invoke('sys:getRelease')
}
app.config.globalProperties.$viewer = this.viewerBody
// 初始化波浪动画
runtimeData.tags.loginWaveTimer = this.waveAnimation(
Expand All @@ -431,7 +430,7 @@
App.createMenu() // Electron:创建菜单
App.createIpc() // Electron:创建 IPC 通信
// 加载开发者相关功能
if (process.env.NODE_ENV == 'development') {
if (this.dev) {
document.title = 'Stapxs QQ Lite (Dev)'
// 布局检查工具
Spacing.start()
Expand All @@ -456,7 +455,7 @@
// 基础初始化完成
logger.debug('欢迎使用 Stapxs QQ Lite!')
logger.debug('当前启动模式为: ' + process.env.NODE_ENV)
logger.debug('当前启动模式为: ' + this.dev ? 'development' : 'production')
logger.debug('Electron 环境: ' + runtimeData.tags.isElectron)
// 加载额外样式
App.loadAppendStyle()
Expand All @@ -475,20 +474,17 @@
// =============================================================
// 初始化完成
// UM:加载 Umami 统计功能
if (
!Option.get('close_ga') &&
process.env.NODE_ENV == 'production'
) {
if (!Option.get('close_ga') && this.dev) {
// 给页面添加一个来源域名方便在 electron 中获取
const config = {
baseUrl: process.env.VUE_APP_MU_ADDRESS,
websiteId: process.env.VUE_APP_MU_ID,
baseUrl: import.meta.env.VITE_UMAMI_URL,
websiteId: import.meta.env.VITE_UMAMI_ID,
} as any
if (runtimeData.tags.isElectron) {
config.hostName = 'electron.stapxs.cn'
}
Umami.initialize(config)
} else if (process.env.NODE_ENV == 'development') {
} else if (this.dev) {
logger.debug('由于运行在调试模式下,分析组件并未初始化 ……')
} else if (Option.get('close_ga')) {
logger.debug('统计功能已被关闭,分析组件并未初始化 ……')
Expand Down Expand Up @@ -533,7 +529,7 @@
// UM:发送页面路由分析
if (
!Option.get('close_ga') &&
process.env.NODE_ENV == 'production'
this.dev
) {
Umami.trackPageView('/' + view)
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/MsgBody.vue
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@
const fistLink = linkList[0]
// 获取链接预览
fetch(
process.env.VUE_APP_LINK_VIEW +
import.meta.env.VITE_APP_LINK_VIEW +
encodeURIComponent(fistLink),
)
.then((res) => res.json())
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/msg-component/CardMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
*/
createMap() {
const json = JSON.parse(this.item.data)
window.createMap(process.env.VUE_APP_AMAP_KEY, this.id, {
window.createMap(import.meta.env.VITE_AMAP_KEY, this.id, {
lat: json.meta['Location.Search'].lat,
lng: json.meta['Location.Search'].lng,
})
Expand Down
6 changes: 6 additions & 0 deletions src/renderer/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ declare interface Window {
lng: number
}) => void
}

// po 文件,按字符串处理
declare module '*.po' {
const value: string
export default value
}
4 changes: 3 additions & 1 deletion src/renderer/src/function/elements/information.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { IpcRenderer } from '@electron-toolkit/preload'

export enum BotMsgType {
CQCode,
Array,
Auto,
}

export interface RunTimeDataElem {
reader?: Electron.IpcRenderer | null
reader?: IpcRenderer | null
sysConfig: { [key: string]: any }
jsonMap?: any
botInfo: { [key: string]: any }
Expand Down
20 changes: 5 additions & 15 deletions src/renderer/src/function/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export class Notify {
*/
public notify(info: NotifyInfo) {
const $t = app.config.globalProperties.$t
const isElectron =
(process.env.IS_ELECTRON as unknown as boolean) &&
window.require != undefined
const isElectron = runtimeData.tags.isElectron
// 判断当前 userId 是否已存在通知
const userId = info.tag.split('/')[0]
if (Notify.userNotifyList[userId] === undefined) {
Expand Down Expand Up @@ -61,9 +59,7 @@ export class Notify {
* @param info 通知信息
*/
public notifySingle(info: NotifyInfo) {
const isElectron =
(process.env.IS_ELECTRON as unknown as boolean) &&
window.require != undefined
const isElectron = runtimeData.tags.isElectron
if (isElectron) {
if (runtimeData.reader)
runtimeData.reader.send('sys:sendNotice', info)
Expand Down Expand Up @@ -92,9 +88,7 @@ export class Notify {
* @param userId 用户 ID
*/
public closeAll(userId: string) {
const isElectron =
(process.env.IS_ELECTRON as unknown as boolean) &&
window.require != undefined
const isElectron = runtimeData.tags.isElectron
if (isElectron) {
if (runtimeData.reader)
runtimeData.reader.send('sys:closeAllNotice', userId)
Expand All @@ -113,9 +107,7 @@ export class Notify {
* 关闭所有通知
*/
public clear() {
const isElectron =
(process.env.IS_ELECTRON as unknown as boolean) &&
window.require != undefined
const isElectron = runtimeData.tags.isElectron
if (isElectron) {
if (runtimeData.reader) runtimeData.reader.send('sys:clearNotice')
} else {
Expand All @@ -133,9 +125,7 @@ export class Notify {
* @param tag 通知标签
*/
private close(tag: string) {
const isElectron =
(process.env.IS_ELECTRON as unknown as boolean) &&
window.require != undefined
const isElectron = runtimeData.tags.isElectron
if (isElectron) {
if (runtimeData.reader)
runtimeData.reader.send('sys:closeNotice', tag)
Expand Down
11 changes: 5 additions & 6 deletions src/renderer/src/function/utils/appUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ export function openLink(url: string, external = false) {
{
text: app.config.globalProperties.$t('打开…'),
fun: () => {
const electron = window.require('electron')
const shell = electron ? electron.shell : null
const shell = window.electron.shell
if (shell) {
shell.openExternal(url)
}
Expand All @@ -93,8 +92,7 @@ export function openLink(url: string, external = false) {
}
runtimeData.popBoxList.push(popInfo)
} else {
const electron = window.require('electron')
const shell = electron ? electron.shell : null
const shell = window.electron.shell
if (shell) {
shell.openExternal(url)
}
Expand Down Expand Up @@ -266,7 +264,8 @@ export async function loadWinColor() {
}

export function updateWinColor(color: string) {
if (process.platform == 'win32') {
const process = window.electron.process
if (process && process.platform == 'win32') {
const red = parseInt(color.substr(0, 2), 16)
const green = parseInt(color.substr(2, 2), 16)
const blue = parseInt(color.substr(4, 2), 16)
Expand Down Expand Up @@ -796,7 +795,7 @@ export function loadJsonMap(name: string) {

// UM:统计事件统一上传方法
export function sendStatEvent(event: string, data: any) {
if (!option.get('close_ga') && process.env.NODE_ENV == 'production') {
if (!option.get('close_ga') && import.meta.env.DEV) {
Umami.trackEvent(event, data)
}
}
4 changes: 2 additions & 2 deletions src/renderer/src/function/utils/msgUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ function replaceJPValue(jpStr: string) {
export function getFace(id: number) {
const pathList = import.meta.glob('@renderer/assets/img/qq-face/public/*/s*.*')
for(const path in pathList) {
if (path.includes(`/${id}.gif`)) {
if (path.includes(`/s${id}.gif`)) {
return path
}
}
for(const path in pathList) {
if (path.includes(`/${id}.png`)) {
if (path.includes(`/s${id}.png`)) {
return path
}
}
Expand Down
30 changes: 13 additions & 17 deletions src/renderer/src/function/utils/systemUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import app from '@renderer/main'

import l10nConfig from '@renderer/assets/l10n/_l10nconfig.json'
import PO from 'pofile'

/**
* 区分安卓、iOS、MacOS 和其他
Expand Down Expand Up @@ -38,23 +39,18 @@ export function getTrueLang(): string {
* @param name 文件名
*/
export function getPortableFileLang(name: string) {
// // eslint-disable-next-line @typescript-eslint/no-var-requires
// const file = require(`@renderer/assets/l10n/${name}.po`)
// const pot = getText.po.parse(file.default)
// // 将翻译平铺为一个对象 {msgid: msgstr}
// const back = {} as Record<string, string>
// for (const item of Object.keys(pot.translations[''])) {
// if (item !== '') {
// // 如果不存在则不添加,防止影响到 vue-i18n 的 fallback
// if (pot.translations[''][item].msgstr) {
// back[item] =
// pot.translations[''][item].msgstr[0] != ''
// ? pot.translations[''][item].msgstr[0]
// : item
// }
// }
// }
return {}
const files = import.meta.glob('@renderer/assets/l10n/*.po', { eager: true, as: 'raw' })
const filePath = Object.keys(files).find(
(item) => item.includes(name))
const final = {} as { [key: string]: string }
if(filePath) {
const file = files[filePath]
const items = PO.parse(file).items
for(const item of items) {
final[item.msgid] = item.msgstr[0] == '' ? item.msgid : item.msgstr[0]
}
}
return final
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const colorList = [
const color = colorList[Math.floor(Math.random() * colorList.length)]
const str = strList[Math.floor(Math.random() * strList.length)]
console.log(
`%c${str}%c Stapxs QQ Lite - ${packageInfo.version} ( ${process.env.NODE_ENV} ) `,
`%c${str}%c Stapxs QQ Lite - ${packageInfo.version} ( ${import.meta.env.DEV ? 'development' : 'production'} ) `,
`font-weight:bold;background:#${color};color:#fff;border-radius:7px 0 0 7px;padding:7px 14px;margin:7px 0 7px 7px;`,
'background:#e3e8ec;color:#000;border-radius:0 7px 7px 0;display:inline-block;padding:7px 14px;margin:7px 7px 7px 0;',
)
8 changes: 1 addition & 7 deletions src/renderer/src/pages/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,7 @@
<img
v-if="context.msg_type === 2"
class="face"
:src="
require(
'./../assets/img/qq-face/gif/s' +
context.face_index +
'.gif',
)
">
:src="getFace(context.face_index)">
<img
v-if="context.msg_type === 3"
:src="context.image_url">
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/pages/chat-view/Chat终端.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
getMsgRawTxt: getMsgRawTxt,
popInfo: new PopInfo(),
packageInfo: packageInfo,
runMode: process.env.NODE_ENV,
runMode: import.meta.env.DEV,
timeLoad: markRaw({
time: Intl.DateTimeFormat(getTrueLang(), {
hour: 'numeric',
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/pages/chat-view/SystemNotice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
</div>
<div
v-else
v-show="NODE_ENV == 'development'">
v-show="dev">
<div>
<img>
<div>
Expand Down Expand Up @@ -133,7 +133,7 @@
return {
trueLang: getTrueLang(),
runtimeData: runtimeData,
NODE_ENV: process.env.NODE_ENV,
dev: import.meta.env.DEV,
}
},
methods: {
Expand Down
Loading

0 comments on commit 3cdd8b6

Please sign in to comment.