| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-17 21:26:26 |
| 4 | * @LastEditTime: 2025-04-01 17:26:37 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: 浏览器视图 |
| 7 | */ |
| 8 | import { dialog, ipcMain } from 'electron'; |
| 9 | import fs from 'fs'; |
| 10 | import { FileUtils } from '../util/file'; |
| 11 | import path from 'path'; |
| 12 | import requestNet from '../plat/requestNet'; |
| 13 | // @ts-ignore |
| 14 | import coordtransform from 'coordtransform'; |
| 15 | import { logger } from '../global/log'; |
| 16 | import KwaiPubListener from './plat/platforms/Kwai/KwaiPubListener'; |
| 17 | |
| 18 | export interface ISaveFileParams { |
| 19 | // 要保存的路由 |
| 20 | saveDir: string; |
| 21 | // 文件名 |
| 22 | filename: string; |
| 23 | // 文件 |
| 24 | file: Uint8Array; |
| 25 | } |
| 26 | |
| 27 | export function views(win: Electron.BrowserWindow) { |
| 28 | // 开始监听快手审核发布 |
| 29 | ipcMain.handle('start-kwai-listen', function () { |
| 30 | KwaiPubListener.start(); |
| 31 | }); |
| 32 | |
| 33 | // 窗口最小化 |
| 34 | ipcMain.handle('window-minimize', function () { |
| 35 | win.minimize(); |
| 36 | }); |
| 37 | // 窗口最大化 |
| 38 | ipcMain.handle('window-maximize', function () { |
| 39 | if (win.isMaximized()) { |
| 40 | win.restore(); |
| 41 | } else { |
| 42 | win.maximize(); |
| 43 | } |
| 44 | }); |
| 45 | // 关闭窗口 |
| 46 | ipcMain.handle('window-close', function () { |
| 47 | win.close(); |
| 48 | }); |
| 49 | |
| 50 | // 获取经纬度 |
| 51 | ipcMain.handle('GET_LOCATION', async () => { |
| 52 | let res: any; |
| 53 | |
| 54 | for (let i = 0; i < 10; i++) { |
| 55 | res = await requestNet({ |
| 56 | url: `https://map.baidu.com/?qt=ipLocation&t=${Date.now()}`, |
| 57 | headers: { |
| 58 | cookie: `BAIDUID=C6DFA184EA0E181B507D36D4E39DE552:FG=1; BAIDUID_BFESS=C6DFA184EA0E181B507D36D4E39DE552:FG=1; BAIDU_WISE_UID=wapp_1740893445646_358; ZFY=gFmlsByYQo2uV:A8KgAXQ5Ou6usNB8S4rufvkkSZ9WfE:C; arialoadData=false; RT="z=1&dm=baidu.com&si=6acd8df5-273b-4d7b-a273-72762d6a27a7&ss=m7snsitx&sl=0&tt=0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&r=32ngg0aq&ul=3pfv&hd=3pg5"; PSTM=1740990133; H_PS_PSSID=61027_61680_62126_62169_62200_62278_62325_62344_62345_62328_62367_62369_62372_62246_62391; BA_HECTOR=24a02la1al052hag8g01a0a1bclj991jsaplo1v; BIDUPSID=256707C2A28F613DE9EA4FC5C7347285`, |
| 59 | }, |
| 60 | method: 'GET', |
| 61 | }); |
| 62 | if (res?.data?.rgc?.result) break; |
| 63 | } |
| 64 | const { lat, lng } = res.data.rgc.result.location; |
| 65 | const gcj02 = coordtransform.bd09togcj02(lng, lat); |
| 66 | return { |
| 67 | bd09: [lng, lat], |
| 68 | wgs84: coordtransform.gcj02towgs84(gcj02[0], gcj02[1]), |
| 69 | gcj02, |
| 70 | city: res.data.rgc.result.addressComponent.city, |
| 71 | }; |
| 72 | }); |
| 73 | |
| 74 | // 打开开发者工具 |
| 75 | ipcMain.handle('OPEN_DEV_TOOLS', () => { |
| 76 | win.webContents.openDevTools({ mode: 'right' }); |
| 77 | }); |
| 78 | |
| 79 | // 保存文件 |
| 80 | ipcMain.handle( |
| 81 | 'ICP_VIEWS_SAVE_FILE', |
| 82 | (event, { saveDir, filename, file }: ISaveFileParams) => { |
| 83 | return new Promise(async (resolve) => { |
| 84 | logger.info('保存文件----111', { saveDir }); |
| 85 | const outputDir = path.join( |
| 86 | FileUtils.getAppDataPath()!, |
| 87 | 'resource/images/cropper', |
| 88 | ); |
| 89 | logger.info('保存文件----222', outputDir); |
| 90 | |
| 91 | await FileUtils.checkDirectories(outputDir + saveDir); |
| 92 | const filePath = outputDir + saveDir + '/' + filename; |
| 93 | logger.info('保存文件----filePath', filePath); |
| 94 | |
| 95 | fs.writeFile(filePath, file, (err) => { |
| 96 | if (err) { |
| 97 | logger.error('保存错误', err); |
| 98 | console.error('保存错误', err); |
| 99 | } else { |
| 100 | logger.info('文件保存成功:', filePath); |
| 101 | console.log('文件保存成功:', filePath); |
| 102 | resolve(filePath); |
| 103 | } |
| 104 | }); |
| 105 | }); |
| 106 | }, |
| 107 | ); |
| 108 | |
| 109 | // 根据路径获取文件流 |
| 110 | ipcMain.handle('ICP_VIEWS_GET_FILE_STREAM', async (event, path: string) => { |
| 111 | return fs.readFileSync(path); |
| 112 | }); |
| 113 | |
| 114 | /** |
| 115 | * 选择视频文件 |
| 116 | * @param isMultiSelections 是否允许多选 |
| 117 | */ |
| 118 | ipcMain.handle('ICP_VIEWS_CHOSE_VIDEO', async (event, isMultiSelections) => { |
| 119 | const properties = ['openFile']; |
| 120 | if (isMultiSelections) properties.push('multiSelections'); |
| 121 | |
| 122 | try { |
| 123 | const result = await dialog.showOpenDialog({ |
| 124 | properties: properties as Array<'openFile'>, |
| 125 | filters: [{ name: 'mp4视频文件', extensions: ['mp4', 'mov'] }], |
| 126 | }); |
| 127 | |
| 128 | if (result.canceled) return null; |
| 129 | return result.filePaths.map((v) => { |
| 130 | return { |
| 131 | path: v, |
| 132 | video: fs.readFileSync(v), |
| 133 | }; |
| 134 | }); |
| 135 | } catch (error) { |
| 136 | console.error('Error selecting video:', error); |
| 137 | return null; |
| 138 | } |
| 139 | }); |
| 140 | |
| 141 | /** |
| 142 | * 选择图片文件 |
| 143 | * @param isMultiSelections 是否允许多选 |
| 144 | */ |
| 145 | ipcMain.handle('ICP_VIEWS_CHOSE_IMG', async (event, isMultiSelections) => { |
| 146 | const properties = ['openFile']; |
| 147 | if (isMultiSelections) properties.push('multiSelections'); |
| 148 | |
| 149 | try { |
| 150 | // 打开文件选择对话框 |
| 151 | const result = await dialog.showOpenDialog({ |
| 152 | properties: properties as Array<'openFile'>, |
| 153 | filters: [{ name: '图片文件', extensions: ['jpg', 'png', 'jpeg'] }], // 只允许图片文件 |
| 154 | }); |
| 155 | |
| 156 | return result.filePaths.map((v) => { |
| 157 | return { |
| 158 | path: v, |
| 159 | file: fs.readFileSync(v), |
| 160 | }; |
| 161 | }); |
| 162 | } catch (error) { |
| 163 | console.error('------- error --------', error); |
| 164 | return ''; |
| 165 | } |
| 166 | }); |
| 167 | } |
| 168 |