fix: 星云SDK支持纯web

This commit is contained in:
范鹏 2024-12-20 19:04:23 +08:00
parent 50954e4158
commit aeea6d5b4a
9 changed files with 195 additions and 34 deletions

View File

@ -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'
/**

13
env.js
View File

@ -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 = {

View File

@ -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
}
}

View File

@ -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')

View File

@ -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')

View File

@ -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"
}
}
}

84
uni.md Normal file
View File

@ -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<AccountInfo>} 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)
```

View File

@ -9,7 +9,6 @@ export { Result }
* @property {Number} uid 用户ID
* @property {String} username 用户名
* @property {String} password 密码
* @property {String} token token 非必填
*/
/**

View File

@ -9,7 +9,6 @@ export { Result }
* @property {Number} uid 用户ID
* @property {String} username 用户名
* @property {String} password 密码
* @property {String} token token 非必填
*/
/**