import { app } from "electron" ; import { CurrentWindow } from "." ; import { Setting , StoreSchema } from "../types" ; import { store , StoreSet } from "../types/setting" ; import { Version } from "../types/version" ; import { Logger } from "./logger" ; import { join , resolve } from "path" ; import { existsSync , mkdirSync } from "fs" ; const logger = Logger . of ( "setting" ) ; export function InitSetting ( ) { let setting = store . get ( "setting" ) ; if ( setting && setting . version ) { const update = new Version ( app . getVersion ( ) ) . greaterThan ( new Version ( setting . version ) ) ; logger . info ( update ? "检测到设置版本需要更新!" : "本地设置无需更新" ) ; if ( update ) { const defaultSetting = GetDefaultSetting ( ) ; logger . info ( "本地设置:" , JSON . stringify ( setting ) ) ; logger . info ( "更新设置值:" , JSON . stringify ( defaultSetting ) ) ; setting . version = app . getVersion ( ) ; const newValue = deepMerge ( defaultSetting , setting ) ; StoreSet ( "setting" , newValue ) ; logger . info ( "合并后的设置值:" , JSON . stringify ( newValue ) ) ; setting = newValue ; } } else { const defaultSetting = GetDefaultSetting ( ) ; logger . info ( "初始化设置值:" , JSON . stringify ( defaultSetting ) ) ; StoreSet ( "setting" , defaultSetting ) ; StoreSet ( "tasks" , [ ] ) ; StoreSet ( "users" , [ ] ) ; initPath ( defaultSetting . system . path ) ; setting = defaultSetting ; } const { path , win } = setting . system ; if ( path ) initPath ( path ) ; if ( win ) CurrentWindow ?. setAlwaysOnTop ( win . isAlwaysOnTop ) ; StoreSet ( "tasks" , [ ] ) ; } export function GetDefaultSetting ( ) { const initSetting = { version : app . getVersion ( ) , common : { task : { maxTasks : 4 , } , } , script : { launch : { binaryPath : getChromePath ( ) || "" , } , script : { taskTimeoutPeriod : 1 , cx : { study : { enable : true , queue : true , review : false , media : { enable : true , playbackRate : 2 , mute : true , } , ppt : true , book : true , qa : { enable : true , autoReport : true , passRate : 60 , } , } , work : { enable : true , } , exam : { enable : true , } , } , zhs : { autoStop : 0.5 , video : { enable : true , playbackRate : 2 , mute : true , } , qa : { enable : true , autoReport : true , passRate : 60 , } , work : { enable : true , } , exam : { enable : true , } , } , } , account : { queryToken : "" , ocr : { username : "" , password : "" , } , } , } , system : { win : { isAlwaysOnTop : CurrentWindow ?. isAlwaysOnTop ( ) || true , } , path : { userData : app . getPath ( "userData" ) , logs : app . getPath ( "logs" ) , courseImg : resolve ( join ( app . getPath ( "userData" ) , "./course-img/" ) ) , } , } , } ; return initSetting ; } function initPath ( path ) { if ( path ) { for ( const key in path ) { const p = ( path as any ) [ key ] ; logger . info ( "设置路径:" + key , p ) ; if ( ! existsSync ( p ) ) { logger . info ( "mkdirs" , p ) ; mkdirs ( p ) ; } try { app . setPath ( key , p ) ; } catch { } } } } export function getChromePath ( ) { let paths = [ process . env . ProgramFiles , process . env [ "ProgramFiles(x86)" ] , "C:\\Program Files" , "C:\\Program Files (x86)" ] ; let chromePath = paths . map ( ( p ) => join ( p || "" , "\\Google\\Chrome\\Application\\chrome.exe" ) ) . find ( ( p ) => existsSync ( p ) ) ; logger . info ( "获取本地chrome浏览器路径:" , chromePath ) ; return chromePath ; } export function mkdirs ( url ) { if ( ! existsSync ( url ) ) { mkdirs ( resolve ( url , "../" ) ) ; mkdirSync ( url ) ; } } function deepMerge ( obj1 , obj2 ) { let key ; for ( key in obj2 ) { obj1 [ key ] = obj1 [ key ] && obj1 [ key ] . toString ( ) === "[object Object]" && obj2 [ key ] && obj2 [ key ] . toString ( ) === "[object Object]" ? deepMerge ( obj1 [ key ] , obj2 [ key ] ) : ( obj1 [ key ] = obj2 [ key ] ) ; } return obj1 ; }