From cbdf3f093cc4de7e5de090f4981019147fc5a3a4 Mon Sep 17 00:00:00 2001 From: MoonLeeeaf <150461955+MoonLeeeaf@users.noreply.github.com> Date: Sun, 16 Mar 2025 00:13:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E7=9F=A5=E9=81=93=E5=96=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/Types.js | 35 +++++++++++++++++++++++++++++++++++ server/api/User.js | 29 +++++++++++++++-------------- server/lib/io.js | 13 ++++++------- server/main.js | 28 ++++++++++------------------ 4 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 server/Types.js diff --git a/server/Types.js b/server/Types.js new file mode 100644 index 0000000..2e006d5 --- /dev/null +++ b/server/Types.js @@ -0,0 +1,35 @@ +export class TheWhiteSilkParams { + /** + * @type { String } + */ + method + /** + * @type { Object } + */ + args +} + +export class CallbackMessage { + static Code = class { + /** + * 无权限 + */ + static PERMISSION_DENIED = 4 + /** + * 服务端错误 + */ + static SERVER_ERROR = 5 + /** + * 请求成功 + */ + static OK = 2 + } + /** + * @type { String } + */ + msg + /** + * @type { Number } + */ + code +} diff --git a/server/api/User.js b/server/api/User.js index a0cf5b4..3f314bf 100644 --- a/server/api/User.js +++ b/server/api/User.js @@ -1,24 +1,31 @@ -class UserManager { - static findUserById(id) { +import io from '../lib/io.js'; +const baseDir = 'whiteslik_data/user' + +export class UserManager { + static getUserProfileDir(id) { + return `${baseDir}/${id}` + } + static findUserById(id) { + let list = io.listFolders(baseDir) + console.log(list) } static findUserByName(name) { - + } /** * 创建新用户 * @param { Object } arg * @param { String } [arg.name] 用户名 - * @param { String } [arg.name] 用户名 * @returns { User } */ - static createUser() { - + static createUser({ name } = {}) { + } } -class User { +export class User { /** @type { Number } */ id /** @type { String } */ @@ -29,14 +36,8 @@ class User { description } -class UserApi { +export class UserApi { static createUser() { } } - -export { - User, - UserManager, - UserApi -} diff --git a/server/lib/io.js b/server/lib/io.js index 9d4abb4..29a90d8 100644 --- a/server/lib/io.js +++ b/server/lib/io.js @@ -8,7 +8,7 @@ import fs from 'node:fs' /** * 简单文件类 */ -class io { +export default class io { /** * 构建函数 * @param { String } path @@ -44,7 +44,7 @@ class io { * @param { Boolean } 是否搜索文件夹内的文件 * @returns { String[] } 文件完整路径列表 */ - static listFiles(path, filter, recursive) { + static listFiles(path, { filter, recursive } = {}) { let a = fs.readdirSync(path, { recursive: recursive }) a.forEach(function(v, index, arrayThis) { arrayThis[index] = `${path}//${v}` @@ -59,11 +59,12 @@ class io { /** * 枚举目录下所有文件夹 * @param { String } 扫描路径 - * @param { Function } 过滤器<文件夹完整路径> - * @param { Boolean } 是否搜索文件夹内的文件夹 + * @param { Object } 额外参数 + * @param { Function } [filter] 额外参数.过滤器<文件夹完整路径> + * @param { Boolean } [recursive] 额外参数.是否搜索文件夹内的文件夹 * @returns { String[] } 文件夹完整路径列表 */ - static listFolders(path, filter, recursive) { + static listFolders(path, { filter, recursive } = {}) { let a = fs.readdirSync(path, { recursive: recursive }) a.forEach(function(v, index, arrayThis) { arrayThis[index] = `${path}//${v}` @@ -245,5 +246,3 @@ class io { delete this.w } } - -export default io diff --git a/server/main.js b/server/main.js index f247539..bcc4425 100644 --- a/server/main.js +++ b/server/main.js @@ -3,41 +3,33 @@ import http from 'node:http' import express from 'express' import { Server as SocketIoServer } from 'socket.io' +// 类型提示 +import { TheWhiteSilkParams, CallbackMessage } from './Types.js' + const app = express() const httpApp = http.createServer(app) const io = new SocketIoServer(httpApp, {}) -class ThewhiteSilkParams { - /** - * @type { String } - */ - method - /** - * @type { Object } - */ - args -} - -const events = [] +const events = {} import { UserApi } from './api/User.js' for (let i of [ - UserApi + UserApi, ]) { for (let i2 of Object.keys(i)) { - events.push(i[i2]) + events[i2] = i[i2] } } io.on("connection", (socket) => { socket.on('the_white_silk', /** - * @param { ThewhiteSilkParams } params - * @param { Function } callback + * @param { TheWhiteSilkParams } params + * @param { Function<> } callback */ (params, callback) => { - if ((params || callback) == null) return; - + if ((params || callback) == null || typeof callback != Function) return; + (events[params.method] || (() => callback({mess})))(params.args) } ) })