2024-07-13 15:44:13 +08:00
|
|
|
|
import fs from 'fs'
|
2024-06-24 13:11:19 +08:00
|
|
|
|
import { version } from '../../../package.json'
|
2024-07-13 15:44:13 +08:00
|
|
|
|
import { define } from '../../define/define'
|
|
|
|
|
|
const fspromises = fs.promises
|
|
|
|
|
|
import { MJSettingService } from '../../define/db/service/SoftWare/mjSettingService'
|
|
|
|
|
|
import _, { isEmpty } from 'lodash'
|
|
|
|
|
|
import { SoftwareService } from '../../define/db/service/SoftWare/softwareService'
|
|
|
|
|
|
import { CheckFileOrDirExist } from '../../define/Tools/file'
|
|
|
|
|
|
import path from 'path'
|
2024-06-24 13:11:19 +08:00
|
|
|
|
|
2024-07-13 15:44:13 +08:00
|
|
|
|
//#region 通用同步
|
2024-06-24 13:11:19 +08:00
|
|
|
|
|
2024-07-13 15:44:13 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 通用的自动初始化配置,所有的配置都在这里面进行初始化
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function GlobalAutoSync() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
let _softwareService = await SoftwareService.getInstance()
|
|
|
|
|
|
let softData = _softwareService.GetSoftwareData(null)
|
|
|
|
|
|
let initConifgPath
|
|
|
|
|
|
let isExist = await CheckFileOrDirExist(define.config_path)
|
|
|
|
|
|
if (isExist) {
|
|
|
|
|
|
initConifgPath = define.config_path
|
|
|
|
|
|
} else {
|
|
|
|
|
|
initConifgPath = path.join(define.init_config_path, 'global_setting.json')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是不是有数据,没有的话初始化整个设置数据,有的话判断是不是有全局设置,没有在设置
|
|
|
|
|
|
if (softData.data.length <= 0) {
|
|
|
|
|
|
// 初始化全局设置
|
|
|
|
|
|
let initGlobalConfig = await fspromises.readFile(initConifgPath, 'utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
let addSfotwareRes = _softwareService.AddSfotware({
|
|
|
|
|
|
theme: 'light',
|
|
|
|
|
|
reverse_display_show: false,
|
|
|
|
|
|
reverse_show_book_striped: false,
|
2024-08-04 15:00:00 +08:00
|
|
|
|
reverse_data_table_size: 'small',
|
2024-07-13 15:44:13 +08:00
|
|
|
|
globalSetting: initGlobalConfig
|
|
|
|
|
|
})
|
|
|
|
|
|
if (addSfotwareRes.code == 1) {
|
|
|
|
|
|
global.logger.info('AutoSunc_GlobalAutoSync', '初始化全局设置成功')
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 有,只需要检查全局设置是不是存在,不存在的话初始化
|
|
|
|
|
|
let softWareData = softData.data[0]
|
|
|
|
|
|
if (softWareData.globalSetting == null || softWareData.globalSetting == '') {
|
|
|
|
|
|
let initGlobalConfig = await fspromises.readFile(initConifgPath, 'utf-8')
|
|
|
|
|
|
softWareData.globalSetting = initGlobalConfig
|
2024-09-12 14:13:09 +08:00
|
|
|
|
_softwareService.UpdateSoftware(softWareData)
|
|
|
|
|
|
global.logger.info('AutoSunc_GlobalAutoSync', '初始化全局设置成功')
|
2024-07-13 15:44:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
// 同步数据不报错,只添加日志
|
|
|
|
|
|
global.logger.error(
|
|
|
|
|
|
'AutoSunc_GlobalAutoSync',
|
|
|
|
|
|
'初始化全局设置失败,失败信息如下' + '\n' + error.toString()
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
//#region 2.2.10 版本 自动同步数据
|
2024-06-24 13:11:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 自动同步MJ配置数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2024-07-13 15:44:13 +08:00
|
|
|
|
async function AutoSyncMJConfig2210() {
|
2024-06-24 13:11:19 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 判断版本
|
|
|
|
|
|
if (version != '2.2.10') {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 同步MJ的配置到服务器中
|
2024-07-13 15:44:13 +08:00
|
|
|
|
let mjConfigJson = JSON.parse(await fspromises.readFile(define.img_base, 'utf-8'))
|
2024-06-24 13:11:19 +08:00
|
|
|
|
// 开始同步APIMJsetting
|
2024-07-13 15:44:13 +08:00
|
|
|
|
let _mjSettingService = await MJSettingService.getInstance()
|
2024-06-24 13:11:19 +08:00
|
|
|
|
|
|
|
|
|
|
if (!mjConfigJson.mj_config) {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
return
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 判断数据库中有没有API数据
|
2024-07-13 15:44:13 +08:00
|
|
|
|
let dbApiSetting = _mjSettingService.GetAPIMjSetting(null)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (dbApiSetting.code == 1 && dbApiSetting.data.length <= 0) {
|
|
|
|
|
|
// 同步API请求配置
|
2024-07-13 15:44:13 +08:00
|
|
|
|
if (
|
|
|
|
|
|
mjConfigJson.mj_config.mj_api_url &&
|
2024-06-24 13:11:19 +08:00
|
|
|
|
mjConfigJson.mj_config.mj_speed &&
|
2024-07-13 15:44:13 +08:00
|
|
|
|
mjConfigJson.mj_config.api_key
|
|
|
|
|
|
) {
|
2024-06-24 13:11:19 +08:00
|
|
|
|
let apiSetting = {
|
|
|
|
|
|
mjApiUrl: mjConfigJson.mj_config.mj_api_url,
|
|
|
|
|
|
mjSpeed: mjConfigJson.mj_config.mj_speed,
|
|
|
|
|
|
apiKey: mjConfigJson.mj_config.api_key
|
2024-07-13 15:44:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
let res = _mjSettingService.AddAPIMjSetting(apiSetting)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (res.code == 1) {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.info('AutoSyncMJConfig2210', '自动同步MJ API 配置数据成功')
|
2024-06-24 13:11:19 +08:00
|
|
|
|
} else {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.error(
|
|
|
|
|
|
'AutoSyncMJConfig2210',
|
|
|
|
|
|
'自动同步MJ API 配置数据失败,错误信息如下:' + '\n' + res.message
|
|
|
|
|
|
)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 判断数据库中是不是存在浏览器配置数据,不存在同步
|
2024-07-13 15:44:13 +08:00
|
|
|
|
let dbBrowserSetting = _mjSettingService.GetBrowserMJSetting(null)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (dbBrowserSetting.code == 1 && dbBrowserSetting.data.length <= 0) {
|
|
|
|
|
|
// 同步浏览器配置,判断数据是不是存在
|
2024-07-13 15:44:13 +08:00
|
|
|
|
if (
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.serviceID) ||
|
2024-06-24 13:11:19 +08:00
|
|
|
|
isEmpty(mjConfigJson.mj_config.channelID) ||
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.token) ||
|
2024-07-13 15:44:13 +08:00
|
|
|
|
!mjConfigJson.mj_config.userAgent ||
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.userAgent) ||
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.userAgent.userAgent)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
) {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
return
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 开始添加浏览器配置
|
|
|
|
|
|
let browserSetting = {
|
|
|
|
|
|
serviceId: mjConfigJson.mj_config.serviceID,
|
|
|
|
|
|
channelId: mjConfigJson.mj_config.channelID,
|
|
|
|
|
|
token: mjConfigJson.mj_config.token,
|
|
|
|
|
|
userAgent: mjConfigJson.mj_config.userAgent.userAgent
|
2024-07-13 15:44:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
let res = _mjSettingService.AddBrowserMJSetting(browserSetting)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (res.code == 1) {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.info('AutoSyncMJConfig2210', '自动同步MJ 浏览器配置数据成功')
|
2024-06-24 13:11:19 +08:00
|
|
|
|
} else {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.error(
|
|
|
|
|
|
'AutoSyncMJConfig2210',
|
|
|
|
|
|
'自动同步MJ 浏览器配置数据失败,错误信息如下:' + '\n' + res.message
|
|
|
|
|
|
)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 判断基础数据,不存在同步
|
2025-02-26 12:03:01 +08:00
|
|
|
|
let mjSetting = _mjSettingService.GetMjSetting()
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (mjSetting.code == 1 && mjSetting.data.length <= 0) {
|
|
|
|
|
|
//判断数据然后选择同步
|
2024-07-13 15:44:13 +08:00
|
|
|
|
if (
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.request_model) ||
|
2024-06-24 13:11:19 +08:00
|
|
|
|
isEmpty(mjConfigJson.mj_config.select_robot) ||
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.image_scale) ||
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.image_model) ||
|
|
|
|
|
|
isEmpty(mjConfigJson.mj_config.image_suffix)
|
|
|
|
|
|
) {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
return
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 开始添加基础配置
|
|
|
|
|
|
let mjSettingData = {
|
|
|
|
|
|
requestModel: mjConfigJson.mj_config.request_model,
|
|
|
|
|
|
selectRobot: mjConfigJson.mj_config.select_robot,
|
|
|
|
|
|
imageScale: mjConfigJson.mj_config.image_scale,
|
|
|
|
|
|
imageModel: mjConfigJson.mj_config.image_model,
|
|
|
|
|
|
imageSuffix: mjConfigJson.mj_config.image_suffix,
|
|
|
|
|
|
taskCount: mjConfigJson.mj_config.task_count ? mjConfigJson.mj_config.task_count : 3,
|
2024-07-13 15:44:13 +08:00
|
|
|
|
spaceTime: mjConfigJson.mj_config.space_time ? mjConfigJson.mj_config.space_time : 5
|
|
|
|
|
|
}
|
|
|
|
|
|
let res = _mjSettingService.AddMJSetting(mjSettingData)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (res.code == 1) {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.info('AutoSyncMJConfig2210', '自动同步MJ 基础配置数据成功')
|
2024-06-24 13:11:19 +08:00
|
|
|
|
} else {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.error(
|
|
|
|
|
|
'AutoSyncMJConfig2210',
|
|
|
|
|
|
'自动同步MJ 基础配置数据失败,错误信息如下:' + '\n' + res.message
|
|
|
|
|
|
)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化默认数据
|
2024-07-13 15:44:13 +08:00
|
|
|
|
let _softwareService = await SoftwareService.getInstance()
|
|
|
|
|
|
let softs = await _softwareService.GetSoftwareData(null)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (softs.code == 1 && softs.data.length <= 0) {
|
|
|
|
|
|
// 添加
|
|
|
|
|
|
let softData = {
|
2024-07-13 15:44:13 +08:00
|
|
|
|
theme: 'light',
|
2024-06-24 13:11:19 +08:00
|
|
|
|
reverse_display_show: false,
|
|
|
|
|
|
reverse_show_book_striped: false,
|
2024-08-04 15:00:00 +08:00
|
|
|
|
reverse_data_table_size: 'small'
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
2024-07-13 15:44:13 +08:00
|
|
|
|
let res = await _softwareService.AddSfotware(softData)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
if (res.code == 0) {
|
|
|
|
|
|
throw new Error(res.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.info('AutoSyncMJConfig2210', '自动同步软件配置数据成功')
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-13 15:44:13 +08:00
|
|
|
|
return mjConfigJson
|
2024-06-24 13:11:19 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
// 同步数据不报错,只添加日志
|
2024-07-13 15:44:13 +08:00
|
|
|
|
global.logger.error(
|
|
|
|
|
|
'AutoSyncMJConfig2210',
|
|
|
|
|
|
'自动同步MJ配置数据失败,错误信息如下:' + '\n' + error.toString()
|
|
|
|
|
|
)
|
2024-06-24 13:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//#endregion
|
2024-07-13 15:44:13 +08:00
|
|
|
|
|
|
|
|
|
|
export async function AutoSync() {
|
|
|
|
|
|
// 2.2.10 版本 自动同步数据
|
|
|
|
|
|
await AutoSyncMJConfig2210()
|
|
|
|
|
|
// 通用同步
|
|
|
|
|
|
await GlobalAutoSync()
|
|
|
|
|
|
}
|