kindring 3 月之前
父节点
当前提交
11fe473dd7

+ 2 - 0
src/apis/ApiAction.ts

@@ -10,4 +10,6 @@ export enum Music_Actions {
   scan_music_select = 'scan_music_select',
   scan_music_add = 'scan_music_add',
   scan_settings = 'scan_settings',
+  scan_music_update = 'scan_music_update',
+  scan_music_delete = 'scan_music_delete',
 }

+ 5 - 0
src/apis/musicControl.ts

@@ -25,3 +25,8 @@ export async function addScanDir(scanSetting: MusicScanSetting)
     return await promise;
 }
 
+export async function fetchScanConfig() : Promise<ResponseData<MusicScanSetting[]>>
+{
+    let [_callId, promise] = api.sendQuery(Music_Actions.scan_settings, {});
+    return await promise;
+}

+ 1 - 1
src/common/db/db.ts

@@ -18,7 +18,7 @@ function createDbFile (dbFileName: string): string {
         let resourceDbIsExist = fs.existsSync(resourceDbPath);
         if (!resourceDbIsExist) {
             console.log("base db not find");
-            fs.ensureFile(resourceDbPath);
+            fs.ensureFileSync(resourceDbPath);
             // 创建初始文件
             let dbInstance = knex({
                 client: "better-sqlite3",

+ 58 - 10
src/common/db/db_music.ts

@@ -3,7 +3,7 @@ import { loadDb} from "@/common/db/db.ts";
 import {handle, PromiseResult} from "@/util/promiseHandle.ts";
 import {AppDbName} from "@/types/appConfig.ts";
 import {Knex} from "knex";
-import {MusicScanSetting, MusicTableName} from "@/types/musicType.ts";
+import {MusicScanSetting, MusicTableName, PlayList} from "@/types/musicType.ts";
 
 let logger = Logger.logger('music_db', 'info');
 
@@ -16,12 +16,14 @@ async function _initScanConfigTable(db : Knex): PromiseResult<boolean> {
         logger.error(`[音频扫描库] ${err.message}`)
         return [new Error('音频扫描库初始化失败'), false]
     }
+    console.log(hasTable)
     if (hasTable) {
         return [null, true];
     }
-    let [createErr, _res] = await handle(db?.schema.createTable('scanConfig', (table) => {
+    let [createErr, _res] = await handle(db?.schema.createTable(MusicTableName.music_scan_setting, (table) => {
         // 初始化扫描配置
-        logger.error(`[初始化音频扫描库]`)
+        logger.info(`[初始化音频扫描库]`)
+        table.increments('id').primary()
         table.string('name')
         table.string('path')
         table.boolean('scanSubDir')
@@ -38,7 +40,7 @@ async function _initScanConfigTable(db : Knex): PromiseResult<boolean> {
 
 async function _initPlayListTable(db : Knex): PromiseResult<boolean> {
     let [err, hasTable] = await handle(
-        db.schema.hasTable(MusicTableName.music_scan_setting)
+        db.schema.hasTable(MusicTableName.music_play_list)
     )
     if (err) {
         err = err as Error;
@@ -50,7 +52,7 @@ async function _initPlayListTable(db : Knex): PromiseResult<boolean> {
     }
     let [createErr, _res] = await handle(
         db.schema.createTable(MusicTableName.music_play_list, (table) => {
-            logger.error(`[初始化音频播放列表库]`)
+            logger.info(`[初始化音频播放列表库]`)
             table.increments('id').primary()
             table.string('name')
             table.string('icon')
@@ -105,7 +107,7 @@ async function _initSongsTable(db : Knex): PromiseResult<boolean>
     }
     let [createErr, _res] = await handle(
         db.schema.createTable(MusicTableName.music_songs, (table) => {
-            logger.error(`[初始化音频库]`)
+            logger.info(`[初始化音频库]`)
             table.increments('id').primary()
             table.string('name')
             table.string('artists')
@@ -145,7 +147,7 @@ async function _initPlaylistSongs(db : Knex): PromiseResult<boolean>
     }
     let [createErr, _res] = await handle(
         db.schema.createTable(MusicTableName.music_play_list_songs, (table) => {
-            logger.error(`[初始化歌单歌曲库]`)
+            logger.info(`[初始化歌单歌曲库]`)
             table.increments('id').primary()
             table.integer('musicId')
             table.integer('playListId')
@@ -247,8 +249,15 @@ export async function addScanConfig(scanConfig: MusicScanSetting) : PromiseResul
         logger.error('数据库初始化失败')
         return [new Error('音乐数据库初始化失败'), false]
     }
+    // 移除其中的 id
+    let addScanConfig = {
+        name: scanConfig.name,
+        path: scanConfig.path,
+        scanSubDir: scanConfig.scanSubDir,
+        isFileRepeat: scanConfig.isFileRepeat
+    }
     let [err, _res] = await handle(
-        db.insert(scanConfig).into(MusicTableName.music_scan_setting)
+        db.insert(addScanConfig).into(MusicTableName.music_scan_setting)
     )
     if (err) {
         err = err as Error;
@@ -274,7 +283,7 @@ export async function updateScanConfig(scanConfig: MusicScanSetting) : PromiseRe
     return [err, true];
 }
 
-export async function deleteScanConfig(path: string) : PromiseResult<boolean>
+export async function deleteScanConfig(id: number) : PromiseResult<boolean>
 {
     let db = loadDb(AppDbName.music_db)
     if(!db){
@@ -282,10 +291,49 @@ export async function deleteScanConfig(path: string) : PromiseResult<boolean>
         return [new Error('音乐数据库初始化失败'), false]
     }
     let [err, _res] = await handle(
-        db.delete().from(MusicTableName.music_scan_setting).where('path', path)
+        db.delete().from(MusicTableName.music_scan_setting).where('id', id)
     )
     if (err) {
         err= err as Error;
     }
     return [err, true];
 }
+
+
+export async function getPlayList() : PromiseResult<PlayList[]>
+{
+    let db = loadDb(AppDbName.music_db)
+    if(!db){
+        logger.error('数据库初始化失败')
+        return [new Error('音乐数据库初始化失败'), null]
+    }
+    let [err, res] = await handle(
+        db.select('id', 'name', 'icon', 'cover', 'description', 'playCount', 'trackCount', 'createTime', 'isTagSearch', 'lastPlayTime', 'isSync', 'isPublic', 'isLike')
+            .from(MusicTableName.music_play_list)
+    )
+    if (err) {
+        err = err as Error;
+        logger.error(`[获取播放列表失败] ${err.message}`)
+        return [err, null];
+    }
+    return [null, res as PlayList[]];
+}
+
+
+export async function addPlayList(playList: PlayList) : PromiseResult<boolean>
+{
+    let db = loadDb(AppDbName.music_db)
+    if(!db){
+        logger.error('数据库初始化失败')
+        return [new Error('音乐数据库初始化失败'), false]
+    }
+    let [err, _res] = await handle(
+        db.insert(playList).into(MusicTableName.music_play_list)
+    )
+    if (err) {
+        err = err as Error;
+        logger.error(`[添加播放列表失败] ${err.message}`)
+        return [err, false];
+    }
+    return [null, true];
+}

+ 16 - 4
src/common/db/magnetDb.ts

@@ -42,18 +42,30 @@ initMagnetData()
 
 
 
-export function findMagnetById(id: string) {
+export async function findMagnetById(id: string) : PromiseResult<SavedMagnet> {
     let db = loadDb(AppDbName.magnet_db)
-    return db?.select(
+    if (!db) {
+        return [new Error('数据库初始化失败'), null];
+    }
+    logger.info(`查询磁贴: ${id}`)
+    let [err, res] = await handle(db.select(
         'id',
         'x',
         'y',
         'type',
         'size'
-    ).from('magnets').where('id', id)
+    ).from('magnets').where('id', id))
+    if (err)
+    {
+        err = err as Error;
+    }
+    if (!res || res.length === 0) {
+        return [null, null];
+    }
+    return [null, res[0]]
 }
 
-export async function getMagnetList(): Promise<[Error | null, SavedMagnet[]]> {
+export async function getMagnetList(): PromiseResult<SavedMagnet[]> {
     let db = loadDb(AppDbName.magnet_db)
     if (!db) {
         return [new Error('数据库初始化失败'), []];

+ 18 - 32
src/components/music/common/mSettingScan.vue

@@ -1,11 +1,13 @@
 <script setup lang="ts">
-import {defineComponent, ref} from "vue";
+import {defineComponent, onBeforeMount, Ref, ref} from "vue";
 import {MusicScanSetting} from "@/types/musicType.ts";
 import EmptyView from "@/components/public/emptyView.vue";
 import {KuiDialogCmd} from "@/components/public/kui-dialog-cmd.ts";
 
 import addScanDialog from "@/components/music/dialog/addScan.vue"
 import message from "@/components/public/kui/message";
+import {fetchScanConfig} from "@/apis/musicControl.ts";
+import {ErrorCode} from "@/types/apiTypes.ts";
 
 defineComponent({
   name: 'm-setting-scan'
@@ -18,37 +20,7 @@ const props  = defineProps({
   }
 })
 
-const scanSetting: MusicScanSetting[] = [];
-scanSetting.push({
-  name: '默认配置',
-  isFileRepeat: true,
-  path: 'D:\\music',
-  scanSubDir: true,
-})
-scanSetting.push({
-  name: '默认配置',
-  isFileRepeat: true,
-  path: 'D:\\music',
-  scanSubDir: true,
-})
-scanSetting.push({
-  name: '默认配置',
-  isFileRepeat: true,
-  path: 'D:\\music',
-  scanSubDir: true,
-})
-scanSetting.push({
-  name: '默认配置',
-  isFileRepeat: true,
-  path: 'D:\\music',
-  scanSubDir: true,
-})
-scanSetting.push({
-  name: '默认配置',
-  isFileRepeat: true,
-  path: 'D:\\music',
-  scanSubDir: true,
-})
+const scanSetting: Ref<MusicScanSetting[]> = ref([]);
 
 function testFn(){
   message.info('test')
@@ -74,6 +46,20 @@ function openDialog()
   }
 }
 
+async function fetchScanSetting()
+{
+  let responseData = await fetchScanConfig();
+  if (responseData.code === ErrorCode.success)
+  {
+    scanSetting.value = responseData.data;
+  }
+  console.log(responseData);
+}
+
+onBeforeMount(()=>{
+  fetchScanSetting();
+})
+
 </script>
 
 <template>

+ 3 - 1
src/components/music/dialog/addScan.vue

@@ -24,6 +24,7 @@ function closeDialog() {
   emits('close');
 }
 
+
 async function selectPathHandle() {
   let responseData = await selectScanDir(dirPath.value);
   // console.log(responseData)
@@ -38,12 +39,13 @@ async function selectPathHandle() {
 
 async function submitHandle() {
   let param: MusicScanSetting = {
+    id: 0,
     name: name.value,
     path: dirPath.value,
     scanSubDir: scanSubDir.value,
     isFileRepeat: isFileRepeat.value
   }
-  if (!param.dirPath)
+  if (!param.path)
   {
     message.warning('请选择需要扫描的子目录');
     return;

+ 10 - 1
src/main/AppControl.ts

@@ -11,6 +11,8 @@ import {initIpc} from "./tools/ipcInit.ts";
 import {initHook} from "./tools/hookInit.ts";
 import hook from "@/util/hook.ts";
 import Path from "path";
+import {initMusicData} from "@/common/db/db_music.ts";
+import {ResType} from "@/util/promiseHandle.ts";
 
 let logger = Logger.logger('controlWindow', 'info');
 
@@ -286,7 +288,7 @@ async function exit(){
 export async function initApp(appConfig: AppConfig, app: Electron.App) : Promise<AppWindow | null>{
     logger.info('start init control window');
     let mainWindow : BrowserWindow = _createMainWindow();
-    let err, port: number, server: FcServer | null;
+    let err, port: number, server: FcServer | null , flag : ResType<boolean> = false;
     _appConfig = appConfig;
     [err,port] = await getAvailablePort(WebPort,300);
     if (port === -1){
@@ -329,7 +331,14 @@ export async function initApp(appConfig: AppConfig, app: Electron.App) : Promise
         logger.info(`[应用初始化] 快捷键注册完成`);
     }
 
+   [err, flag] = await initMusicData();
 
+    if(err){
+        logger.error(`[应用初始化] 初始化音乐库失败: ${err}`);
+    }
+    if(flag){
+        logger.info(`[应用初始化] 初始化音乐库完成`);
+    }
 
     logger.info(`[应用初始化] 初始化完成`);
 

+ 5 - 2
src/main/control/api_router.ts

@@ -24,12 +24,15 @@ export async function apiRouter(requestData: RequestData<any>){
             responseData = await c_scanMusicSelect(requestData);
             break;
         case Music_Actions.scan_music_add:
-            responseData = await c_scanMusicSelect(requestData);
+            responseData = await c_scanMusicAdd(requestData);
             break;
         case Music_Actions.scan_settings:
             responseData = await c_scanSettings(requestData);
             break;
-        case Music_Actions.scan_music_add:
+        case Music_Actions.scan_music_update:
+            responseData = await c_scanSettings(requestData);
+            break;
+        case Music_Actions.scan_music_delete:
             responseData = await c_scanMusicAdd(requestData);
             break;
         default:

+ 3 - 4
src/main/control/magnet/magnet.ts

@@ -6,8 +6,8 @@ import Logger from "@/util/logger.ts";
 
 let logger = Logger.logger('magnet_control', 'info');
 
-export async function c_fetchMagnetList(requestData: RequestData<null>){
-    let [err, result] = await handle(getMagnetList());
+export async function c_fetchMagnetList(requestData: RequestData<null>) {
+    let [err, result] = await getMagnetList();
     let responseData: ResponseData<any>
     if (err) {
         err = err as Error;
@@ -21,7 +21,6 @@ export async function c_fetchMagnetList(requestData: RequestData<null>){
         }
         return responseData;
     }
-    console.log(result)
     responseData = {
         type: ApiType.res,
         code: ErrorCode.success,
@@ -98,7 +97,7 @@ export async function c_magnet_delete(requestData: RequestData<{magnetId: string
         }
         return responseData;
     }
-    let [err, _result] = await handle(db_deleteMagnet(data.magnetId));
+    let [err, _result] = await db_deleteMagnet(data.magnetId);
     if (err) {
         err = err as Error;
         logger.error(`[删除数据失败] ${err.message}`)

+ 46 - 3
src/main/control/magnet/music.ts

@@ -2,7 +2,7 @@ import {dialog} from "electron"
 import {ApiType, ErrorCode, RequestData, ResponseData} from "@/types/apiTypes.ts";
 import Logger from "@/util/logger.ts";
 import {MusicScanSetting} from "@/types/musicType.ts";
-import {addScanConfig, getScanConfig, getScanConfigByPath} from "@/common/db/db_music.ts";
+import {addScanConfig, getScanConfig, getScanConfigByPath, updateScanConfig} from "@/common/db/db_music.ts";
 import {ResType} from "@/util/promiseHandle.ts";
 import {t_gen_res, t_res_ok} from "@/main/tools/ipcRouter.ts";
 let logger = Logger.logger('music', 'info');
@@ -24,7 +24,10 @@ export async function c_fetchPlayList(requestData: RequestData<null>)
     return responseData;
 }
 
-
+/**
+ * 选择扫描目录
+ * @param requestData
+ */
 export async function c_scanMusicSelect(requestData: RequestData<string>): Promise<ResponseData<string>>
 {
     let defaultPath = requestData.data;
@@ -40,7 +43,10 @@ export async function c_scanMusicSelect(requestData: RequestData<string>): Promi
 }
 
 
-// 获取扫描设置列表
+/**
+ * 获取扫描设置列表
+ * @param requestData
+ */
 export async function c_scanSettings(requestData: RequestData<null>) : Promise<ResponseData<MusicScanSetting[]>>
 {
     let responseData: ResponseData<any>
@@ -52,6 +58,12 @@ export async function c_scanSettings(requestData: RequestData<null>) : Promise<R
     responseData = t_gen_res(requestData, ErrorCode.success, '', scanSettingList)
     return responseData;
 }
+
+
+/**
+ * 添加扫描设置
+ * @param requestData
+ */
 export async function c_scanMusicAdd(requestData: RequestData<MusicScanSetting>) : Promise<ResponseData<boolean>>
 {
     let scanSetting: MusicScanSetting = requestData.data;
@@ -75,3 +87,34 @@ export async function c_scanMusicAdd(requestData: RequestData<MusicScanSetting>)
     res = res as boolean;
     return t_res_ok(requestData,  res)
 }
+
+
+/**
+ * 更新扫描配置
+ * @param requestData
+ */
+export async function c_scanMusicUpdate(requestData: RequestData<MusicScanSetting>) : Promise<ResponseData<boolean>>
+{
+    let scanSetting: MusicScanSetting = requestData.data;
+    let res: ResType<any> = false;
+    // 判断路径是否重复
+    let [err, scanSettingList] = await getScanConfigByPath(scanSetting.path)
+    if (err) {
+        logger.error(`[获取扫描设置列表失败] ${err.message}`)
+        return t_gen_res(requestData, ErrorCode.db, '获取扫描设置列表失败', false)
+    }
+    scanSettingList = scanSettingList as MusicScanSetting[];
+    if (scanSettingList.length> 0)
+    {
+        logger.error(`[扫描路径重复] ${scanSetting.path}`)
+        return t_gen_res(requestData, ErrorCode.params, '扫描路径重复', false)
+    }
+    [err, res] = await updateScanConfig(scanSetting);
+
+    if (err) {
+        logger.error(`[更新扫描设置失败] ${err.message}`)
+        return t_gen_res(requestData, ErrorCode.db, '更新扫描设置失败', false)
+    }
+    res = res as boolean;
+    return t_res_ok(requestData,  res)
+}

+ 1 - 0
src/types/musicType.ts

@@ -50,6 +50,7 @@ export interface MusicSearchInfo {
 }
 
 export interface MusicScanSetting {
+    id: number;
     name: string;// 配置名称
     path: string;// 扫描路径
     scanSubDir: boolean;// 是否扫描子目录