diff --git a/common.js b/common.js index 68fbdbd..44145de 100644 --- a/common.js +++ b/common.js @@ -7,7 +7,7 @@ import { timestampToArray, uint8ArrayToString } from './format' -import { buildNumber, configs, version } from './env' +import { configs } from './env' import { cmdIds, Result, subCmdIds } from './constant' import { closeBLEConnection, @@ -28,7 +28,7 @@ import { updatePasswordRequest, uploadRecordRequest } from './api' -import { getStorage, setStorage } from './export' +import { buildNumber, getStorage, setStorage, version } from './export' import log from './log' /** diff --git a/env.js b/env.js index 8c9fe3c..dbbb168 100644 --- a/env.js +++ b/env.js @@ -1,7 +1,12 @@ -// 版本号 -export const version = '1.0.0' -// 构建号 -export const buildNumber = 1 +// uni版本号 +export const uniVersion = '1.0.0' +// uni构建号 +export const uniBuildNumber = 1 + +// web版本号 +export const webVersion = '1.0.0' +// web构建号 +export const webBuildNumber = 1 // 环境配置 export const configs = { diff --git a/export.js b/export.js index e215fa7..d4cb5b8 100644 --- a/export.js +++ b/export.js @@ -3,6 +3,7 @@ import { getStorageWeb, removeStorageWeb, setStorageWeb } from './web/storage' import requestUni from './uni/request' import requestWeb from './web/request' import starCloudInstance from './star-cloud' +import { uniBuildNumber, uniVersion, webBuildNumber, webVersion } from './env' export const setStorage = (key, value) => { if (starCloudInstance.platform === 1) { @@ -39,3 +40,21 @@ export const request = async params => { return await requestWeb(params) } } + +export const version = () => { + if (starCloudInstance.platform === 1) { + return uniVersion + } + if (starCloudInstance.platform === 2) { + return webVersion + } +} + +export const buildNumber = () => { + if (starCloudInstance.platform === 1) { + return uniBuildNumber + } + if (starCloudInstance.platform === 2) { + return webBuildNumber + } +} diff --git a/obfuscate-uni.js b/obfuscate-uni.js index 0946b10..3ed9342 100644 --- a/obfuscate-uni.js +++ b/obfuscate-uni.js @@ -1,18 +1,17 @@ -const JavaScriptObfuscator = require('javascript-obfuscator') -const fs = require('fs') -const path = require('path') -const ignore = require('ignore') +import JavaScriptObfuscator from 'javascript-obfuscator' +import fs from 'fs' +import path from 'path' +import ignore from 'ignore' +import { uniVersion } from './env.js' // 导入 webVersion -const sourceDir = __dirname -const distDir = path.join(__dirname, 'dist') -const gitignorePath = path.join(__dirname, '.gitignore') +// 获取当前目录和构建目标目录 +const sourceDir = path.dirname(new URL(import.meta.url).pathname) +const distDir = path.join(sourceDir, 'dist') +const gitignorePath = path.join(sourceDir, '.gitignore') // 定义需要排除的文件和文件夹 const excludedFilesAndDirs = [distDir, 'node_modules'] -// 将 .gitignore 路径转换为相对路径 -const relativeGitignorePath = path.relative(sourceDir, gitignorePath) - // 读取 .gitignore 文件并解析 const ig = ignore().add(fs.readFileSync(gitignorePath, 'utf-8')) @@ -23,6 +22,19 @@ const createDir = dir => { } } +// 修改 package.json 文件 +const updatePackageJsonForWeb = () => { + const packageJsonPath = path.join(sourceDir, 'package.json') + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) + + packageJson.name = 'star-cloud-uni' + packageJson.main = './uni/index.js' + packageJson.version = uniVersion + + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)) + console.log('Uni build configuration updated in package.json!') +} + // 处理文件 const processFile = (filePath, targetPath) => { const code = fs.readFileSync(filePath, 'utf-8') @@ -69,13 +81,27 @@ const traverseAndProcess = (currentDir, targetDir) => { return } - // 针对 web/index.js 文件,不进行混淆,直接复制到 dist 目录 - if (sourcePath === path.join(__dirname, 'uni', 'index.js')) { + // 针对 uni/index.js 文件,不进行混淆,直接复制到 dist 目录 + if (sourcePath === path.join(sourceDir, 'uni', 'index.js')) { console.log(`复制入口文件: ${sourcePath}`) fs.copyFileSync(sourcePath, targetPath) return } + if (sourcePath === path.join(sourceDir, 'constant.js')) { + console.log(`复制入口文件: ${sourcePath}`) + fs.copyFileSync(sourcePath, targetPath) + return + } + + // 针对 uni.md 文件,直接复制到 dist 目录,并重命名为 README.md + if (sourcePath === path.join(sourceDir, 'uni.md')) { + console.log(`复制 uni.md 文件并重命名为 README.md: ${sourcePath}`) + const readmePath = path.join(targetDir, 'README.md') + fs.copyFileSync(sourcePath, readmePath) + return + } + // 对其他 JS 文件进行混淆 if (stats.isDirectory()) { traverseAndProcess(sourcePath, targetPath) @@ -96,6 +122,7 @@ if (fs.existsSync(distDir)) { } createDir(distDir) +updatePackageJsonForWeb() // 在开始混淆前先修改 package.json traverseAndProcess(sourceDir, distDir) console.log('混淆完成,输出目录为 dist!') diff --git a/obfuscate-web.js b/obfuscate-web.js index ae3c7fb..87d6f6d 100644 --- a/obfuscate-web.js +++ b/obfuscate-web.js @@ -1,18 +1,17 @@ -const JavaScriptObfuscator = require('javascript-obfuscator') -const fs = require('fs') -const path = require('path') -const ignore = require('ignore') +import JavaScriptObfuscator from 'javascript-obfuscator' +import fs from 'fs' +import path from 'path' +import ignore from 'ignore' +import { webVersion } from './env.js' // 导入 webVersion -const sourceDir = __dirname -const distDir = path.join(__dirname, 'dist') -const gitignorePath = path.join(__dirname, '.gitignore') +// 获取当前目录和构建目标目录 +const sourceDir = path.dirname(new URL(import.meta.url).pathname) +const distDir = path.join(sourceDir, 'dist') +const gitignorePath = path.join(sourceDir, '.gitignore') // 定义需要排除的文件和文件夹 const excludedFilesAndDirs = [distDir, 'node_modules'] -// 将 .gitignore 路径转换为相对路径 -const relativeGitignorePath = path.relative(sourceDir, gitignorePath) - // 读取 .gitignore 文件并解析 const ig = ignore().add(fs.readFileSync(gitignorePath, 'utf-8')) @@ -23,6 +22,19 @@ const createDir = dir => { } } +// 修改 package.json 文件 +const updatePackageJsonForWeb = () => { + const packageJsonPath = path.join(sourceDir, 'package.json') + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) + + packageJson.name = 'star-cloud-web-1' + packageJson.main = './web/index.js' + packageJson.version = webVersion + + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)) + console.log('Web build configuration updated in package.json!') +} + // 处理文件 const processFile = (filePath, targetPath) => { const code = fs.readFileSync(filePath, 'utf-8') @@ -70,12 +82,26 @@ const traverseAndProcess = (currentDir, targetDir) => { } // 针对 web/index.js 文件,不进行混淆,直接复制到 dist 目录 - if (sourcePath === path.join(__dirname, 'web', 'index.js')) { + if (sourcePath === path.join(sourceDir, 'web', 'index.js')) { console.log(`复制入口文件: ${sourcePath}`) fs.copyFileSync(sourcePath, targetPath) return } + if (sourcePath === path.join(sourceDir, 'constant.js')) { + console.log(`复制入口文件: ${sourcePath}`) + fs.copyFileSync(sourcePath, targetPath) + return + } + + // 针对 web.md 文件,直接复制到 dist 目录,并重命名为 README.md + if (sourcePath === path.join(sourceDir, 'web.md')) { + console.log(`复制 web.md 文件并重命名为 README.md: ${sourcePath}`) + const readmePath = path.join(targetDir, 'README.md') + fs.copyFileSync(sourcePath, readmePath) + return + } + // 对其他 JS 文件进行混淆 if (stats.isDirectory()) { traverseAndProcess(sourcePath, targetPath) @@ -96,6 +122,7 @@ if (fs.existsSync(distDir)) { } createDir(distDir) +updatePackageJsonForWeb() // 在开始混淆前先修改 package.json traverseAndProcess(sourceDir, distDir) console.log('混淆完成,输出目录为 dist!') diff --git a/package.json b/package.json index 80707ae..5d34df7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { - "name": "star-cloud", - "version": "1.0.2", + "name": "star-cloud-web-1", + "version": "1.0.0", + "type": "module", "main": "./web/index.js", "author": "zzc059", "scripts": { @@ -16,4 +17,4 @@ "devDependencies": { "javascript-obfuscator": "^4.1.1" } -} +} \ No newline at end of file diff --git a/uni.md b/uni.md new file mode 100644 index 0000000..955baa0 --- /dev/null +++ b/uni.md @@ -0,0 +1,84 @@ +## 星云SDK + +### 1. 安装 + +```git +npm install star-cloud-uni +``` + +### 2. 使用 + +```javascript +import { + init, + Result, + register, + logout, + getOfflinePassword, + removeBadLock, + getLockSupportFeatures, + getServerTimestamp +} from 'star-cloud-uni' + +/** + * 初始化星云 + * @param params + * @param {String} params.clientId 客户端Id + * @param {String} params.clientSecret 客户端密码 + * @param {Array} params.accounts 账号列表,后续方法中需传入uid,若未传入则默认使用第一个账号 + * @param {String} params.env 环境 + */ +init(params) + +/** + * 注册星云 + * @returns Result + */ +const { code, data, message } = await register() +if (code === Result.Success.code) { + // 逻辑代码 +} else { + // 错误处理 +} + +/** + * 退出登录 + * @param params + * @param {Number} params.uid 用户ID + * @returns Result + */ +const { code, data, message } = await logout(params) + +/** + * 获取离线密码 + * @param params + * @param {Number} [params.uid] 用户ID + * @param {OfflinePassword} params.password 密码信息 + * @returns Result + */ +const data = await getOfflinePassword(params) + +/** + * 移除坏锁 + * @param params + * @param {Number} [params.uid] 用户ID + * @param {List[Number]} params.lockIds 锁Id列表 + * @returns Result + */ +const data = await removeBadLock(params) + +/** + * 获取服务器时间 + * @returns Result + */ +const data = await getServerTimestamp() + +/** + * 获取锁支持项 + * @param params + * @param {Number} [params.uid] 用户ID + * @param {Number} params.lockId 锁 Id + * @returns Result + */ +const data = await getLockSupportFeatures(params) +``` diff --git a/uni/index.js b/uni/index.js index 4c3fc0c..59b6b2d 100644 --- a/uni/index.js +++ b/uni/index.js @@ -9,7 +9,6 @@ export { Result } * @property {Number} uid 用户ID * @property {String} username 用户名 * @property {String} password 密码 - * @property {String} token token 非必填 */ /** diff --git a/web/index.js b/web/index.js index 3996384..359bfa2 100644 --- a/web/index.js +++ b/web/index.js @@ -9,7 +9,6 @@ export { Result } * @property {Number} uid 用户ID * @property {String} username 用户名 * @property {String} password 密码 - * @property {String} token token 非必填 */ /**